Exemple #1
0
def resetUnfinishedTools():
    """
    Reset all tools running to a ready state. This is useful if a command was running on a worker and the auto scanning was interrupted.
    """
    # test all the cases if datef is defined or not.
    # Normally, only the first one is necessary
    from core.Models.Tool import Tool
    tools = Tool.fetchObjects({"datef": "None", "scanner_ip": {"$ne": "None"}})
    for tool in tools:
        tool.markAsNotDone()
    tools = Tool.fetchObjects({"datef": "None", "dated": {"$ne": "None"}})
    for tool in tools:
        tool.markAsNotDone()
    tools = Tool.fetchObjects({
        "datef": {
            "$exists": False
        },
        "dated": {
            "$ne": "None"
        }
    })
    for tool in tools:
        tool.markAsNotDone()
    tools = Tool.fetchObjects({
        "datef": {
            "$exists": False
        },
        "scanner_ip": {
            "$ne": "None"
        }
    })
    for tool in tools:
        tool.markAsNotDone()
 def delete(self):
     """
     Delete the Scope represented by this model in database.
     Also delete the tools associated with this scope
     Also remove this scope from ips in_scopes attributes
     """
     # deleting tool with scope lvl
     tools = Tool.fetchObjects({
         "scope":
         self.scope,
         "wave":
         self.wave,
         "$or": [{
             "lvl": "network"
         }, {
             "lvl": "domain"
         }]
     })
     for tool in tools:
         tool.delete()
     # Deleting this scope against every ips
     ips = Ip.getIpsInScope(self._id)
     for ip in ips:
         ip.removeScopeFitting(self._id)
     mongoInstance = MongoCalendar.getInstance()
     mongoInstance.delete("scopes", {"_id": self._id})
     parent_wave = mongoInstance.find("waves", {"wave": self.wave}, False)
     if parent_wave is None:
         return
     mongoInstance.notify(mongoInstance.calendarName, "waves",
                          parent_wave["_id"], "update", "")
 def loadData(self):
     """
     Fetch data from database
     """
     self.ports = Port.fetchObjects({})
     self.ips = Ip.fetchObjects({})
     self.tools = Tool.fetchObjects({})
Exemple #4
0
 def getAllTools(self):
     """Return all tools being part of this wave as a list of mongo fetched tools dict.
     Differs from getTools as it fetches all tools of the name and not only tools of level wave.
     Returns:
         list of defect raw mongo data dictionnaries
     """
     return Tool.fetchObjects({"wave": self.wave})
Exemple #5
0
 def setToolsInTime(self):
     """Get all OOT (Out of Time) tools in this wave and checks if this Interval makes them in time. 
     If it is the case, set them in time.
     """
     if Utils.fitNowTime(self.dated, self.datef):
         tools = Tool.fetchObjects({"wave": self.wave, "status": "OOT"})
         for tool in tools:
             tool.setInTime()
Exemple #6
0
 def removeScopeFitting(self, scopeId):
     """Remove the given scopeId from the list of scopes this IP fits in.
     Args:
         scopeId: a mongo ObjectId of a scope object.
     """
     if str(scopeId) in self.in_scopes:
         self.in_scopes.remove(str(scopeId))
         self.update()
         if not self.in_scopes:
             tools = Tool.fetchObjects({"ip": self.ip})
             for tool in tools:
                 tool.setOutOfScope()
Exemple #7
0
 def addScopeFitting(self, scopeId):
     """Add the given scopeId to the list of scopes this IP fits in.
     Args:
         scopeId: a mongo ObjectId of a Scope object.
     """
     if not self.in_scopes:
         tools = Tool.fetchObjects({"ip": self.ip})
         for tool in tools:
             tool.setInScope()
     if str(scopeId) not in self.in_scopes:
         self.in_scopes.append(str(scopeId))
         self.update()
Exemple #8
0
    def removeAllTool(self, command_name):
        """
        Remove from every member of this wave the old tool corresponding to given command name but only if the tool is not done.
        We preserve history

        Args:
            command_name: The command that we want to remove all the tools.
        """
        tools = Tool.fetchObjects({"name": command_name, "wave": self.wave})
        for tool in tools:
            if "done" not in tool.getStatus():
                tool.delete()
Exemple #9
0
 def refreshUI(self):
     """Reload informations and renew widgets"""
     mongoInstance = MongoCalendar.getInstance()
     workernames = self.monitor.getWorkerList()
     running_scans = Tool.fetchObjects({"status":"running"})
     for children in self.scanTv.get_children():
         self.scanTv.delete(children)
     for running_scan in running_scans:
         self.scanTv.insert('','end', running_scan.getId(), text=running_scan.name, values=(running_scan.dated), image=self.running_icon)
     for children in self.workerTv.get_children():
         self.workerTv.delete(children)
     registeredCommands = set()
     for workername in workernames:
         try:
             worker_node = self.workerTv.insert(
                 '', 'end', workername, text=workername, image=self.ok_icon)
         except tk.TclError:
             worker_node = self.workerTv.item(workername)
         worker_registered = mongoInstance.findInDb("pollenisator", "workers", {"name":workername}, False)
         commands_registered = worker_registered["registeredCommands"]
         for command in commands_registered:
             try:
                 self.workerTv.insert(
                     worker_node, 'end', command, text=command, image=self.ok_icon)
             except tk.TclError:
                 pass
             registeredCommands.add(str(command))
         allCommands = Command.getList(None, mongoInstance.calendarName)
         for command in allCommands:
             if command not in registeredCommands:
                 try:
                     self.workerTv.insert(
                         worker_node, '0', 'notRegistered|'+command, text=command, image=self.nok_icon)
                 except tk.TclError:
                     pass
             else:
                 try:
                     self.workerTv.delete('notRegistered|'+command)
                 except tk.TclError:
                     pass
     if len(registeredCommands) > 0 and self.btn_autoscan is None:
         if self.running_auto_scans:
             self.btn_autoscan = ttk.Button(
                 self.parent, text="Stop Scanning", command=self.stopAutoscan)
             self.btn_autoscan.pack()
         else:
             self.btn_autoscan = ttk.Button(
                 self.parent, text="Start Scanning", command=self.startAutoscan)
             self.btn_autoscan.pack()
Exemple #10
0
    def announce_offline_worker(self, event):
        """
        Called when a celery worker gets offline nicely. Is used to remove this worker from the worker_list

        Args:
            event: created automatically when the event occurs. Contains some info about the worker
        """
        worker_hostname = event["hostname"].strip()
        workers = self.getWorkerList()
        print("RECEIVE OFFLINE WORKER "+str(worker_hostname))

        if worker_hostname in list(workers):
            toolsToReset = Tool.fetchObjects(
                {"datef": "None", "scanner_ip": worker_hostname})
            for tool in toolsToReset:
                tool.markAsNotDone()
            self.removeWorker(worker_hostname)
Exemple #11
0
 def delete(self):
     """
     Delete the Interval represented by this model in database.
     """
     mongoInstance = MongoCalendar.getInstance()
     mongoInstance.delete(
         "intervals", {"_id": self._id})
     parent_wave = mongoInstance.find("waves", {"wave": self.wave}, False)
     self._id = None
     if parent_wave is None:
         return
     mongoInstance.notify(mongoInstance.calendarName,
                          "waves", parent_wave["_id"], "update", "")
     other_intervals = Interval.fetchObjects({"wave": self.wave})
     no_interval_in_time = True
     for other_interval in other_intervals:
         if Utils.fitNowTime(other_interval.dated, other_interval.datef):
             no_interval_in_time = False
             break
     if no_interval_in_time:
         tools = Tool.fetchObjects({"wave": self.wave})
         for tool in tools:
             tool.setOutOfTime()
Exemple #12
0
 def initUI(self, parent):
     """Create widgets and initialize them
     Args:
         parent: the parent tkinter widget container."""
     if self.workerTv is not None:
         self.refreshUI()
         return
     mongoInstance = MongoCalendar.getInstance()
     self.parent = parent
     ### WORKER TREEVIEW : Which worker knows which commands
     lblworker = ttk.Label(self.parent, text="Workers:")
     lblworker.pack(side=tk.TOP, padx=10, pady=5, fill=tk.X)
     self.workerTv = ttk.Treeview(self.parent)
     self.workerTv['columns'] = ('workers')
     self.workerTv.heading("#0", text='Workers', anchor=tk.W)
     self.workerTv.column("#0", anchor=tk.W)
     self.workerTv.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
     registeredCommands = set()
     workernames = self.monitor.getWorkerList()
     for workername in workernames:
         worker_node = self.workerTv.insert('',
                                            'end',
                                            workername,
                                            text=workername,
                                            image=self.ok_icon)
         commands_registered = mongoInstance.getRegisteredCommands(
             workername)
         for command in commands_registered:
             self.workerTv.insert(worker_node,
                                  'end',
                                  None,
                                  text=command,
                                  image=self.ok_icon)
             registeredCommands.add(str(command))
     allCommands = Command.getList()
     for command in allCommands:
         if command not in registeredCommands:
             try:
                 self.workerTv.insert('',
                                      'end',
                                      'notRegistered',
                                      text='Laking commands',
                                      image=self.nok_icon)
             except tk.TclError:
                 self.workerTv.item('notRegistered')
             try:
                 self.workerTv.insert('notRegistered',
                                      'end',
                                      'notRegistered|' + str(command),
                                      text=str(command),
                                      image=self.nok_icon)
             except tk.TclError:
                 pass
     #### TREEVIEW SCANS : overview of ongoing auto scan####
     lblscan = ttk.Label(self.parent, text="Scan overview:")
     lblscan.pack(side=tk.TOP, padx=10, pady=5, fill=tk.X)
     self.scanTv = ttk.Treeview(self.parent)
     self.scanTv['columns'] = ('Started at')
     self.scanTv.heading("#0", text='Scans', anchor=tk.W)
     self.scanTv.column("#0", anchor=tk.W)
     self.scanTv.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
     self.scanTv.bind("<Double-Button-1>", self.OnDoubleClick)
     running_scans = Tool.fetchObjects({"status": "running"})
     for running_scan in running_scans:
         self.scanTv.insert('',
                            'end',
                            running_scan.getId(),
                            text=running_scan.name,
                            values=(running_scan.dated),
                            image=self.running_icon)
     #### BUTTONS FOR AUTO SCANNING ####
     if len(registeredCommands) > 0:
         if self.running_auto_scans:
             self.btn_autoscan = ttk.Button(self.parent,
                                            text="Stop Scanning",
                                            command=self.stopAutoscan)
             self.btn_autoscan.pack()
         else:
             self.btn_autoscan = ttk.Button(self.parent,
                                            text="Start Scanning",
                                            command=self.startAutoscan)
             self.btn_autoscan.pack()
     btn_parse_scans = ttk.Button(self.parent,
                                  text="Parse existing files",
                                  command=self.parseFiles)
     btn_parse_scans.pack()
Exemple #13
0
 def getTools(self):
     """Return scope assigned tools as a list of mongo fetched tools dict
     Returns:
         list of defect raw mongo data dictionnaries
     """
     return Tool.fetchObjects({"wave": self.wave, "lvl": "wave"})
 def _load(self):
     """
     Load the treeview with database information
     """
     mongoInstance = MongoCalendar.getInstance()
     dialog = ChildDialogProgress(self.appli, "Loading "+str(
         mongoInstance.calendarName), "Opening "+str(mongoInstance.calendarName) + ". Please wait for a few seconds.", 200, "determinate")
     step = 0
     dialog.show(100)
     nbObjects = mongoInstance.find("waves").count()
     nbObjects += mongoInstance.find("scopes").count()
     nbObjects += mongoInstance.find("intervals").count()
     nbObjects += mongoInstance.find("scopes").count()
     nbObjects += mongoInstance.find("ips").count()
     nbObjects += mongoInstance.find("ports").count()
     nbObjects += mongoInstance.find("tools").count()
     onePercentNbObject = nbObjects//100 if nbObjects > 100 else 1
     nbObjectTreated = 0
     for child in self.get_children():
         self.delete(child)
     self._hidden = []
     self._detached = []
     self.waves_node = self.insert("", "end", str(
         "waves"), text="Waves", image=WaveView.getClassIcon())
     # Loading every category separatly is faster than recursivly.
     # This is due to cursor.next function calls in pymongo
     # Adding wave objects
     waves = Wave.fetchObjects({})
     for wave in waves:
         wave_o = WaveController(wave)
         wave_vw = WaveView(self, self.appli.viewframe, self.appli, wave_o)
         wave_vw.addInTreeview(self.waves_node, False)
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     scopes = Scope.fetchObjects({})
     for scope in scopes:
         scope_o = ScopeController(scope)
         scope_vw = ScopeView(self, self.appli.viewframe, self.appli, scope_o)
         scope_vw.addInTreeview(None, False)
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     intervals = Interval.fetchObjects({})
     for interval in intervals:
         interval_o = IntervalController(interval)
         interval_vw = IntervalView(self, self.appli.viewframe, self.appli, interval_o)
         interval_vw.addInTreeview(None, False)
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     # Adding ip objects
     self.ips_node = self.insert("", "end", str(
         "ips"), text="IPs", image=IpView.getClassIcon())
     ips = Ip.fetchObjects({})
     for ip in ips:
         ip_o = IpController(ip)
         ip_vw = IpView(self, self.appli.viewframe, self.appli, ip_o)
         ip_vw.addInTreeview(None, False)
         self.appli.statusbar.notify(ip_vw.controller.getTags())
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     # Adding port objects
     ports = Port.fetchObjects({})
     for port in ports:
         port_o = PortController(port)
         port_vw = PortView(self, self.appli.viewframe, self.appli, port_o)
         port_vw.addInTreeview(None, False)
         self.appli.statusbar.notify(port_vw.controller.getTags())
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     # Adding defect objects
     defects = Defect.fetchObjects({"ip":{"$ne":""}})
     for defect in defects:
         defect_o = DefectController(defect)
         defect_vw = DefectView(
             self, self.appli.viewframe, self.appli, defect_o)
         defect_vw.addInTreeview(None)
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     # Adding tool objects
     tools = Tool.fetchObjects({})
     for tool in tools:
         tool_o = ToolController(tool)
         tool_vw = ToolView(self, self.appli.viewframe, self.appli, tool_o)
         tool_vw.addInTreeview(None, False)
         self.appli.statusbar.notify(tool_vw.controller.getTags())
         nbObjectTreated += 1
         if nbObjectTreated % onePercentNbObject == 0:
             step += 1
             dialog.update(step)
     self.sort(self.ips_node)
     self.appli.statusbar.update()
     dialog.destroy()