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. """ tmp = value[indice:] size = len(tmp) if size <= 8: self.log.debug("Too small, not even 8 bits available (1 number)") return -1 for i in range(size, 8, -1): subValue = value[indice:indice + i - 1] strVal = TypeConvertor.bin2string( TypeConvertor.strBitarray2Bitarray(subValue)) typeIdentifier = TypeIdentifier() if typeIdentifier.isAscii(strVal): if (strVal.isdigit()): self.log.debug("Its a numeric : (" + str(strVal) + ")") return i + indice - 1 self.log.debug("the value " + str( TypeConvertor.bin2string(TypeConvertor.strBitarray2Bitarray(tmp))) + " cannot be parsed as a decimalWord") return -1
def getSearchedDataForHexadecimal(self, value, extraInfo=None): typeIdentifier = TypeIdentifier() if not typeIdentifier.isHexString(value): return [] # Creation of a SearchTask task = SearchTask(value, value, Format.HEX) task.registerVariation( value, "Hex repr of '{0}'({1}))".format(value, extraInfo)) # task.registerVariation(value[::-1], "Inverted representation of '{0}'".format(value[::-1])) print task return [task]
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. """ tmp = value[indice:] size = len(tmp) if size <= 16: self.log.debug("Too small, not even 16 bits available (2 letters)") return -1 for i in range(size, 16, -1): subValue = value[indice:indice + i - 1] if (i - 1) % 8 == 0: strVal = TypeConvertor.bin2string( TypeConvertor.strBitarray2Bitarray(subValue)) typeIdentifier = TypeIdentifier() if typeIdentifier.isAscii(strVal): self.log.debug("Its an ascii : (" + str(strVal) + ")") if (not ' ' in strVal and not '\n' in strVal and not '\r' in strVal): self.log.debug("Its an ascii without space : (" + str(strVal) + ")") self.log.debug( "Binary value of the ascii : %s" % str(TypeConvertor.strBitarray2Bitarray(subValue))) return indice + i - 1 return -1
def test_getTypesBinary(self): alphabet = list(map(chr, list(range(97, 123)))) alpha = alphabet[random.randint(0, len(alphabet) - 1)] typeIdentifier = TypeIdentifier() hexOfNumber = str(hex(ord(alpha)))[2:] self.assertIn(Format.BINARY, typeIdentifier.getTypes(hexOfNumber))
def test_getTypesBase64(self): string = "Vive Netzob !" base64String = base64.encodestring(string) typeIdentifier = TypeIdentifier() self.assertIn(Format.BASE64_ENC, typeIdentifier.getTypes(base64String))
def test_getTypesAscii(self): alphabet = list(map(chr, list(range(97, 123)))) alpha = alphabet[random.randint(0, len(alphabet) - 1)] typeIdentifier = TypeIdentifier() self.assertIn(Format.ASCII, typeIdentifier.getTypes(alpha))
def test_getTypesNum(self): number = random.randint(0, 10000) typeIdentifier = TypeIdentifier() self.assertIn(Format.DECIMAL, typeIdentifier.getTypes(number))
def test_getTypesAlpha(self): alphabet = map(chr, range(97, 123)) alpha = alphabet[random.randint(0, len(alphabet) - 1)] typeIdentifier = TypeIdentifier() self.assertIn(Format.ALPHA, typeIdentifier.getTypes(alpha))