import glob import wx import logging import sys import wx.lib.buttons as buttons import numpy as np import pandas as pd from util import sysid from util import setup_child from util import fprint from util import find_data_file import util BG_IMG = 'icon.png' filename = sysid + "gendata.csv" COLUMN_NAMES = np.flip(['Executable', 'Proto', 'Source IP', 'Destination IP', 'Status', 'PID']) COLUMN_SIZES = np.flip([200, 50, 200, 200, 140, 100]) TEST_FILE = None SEC_LEVELS = ["0: Backend analysis only.", "1: Kill offending processes.", "2: Block offending connections using firewall.", "3: Kill and block connections and processes."] displaydata = None settings = None killme = False def loaddata(): global TEST_FILE try: TEST_FILE = pd.read_csv(find_data_file(filename), skiprows=1) TEST_FILE = TEST_FILE.iloc[1:, :] TEST_FILE.columns = ['Executable', 'Protocol', 'Source IP', 'Destination IP', 'Status', 'PID'] #fprint(TEST_FILE) #fprint(len(TEST_FILE)) #fprint(TEST_FILE.iloc[1, 1]) except FileNotFoundError: pass class OtherFrame(wx.Frame): """ Class used for creating frames other than the main one """ def __init__(self): wx.Frame.__init__(self, None, -1, 'Server Panel', size=(1000, 675)) panel = ServerPanel(self) self.SetIcon(wx.Icon(find_data_file("icon.ico"), wx.BITMAP_TYPE_ICO)) self.Show() def on_edit(self, event): fprint('in on_edit', settings) def on_window(self, event): return class HelpFrame(wx.Frame): """ Class used for creating frames other than the main one """ def __init__(self): wx.Frame.__init__(self, None, -1, 'Help', size=(600, 250)) panel = HelpPanel(self) self.SetIcon(wx.Icon(find_data_file("icon.ico"), wx.BITMAP_TYPE_ICO)) self.Show() # Panel with all the login widgets class LoginPanel(wx.Panel): def __init__(self, parent): super(LoginPanel, self).__init__(parent) self.SetBackgroundColour((44, 51, 51)) basicLabel = wx.StaticText(self, -1, "Username") basicLabel.SetForegroundColour((255,255,255)) global basicText global pwdText basicText = wx.TextCtrl(self, -1, "", size=(175, -1)) # basicText.SetInsertionPoint(0) pwdLabel = wx.StaticText(self, -1, "Password") pwdText = wx.TextCtrl(self, -1, "", size=(175, -1), style=wx.TE_PASSWORD) #pwdText.Bind(wx.EVT_TEXT_ENTER, self.on_login) # press enter in password field to login pwdLabel.SetForegroundColour((255, 255, 255)) vbox = wx.BoxSizer(wx.VERTICAL) hbox = wx.BoxSizer(wx.HORIZONTAL) vbox.Add(basicLabel, 0, wx.ALIGN_CENTER | 100, 5) vbox.Add(basicText, 0, wx.ALIGN_CENTER | 100, 5) vbox.Add(pwdLabel, 0, wx.ALIGN_CENTER | 100, 5) vbox.Add(pwdText, 0, wx.ALIGN_CENTER | 100, 5) login_button = wx.Button(self, label='Login') login_button.Bind(wx.EVT_BUTTON, self.on_login) #help_button = wx.Button(self, label='Help') #help_button.Bind(wx.EVT_BUTTON, self.on_help) # signup_button = wx.Button(self, label='Sign Up') hbox.Add(login_button, 0, wx.ALL | 200, 20) # hbox.Add(signup_button, 0, wx.ALL | 200, 20) #hbox.Add(help_button, 0, wx.ALL | 200, 20) vbox.Add(hbox, 0, wx.ALIGN_CENTER | 100, 5) self.SetSizer(vbox) def on_login(self, event): # check for login verification settings["username"] = basicText.GetValue() settings["password"] = pwdText.GetValue() settings["login"] = True #self.Destroy() self.Close() self.Parent.Close() #OtherFrame() def on_help(self, event): HelpFrame() # Panel with all the login widgets class HelpPanel(wx.Panel): def __init__(self, parent): super(HelpPanel, self).__init__(parent) self.SetBackgroundColour((44, 51, 51)) # basicLabel = wx.StaticText(self, -1, "Username") # basicLabel.SetForegroundColour((255,255,255)) # basicText = wx.TextCtrl(self, -1, "", size=(175, -1)) # # # basicText.SetInsertionPoint(0) # pwdLabel = wx.StaticText(self, -1, "Password") # pwdText = wx.TextCtrl(self, -1, "", size=(175, -1), style=wx.TE_PASSWORD) # pwdLabel.SetForegroundColour((255, 255, 255)) # vbox = wx.BoxSizer(wx.VERTICAL) # hbox = wx.BoxSizer(wx.HORIZONTAL) # vbox.Add(basicLabel, 0, wx.ALIGN_CENTER | 100, 5) # vbox.Add(basicText, 0, wx.ALIGN_CENTER | 100, 5) # vbox.Add(pwdLabel, 0, wx.ALIGN_CENTER | 100, 5) # vbox.Add(pwdText, 0, wx.ALIGN_CENTER | 100, 5) # login_button = wx.Button(self, label='Login') # login_button.Bind(wx.EVT_BUTTON, self.on_login) # help_button = wx.Button(self, label='Help') # # signup_button = wx.Button(self, label='Sign Up') # hbox.Add(login_button, 0, wx.ALL | 200, 20) # # hbox.Add(signup_button, 0, wx.ALL | 200, 20) # hbox.Add(help_button, 0, wx.ALL | 200, 20) # vbox.Add(hbox, 0, wx.ALIGN_CENTER | 100, 5) # self.SetSizer(vbox) class ServerPanel(wx.Panel): def __init__(self, parent): super().__init__(parent, size=(500, 500)) #self.SetBackgroundColour((44, 51, 51)) self.main_sizer = wx.BoxSizer(wx.VERTICAL) 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.sub_sizer_filter = wx.BoxSizer(wx.VERTICAL) self.row_obj_dict = {} self.list_ctrl = wx.ListCtrl( self, size=(-1, 400), style=wx.LC_REPORT | wx.BORDER_SUNKEN ) #tb = wx.ToolBar( self, -1) #self.ToolBar = tb #tb.SetToolBitmapSize(wx.Size(30, 3)) #tb.AddTool(wx.ID_ANY, '',wx.Bitmap(find_data_file("WXPython_30x30.png")),) #tb.AddTool(wx.ID_ANY, '',wx.Bitmap(find_data_file("settings_30x30.png"))) #tb.Realize() if settings["running"] == True: txt = "Status: Running (" + str(settings["config"]["core"]["level"]) + ")" else: txt = "Status: Not running" if settings["loggedin"] == True: 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("{:.4f}".format(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, 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_filter, 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) #start_button.Bind(wx.EVT_ENTER_WINDOW, self.on_start) self.stop_button = wx.Button(self, label='Stop IPPigeon') self.stop_button.SetBackgroundColour('#F08080') self.secondary_frame_button = wx.Button(self, label='Settings') self.secondary_frame_button.Bind(wx.EVT_BUTTON, self.on_window) # wx.BORDER(stop_button, wx.BORDER_NONE) self.stop_button.Bind(wx.EVT_BUTTON, self.on_stop) self.login_button = wx.Button(self, label='Login') #self.login_button.SetBackgroundColour((205, 215, 206)) self.login_button.Bind(wx.EVT_BUTTON, self.on_login) self.quit_button = wx.Button(self, label='Quit IPPigeon') #self.login_button.SetBackgroundColour((205, 215, 206)) self.quit_button.Bind(wx.EVT_BUTTON, self.on_quit) self.blacklist_button = wx.Button(self, label='Add to blacklist') #self.login_button.SetBackgroundColour((205, 215, 206)) self.blacklist_button.Bind(wx.EVT_BUTTON, self.on_blacklist) self.whitelist_button = wx.Button(self, label='Add to whitelist') #self.login_button.SetBackgroundColour((205, 215, 206)) self.whitelist_button.Bind(wx.EVT_BUTTON, self.on_whitelist) self.log_button = wx.Button(self, label='Logs') self.log_button.Bind(wx.EVT_BUTTON, self.on_log) #self.log_window = wx.LogWindow(self, 'Log Window', True) #show_log_button = wx.Button(self, wx.NewId(), 'Show Log') #show_log_button.Bind(wx.EVT_BUTTON, self._show_log) self.sub_sizer_right.Add(self.statustext, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_right.Add(self.logintext, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_left.Add(self.start_button, 0, wx.CENTER | wx.ALL | 100, 5) 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_left.Add(self.log_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_right.Add(self.quit_button, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_stats.Add(self.stattext, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_filter.Add(self.blacklist_button, 0, wx.CENTER | wx.ALL | 100, 5) self.sub_sizer_filter.Add(self.whitelist_button, 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"]) self.secondary_frame_button.Enable(enable=settings["loggedin"]) wx.CallAfter(self.updatedata) wx.GetApp().ProcessPendingEvents() def handle_columns(self): for col in range(len(COLUMN_NAMES)): self.list_ctrl.InsertColumn(0, COLUMN_NAMES[col], width=COLUMN_SIZES[col]) def checklogin(self): self.start_button.Enable(enable=settings["loggedin"]) self.stop_button.Enable(enable=settings["loggedin"]) self.secondary_frame_button.Enable(enable=settings["loggedin"]) self.quit_button.Enable(enable=settings["loggedin"]) if self.list_ctrl.GetFirstSelected() < 0: self.blacklist_button.Enable(enable=False) self.whitelist_button.Enable(enable=False) else: self.blacklist_button.Enable(enable=settings["loggedin"]) self.whitelist_button.Enable(enable=settings["loggedin"]) def updatedata(self): global settings if settings["running"] == True: txt = "Status: Running (" + str(settings["config"]["core"]["level"]) + ")" else: txt = "Status: Not running" #self.list_ctrl.SetSize(self.GetSize()[0] - 50, self.GetSize()[1] - 200) self.checklogin() if settings["loggedin"] == True: logintxt = " Logged in" self.login_button.SetLabelText("Logout") 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("{:.4f}".format(settings["stats"][4])) self.statustext.SetLabel(txt) self.logintext.SetLabel(logintxt) self.stattext.SetLabel(stattxt) if settings["newdata"] == True: settings["newdata"] = False else: wx.CallLater(100, self.updatedata) return fprint("updatedata called", settings) loaddata() list_total = self.list_ctrl.GetItemCount() list_top = self.list_ctrl.GetTopItem() list_pp = self.list_ctrl.GetCountPerPage() list_bottom = min(list_top + list_pp, list_total - 1) if self.list_ctrl.DeleteAllItems(): fprint("Items deleted", settings) else: fprint("Unable to delete", settings) for i in range(len(TEST_FILE)): if str(TEST_FILE.iloc[i, 4]).find("TIME_WAIT") >= 0 or str(TEST_FILE.iloc[i, 4]).find("FIN_WAIT_2") >= 0: continue idx = 0 for app in settings["badapps"]: #fprint(pid) idx = i if TEST_FILE.iloc[i, 5] == app: # "bad" pid, highlight in red at the top fprint("BAD APP UI: " + str(app), settings) idx = self.list_ctrl.InsertItem(0, TEST_FILE.iloc[i, 0]) self.list_ctrl.SetItemBackgroundColour(idx, wx.Colour(200, 51, 51)) break else: idx = self.list_ctrl.InsertItem(i, TEST_FILE.iloc[i, 0]) #fprint(TEST_FILE.iloc[i, 5] + " in " + str(settings["badapps"])) #if TEST_FILE.iloc[i, 5] in settings["badapps"]: #fprint("Got " + TEST_FILE.iloc[i, 5]) for j in range(1, 6): #fprint(str(idx) + " " + str(TEST_FILE.iloc[i, 0])) self.list_ctrl.SetItem(idx, j, str(TEST_FILE.iloc[i, j])) #fprint(i, j, TEST_FILE.iloc[i, j]) #self.SetSizer(self.main_sizer) self.list_ctrl.EnsureVisible((list_bottom - 1)) wx.CallLater(100, self.updatedata) def on_start(self, event): fprint('in on_start', settings) settings["running"] = True def on_stop(self, event): fprint('in on_stop', settings) settings["running"] = False util.clear_fwll() def on_window(self, event): fprint("open settings", settings) dg = GetData(parent = None) dg.ShowModal() def on_login(self, event): fprint("open login", settings) if settings["loggedin"] == True: settings["loggedin"] = False return dg2 = ServerFrame() #dg2.ShowModal() def on_quit(self, event): global killme killme.value += 1 self.Close() self.Parent.Close() def on_blacklist(self, event): global settings tmp = settings["appendbad"] idx = self.list_ctrl.GetFirstSelected() if idx < 0: return proto = self.list_ctrl.GetItem(idx, 1).GetText() dest = self.list_ctrl.GetItem(idx, 3).GetText() destip, destport = dest.split(":") fprint([proto, destip, destport], settings) tmp.append(["N/A", proto, "N/A", "N/A", destip, destport, "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"]) settings["appendbad"] = tmp def on_whitelist(self, event): global settings tmp = settings["whitelist"] idx = self.list_ctrl.GetFirstSelected() if idx < 0: return proto = self.list_ctrl.GetItem(idx, 1).GetText() dest = self.list_ctrl.GetItem(idx, 3).GetText() pid = self.list_ctrl.GetItem(idx, 5).GetText() destip, destport = dest.split(":") fprint([proto, destip, destport], settings) tmp.append([destip, destport, int(pid)]) settings["whitelist"] = tmp def on_log(self, event): fprint("open log", settings) dg = logData(parent = None) #fprint("attempting logLoop", settings) logData.logLoop(dg) #fprint("did logLoop work?", settings) dg.ShowModal() def ShowImage(self, imageFile): if imageFile == "": self.bitmap = wx.StaticBitmap(self, -1, size=(0, 0)) else: bmp = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.bitmap = wx.StaticBitmap(self, -1, bmp, (0, 0)) class ServerFrame(wx.Frame): def __init__(self): super().__init__(parent=None, title='Server Dashboard') self.panel = LoginPanel(self) # image = wx.StaticBitmap(self, wx.ID_ANY) # image.SetBitmap(wx.Bitmap('WXPython.png')) self.Show() def openwindow(data, sets, kill): setup_child() global killme global settings global displaydata killme = kill displaydata = data settings = sets #if settings["loggedin"]: fprint("Creating server panel", settings) loaddata() app = wx.App(False) frame = OtherFrame() frame.SetIcon(wx.Icon(find_data_file("icon.ico"), wx.BITMAP_TYPE_ICO)) app.MainLoop() #else: #fprint("Creating login panel") #app = wx.App(False) #frame = ServerFrame() #frame.SetIcon(wx.Icon(find_data_file("icon.ico"), wx.BITMAP_TYPE_ICO)) #app.MainLoop() class RedirectText(object): def __init__(self,aWxTextCtrl): self.out = aWxTextCtrl def write(self,string): self.out.WriteText(string) class logData(wx.Dialog): def __init__(self, parent): wx.Dialog.__init__(self, parent, wx.ID_ANY, "Logs: ", size = (800,500)) self.p = wx.Panel(self, wx.ID_ANY) #self.logBox = wx.BoxSizer(wx.VERTICAL) self.logsTitle = wx.StaticText(self.p, label="Logs", pos=(20,20)) self.logs = wx.TextCtrl(self.p, pos=(20,40), size= (700, 400), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL) self.logs.WriteText("init\n") #keepLoop = False def logLoop(self): #self.logs.WriteText("before while loop\n") i=0 #self.logs.Clear() tmp = settings["logMsg"] if (tmp is not None): while(i < len(settings["logMsg"])): #for i in range(len(settings["logMsg"])): #self.logs.WriteText("in while loop\n") self.logs.WriteText(settings["logMsg"][i]) self.logs.WriteText("\n") tmp.remove(settings["logMsg"][i]) i+=1 else: #self.logs.WriteText("settings is none :(") tmp.add("init string for list") logData.logLoop(self) settings["logMsg"] = tmp wx.CallLater(100, self.logLoop) #self.logs.WriteText("after while loop\n") #redir = RedirectText(self.logs) #sys.stdout = redir #self.logs.WriteText(redir) #self.logs.LoadFile('output.log') #def doLogging(self): #self.logs.WriteText("hi from doLogging\n") #self.logs.WriteText(settings["logMsg"]) class GetData(wx.Dialog): def __init__(self, parent): wx.Dialog.__init__(self, parent, wx.ID_ANY, "User Settings: ", size = (800,500)) self.p = wx.Panel(self, wx.ID_ANY) self.hostname = wx.StaticText(self.p, label="Host:", pos=(20,20)) self.hostnametext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["host"], pos=(180,20), size=(500,-1)) self.user = wx.StaticText(self.p, label="User: ", pos=(20,60)) self.usertext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["user"], pos=(180,60), size=(500,-1)) self.port = wx.StaticText(self.p, label="Port: ", pos=(20,100)) self.porttext = wx.TextCtrl(self.p, value=str(settings["config"]["sftp"]["port"]), pos=(180,100), size=(500,-1)) self.keyfile = wx.StaticText(self.p, label="Keyfile: ", pos=(20,140)) self.keyfiletext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["keyfile"], pos=(180,140), size=(500,-1)) self.filepathsend = wx.StaticText(self.p, label="Sending File Path: ", pos=(20,180)) self.filepathsendtext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["filepath"]["send"], pos=(180,180), size=(500,-1)) self.filepathsendlogin = wx.StaticText(self.p, label="Sending Login Path: ", pos=(20,220)) self.filepathsendlogintext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["filepath"]["sendlogin"], pos=(180,220), size=(500,-1)) self.filepathreceive = wx.StaticText(self.p, label="Receiving File Path: ", pos=(20,260)) self.filepathreceivetext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["filepath"]["receive"], pos=(180,260), size=(500,-1)) self.filepathreceivelogin = wx.StaticText(self.p, label="Receiving Login Path: ", pos=(20,300)) self.filepathreceivelogintext = wx.TextCtrl(self.p, value=settings["config"]["sftp"]["filepath"]["receivelogin"], pos=(180,300), size=(500,-1)) self.darkmode = wx.StaticText(self.p, label="Dark mode (On/Off): ", pos=(20,340)) self.darkmodetext = wx.TextCtrl(self.p, value=str(settings["config"]["ui"]["darkmode"]), pos=(180,340), size=(500,-1)) self.interval = wx.StaticText(self.p, label="Interval (sec): ", pos=(20,380)) self.intervaltext = wx.TextCtrl(self.p, value=str(settings["config"]["core"]["interval"]), pos=(180,380), size=(500,-1)) self.seclevel = wx.StaticText(self.p, label="Security Strength Level: ", pos=(20,420)) self.seclevelslider = wx.Slider(self.p, pos=(180,410), minValue=0, maxValue=3, style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS, value=settings["config"]["core"]["level"]) self.seclevel2 = wx.StaticText(self.p, label=SEC_LEVELS[settings["config"]["core"]["level"]], pos=(300,420)) self.seclevelslider.Bind(wx.EVT_SCROLL, self.level_upd) self.Bind(wx.EVT_CLOSE, self.OnQuit) def level_upd(self, event): self.seclevel2.SetLabel(SEC_LEVELS[self.seclevelslider.GetValue()]) def OnQuit(self, event): # save changes fprint("saving changes...", settings) config = settings["config"] config["sftp"]["host"] = self.hostnametext.GetValue() config["sftp"]["user"] = self.usertext.GetValue() config["sftp"]["port"] = int(self.porttext.GetValue()) config["sftp"]["keyfile"] = self.keyfiletext.GetValue() config["sftp"]["filepath"]["send"] = self.filepathsendtext.GetValue() config["sftp"]["filepath"]["sendlogin"] = self.filepathsendlogintext.GetValue() config["sftp"]["filepath"]["receive"] = self.filepathreceivetext.GetValue() config["sftp"]["filepath"]["receivelogin"] = self.filepathreceivelogintext.GetValue() config["ui"]["darkmode"] = bool(self.darkmodetext.GetValue()) config["core"]["interval"] = int(self.intervaltext.GetValue()) config["core"]["level"] = int(self.seclevelslider.GetValue()) settings["config"] = config settings["applyconfig"] = True self.result_name = None self.Destroy() def on_edit(self, event): print('in on_edit') if __name__ == '__main__': openwindow(list(), dict(), int())