예제 #1
0
    def receiveSymbolWithTimeout(self, timeout):
        # First we read from the input the message
        receivedData = self.communicationChannel.read(timeout)

        nbMaxAttempts = 5

        if receivedData is None:
            self.log.warn("The communication channel seems to be closed !")
#            return (EmptySymbol(), None)
            return (None, None)

        if len(receivedData) > 0:
            now = datetime.datetime.now()
            receptionTime = now.strftime("%H:%M:%S")
            self.log.info("Received following message : " + TypeConvertor.bin2strhex(receivedData))

            # Now we abstract the message
            symbol = self.abstract(receivedData)

            # We store the received messages its time and its abstract representation
            self.inputMessages.append([receptionTime, TypeConvertor.bin2strhex(receivedData), symbol.getName()])
            self.registerInputSymbol(receptionTime, TypeConvertor.bin2strhex(receivedData), symbol)

            return (symbol, receivedData)
        else:
            if len(self.manipulatedSymbols) > nbMaxAttempts:
                if self.manipulatedSymbols[len(self.manipulatedSymbols) - 1].getType() == EmptySymbol.TYPE or self.manipulatedSymbols[len(self.manipulatedSymbols) - 1].getType() == UnknownSymbol.TYPE:
                    self.log.warn("Consider client has disconnected since no valid symbol received after " + str(nbMaxAttempts) + " attempts")
                    return (None, None)
            now = datetime.datetime.now()
            receptionTime = now.strftime("%H:%M:%S")
            symbol = EmptySymbol()
            self.registerInputSymbol(receptionTime, "", symbol)
            return (symbol, None)
    def read(self, timeout):
        self.log.debug("Reading from the socket some data (timeout = " + str(timeout))
        result = bitarray(endian='big')

        chars = []
        try:
            if timeout > 0:
                ready = select.select([self.socket], [], [], timeout)
                if ready[0]:
                    chars = self.socket.recv(4096)
            else:
                ready = select.select([self.socket], [], [])
                self.log.debug("ready = " + str(ready[0]))
                if ready[0]:
                    chars = self.socket.recv(4096)
        except:
            self.log.debug("Impossible to read from the network socket")
            return None

        if (len(chars) == 0):
            return result
        result = TypeConvertor.stringB2bin(chars)

        self.log.debug("Received : {0}".format(TypeConvertor.bin2strhex(result)))
        return result
예제 #3
0
    def receiveSymbolWithTimeout(self, timeout):
        # First we read from the input the message
        receivedData = self.communicationChannel.read(timeout)

        nbMaxAttempts = 5

        if receivedData is None:
            self.log.warn("The communication channel seems to be closed !")
            #            return (EmptySymbol(), None)
            return (None, None)

        if len(receivedData) > 0:
            now = datetime.datetime.now()
            receptionTime = now.strftime("%H:%M:%S")
            self.log.info("Received following message : " +
                          TypeConvertor.bin2strhex(receivedData))

            # Now we abstract the message
            symbol = self.abstract(receivedData)

            # We store the received messages its time and its abstract representation
            self.inputMessages.append([
                receptionTime,
                TypeConvertor.bin2strhex(receivedData),
                symbol.getName()
            ])
            self.registerInputSymbol(receptionTime,
                                     TypeConvertor.bin2strhex(receivedData),
                                     symbol)

            return (symbol, receivedData)
        else:
            if len(self.manipulatedSymbols) > nbMaxAttempts:
                if self.manipulatedSymbols[
                        len(self.manipulatedSymbols) - 1].getType(
                        ) == EmptySymbol.TYPE or self.manipulatedSymbols[
                            len(self.manipulatedSymbols) -
                            1].getType() == UnknownSymbol.TYPE:
                    self.log.warn(
                        "Consider client has disconnected since no valid symbol received after "
                        + str(nbMaxAttempts) + " attempts")
                    return (None, None)
            now = datetime.datetime.now()
            receptionTime = now.strftime("%H:%M:%S")
            symbol = EmptySymbol()
            self.registerInputSymbol(receptionTime, "", symbol)
            return (symbol, None)
예제 #4
0
    def toString(self):
        """toString:
                For debugging purpose.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(self.originalValue)
        else:
            readableValue = self.bin2str(self.originalValue)

        return "[Data] {0}, type: {1}, original value: {2}".format(AbstractVariable.toString(self), self.type.toString(), readableValue)
예제 #5
0
    def getDescription(self, writingToken):
        """getDescription:
                Get the full description of the variable.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(self.getValue(writingToken))
        else:
            readableValue = str(self.bin2str(self.getValue(writingToken)))

        return "{0}, value: {1}".format(self.toString(), readableValue)
예제 #6
0
    def getDescription(self, writingToken):
        """getDescription:
                Get the full description of the variable.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(
                self.getValue(writingToken))
        else:
            readableValue = str(self.bin2str(self.getValue(writingToken)))

        return "{0}, value: {1}".format(self.toString(), readableValue)
예제 #7
0
    def toString(self):
        """toString:
                For debugging purpose.
        """
        # We simply avoid to print unreadable binary.
        if self.type.getType() == BinaryType.TYPE:
            readableValue = TypeConvertor.bin2strhex(self.originalValue)
        else:
            readableValue = self.bin2str(self.originalValue)

        return "[Data] {0}, type: {1}, original value: {2}".format(
            AbstractVariable.toString(self), self.type.toString(),
            readableValue)
예제 #8
0
파일: Memory.py 프로젝트: windli4367/netzob
    def memorize(self, variable):
        """memorize:
                Save the current value of a variable in memory.

                @param variable: the given variable, the value of which we want to save.
        """
        if variable.getCurrentValue() is not None:
            self.temporaryMemory[variable.getID()] = variable.getCurrentValue()
            if self.memory_acces_cb is not None:
                value = variable.getCurrentValue()
                if value is not None:
                    value = TypeConvertor.bin2strhex(value)
                self.memory_acces_cb("W", variable, value)
예제 #9
0
파일: Memory.py 프로젝트: windli4367/netzob
    def restore(self, variable):
        """restore:
                Copy back the value of a variable from the real memory in the temporary memory.

                @param variable: the given variable, the value of which we want to restore.
        """
        if variable.getID() in self.memory.keys():
            self.temporaryMemory[variable.getID()] = self.memory[variable.getID()]
            if self.memory_acces_cb is not None:
                value = variable.getCurrentValue()
                if value is not None:
                    value = TypeConvertor.bin2strhex(value)
                self.memory_acces_cb("W", variable, value)
예제 #10
0
    def abstract(self, message):
        """abstract:
                Searches in the vocabulary the symbol which abstract the received message.

                @type message: netzob.Common.Models.AbstractMessage
                @param message: the message that is being read/compare/learn.
                @rtype: netzob.Common.Symbol
                @return: the symbol which content matches the message.
        """
        self.log.debug("We abstract the received message : " +
                       TypeConvertor.bin2strhex(message))
        # we search in the vocabulary an entry which match the message
        for symbol in self.vocabulary.getSymbols():
            self.log.debug("Try to abstract message through : {0}.".format(
                symbol.getName()))
            readingToken = VariableReadingToken(
                False, self.vocabulary, self.memory,
                TypeConvertor.strBitarray2Bitarray(message), 0)
            symbol.getRoot().read(readingToken)

            logging.debug(
                "ReadingToken: isOk: {0}, index: {1}, len(value): {2}".format(
                    str(readingToken.isOk()), str(readingToken.getIndex()),
                    str(len(readingToken.getValue()))))
            # The message matches if the read is ok and the whole entry was read.
            if readingToken.isOk() and readingToken.getIndex() == len(
                    readingToken.getValue()):
                self.log.debug("The message matches symbol {0}.".format(
                    symbol.getName()))
                # It matches so we learn from it if it's possible
                return symbol
            else:
                self.log.debug("The message doesn't match symbol {0}.".format(
                    symbol.getName()))
            # This is now managed in the variables modules.
            #===================================================================
            #    self.memory.createMemory()
            #    self.log.debug("We memorize the symbol " + str(symbol.getRoot()))
            #    readingToken = VariableReadingToken(False, self.vocabulary, self.memory, TypeConvertor.strBitarray2Bitarray(message), 0)
            #    symbol.getRoot().learn(readingToken)
            #    self.memory.persistMemory()
            #    return symbol
            # else:
            #    self.log.debug("Entry " + str(symbol.getID()) + " doesn't match")
            #    # we first restore a possibly learned value
            #    self.log.debug("Restore possibly learned value")
            #    processingToken = AbstractVariableProcessingToken(False, self.vocabulary, self.memory)
            #    symbol.getRoot().restore(processingToken)
            #===================================================================
        return UnknownSymbol()
예제 #11
0
    def generateValue(self, negative, dictionary):
        # Retrieve the value of the data to hash
        var = dictionary.getVariableByID(self.id_var)
        (binToHash, strToHash) = var.getValue(negative, dictionary)

        toHash = TypeConvertor.bin2string(binToHash)
        self.log.debug("Will hash the followings : " + toHash)

        md5core = hashlib.md5(self.init)
        md5core.update(toHash)

        md5Hex = md5core.digest()
        self.binVal = TypeConvertor.hex2bin(md5Hex)
        self.strVal = TypeConvertor.bin2strhex(self.binVal)
        self.log.debug("Generated MD5 = " + self.strVal)
예제 #12
0
    def generateValue(self, negative, dictionary):
        # Retrieve the value of the data to hash
        var = dictionary.getVariableByID(self.id_var)
        (binToHash, strToHash) = var.getValue(negative, dictionary)

        toHash = TypeConvertor.bin2string(binToHash)
        self.log.debug("Will hash the followings : " + toHash)

        md5core = hashlib.md5(self.init)
        md5core.update(toHash)

        md5Hex = md5core.digest()
        self.binVal = TypeConvertor.hex2bin(md5Hex)
        self.strVal = TypeConvertor.bin2strhex(self.binVal)
        self.log.debug("Generated MD5 = " + self.strVal)
예제 #13
0
파일: Memory.py 프로젝트: windli4367/netzob
    def recall(self, variable):
        """recall:
                Return the value of one variable store in the temporary memory.

                @param variable: the variable, the value of which we are searching.
                @return: the value of the given variable in the temporary memory.
        """
        if self.hasMemorized(variable):
            if self.memory_acces_cb is not None:
                value = self.temporaryMemory[variable.getID()]
                if value is not None:
                    value = TypeConvertor.bin2strhex(value)
                self.memory_acces_cb("R", variable, value)
            return self.temporaryMemory[variable.getID()]
        else:
            return None
예제 #14
0
    def writeSymbol(self, symbol):

        self.log.info("Sending symbol '" + str(symbol) + "' over the communication channel")
        # First we specialize the symbol in a message
        binMessage = self.specialize(symbol)
        if type(binMessage) == tuple:  # Means EmptySymbol or UnknownSymbol
            (binMessage, dummy) = binMessage
        strMessage = TypeConvertor.bin2strhex(binMessage)
        self.log.info("Write str message = '" + strMessage + "'")

        # now we send it
        now = datetime.datetime.now()
        sendingTime = now.strftime("%H:%M:%S")
        self.outputMessages.append([sendingTime, strMessage, symbol.getName()])
        self.registerOutputSymbol(sendingTime, strMessage, symbol)

        self.communicationChannel.write(binMessage)
예제 #15
0
    def writeSymbol(self, symbol):

        self.log.info("Sending symbol '" + str(symbol) +
                      "' over the communication channel")
        # First we specialize the symbol in a message
        binMessage = self.specialize(symbol)
        if type(binMessage) == tuple:  # Means EmptySymbol or UnknownSymbol
            (binMessage, dummy) = binMessage
        strMessage = TypeConvertor.bin2strhex(binMessage)
        self.log.info("Write str message = '" + strMessage + "'")

        # now we send it
        now = datetime.datetime.now()
        sendingTime = now.strftime("%H:%M:%S")
        self.outputMessages.append([sendingTime, strMessage, symbol.getName()])
        self.registerOutputSymbol(sendingTime, strMessage, symbol)

        self.communicationChannel.write(binMessage)
예제 #16
0
    def abstract(self, message):
        """abstract:
                Searches in the vocabulary the symbol which abstract the received message.

                @type message: netzob.Common.Models.AbstractMessage
                @param message: the message that is being read/compare/learn.
                @rtype: netzob.Common.Symbol
                @return: the symbol which content matches the message.
        """
        self.log.debug("We abstract the received message : " + TypeConvertor.bin2strhex(message))
        # we search in the vocabulary an entry which match the message
        for symbol in self.vocabulary.getSymbols():
            self.log.debug("Try to abstract message through : {0}.".format(symbol.getName()))
            readingToken = VariableReadingToken(False, self.vocabulary, self.memory, TypeConvertor.strBitarray2Bitarray(message), 0)
            symbol.getRoot().read(readingToken)

            logging.debug("ReadingToken: isOk: {0}, index: {1}, len(value): {2}".format(str(readingToken.isOk()), str(readingToken.getIndex()), str(len(readingToken.getValue()))))
            # The message matches if the read is ok and the whole entry was read.
            if readingToken.isOk() and readingToken.getIndex() == len(readingToken.getValue()):
                self.log.debug("The message matches symbol {0}.".format(symbol.getName()))
                # It matches so we learn from it if it's possible
                return symbol
            else:
                self.log.debug("The message doesn't match symbol {0}.".format(symbol.getName()))
            # This is now managed in the variables modules.
            #===================================================================
            #    self.memory.createMemory()
            #    self.log.debug("We memorize the symbol " + str(symbol.getRoot()))
            #    readingToken = VariableReadingToken(False, self.vocabulary, self.memory, TypeConvertor.strBitarray2Bitarray(message), 0)
            #    symbol.getRoot().learn(readingToken)
            #    self.memory.persistMemory()
            #    return symbol
            # else:
            #    self.log.debug("Entry " + str(symbol.getID()) + " doesn't match")
            #    # we first restore a possibly learned value
            #    self.log.debug("Restore possibly learned value")
            #    processingToken = AbstractVariableProcessingToken(False, self.vocabulary, self.memory)
            #    symbol.getRoot().restore(processingToken)
            #===================================================================
        return UnknownSymbol()
예제 #17
0
    def learn(self, val, indice, isForced, dictionary):

        if self.strVal is None or isForced:
            tmp = val[indice:]
            self.log.debug("Taille MD5 " + str(len(tmp)))
            # MD5 size = 16 bytes = 16*8 = 128
            if (len(tmp) >= 128):
                binVal = tmp[0:128]
                # We verify its realy the MD5
                var = dictionary.getVariableByID(self.id_var)
                (binToHash, strToHash) = var.getValue(False, dictionary)

                toHash = TypeConvertor.bin2string(binToHash)
                self.log.debug("Will hash the followings : " + toHash)

                md5core = hashlib.md5(self.init)
                md5core.update(toHash)

                md5Hex = md5core.digest()

                self.log.debug("We should received an MD5 = " +
                               str(TypeConvertor.hex2bin(md5Hex)))
                self.log.debug("We have received " + str(binVal))

                if (TypeConvertor.hex2bin(md5Hex) == binVal):
                    self.binVal = TypeConvertor.hex2bin(md5Hex)
                    self.strVal = TypeConvertor.bin2strhex(self.binVal)
                    self.log.debug("Perfect, there are equals we return  " +
                                   str(len(binVal)))
                    return indice + len(binVal)
                else:
                    return -1

            else:
                return -1

        self.log.debug("value = " + str(self.strVal) + ", isForced = " +
                       str(isForced))
        return -1
예제 #18
0
 def read(self, timeout):
     chars = []
     try:
         if timeout > 0:
             self.log.debug("Reading from the socket with a timeout of " + str(timeout))
             ready = select.select([self.socket], [], [], timeout)
             if ready[0]:
                 chars = self.socket.recv(4096)
         else:
             self.log.debug("Reading from the socket without any timeout")
             ready = select.select([self.socket], [], [])
             if ready[0]:
                 chars = self.socket.recv(4096)
     except:
         self.log.debug("Impossible to read from the network socket")
         return None
     result = TypeConvertor.string2bin("".join(chars), "big")
     self.log.debug("Read finished")
     if (len(chars) == 0):
         return result
     self.log.debug("Received : {0}".format(TypeConvertor.bin2strhex(result)))
     return result
예제 #19
0
 def read(self, timeout):
     chars = []
     try:
         if timeout > 0:
             self.log.debug("Reading from the socket with a timeout of " +
                            str(timeout))
             ready = select.select([self.socket], [], [], timeout)
             if ready[0]:
                 chars = self.socket.recv(4096)
         else:
             self.log.debug("Reading from the socket without any timeout")
             ready = select.select([self.socket], [], [])
             if ready[0]:
                 chars = self.socket.recv(4096)
     except:
         self.log.debug("Impossible to read from the network socket")
         return None
     result = TypeConvertor.string2bin("".join(chars), "big")
     self.log.debug("Read finished")
     if (len(chars) == 0):
         return result
     self.log.debug("Received : {0}".format(
         TypeConvertor.bin2strhex(result)))
     return result
예제 #20
0
    def learn(self, val, indice, isForced, dictionary):

        if self.strVal is None or isForced:
            tmp = val[indice:]
            self.log.debug("Taille MD5 " + str(len(tmp)))
            # MD5 size = 16 bytes = 16*8 = 128
            if (len(tmp) >= 128):
                binVal = tmp[0:128]
                # We verify its realy the MD5
                var = dictionary.getVariableByID(self.id_var)
                (binToHash, strToHash) = var.getValue(False, dictionary)

                toHash = TypeConvertor.bin2string(binToHash)
                self.log.debug("Will hash the followings : " + toHash)

                md5core = hashlib.md5(self.init)
                md5core.update(toHash)

                md5Hex = md5core.digest()

                self.log.debug("We should received an MD5 = " + str(TypeConvertor.hex2bin(md5Hex)))
                self.log.debug("We have received " + str(binVal))

                if (TypeConvertor.hex2bin(md5Hex) == binVal):
                    self.binVal = TypeConvertor.hex2bin(md5Hex)
                    self.strVal = TypeConvertor.bin2strhex(self.binVal)
                    self.log.debug("Perfect, there are equals we return  " + str(len(binVal)))
                    return indice + len(binVal)
                else:
                    return -1

            else:
                return -1

        self.log.debug("value = " + str(self.strVal) + ", isForced = " + str(isForced))
        return -1
예제 #21
0
 def toString(self):
     """toString:
             Used for debug purpose.
     """
     return "WritingToken: isOk: {0}, value: {1}".format(
         str(self.isOk()), TypeConvertor.bin2strhex(self.value))
예제 #22
0
 def toString(self):
     """toString:
             Used for debug purpose.
     """
     return "WritingToken: isOk: {0}, value: {1}".format(str(self.isOk()), TypeConvertor.bin2strhex(self.value))
예제 #23
0
 def toString(self):
     """toString:
             Used for debug purpose.
     """
     return "ReadingToken: isOk: {0}, value left: {1}".format(str(self.isOk()), TypeConvertor.bin2strhex(self.value[self.index:]))