예제 #1
0
    def learn(self, value, indice, negative, vocabulary, memory):
        if self.format == Format.ASCII:
            currentContent = TypeConvertor.bin2string(value[indice:])
            IPRegex = re.compile("(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))")
            hasMatched = False
            for t in range(min(len(currentContent), 15), 7, -1):
                currentPossibleIP = currentContent[:t]
                result = IPRegex.match(currentPossibleIP)
                if result != None:
                    hasMatched = True
                elif hasMatched:
                    break

            if hasMatched:
                result = currentContent[:t + 2]
                self.log.debug("Learn from received message : " + str(result))

                strCurrentValue = str(result)
                binCurrentValue = TypeConvertor.string2bin(result, 'big')
                memory.memorize(self, (binCurrentValue, strCurrentValue))

                return len(TypeConvertor.string2bin(result, 'big'))
            else:
                self.log.debug("Compare on format was not successfull")
                return -1
        else:
            raise NotImplementedError("Error, the current variable (IPv4Variable) doesn't support function compareFormat in this case")
예제 #2
0
 def computeCurrentValue(self, strValue):
     if strValue != None:
         strCurrentValue = strValue
         binCurrentValue = TypeConvertor.string2bin(strValue)
         self.currentValue = (binCurrentValue, strCurrentValue)
     else:
         self.currentValue = None
예제 #3
0
    def __init__(self, text):
        AbstractValue.__init__(self, "TextValue")
        # create logger with the given configuration
        self.log = logging.getLogger('netzob.Common.MMSTD.Dictionary.Values.TextValue.py')

        self.strtext = text
        self.bintext = TypeConvertor.string2bin(self.strtext, 'big')
예제 #4
0
    def __init__(self, text):
        AbstractValue.__init__(self, "TextValue")
        # create logger with the given configuration
        self.log = logging.getLogger(
            'netzob.Common.MMSTD.Dictionary.Values.TextValue.py')

        self.strtext = text
        self.bintext = TypeConvertor.string2bin(self.strtext, 'big')
예제 #5
0
    def generateValue(self, negative, dictionary):

        variable = dictionary.getVariableByID(self.idVar)
        (binValue, strValue) = variable.getValue(negative, dictionary)

        self.log.debug("GENERATE VALUE of size : " + str(binValue))
        nb_letter = TypeConvertor.bin2int(binValue)
        self.strVal = ''.join(random.choice(string.ascii_letters) for x in range(nb_letter))
        self.binVal = TypeConvertor.string2bin(self.strVal, 'big')
        self.log.debug("Generated value = " + self.strVal)
        self.log.debug("Generated value = " + str(self.binVal))
예제 #6
0
    def generateValue(self, negative, dictionary):

        variable = dictionary.getVariableByID(self.idVar)
        (binValue, strValue) = variable.getValue(negative, dictionary)

        self.log.debug("GENERATE VALUE of size : " + str(binValue))
        nb_letter = TypeConvertor.bin2int(binValue)
        self.strVal = ''.join(random.choice(string.ascii_letters) for x in range(nb_letter))
        self.binVal = TypeConvertor.string2bin(self.strVal, 'big')
        self.log.debug("Generated value = " + self.strVal)
        self.log.debug("Generated value = " + str(self.binVal))
예제 #7
0
    def computeCurrentValue(self, strValue):
        """computeCurrentValue:
                Compute a couple of binary and string values for the current variable.

                @type strValue: string
                @param strValue: a string value proposed as default value for this variable.
        """
        if strValue is not None:
            strCurrentValue = strValue
            binCurrentValue = TypeConvertor.string2bin(strValue)
            self.currentValue = (binCurrentValue, strCurrentValue)
        else:
            self.currentValue = None
예제 #8
0
    def computeCurrentValue(self, strValue):
        """computeCurrentValue:
                Compute a couple of binary and string values for the current variable.

                @type strValue: string
                @param strValue: a string value proposed as default value for this variable.
        """
        if strValue is not None:
            strCurrentValue = strValue
            binCurrentValue = TypeConvertor.string2bin(strValue)
            self.currentValue = (binCurrentValue, strCurrentValue)
        else:
            self.currentValue = None
예제 #9
0
    def learn(self, value, indice, negative, vocabulary, memory):
        """learn:
                Compare the current variable to the end (starting at the "indice"-th character) of value.
                Moreover it stores learns from the provided message.
                Return the number of letters that matches, -1 if it does not match.
        """
        if self.format == Format.ASCII:
            currentContent = TypeConvertor.bin2string(value[indice:])
            IPRegex = re.compile(
                "(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
            )
            hasMatched = False
            for t in range(min(len(currentContent), 15), 7, -1):
                currentPossibleIP = currentContent[:t]
                result = IPRegex.match(currentPossibleIP)
                if result is not None:
                    hasMatched = True
                elif hasMatched:
                    break

            if hasMatched:
                result = currentContent[:t + 2]
                self.log.debug("Learn from received message : " + str(result))

                strCurrentValue = str(result)
                binCurrentValue = TypeConvertor.string2bin(result, 'big')
                memory.memorize(self, (binCurrentValue, strCurrentValue))

                return len(TypeConvertor.string2bin(result, 'big'))
            else:
                self.log.debug("Compare on format was not successfull")
                return -1
        else:
            raise NotImplementedError(
                "Error, the current variable (IPv4Variable) doesn't support function compareFormat in this case"
            )
예제 #10
0
    def learn(self, value, indice, negative, vocabulary, memory):
        """learn:
                Compare the current variable to the end (starting at the "indice"-th character) of value.
                Moreover it stores learns from the provided message.
                Return the number of letters that matches, -1 if it does not match.
        """
        if self.format == Format.ASCII:
            currentContent = TypeConvertor.bin2string(value[indice:])
            IPRegex = re.compile(
                "(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
            )
            hasMatched = False
            for t in range(min(len(currentContent), 15), 7, -1):
                currentPossibleIP = currentContent[:t]
                result = IPRegex.match(currentPossibleIP)
                if result is not None:
                    hasMatched = True
                elif hasMatched:
                    break

            if hasMatched:
                result = currentContent[: t + 2]
                self.log.debug("Learn from received message : " + str(result))

                strCurrentValue = str(result)
                binCurrentValue = TypeConvertor.string2bin(result, "big")
                memory.memorize(self, (binCurrentValue, strCurrentValue))

                return len(TypeConvertor.string2bin(result, "big"))
            else:
                self.log.debug("Compare on format was not successfull")
                return -1
        else:
            raise NotImplementedError(
                "Error, the current variable (IPv4Variable) doesn't support function compareFormat in this case"
            )
예제 #11
0
 def computeCurrentValue(self, strValue):
     if strValue != None:
         if self.format == Format.ASCII:
             strCurrentValue = str(strValue)
             binCurrentValue = TypeConvertor.string2bin(strValue, 'big')
         elif self.format == Format.HEX:
             hexVal = TypeConvertor.ipToNetzobRaw(strValue)
             if hexVal != None:
                 strCurrentValue = str(strValue)
                 binCurrentValue = TypeConvertor.netzobRawToBitArray(hexVal)
             else:
                 strCurrentValue = str("None:Error")
                 binCurrentValue = None
         self.currentValue = (binCurrentValue, strCurrentValue)
     else:
         self.currentValue = None
예제 #12
0
    def getValueToSend(self, negative, vocabulary, memory):
        if self.getCurrentValue() != None:
            return self.getCurrentValue()

        if memory.hasMemorized(self):
            return memory.recall(self)

        # We generate a new value
        strValue = self.generateValue()
        binValue = TypeConvertor.string2bin(strValue)

        # We save in memory the current value
        memory.memorize(self, (binValue, strValue))

        # We return the newly generated and memorized value
        return (binValue, strValue)
예제 #13
0
    def getValueToSend(self, negative, vocabulary, memory):
        """getValueToSend:
                Get the current value of the variable it can be the original value if its set and not forget or the value in memory if it has one or it generates one and save its value in memory.
        """
        if self.getCurrentValue() is not None:
            return self.getCurrentValue()

        if memory.hasMemorized(self):
            return memory.recall(self)

        # We generate a new value
        strValue = self.generateValue()
        binValue = TypeConvertor.string2bin(strValue)

        # We save in memory the current value
        memory.memorize(self, (binValue, strValue))

        # We return the newly generated and memorized value
        return (binValue, strValue)
예제 #14
0
    def getValueToSend(self, negative, vocabulary, memory):
        """getValueToSend:
                Get the current value of the variable it can be the original value if its set and not forget or the value in memory if it has one or it generates one and save its value in memory.
        """
        if self.getCurrentValue() is not None:
            return self.getCurrentValue()

        if memory.hasMemorized(self):
            return memory.recall(self)

        # We generate a new value
        strValue = self.generateValue()
        binValue = TypeConvertor.string2bin(strValue)

        # We save in memory the current value
        memory.memorize(self, (binValue, strValue))

        # We return the newly generated and memorized value
        return (binValue, strValue)
예제 #15
0
    def compareFormat(self, value, indice, negative, vocabulary, memory):
        """compareFormat:
                Compute if the provided data is "format-compliant" and return the size of the biggest compliant data.

                @type value: bitarray.bitarray
                @param value: a bit array a subarray of which we compare to the current variable binray value.
                @type indice: integer
                @param indice: the starting point of comparison in value.
                @type negative: boolean
                @param negative: tells if we use the variable or a logical not of it.
                @type vocabulary: netzob.Common.Vocabulary.Vocabulary
                @param vocabulary: the vocabulary of the current project.
                @type memory: netzob.Common.MMSTD.Memory.Memory
                @param memory: a memory which can contain a former value of the variable.
                @rtype: integer
                @return: the size of the biggest compliant data, -1 if it does not comply.
        """
        if self.format == Format.ASCII:
            currentContent = TypeConvertor.bin2string(value[indice:])
            IPRegex = re.compile(
                "(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
            )
            hasMatched = False
            for t in range(min(len(currentContent), 15), 7, -1):
                currentPossibleIP = currentContent[:t]
                result = IPRegex.match(currentPossibleIP)
                if result is not None:
                    hasMatched = True
                elif hasMatched:
                    break

            if hasMatched:
                result = currentContent[:t + 2]
                self.log.debug("Compare on format was successfull : " +
                               str(result))
                return len(TypeConvertor.string2bin(result, 'big'))
            else:
                self.log.debug("Compare on format was not successfull")
                return -1
        else:
            raise NotImplementedError(
                "Error, the current variable (IPv4Variable) doesn't support function compareFormat in this case"
            )
예제 #16
0
    def compareFormat(self, value, indice, negative, vocabulary, memory):
        """compareFormat:
                Compute if the provided data is "format-compliant" and return the size of the biggest compliant data.

                @type value: bitarray.bitarray
                @param value: a bit array a subarray of which we compare to the current variable binray value.
                @type indice: integer
                @param indice: the starting point of comparison in value.
                @type negative: boolean
                @param negative: tells if we use the variable or a logical not of it.
                @type vocabulary: netzob.Common.Vocabulary.Vocabulary
                @param vocabulary: the vocabulary of the current project.
                @type memory: netzob.Common.MMSTD.Memory.Memory
                @param memory: a memory which can contain a former value of the variable.
                @rtype: integer
                @return: the size of the biggest compliant data, -1 if it does not comply.
        """
        if self.format == Format.ASCII:
            currentContent = TypeConvertor.bin2string(value[indice:])
            IPRegex = re.compile(
                "(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
            )
            hasMatched = False
            for t in range(min(len(currentContent), 15), 7, -1):
                currentPossibleIP = currentContent[:t]
                result = IPRegex.match(currentPossibleIP)
                if result is not None:
                    hasMatched = True
                elif hasMatched:
                    break

            if hasMatched:
                result = currentContent[: t + 2]
                self.log.debug("Compare on format was successfull : " + str(result))
                return len(TypeConvertor.string2bin(result, "big"))
            else:
                self.log.debug("Compare on format was not successfull")
                return -1
        else:
            raise NotImplementedError(
                "Error, the current variable (IPv4Variable) doesn't support function compareFormat in this case"
            )
예제 #17
0
    def computeCurrentValue(self, strValue):
        """computeCurrentValue:
                Compute a couple of binary and string values for the current variable.

                @type strValue: string
                @param strValue: a string value proposed as default value for this variable, must be a "real" ip address declared in ASCII like : "192.168.0.10".
        """
        if strValue is not None:
            if self.format == Format.ASCII:
                strCurrentValue = str(strValue)
                binCurrentValue = TypeConvertor.string2bin(strValue, "big")
            elif self.format == Format.HEX:
                hexVal = TypeConvertor.ipToNetzobRaw(strValue)
                if hexVal is not None:
                    strCurrentValue = str(strValue)
                    binCurrentValue = TypeConvertor.netzobRawToBitArray(hexVal)
                else:
                    strCurrentValue = str("None:Error")
                    binCurrentValue = None
            self.currentValue = (binCurrentValue, strCurrentValue)
        else:
            self.currentValue = None
예제 #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 computeCurrentValue(self, strValue):
        """computeCurrentValue:
                Compute a couple of binary and string values for the current variable.

                @type strValue: string
                @param strValue: a string value proposed as default value for this variable, must be a "real" ip address declared in ASCII like : "192.168.0.10".
        """
        if strValue is not None:
            if self.format == Format.ASCII:
                strCurrentValue = str(strValue)
                binCurrentValue = TypeConvertor.string2bin(strValue, 'big')
            elif self.format == Format.HEX:
                hexVal = TypeConvertor.ipToNetzobRaw(strValue)
                if hexVal is not None:
                    strCurrentValue = str(strValue)
                    binCurrentValue = TypeConvertor.netzobRawToBitArray(hexVal)
                else:
                    strCurrentValue = str("None:Error")
                    binCurrentValue = None
            self.currentValue = (binCurrentValue, strCurrentValue)
        else:
            self.currentValue = None
예제 #20
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
예제 #21
0
 def str2bin(self, stri):
     return TypeConvertor.string2bin(stri)