def getLastProject(self): if self.lastProjectPath is None: return None from netzob.Common.Project import Project project = Project.loadProject(self, self.lastProjectPath) return project
def importProjectApplyButton_clicked_cb(self, button): selectedFile = self.view.importProjectFileChooserButton.get_filename() if selectedFile is None: self.view.showErrorMessage(_("No file selected")) else: # Verify the file is a valid definition of a project if Project.loadProjectFromFile(selectedFile) is None: self.view.showErrorMessage(_("The file doesn't define a valid project.")) else: try: Project.importNewXMLProject(self.workspace, selectedFile) self.mainController.updateListOfAvailableProjects() self.view.destroy() except ProjectException, e: self.view.showErrorMessage(str(e))
def projectsTreeviewSelection_changed_cb(self, selection): (model, treeiter) = selection.get_selected() if treeiter is not None: (projectPath, projectName) = self._getSelectedProject() self.log.debug("Selected project: '{0}' (path: {1})".format( projectName, projectPath)) self.selectedProject = Project.loadProject(self.workspace, projectPath) self._refreshProjectProperties() self.view.projectsDuplicateButton.set_sensitive(True) self.view.projectsDeleteButton.set_sensitive(True) self.view.projectsExportButton.set_sensitive(True) self.view.projectsConfigureButton.set_sensitive(True) else: self.selectedProject = None self._refreshProjectProperties() self.view.projectsDuplicateButton.set_sensitive(False) self.view.projectsDeleteButton.set_sensitive(False) self.view.projectsExportButton.set_sensitive(False) self.view.projectsConfigureButton.set_sensitive(False)
def test_semanticAlignment_bug1(self): """test_semanticAlignment_bug1: A bug on the semantic alignment has been identified which prevent the computation of a valid regex. This test verifies the bug is not comming back. @date 18/04/2013 """ firstname1 = "antoine" email1 = "*****@*****.**" firstname2 = "luc" email2 = "*****@*****.**" msg1 = RawMessage(uuid.uuid4(), None, TypeConvertor.stringToNetzobRaw("6" + firstname1 + "GAHFSHQS" + email1)) msg2 = RawMessage(uuid.uuid4(), None, TypeConvertor.stringToNetzobRaw("3" + firstname2 + "CVSDHISD" + email2)) project = Project(uuid.uuid4(), "Experiment", datetime.now(), "") nwEngine = NeedlemanAndWunsch(8, project, False, None) symbol = Symbol(uuid.uuid4(), "Test", project) symbol.addMessages([msg1, msg2]) msg1.addSemanticTag("firstname", 2, 2 + len(firstname1) * 2) msg1.addSemanticTag("email", 2 + len(firstname1) * 2 + 16, 2 + len(firstname1) * 2 + 16 + len(email1) * 2) msg2.addSemanticTag("firstname", 2, 2 + len(firstname2) * 2) msg2.addSemanticTag("email", 2 + len(firstname2) * 2 + 16, 2 + len(firstname2) * 2 + 16 + len(email2) * 2) nwEngine.alignField(symbol.getField()) symbol.getField().setFormat(Format.STRING) print("Computed Regex : {0}".format(symbol.getRegex())) print(symbol.getCells(True)) computedFields = symbol.getExtendedFields() self.assertTrue(len(computedFields) > 1, "Only one field has been computed which tells us something went wrong.")
def createProjectAction_cb(self, button, entry, dialog): projectName = entry.get_text() # we verify a name has been provided if projectName == None or projectName == "": logging.warn(_("Unable to create a project with an empty name.")) errorDialog = NetzobErrorMessage(_("Unable to create a project with an empty name.")) return # We verify the project name doesn't already exist found = False for project in self.netzob.getCurrentWorkspace().getProjects(): if project.getName() == projectName: found = True if found: dialogBis = gtk.Dialog(title=_("Error"), flags=0, buttons=None) label = gtk.Label(_("This project name already exists")) label.show() dialogBis.action_area.pack_start(label, True, True, 0) dialogBis.set_size_request(250, 50) dialogBis.show() dialog.destroy() return # Creation of the project project = Project.createProject(self.netzob.getCurrentWorkspace(), projectName) self.netzob.switchCurrentProject(project) # Refresh menu self.update() dialog.destroy()
def importProjectAction(self, widget, data): chooser = gtk.FileChooserDialog(title=_("Export as"), action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) res = chooser.run() if res == gtk.RESPONSE_OK: fileName = chooser.get_filename() chooser.destroy() if os.path.isfile(fileName): idProject = str(uuid.uuid4()) # First we verify and create if necessary the directory of the project projectPath = "projects/" + idProject + "/" destPath = os.path.join(os.path.join(self.netzob.getCurrentWorkspace().getPath(), projectPath)) if not os.path.exists(destPath): logging.info("Creation of the directory " + destPath) os.mkdir(destPath) # Retrieving and storing of the config file try: destFile = os.path.join(destPath, Project.CONFIGURATION_FILENAME) shutil.copy(fileName, destFile) except IOError, e: logging.warn("Error when importing project: " + str(e)) return None project = Project.loadProject(self.netzob.getCurrentWorkspace(), destPath) project.setID(idProject) project.setName(_("Copy of {0}").format(project.getName())) project.setPath(projectPath) project.saveConfigFile(self.netzob.getCurrentWorkspace()) self.netzob.getCurrentWorkspace().referenceProject(project.getPath()) self.netzob.getCurrentWorkspace().saveConfigFile() NetzobInfoMessage(_("Project '{0}' correctly imported").format(project.getName())) self.update()
def importProjectApplyButton_clicked_cb(self, button): selectedFile = self.view.importProjectFileChooserButton.get_filename() if selectedFile is None: self.view.showErrorMessage(_("No file selected")) else: # Verify the file is a valid definition of a project if Project.loadProjectFromFile(selectedFile) is None: self.view.showErrorMessage( _("The file doesn't define a valid project.")) else: try: Project.importNewXMLProject(self.workspace, selectedFile) self.mainController.updateListOfAvailableProjects() self.view.destroy() except ProjectException, e: self.view.showErrorMessage(str(e))
def getNameOfProjects(self): nameOfProjects = [] for project_path in self.getProjectsPath(): from netzob.Common.Project import Project projectName = Project.getNameOfProject(self, project_path) if projectName is not None: nameOfProjects.append((projectName, project_path)) return nameOfProjects
def getProjects(self): projects = [] for project_path in self.getProjectsPath(): from netzob.Common.Project import Project project = Project.loadProject(self, project_path) if project is not None: projects.append(project) return projects
def switchProjectAction(self, widget, projectPath): newProject = Project.loadProject(self.netzob.getCurrentWorkspace(), projectPath) if newProject == None: logging.error("Impossible to switch to requested project due to an error in the loading process.") return # Loading the project based on its name self.netzob.switchCurrentProject(newProject) self.update()
def test_workspaceParsing(self): # We first load the workspace workspace = Workspace.loadWorkspace(ResourcesConfiguration.getWorkspaceFile()) self.assertNotEqual(workspace, None) # Now we load all the project which are declared in for project_path in workspace.getProjectsPath() : project = Project.loadProject(workspace, project_path) if project != None : logging.info("The project " + project.getName() + " has been loaded !")
def newProject_activate_cb(self, action): """Display the dialog in order to create a new project when the user request it through the menu.""" finish = False errorMessage = None while not finish: # open Dialogbox builder2 = Gtk.Builder() builder2.add_from_file(os.path.join(ResourcesConfiguration.getStaticResources(), "ui", "dialogbox.glade")) dialog = builder2.get_object("newProject") dialog.set_transient_for(self.view.mainWindow) applybutton = builder2.get_object("newProjectApplyButton") entry = builder2.get_object("entry4") entry.connect("changed", self.entry_disableButtonIfEmpty_cb, applybutton) if errorMessage is not None: # Display a warning message on the dialog box warnLabel = builder2.get_object("NewProjectWarnLabel") warnLabel.set_text(errorMessage) warnBox = builder2.get_object("newProjectWarnBox") warnBox.show_all() # Run the dialog window and wait for the result result = dialog.run() if result == 0: newProjectName = entry.get_text() dialog.destroy() self.log.debug("Verify the uniqueness of project name: {0}".format(newProjectName)) found = False for (projectName, projectPath) in self.currentWorkspace.getNameOfProjects(): if projectName == newProjectName: found = True break if found: self.log.info("A project with the same name already exists ({0}, {1}), please change it.".format(projectName, projectPath)) errorMessage = _("A project with this name exists") else: self.log.debug("Create new project {0}".format(newProjectName)) newProject = Project.createProject(self.getCurrentWorkspace(), newProjectName) self.switchProject(newProject.getPath()) finish = True errorMessage = None else: dialog.destroy() finish = True
def switchProject(self, projectPath): """Change the current project with the project declared in file projectPath. If the loading is successful the view is updated. If a current project is already loaded, it offers to save pending modifications before changing. @param projectPath: the path to the project to load @type projectPath: str """ if projectPath is not None: logging.debug("Switch to the project declared in {0}".format(projectPath)) newProject = Project.loadProject(self.currentWorkspace, projectPath) if newProject is not None and self.closeCurrentProject(): self.currentProject = newProject # Emit a signal for toolbar upgrade self.getSignalsManager().emitSignal(SignalsManager.SIG_PROJECT_OPEN) self.view.currentProjectHasChanged() else: self.view.currentProjectHasChanged()
def test_semanticAlignment_simple(self): """test_semanticAlignment_simple: Test that messages with embedded semantic are efficiently aligned. Format : <random 10 bytes><random username><random 5 ASCII><random email> Optimal Needleman & Wunsch Parameters : // Cost definitions for the alignment static const short int MATCH = 5; static const short int SEMANTIC_MATCH = 30; static const short int MISMATCH = -5; static const short int GAP = 0; static const short int BLEN = 10; // Consts for the definition of a mask static const unsigned char END = 2; static const unsigned char DIFFERENT = 1; static const unsigned char EQUAL = 0; """ project = Project(uuid.uuid4(), "Experiment", datetime.now(), "") symbol = Symbol(uuid.uuid4(), "Test", project) nbMessage = 500 usernames = [] emails = [] for iMessage in range(0, nbMessage): str_username = self.generateRandomString(4, 10) username = TypeConvertor.stringToNetzobRaw(str_username) usernames.append(str_username) email_prefix = self.generateRandomString(4, 10) email_domain = self.generateRandomString(4, 10) email_extension = self.generateRandomString(2, 3) str_email = "{0}@{1}.{2}".format(email_prefix, email_domain, email_extension) emails.append(str_email) email = TypeConvertor.stringToNetzobRaw(str_email) random10Bytes = self.generateRandomBytes(10, 10) random5ASCII = TypeConvertor.stringToNetzobRaw(self.generateRandomString(5, 5)) data = "{0}{1}{2}{3}".format(random10Bytes, username, random5ASCII, email) message = RawMessage(uuid.uuid4(), None, data) message.addSemanticTag("username", len(random10Bytes), len(random10Bytes) + len(username)) message.addSemanticTag("email", len(random10Bytes) + len(username) + len(random5ASCII), len(random10Bytes) + len(username) + len(random5ASCII) + len(email)) symbol.addMessage(message) nwEngine = NeedlemanAndWunsch(8, project, False, None) nwEngine.alignField(symbol.getField()) symbol.getField().setFormat(Format.STRING) print("Number of computed fields : {0}".format(len(symbol.getExtendedFields()))) self.assertEqual(4, len(symbol.getExtendedFields())) nbValidMessages = 0 for message in symbol.getMessages(): isValid = symbol.getField().isRegexValidForMessage(message) if isValid: nbValidMessages += 1 self.assertTrue(isValid) print(symbol.getCells()) print("Computed regex is valid for {0}/{1} messages.".format(nbValidMessages, len(symbol.getMessages())))