From 599a84a14305e8584ba0ce46d4a4fcc81cc449fa Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Tue, 15 Nov 2022 21:00:26 -0600 Subject: [PATCH] add statistics --- block.py | 12 +++++++++--- ippigeon.py | 35 ++++++++++++++++++++++++++++++++--- netstat.py | 1 - panel.py | 14 ++++++++++++-- util.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/block.py b/block.py index ac6aebb..2b05eef 100644 --- a/block.py +++ b/block.py @@ -52,7 +52,7 @@ def block_conn(config, datafile, res): baddata = res fprint("Local loaded successfully") - + goodct = 0 #fprint(mydata) for line in mydata: #fprint(line) @@ -67,13 +67,15 @@ def block_conn(config, datafile, res): pid = int(pid) except ValueError: continue - + found = False for line in baddata: #fprint(destip + " " + line[4]) badsrcip = line[2] baddestip = line[4] badpid = line[11] + if srcip == badsrcip or destip == baddestip and not pid in badapps: + found = True fprint("FLAG " + srcip + " " + destip + " " + str(pid)) badapps.append(pid) badips.append(baddestip) @@ -81,4 +83,8 @@ def block_conn(config, datafile, res): #fprint(badapps) #fprint("FLAG " + srcip + " " + destip + " " + str(pid)) #kill(pid) - return badapps, badips, badlines + + if not found: + goodct = goodct + 1 + + return badapps, badips, badlines, goodct diff --git a/ippigeon.py b/ippigeon.py index 991c05d..6910dba 100644 --- a/ippigeon.py +++ b/ippigeon.py @@ -67,10 +67,14 @@ def process_done(res): if settings["running"] == True: fprint("uploading to sftp...") #ssh.sftp_send_data(res, config, datafile) + procdata_res = pool.apply_async(ssh.sftp_send_data, (config, datafile, 'send'), callback=upload_done) def upload_done(res): settings["block"] = True + tmpstat = settings["stats"] + tmpstat[2] += 1 + settings["stats"] = tmpstat def login_done(res): if not res: @@ -87,7 +91,16 @@ def blockdata_done(res): tmpkill = settings["kill"] settings["kill"] = False #block_res = pool.apply_async(block.block_conn, (config, datafile, res, settings)) - block_pids, block_ips, block_data = block.block_conn(config, datafile, res) + block_pids, block_ips, block_data, goodct = block.block_conn(config, datafile, res) + tmpstat = settings["stats"] + tmpstat[1] += goodct + if tmpstat[0] > 0 and goodct > 0: + tmpstat[4] = 1.0 / (goodct * 100.0 / tmpstat[0]) + else: + tmpstat[4] = 0.0 + tmpstat[3] += 1 + settings["stats"] = tmpstat + tmplist = settings["badapps"] for x in block_pids: @@ -117,6 +130,11 @@ def blockdata_done(res): settings["newdata"] = True +def readstat_done(res): + settings["stats"] = res + fprint("Read stats!" + str(settings["stats"])) + + def killall(): kids = active_children() for kid in kids: @@ -143,6 +161,8 @@ def mainloop(pool): #print(res.get(timeout=1)) if counter == 0: # runs every INTERVAL #fprint("start loop") + if settings["stats"][1] > 0: + util.write_stats(settings["stats"]) if netdata_res is None or netdata_res.ready(): #rawdata = netdata_res.get() #procdata_res = pool.apply_async(process_netstat, (rawdata)) @@ -200,6 +220,9 @@ def mainloop(pool): if settings["fwll"] == True: tmplist = settings["badlines"] + tmpstat = settings["stats"] + tmpstat[0] += len(tmplist) + settings["stats"] = tmpstat settings["badlines"] = list() for line in tmplist: badproto = line[1] @@ -263,6 +286,7 @@ if __name__ == '__main__': interval = config['core']['interval'] displaydata = manager.list(range(2)) # data to be printed settings = manager.dict() # configuration + # setup shared data variables settings["login"] = False settings["loggedin"] = False settings["showui"] = False @@ -273,16 +297,20 @@ if __name__ == '__main__': settings["badlines"] = list() settings["block"] = False settings["kill"] = False - settings["config"] = config + settings["config"] = config settings["applyconfig"] = False settings["fwll"] = 0 settings["running"] = config["core"]["autostart"] settings["newdata"] = False + # connections blocked, total connections allowed, count of data sent, data recieved, ratio blocked-unblocked + settings["stats"] = [0, 0, 0, 0, 0.0] + killme = manager.Value('d', 0) #killme = False # launch background UI app as process util.clear_fwll() p = Process(target=taskbartool.background, args=(displaydata,settings,killme)) + p.start() #p.join() # not a foreground job, so let's not join it keeprunning = True @@ -290,12 +318,13 @@ if __name__ == '__main__': # initial setup #netdata_res = pool.apply_async(netstat, callback=netstat_done) if linux: + # clear existing nftables entries, create new table run_cmd("nft delete table ip ippigeon") run_cmd("nft create table ip ippigeon") run_cmd("nft add chain ip ippigeon output { type filter hook output priority 0 \; policy accept\; }") #run_cmd("nft add chain ippigeon filter") - + pool.apply_async(util.read_stats, callback=readstat_done) # launch loop - non-blocking! counter = 0 while(keeprunning): diff --git a/netstat.py b/netstat.py index 828141e..c91f41f 100644 --- a/netstat.py +++ b/netstat.py @@ -146,7 +146,6 @@ def process(data): writer.writerows(output2) fprint("done creating csv") - def start(): setup_child() diff --git a/panel.py b/panel.py index ba40f35..594d864 100644 --- a/panel.py +++ b/panel.py @@ -148,6 +148,7 @@ class ServerPanel(wx.Panel): self.secondary_sizer = wx.BoxSizer(wx.HORIZONTAL) self.sub_sizer_left = wx.BoxSizer(wx.VERTICAL) self.sub_sizer_right = wx.BoxSizer(wx.VERTICAL) + self.sub_sizer_stats = wx.BoxSizer(wx.VERTICAL) self.row_obj_dict = {} self.list_ctrl = wx.ListCtrl( self, size=(-1, 400), @@ -168,18 +169,22 @@ class ServerPanel(wx.Panel): logintxt = "Logged in" else: logintxt = "Not logged in" + + stattxt = "Statistics:\nBlocked Connections: " + str(settings["stats"][0]) + " \nAllowed Connections: " + str(settings["stats"][1]) + " \nUpload count: " + str(settings["stats"][2]) + " \nDownload count: " + str(settings["stats"][3]) + " \nBlock ratio: " + str(settings["stats"][4]) self.statustext = wx.StaticText(self, label=txt) # pos=(20,20)) self.logintext = wx.StaticText(self, label=logintxt) # pos=(20,20)) + self.stattext = wx.StaticText(self, label=stattxt) #self.main_sizer.Add(tb) #main_sizer.SetBackgroundColour((44, 51, 51)) # self.pnl1.SetBackgroundColour(wx.BLACK) self.handle_columns() #self.updatedata() - self.main_sizer.Add(self.list_ctrl, 0, wx.ALL | wx.EXPAND, 20) + self.main_sizer.Add(self.list_ctrl, 1, wx.ALL | wx.EXPAND, 20) self.main_sizer.Add(self.secondary_sizer, 0, wx.ALL | wx.EXPAND, 20) self.secondary_sizer.Add(self.sub_sizer_left, 0, wx.ALL | wx.CENTER, 20) self.secondary_sizer.Add(self.sub_sizer_right, 0, wx.ALL | wx.CENTER, 20) + self.secondary_sizer.Add(self.sub_sizer_stats, 0, wx.ALL | wx.CENTER, 20) self.start_button = wx.Button(self, label='Start IPPigeon') self.start_button.SetBackgroundColour((205, 215, 206)) self.start_button.Bind(wx.EVT_BUTTON, self.on_start) @@ -201,6 +206,7 @@ class ServerPanel(wx.Panel): self.sub_sizer_left.Add(self.stop_button, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_left.Add(self.secondary_frame_button, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_right.Add(self.login_button, 0, wx.CENTER | wx.ALL | 100, 5) + self.sub_sizer_stats.Add(self.stattext, 0, wx.CENTER | wx.ALL | 100, 5) self.SetSizer(self.main_sizer) self.start_button.Enable(enable=settings["loggedin"]) self.stop_button.Enable(enable=settings["loggedin"]) @@ -225,7 +231,7 @@ class ServerPanel(wx.Panel): else: txt = "Status: Not running" - self.list_ctrl.SetSize(self.GetSize()[0] - 50, self.GetSize()[1] - 200) + #self.list_ctrl.SetSize(self.GetSize()[0] - 50, self.GetSize()[1] - 200) self.checklogin() if settings["loggedin"] == True: logintxt = " Logged in" @@ -233,8 +239,12 @@ class ServerPanel(wx.Panel): else: self.login_button.SetLabelText("Login") logintxt = "Not logged in" + + stattxt = "Statistics:\nBlocked Connections: " + str(settings["stats"][0]) + " \nAllowed Connections: " + str(settings["stats"][1]) + " \nUpload count: " + str(settings["stats"][2]) + " \nDownload count: " + str(settings["stats"][3]) + " \nBlock ratio: " + str(settings["stats"][4]) + self.statustext.SetLabel(txt) self.logintext.SetLabel(logintxt) + self.stattext.SetLabel(stattxt) if settings["newdata"] == True: settings["newdata"] = False else: diff --git a/util.py b/util.py index c2840ab..db07554 100644 --- a/util.py +++ b/util.py @@ -6,6 +6,7 @@ from sys import platform import time as t from time import sleep import uuid +import csv win32 = platform == "win32" linux = platform == "linux" or platform == "linux2" @@ -124,3 +125,33 @@ def clear_fwll(): run_cmd('Remove-NetFirewallRule -Group "IPPigeon"') if linux: run_cmd("nft delete table ip ippigeon") + + +def write_stats(stats): + fprint("Writing stats") + tmp = list() + tmp.append(["connections blocked", "connections allowed", "data uploaded", "data recieved", "block ratio"]) + tmp.append(stats) + + with open(find_data_file("stats.csv"), "w", newline="") as f: + writer = csv.writer(f) + writer.writerows(tmp) + fprint("Done writing stats") + +def read_stats(): + with open(find_data_file("stats.csv"), newline='') as csvfile: + csvreader = csv.reader(csvfile, delimiter=',', quotechar='|') + header = True + fprint(csvreader) + data = list() + for line in csvreader: + fprint(line) + if header: + header = False + continue + data = line + for idx in range(len(data) - 1): + data[idx] = int(data[idx]) + data[len(data) - 1] = float(data[len(data) - 1]) + return data + \ No newline at end of file