def loadFromXML(xmlRoot, namespace, version): if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:NormalState": from netzob.Common.MMSTD.States.impl.NormalState import NormalState return NormalState.loadFromXML(xmlRoot, namespace, version) else: raise NameError("The parsed xml doesn't represent a valid type message.") return None
def loadFromXML(xmlRoot, namespace, version): if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:NormalState": from netzob.Common.MMSTD.States.impl.NormalState import NormalState return NormalState.loadFromXML(xmlRoot, namespace, version) else: raise NameError( "The parsed xml doesn't represent a valid type message.") return None
def toMMSTD(self, dictionary, isMaster): # We create an MMSTD which will submit the following symbols generatedStates = [] # Create the transition which opens the connection rootState = NormalState(0, "State 0") generatedStates.append(rootState) initialState = NormalState(1, "State 1") generatedStates.append(initialState) openingTransition = OpenChannelTransition(0, "Connection", rootState, initialState, 15000, 3) rootState.registerTransition(openingTransition) previousState = initialState idState = 2 for symbol in self.symbols: # we create the current state currentState = NormalState(idState, "State " + str(idState)) generatedStates.append(currentState) # we create a normal transition between it and the previous state idTransition = idState - 1 transition = SimpleTransition(idTransition, "Transition " + str(idTransition), previousState, currentState, 1000, symbol) previousState.registerTransition(transition) idState = idState + 1 previousState = currentState if not isMaster: # We create the opening transition to listen for the first entry currentState = NormalState(idState, "State " + str(idState)) generatedStates.append(currentState) transition = SimpleTransition(idState - 1, "Transition " + str(idState - 1), previousState, currentState, 1000, EmptySymbol()) previousState.registerTransition(transition) previousState = currentState idState += 1 # Create the transition which close the connection endState = NormalState(idState, "State " + str(idState)) generatedStates.append(endState) closingTransition = CloseChannelTransition(idState - 1, "Disconnection", currentState, endState, 1000) currentState.registerTransition(closingTransition) mmstd = MMSTD(rootState, dictionary) for state in generatedStates: mmstd.addState(state) return mmstd
def createButton_clicked_cb(self, event): currentProject = self.grammarController.getCurrentProject() """callback executed when the user clicks on the create button""" initialState = self._view.initialStateCheckButton.get_active() stateName = self._view.nameEntry.get_text() automata = currentProject.getGrammar().getAutomata() errorMessage = None # verify initialState is valid if not initialState and automata is None: errorMessage = _("The first created state must be an initial state") self.displayErrorMessage(errorMessage) return # verify the name of the state is unique found = False if automata is not None: for state in automata.getStates(): if state.getName() == stateName: found = True break if found: errorMessage = _("A state already has this name, please specify another one") self.displayErrorMessage(errorMessage) return newState = NormalState(self.idState, stateName) if automata is None: automata = MMSTD(newState, currentProject.getVocabulary()) currentProject.getGrammar().setAutomata(automata) automata.addState(newState) self._view.destroy() self.grammarController.restart()
def executeAsClient(self, abstractionLayer): self.log.debug( "Client is it a server ? " + str(abstractionLayer.getCommunicationChannel().isServer())) if abstractionLayer.getCommunicationChannel().isServer(): self.log.debug("Cleaning the memory") # abstractionLayer.getMemory().cleanMemory() self.activate() self.log.info( "We instanciate a new server and close the current MMSTD") abstractionLayer.openServer(abstractionLayer.getVocabulary(), self.outputState, False) self.deactivate() startTime = datetime.now() # Here we wait for someone to connect to our server ! finish = abstractionLayer.isConnected() error = False while (not finish): self.log.info("No one is connected !") currentTime = datetime.now() if ((currentTime - startTime).microseconds > self.connectionTime): finish = True error = True else: finish = abstractionLayer.isConnected() time.sleep(1) if error: self.log.warn("No client has connect to our oracle.") return None else: error = False startTime = datetime.now() finish = not abstractionLayer.isConnected() while (not finish): currentTime = datetime.now() if ((currentTime - startTime).microseconds > 60000): finish = True error = True else: finish = not abstractionLayer.isConnected() time.sleep(1) if error: self.log.warn( "Stop the server even if the client are still up") self.log.debug( "The openChannelTransition finishes (the generated instance has been closed)!" ) # We create a Close Channel Transition to close the server inputState = NormalState( str(uuid.uuid4()), "Input State of the close server transition") outputState = NormalState( str(uuid.uuid4()), "Output State of the close server transition") closeChannelTransition = CloseChannelTransition( str(uuid.uuid4()), "Close Server transition", inputState, outputState, 300) inputState.registerTransition(closeChannelTransition) return inputState else: self.activate() result = self.openConnection(abstractionLayer) self.deactivate() if result: return self.outputState else: return None
def executeAsClient(self, abstractionLayer): self.log.debug("Client is it a server ? " + str(abstractionLayer.getCommunicationChannel().isServer())) if abstractionLayer.getCommunicationChannel().isServer(): self.log.debug("Cleaning the memory") abstractionLayer.getMemory().cleanMemory() self.activate() self.log.info("We instanciate a new server and close the current MMSTD") abstractionLayer.openServer(abstractionLayer.getVocabulary(), self.outputState, False) self.deactivate() startTime = datetime.now() # Here we wait for someone to connect to our server ! finish = abstractionLayer.isConnected() error = False while (not finish): self.log.info("No one is connected !") currentTime = datetime.now() if ((currentTime - startTime).microseconds > self.connectionTime): finish = True error = True else: finish = abstractionLayer.isConnected() time.sleep(1) if error: self.log.warn("No client has connect to our oracle.") return None else: error = False startTime = datetime.now() finish = not abstractionLayer.isConnected() while (not finish): currentTime = datetime.now() if ((currentTime - startTime).microseconds > 60000): finish = True error = True else: finish = not abstractionLayer.isConnected() time.sleep(1) if error: self.log.warn("Stop the server even if the client are still up") self.log.debug("The openChannelTransition finishes (the generated instance has been closed)!") # We create a Close Channel Transition to close the server inputState = NormalState(uuid.uuid4(), "Input State of the close server transition") outputState = NormalState(uuid.uuid4(), "Output State of the close server transition") closeChannelTransition = CloseChannelTransition(uuid.uuid4(), "Close Server transition", inputState, outputState, 300) inputState.registerTransition(closeChannelTransition) return inputState else: self.activate() result = self.openConnection(abstractionLayer) self.deactivate() if result: return self.outputState else: return None
def computeAutomata(self): wordAndStates = [] startState = None idState = 0 idTransition = 0 states = [] self.log.info("Compute the automata...") # Create the states of the automata uniqueRowsInS = self.getUniqueRowsInS() for (w, r) in uniqueRowsInS: self.log.info("The row with word {0} is unique !".format(str(w))) # We create a State for each unique row nameState = self.appendValuesInRow(r) self.log.info("Create state: {0}".format(nameState)) currentState = NormalState(idState, nameState) states.append(currentState) wordAndStates.append((w, currentState)) # Is it the starting state (wordS = [EmptySymbol]) if startState is None and w == MembershipQuery([EmptySymbol()]): startState = currentState self.log.info("Its the starting state") idState = idState + 1 self.log.debug("Create the transition of the automata") # Create the transitions of the automata for (word, state) in wordAndStates: self.log.debug("Working on state: {0}".format(str(state.getName()))) for symbol in self.initialD: # retrieve the value: dicValue = self.observationTable[symbol] value = dicValue[word] # search for the output state mq = word.getMQSuffixedWithMQ(symbol) self.log.debug("> What happen when we send " + str(symbol) + " after " + str(word)) self.log.debug(">> " + str(mq)) for wordSandSA in self.getSandSAWords(): self.log.info("IS " + str(wordSandSA) + " eq " + str(mq)) if wordSandSA == mq: self.log.info("YES its equal") rowOutputState = self.getRowOfObservationTable(wordSandSA) outputStateName = self.appendValuesInRow(rowOutputState) self.log.debug("rowOutputState = " + str(rowOutputState)) self.log.debug("outputStateName = " + str(outputStateName)) # search for the state having this name: outputState = None self.log.info("Search for the output state: {0}".format(outputStateName)) for (w2, s2) in wordAndStates: if s2.getName() == outputStateName: outputState = s2 self.log.info(" == " + str(s2.getName())) else: self.log.info(" != " + str(s2.getName())) if outputState is not None: inputSymbol = symbol.getSymbolsWhichAreNotEmpty()[0] self.log.info("We create a transition from " + str(state.getName()) + "=>" + str(outputState.getName())) self.log.info(" input: {0}".format(str(inputSymbol))) self.log.info(" output: {0}".format(str(value))) transition = SemiStochasticTransition(idTransition, "Transition " + str(idTransition), state, outputState, inputSymbol) transition.addOutputSymbol(value, 100, 1000) state.registerTransition(transition) idTransition = idTransition + 1 else: self.log.error("<!!> Impossible to retrieve the output state named " + str(s2.getName())) if startState is not None: self.log.info("An infered automata has been computed.") self.inferedAutomata = MMSTD(startState, self.dictionary) for state in states: self.inferedAutomata.addState(state) self.log.info(self.inferedAutomata.getDotCode())