Example #1
0
    def setUp(self):
        self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController))
        self.temp_workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                        workspaceClass=WorkspaceOnCouch) 

        self.wm.setActiveWorkspace(self.temp_workspace)
        WorkspacePersister.stopThreads()
Example #2
0
 def setUp(self):
     self.wm = WorkspaceManager(self.model_controller,
                                mock(plcore.PluginController))
     self.workspace = self.wm.createWorkspace(
         new_random_workspace_name(), workspaceClass=WorkspaceOnCouch)
     self.wm.setActiveWorkspace(self.workspace)
     WorkspacePersister.stopThreads()
Example #3
0
    def setUpClass(cls):
        cls.model_controller = controller.ModelController(mock())
        api.setUpAPIs(cls.model_controller)
        cls.wm = WorkspaceManager(cls.model_controller,
                                  mock(plcore.PluginController))
        cls.temp_workspace = cls.wm.createWorkspace(
            test_utils.new_random_workspace_name(),
            workspaceClass=WorkspaceOnCouch)

        cls.wm.setActiveWorkspace(cls.temp_workspace)
        WorkspacePersister.stopThreads()
Example #4
0
    def setUpClass(cls):
        cls.model_controller = controller.ModelController(mock())
        api.setUpAPIs(cls.model_controller)
        cls.wm = WorkspaceManager(cls.model_controller,
                                  mock(plcore.PluginController))
        cls.temp_workspace = cls.wm.createWorkspace(
            test_utils.new_random_workspace_name(),
            workspaceClass=WorkspaceOnCouch)

        cls.wm.setActiveWorkspace(cls.temp_workspace)
        WorkspacePersister.stopThreads()
Example #5
0
    def _test_remove_active_workspace_fs(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnFS)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        host1 = create_host(self, "coquito")

        self.wm.removeWorkspace(workspace.name)

        self.assertNotIn(host1, self.model_controller.getAllHosts(),
                         'Host not removed while removing active workspace')
Example #6
0
    def _test_remove_active_workspace_fs(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnFS)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        host1 = create_host(self, "coquito")

        self.wm.removeWorkspace(workspace.name)

        self.assertNotIn(host1, self.model_controller.getAllHosts(),
                         'Host not removed while removing active workspace')
Example #7
0
 def wrapper(self, *args, **kwargs):
     try:
         if self._lostConnection:
             WorkspacePersister.addPendingAction(self, func, args, kwargs)
         return func(self, *args, **kwargs)
     except restkit.errors.RequestError as req_error:
         self.lostConnectionResolv()
         WorkspacePersister.stopThreads()
         WorkspacePersister.addPendingAction(self, func, args, kwargs)
         WorkspacePersister.notifyPersisterConnectionLost()
         model.api.devlog("Operation [%s] timeout" % func.__name__)
         return func(self, *args, **kwargs)
Example #8
0
 def wrapper(self, *args, **kwargs):
     try:
         if self._lostConnection:
             WorkspacePersister.addPendingAction(self, func, args, kwargs)
         return func(self, *args, **kwargs)
     except restkit.errors.RequestError as req_error:
         self.lostConnectionResolv()
         WorkspacePersister.stopThreads()
         WorkspacePersister.addPendingAction(self, func, args, kwargs)
         WorkspacePersister.notifyPersisterConnectionLost()
         model.api.devlog("Operation [%s] timeout" % func.__name__)
         return func(self, *args, **kwargs)
Example #9
0
    def _test_remove_another_workspace(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnCouch)

        workspace2 = self.wm.createWorkspace(new_random_workspace_name(),
                                             workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace2.name)

        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        create_host(self, "coquito")
        self.wm.setActiveWorkspace(workspace2)
        WorkspacePersister.stopThreads()
        self.wm.removeWorkspace(workspace.name)

        self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(),
                         "Workspace not removed")
        self.assertIn(workspace2.name, self.wm.getWorkspacesNames(),
                      "Workspace removed while removing another workspace")
Example #10
0
    def _test_remove_another_workspace(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnCouch)

        workspace2 = self.wm.createWorkspace(new_random_workspace_name(),
                                             workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace2.name)

        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        create_host(self, "coquito")
        self.wm.setActiveWorkspace(workspace2)
        WorkspacePersister.stopThreads()
        self.wm.removeWorkspace(workspace.name)

        self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(),
                         "Workspace not removed")
        self.assertIn(workspace2.name, self.wm.getWorkspacesNames(),
                      "Workspace removed while removing another workspace")
Example #11
0
    def _test_switch_workspace_with_objects(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace.name)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()

        host1 = create_host(self, "coquito")
        interface1 = create_interface(self, host1, iname="pepito")
        service1 = create_service(self, host1, interface1)

        self.assertIn(host1, self.model_controller.getAllHosts(),
                      "Host not in controller")
        self.assertIn(interface1, host1.getAllInterfaces(),
                      "Interface not in host!")
        self.assertIn(service1, interface1.getAllServices(),
                      "Service not in Interface!")

        workspace2 = self.wm.createWorkspace(new_random_workspace_name(),
                                             workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace2.name)
        self.wm.setActiveWorkspace(workspace2)
        WorkspacePersister.stopThreads()

        self.assertNotIn(
            host1, self.model_controller.getAllHosts(),
            "Host in controller, should be removed when \
                         switching workspaces")

        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        self.assertIn(host1, self.model_controller.getAllHosts(),
                      "Host not in controller")
        self.assertIn(interface1, host1.getAllInterfaces(),
                      "Interface not in host!")
        self.assertIn(service1, interface1.getAllServices(),
                      "Service not in Interface!")
Example #12
0
    def _test_switch_workspace_with_objects(self):
        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace.name)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()

        host1 = create_host(self, "coquito")
        interface1 = create_interface(self, host1, iname="pepito")
        service1 = create_service(self, host1, interface1)

        self.assertIn(host1, self.model_controller.getAllHosts(),
                      "Host not in controller")
        self.assertIn(interface1, host1.getAllInterfaces(),
                      "Interface not in host!")
        self.assertIn(service1, interface1.getAllServices(),
                      "Service not in Interface!")

        workspace2 = self.wm.createWorkspace(new_random_workspace_name(),
                                             workspaceClass=WorkspaceOnCouch)
        self._couchdb_workspaces.append(workspace2.name)
        self.wm.setActiveWorkspace(workspace2)
        WorkspacePersister.stopThreads()

        self.assertNotIn(host1, self.model_controller.getAllHosts(),
                         "Host in controller, should be removed when \
                         switching workspaces")

        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()
        self.assertIn(host1, self.model_controller.getAllHosts(),
                      "Host not in controller")
        self.assertIn(interface1, host1.getAllInterfaces(),
                      "Interface not in host!")
        self.assertIn(service1, interface1.getAllServices(),
                      "Service not in Interface!")
Example #13
0
 def tearDownClass(cls):
     WorkspacePersister.stopThreads()
     cls.wm.removeWorkspace(cls.temp_workspace.name)
Example #14
0
    def test_load_workspace_on_fs(self):
        """ This test case creates a host within the Model Controller context
        adds an interface to it then adds a VulnWeb"""

        """
        We are going to test this structure:
        host -> interface1 -> service1 -> vuln_web
                                       -> vuln
                                       -> note
                           -> service2 -> vuln
                                       -> vuln
             -> vuln
             -> note
             -> note

             -> interface2 -> service3 -> note
                                       -> credential
                                       -> vuln
                           -> vuln
        """

        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnFS)
        #self._couchdb_workspaces.append(workspace.name)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()

        host = create_host(self)
        interface = create_interface(self, host, ip="127.0.0.1")
        interface2 = create_interface(self, host, ip="127.0.0.2")
        service = create_service(self, host, interface, ports=1)
        service2 = create_service(self, host, interface, ports=2)
        service3 = create_service(self, host, interface2, ports=3)

        vulnweb = ModelObjectVulnWeb(name='VulnWebTest',
                                     desc='TestDescription',
                                     severity='high')

        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service.getID(),
                                                   vulnweb)

        vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription',
                               severity='high')
        vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription',
                                severity='high')
        vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription',
                                severity='high')
        vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription',
                                severity='high')
        vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription',
                                severity='high')
        vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription',
                                severity='high')

        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service.getID(),
                                                   vuln)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service2.getID(),
                                                   vuln2)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service2.getID(),
                                                   vuln3)
        self.model_controller.addVulnToHostSYNC(host.getID(),
                                                vuln4)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service3.getID(),
                                                   vuln5)
        self.model_controller.addVulnToInterfaceSYNC(host.getID(),
                                                     interface2.getID(),
                                                     vuln6)

        note = ModelObjectNote(name='NoteTest', text='TestDescription')
        note2 = ModelObjectNote(name='NoteTest2', text='TestDescription')
        note3 = ModelObjectNote(name='NoteTest3', text='TestDescription')
        note4 = ModelObjectNote(name='NoteTest4', text='TestDescription')

        self.model_controller.addNoteToServiceSYNC(host.getID(),
                                                   service.getID(),
                                                   note)
        self.model_controller.addNoteToHostSYNC(host.getID(),
                                                note2)
        self.model_controller.addNoteToHostSYNC(host.getID(),
                                                note3)
        self.model_controller.addNoteToServiceSYNC(host.getID(),
                                                   service3.getID(),
                                                   note4)

        cred = ModelObjectCred(username='******', password='******')

        self.model_controller.addCredToServiceSYNC(host.getID(),
                                                   service3.getID(),
                                                   cred)

        # First, we test if the structure was correctly created

        # one host with two interfaces, one vuln and two notes

        self.assertEquals(len(self.model_controller.getAllHosts()), 1,
                          "Host not created")
        added_host = self.model_controller.getHost(host.getID())

        self.assertEquals(len(added_host.getAllInterfaces()), 2,
                          "Interfaces not added to Host")
        self.assertEquals(len(added_host.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_host.getNotes()), 2,
                          "Notes not created")

        # one interface with two services, and another one
        # with a service and a vuln

        added_interface1 = added_host.getInterface(interface.getID())
        added_interface2 = added_host.getInterface(interface2.getID())

        self.assertEquals(len(added_interface1.getAllServices()), 2,
                          "Services not created")

        self.assertEquals(len(added_interface2.getAllServices()), 1,
                          "Service not created")

        self.assertEquals(len(added_interface2.getVulns()), 1,
                          "Vulns not created")

        # one service with a note, a vuln and a vuln web
        added_service1 = added_interface1.getService(service.getID())
        self.assertEquals(len(added_service1.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service1.getVulns()), 2,
                          "Vulns not created")
        added_vuln_web = added_service1.getVuln(vulnweb.getID())
        self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb",
                          "Not a vuln web")

        # one service with two vulns
        added_service2 = added_interface1.getService(service2.getID())
        self.assertEquals(len(added_service2.getVulns()), 2,
                          "Services not created")

        # one service with a note, a vuln and a credential

        added_service3 = added_interface2.getService(service3.getID())
        self.assertEquals(len(added_service3.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_service3.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service3.getCreds()), 1,
                          "Cred not created")

        # So, now we reload the worskpace and check everything again
        print workspace.name

        workspace.load()

        # one host with two interfaces, one vuln and two notes

        self.assertEquals(len(self.model_controller.getAllHosts()), 1,
                          "Host not created")
        added_host = self.model_controller.getHost(host.getID())

        self.assertEquals(len(added_host.getAllInterfaces()), 2,
                          "Interfaces not added to Host")
        self.assertEquals(len(added_host.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_host.getNotes()), 2,
                          "Notes not created")

        # one interface with two services, and another one
        # with a service and a vuln

        added_interface1 = added_host.getInterface(interface.getID())
        added_interface2 = added_host.getInterface(interface2.getID())

        self.assertEquals(len(added_interface1.getAllServices()), 2,
                          "Services not created")

        self.assertEquals(len(added_interface2.getAllServices()), 1,
                          "Service not created")

        self.assertEquals(len(added_interface2.getVulns()), 1,
                          "Vulns not created")

        # one service with a note, a vuln and a vuln web
        added_service1 = added_interface1.getService(service.getID())
        self.assertEquals(len(added_service1.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service1.getVulns()), 2,
                          "Vulns not created")
        added_vuln_web = added_service1.getVuln(vulnweb.getID())
        self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb",
                          "Not a vuln web")

        # one service with two vulns
        added_service2 = added_interface1.getService(service2.getID())
        self.assertEquals(len(added_service2.getVulns()), 2,
                          "Services not created")

        # one service with a note, a vuln and a credential

        added_service3 = added_interface2.getService(service3.getID())
        self.assertEquals(len(added_service3.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_service3.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service3.getCreds()), 1,
                          "Cred not created")
Example #15
0
 def tearDownClass(cls):
     WorkspacePersister.stopThreads()
     cls.wm.removeWorkspace(cls.temp_workspace.name)
Example #16
0
    def test_load_workspace_on_fs(self):
        """ This test case creates a host within the Model Controller context
        adds an interface to it then adds a VulnWeb"""
        """
        We are going to test this structure:
        host -> interface1 -> service1 -> vuln_web
                                       -> vuln
                                       -> note
                           -> service2 -> vuln
                                       -> vuln
             -> vuln
             -> note
             -> note

             -> interface2 -> service3 -> note
                                       -> credential
                                       -> vuln
                           -> vuln
        """

        workspace = self.wm.createWorkspace(new_random_workspace_name(),
                                            workspaceClass=WorkspaceOnFS)
        #self._couchdb_workspaces.append(workspace.name)
        self.wm.setActiveWorkspace(workspace)
        WorkspacePersister.stopThreads()

        host = create_host(self)
        interface = create_interface(self, host, ip="127.0.0.1")
        interface2 = create_interface(self, host, ip="127.0.0.2")
        service = create_service(self, host, interface, ports=1)
        service2 = create_service(self, host, interface, ports=2)
        service3 = create_service(self, host, interface2, ports=3)

        vulnweb = ModelObjectVulnWeb(name='VulnWebTest',
                                     desc='TestDescription',
                                     severity='high')

        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service.getID(), vulnweb)

        vuln = ModelObjectVuln(name='VulnTest',
                               desc='TestDescription',
                               severity='high')
        vuln2 = ModelObjectVuln(name='VulnTest2',
                                desc='TestDescription',
                                severity='high')
        vuln3 = ModelObjectVuln(name='VulnTest3',
                                desc='TestDescription',
                                severity='high')
        vuln4 = ModelObjectVuln(name='VulnTest4',
                                desc='TestDescription',
                                severity='high')
        vuln5 = ModelObjectVuln(name='VulnTest5',
                                desc='TestDescription',
                                severity='high')
        vuln6 = ModelObjectVuln(name='VulnTest6',
                                desc='TestDescription',
                                severity='high')

        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service.getID(), vuln)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service2.getID(), vuln2)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service2.getID(), vuln3)
        self.model_controller.addVulnToHostSYNC(host.getID(), vuln4)
        self.model_controller.addVulnToServiceSYNC(host.getID(),
                                                   service3.getID(), vuln5)
        self.model_controller.addVulnToInterfaceSYNC(host.getID(),
                                                     interface2.getID(), vuln6)

        note = ModelObjectNote(name='NoteTest', text='TestDescription')
        note2 = ModelObjectNote(name='NoteTest2', text='TestDescription')
        note3 = ModelObjectNote(name='NoteTest3', text='TestDescription')
        note4 = ModelObjectNote(name='NoteTest4', text='TestDescription')

        self.model_controller.addNoteToServiceSYNC(host.getID(),
                                                   service.getID(), note)
        self.model_controller.addNoteToHostSYNC(host.getID(), note2)
        self.model_controller.addNoteToHostSYNC(host.getID(), note3)
        self.model_controller.addNoteToServiceSYNC(host.getID(),
                                                   service3.getID(), note4)

        cred = ModelObjectCred(username='******', password='******')

        self.model_controller.addCredToServiceSYNC(host.getID(),
                                                   service3.getID(), cred)

        # First, we test if the structure was correctly created

        # one host with two interfaces, one vuln and two notes

        self.assertEquals(len(self.model_controller.getAllHosts()), 1,
                          "Host not created")
        added_host = self.model_controller.getHost(host.getID())

        self.assertEquals(len(added_host.getAllInterfaces()), 2,
                          "Interfaces not added to Host")
        self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created")
        self.assertEquals(len(added_host.getNotes()), 2, "Notes not created")

        # one interface with two services, and another one
        # with a service and a vuln

        added_interface1 = added_host.getInterface(interface.getID())
        added_interface2 = added_host.getInterface(interface2.getID())

        self.assertEquals(len(added_interface1.getAllServices()), 2,
                          "Services not created")

        self.assertEquals(len(added_interface2.getAllServices()), 1,
                          "Service not created")

        self.assertEquals(len(added_interface2.getVulns()), 1,
                          "Vulns not created")

        # one service with a note, a vuln and a vuln web
        added_service1 = added_interface1.getService(service.getID())
        self.assertEquals(len(added_service1.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service1.getVulns()), 2,
                          "Vulns not created")
        added_vuln_web = added_service1.getVuln(vulnweb.getID())
        self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb",
                          "Not a vuln web")

        # one service with two vulns
        added_service2 = added_interface1.getService(service2.getID())
        self.assertEquals(len(added_service2.getVulns()), 2,
                          "Services not created")

        # one service with a note, a vuln and a credential

        added_service3 = added_interface2.getService(service3.getID())
        self.assertEquals(len(added_service3.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_service3.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service3.getCreds()), 1,
                          "Cred not created")

        # So, now we reload the worskpace and check everything again
        print workspace.name

        workspace.load()

        # one host with two interfaces, one vuln and two notes

        self.assertEquals(len(self.model_controller.getAllHosts()), 1,
                          "Host not created")
        added_host = self.model_controller.getHost(host.getID())

        self.assertEquals(len(added_host.getAllInterfaces()), 2,
                          "Interfaces not added to Host")
        self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created")
        self.assertEquals(len(added_host.getNotes()), 2, "Notes not created")

        # one interface with two services, and another one
        # with a service and a vuln

        added_interface1 = added_host.getInterface(interface.getID())
        added_interface2 = added_host.getInterface(interface2.getID())

        self.assertEquals(len(added_interface1.getAllServices()), 2,
                          "Services not created")

        self.assertEquals(len(added_interface2.getAllServices()), 1,
                          "Service not created")

        self.assertEquals(len(added_interface2.getVulns()), 1,
                          "Vulns not created")

        # one service with a note, a vuln and a vuln web
        added_service1 = added_interface1.getService(service.getID())
        self.assertEquals(len(added_service1.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service1.getVulns()), 2,
                          "Vulns not created")
        added_vuln_web = added_service1.getVuln(vulnweb.getID())
        self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb",
                          "Not a vuln web")

        # one service with two vulns
        added_service2 = added_interface1.getService(service2.getID())
        self.assertEquals(len(added_service2.getVulns()), 2,
                          "Services not created")

        # one service with a note, a vuln and a credential

        added_service3 = added_interface2.getService(service3.getID())
        self.assertEquals(len(added_service3.getVulns()), 1,
                          "Vuln not created")
        self.assertEquals(len(added_service3.getNotes()), 1,
                          "Note not created")
        self.assertEquals(len(added_service3.getCreds()), 1,
                          "Cred not created")
Example #17
0
 def tearDownClass(cls):
     WorkspacePersister.stopThreads()
     cls.cleanCouchDatabases()