def __init__(self, _type, unitSize=None, endianness=None, sign=None): """Creates a new encoding function that will encode the data with the specified types and following its attributes. If an attribute is not specified (or set to None), it takes its default value defined in :class:`netzob.Model.Vocabulary.Types.AbstractType.AbstractType`. :parameter _type: the type that will be used to encode :type _type: :class:`type` :keyword unitSize: the unitsize of the expected result. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness of the expected result. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign of the expected result. Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str """ self.type = _type if unitSize is None: unitSize = AbstractType.defaultUnitSize() self.unitSize = unitSize if endianness is None: endianness = AbstractType.defaultEndianness() self.endianness = endianness if sign is None: sign = AbstractType.defaultSign() self.sign = sign
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> from netzob.Model.Vocabulary.Types.BitArray import BitArray >>> d = ASCII.decode("hello netzob") >>> r = BitArray.encode(d) >>> print(r.to01()) 011010000110010101101100011011000110111100100000011011100110010101110100011110100110111101100010 >>> t = BitArray.decode(r) >>> print(t) b'hello netzob' :param data: the data encoded in BitArray which will be decoded in raw :type data: bitarray :keyword unitSize: the unit size of the specified data :type unitSize: :class:`netzob.Model.Vocabulary.Types.UnitSize.UnitSize` :keyword endianness: the endianness of the specified data :type endianness: :class:`netzob.Model.Vocabulary.Types.Endianness.Endianness` :keyword sign: the sign of the specified data :type sign: :class:`netzob.Model.Vocabulary.Types.Sign.Sign` :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") return data.tobytes()
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the ASCII. >>> from netzob.all import * >>> raw = ASCII.decode("hello zoby!") >>> print(ASCII.encode(raw)) hello zoby! :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") res = "" for ordElt in data: if ordElt >= 0x20 and ordElt <= 0x7e: # means between ' ' and '~' res += chr(ordElt) else: res += "." return res
def __init__(self, value=None, nbBytes=None, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign(), alphabet=None): if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray if isinstance(value, str): value = TypeConverter.convert(bytes(value, "utf-8"), Raw, BitArray) elif isinstance(value, bytes): value = TypeConverter.convert(value, Raw, BitArray) nbBits = self._convertNbBytesinNbBits(nbBytes) self.alphabet = alphabet super(Raw, self).__init__(self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method returns True if data is a Integer. For the moment its always true because we consider the integer type to be very similar to the raw type. >>> from netzob.all import * >>> Integer().canParse(TypeConverter.convert("hello netzob", ASCII, Raw)) True :param data: the data to check :type data: python raw :return: True if data is can be parsed as a Integer :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if len(data) == 0: return False return True
def __init__(self, value=None, interval=None, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray interval = value value = TypeConverter.convert(value, Integer, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) else: value = None if interval is not None: nbBits = int( self._computeNbUnitSizeForInterval(interval, unitSize, sign)) * int(unitSize) else: nbBits = int(unitSize) super(Integer, self).__init__(self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> ASCII.decode("hello") b'hello' >>> ASCII.decode('\x5a\x6f\x62\x79\x20\x69\x73\x20\x64\x61\x20\x70\x6c\x61\x63\x65\x20\x21') b'Zoby is da place !' >>> ASCII.decode(1021) b'1021' :param data: the data encoded in ASCII which will be decoded in raw :type data: the current type :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") return str(data).encode('utf-8')
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method returns True if data is an ASCII (utf-8) >>> from netzob.all import * >>> ASCII().canParse(TypeConverter.convert("hello netzob", ASCII, BitArray)) True The ascii table is defined from 0 to 127: >>> ASCII().canParse(TypeConverter.convert(128, Integer, BitArray, src_sign=AbstractType.SIGN_UNSIGNED)) False >>> a = ASCII(nbChars=10) >>> a.canParse(TypeConverter.convert("hellohello", ASCII, BitArray)) True >>> a.canParse(TypeConverter.convert("hello hello", ASCII, BitArray)) False >>> a = ASCII(nbChars=(2,20)) >>> a.canParse(TypeConverter.convert("Netzob", ASCII, BitArray)) True >>> a.canParse(TypeConverter.convert("Hello netzob, what's up ?", ASCII, BitArray)) False :param data: the data to check :type data: python raw :return: True if data can be parsed as an ASCII :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if len(data) == 0: return False # Ascii must be 8 bits modulo length if len(data) % 8 != 0: return False rawData = data.tobytes() try: rawData.decode('utf-8') except: return False (minChar, maxChar) = self.nbChars if minChar is not None: if len(rawData) < minChar: return False if maxChar is not None: if len(rawData) > maxChar: return False return True
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the ASCII. >>> from netzob.all import * >>> raw = ASCII.decode("hello zoby!") >>> print(ASCII.encode(raw)) hello zoby! :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") res = "" for ordElt in data: if ordElt >= 0x20 and ordElt <= 0x7e: # means between ' ' and '~' res += chr(ordElt) else: res += "." return res
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method returns True if data is a Integer. For the moment its always true because we consider the integer type to be very similar to the raw type. >>> from netzob.all import * >>> Integer().canParse(TypeConverter.convert("hello netzob", ASCII, Raw)) True :param data: the data to check :type data: python raw :return: True if data is can be parsed as a Integer :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if len(data) == 0: return False return True
def __init__(self, value=None, network=None, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Builds an IPv4 domain with optional constraints. :parameter value: specify a constraints over the expected value. :type value: an str, an IPAddress or an int which can be parsed as an IPv4 (ex. "192.168.0.10") :parameter network: if no value is specified (None), a constraints over the network the parsed IP belongs can be specified with this parameter (ex. "192.168.0.0/24") :type network: an str or an IPAddress which can be parsed as a network IPv4 """ if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray value = TypeConverter.convert(value, IPv4, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) self.network = network super(IPv4, self).__init__(self.__class__.__name__, value, 32, unitSize=unitSize, endianness=endianness, sign=sign)
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> from netzob.Model.Vocabulary.Types.BitArray import BitArray >>> d = ASCII.decode("hello netzob") >>> r = BitArray.encode(d) >>> print(r.to01()) 011010000110010101101100011011000110111100100000011011100110010101110100011110100110111101100010 >>> t = BitArray.decode(r) >>> print(t) b'hello netzob' :param data: the data encoded in BitArray which will be decoded in raw :type data: bitarray :keyword unitSize: the unit size of the specified data :type unitSize: :class:`netzob.Model.Vocabulary.Types.UnitSize.UnitSize` :keyword endianness: the endianness of the specified data :type endianness: :class:`netzob.Model.Vocabulary.Types.Endianness.Endianness` :keyword sign: the sign of the specified data :type sign: :class:`netzob.Model.Vocabulary.Types.Sign.Sign` :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") return data.tobytes()
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method returns True if data is an ASCII (utf-8) >>> from netzob.all import * >>> ASCII().canParse(TypeConverter.convert("hello netzob", ASCII, BitArray)) True The ascii table is defined from 0 to 127: >>> ASCII().canParse(TypeConverter.convert(128, Integer, BitArray, src_sign=AbstractType.SIGN_UNSIGNED)) False >>> a = ASCII(nbChars=10) >>> a.canParse(TypeConverter.convert("hellohello", ASCII, BitArray)) True >>> a.canParse(TypeConverter.convert("hello hello", ASCII, BitArray)) False >>> a = ASCII(nbChars=(2,20)) >>> a.canParse(TypeConverter.convert("Netzob", ASCII, BitArray)) True >>> a.canParse(TypeConverter.convert("Hello netzob, what's up ?", ASCII, BitArray)) False :param data: the data to check :type data: python raw :return: True if data can be parsed as an ASCII :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if len(data) == 0: return False # Ascii must be 8 bits modulo length if len(data) % 8 != 0: return False rawData = data.tobytes() try: rawData.decode('utf-8') except: return False (minChar, maxChar) = self.nbChars if minChar is not None: if len(rawData) < minChar: return False if maxChar is not None: if len(rawData) > maxChar: return False return True
def __init__(self, value=None, nbChars=(None, None), unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray value = TypeConverter.convert( value, ASCII, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) else: value = None self.nbChars = nbChars nbBits = self._convertNbCharsInNbBits(self.nbChars) super(ASCII, self).__init__( self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def __init__(self, value=None, nbChars=(None, None), unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray value = TypeConverter.convert(value, ASCII, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) else: value = None self.nbChars = nbChars nbBits = self._convertNbCharsInNbBits(self.nbChars) super(ASCII, self).__init__(self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Computes if specified data can be parsed as a Timestamp with the predefined constraints. >>> from netzob.all import * >>> time = Timestamp() >>> time.canParse(TypeConverter.convert(1444494130, Integer, BitArray, src_unitSize=AbstractType.UNITSIZE_32)) True >>> # A timestamp is nothing else than 32bits parsed as an unsigned long >>> time.canParse(TypeConverter.convert("test", ASCII, BitArray)) True >>> time.canParse(TypeConverter.convert("te", ASCII, BitArray)) False However, some constrains over the definition of the Timestamp can be set to restrain the accepted values >>> from netzob.all import * >>> time = Timestamp(epoch=Timestamp.EPOCH_WINDOWS, unity=Timestamp.UNITY_NANOSECOND, unitSize = AbstractType.UNITSIZE_64) >>> # the returned year is < 1900 >>> time.canParse(TypeConverter.convert("test", ASCII, BitArray)) False """ if data is None: raise TypeError("data cannot be None") # Timestamp must be 8 bits modulo length if len(data) % 8 != 0: return False if len(data) < int(self.unitSize): return False try: value = TypeConverter.convert( data[:int(self.unitSize)], BitArray, Integer, dst_unitSize=AbstractType.UNITSIZE_32, dst_sign=AbstractType.SIGN_UNSIGNED) # convert the value in seconds value = value / self.unity # add the utc now with the epoch timestamp_datetime = self.epoch + timedelta(seconds=value) # convert obtained datetime to timestamp in seconds result_sec = int(timestamp_datetime.strftime('%s')) datetime.fromtimestamp(result_sec) except Exception: return False return True
def __init__(self, _type, unitSize=None, endianness=None, sign=None): """Creates a new encoding function that will encode the data with the specified types and following its attributes. If an attribute is not specified (or set to None), it takes its default value defined in :class:`netzob.Model.Vocabulary.Types.AbstractType.AbstractType`. :parameter _type: the type that will be used to encode :type _type: :class:`type` :keyword unitSize: the unitsize of the expected result. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness of the expected result. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign of the expected result. Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str """ self.type = _type if unitSize is None: unitSize = AbstractType.defaultUnitSize() self.unitSize = unitSize if endianness is None: endianness = AbstractType.defaultEndianness() self.endianness = endianness if sign is None: sign = AbstractType.defaultSign() self.sign = sign
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> ASCII.decode("hello") b'hello' >>> ASCII.decode('\x5a\x6f\x62\x79\x20\x69\x73\x20\x64\x61\x20\x70\x6c\x61\x63\x65\x20\x21') b'Zoby is da place !' >>> ASCII.decode(1021) b'1021' :param data: the data encoded in ASCII which will be decoded in raw :type data: the current type :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") return str(data).encode('utf-8')
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> print(Integer.decode(23)) b'\\x17' >>> print(Integer.decode(-1, sign=AbstractType.SIGN_UNSIGNED)) Traceback (most recent call last): ... struct.error: ubyte format requires 0 <= number <= 255 >>> print(Integer.decode(-1, sign=AbstractType.SIGN_SIGNED)) b'\\xff' >>> print(Integer.decode(2000000000000000)) Traceback (most recent call last): ... struct.error: ubyte format requires 0 <= number <= 255 >>> print(Integer.decode(2000000000000000, unitSize=AbstractType.UNITSIZE_64)) b'\\x00\\x07\\x1a\\xfdI\\x8d\\x00\\x00' >>> print(Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE)) b'\\x19\\x00' >>> print(Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG)) b'\\x00\\x19' >>> val = 167749568 >>> a = Integer.decode(val, unitSize=AbstractType.UNITSIZE_32) >>> b = Integer.encode(a, unitSize=AbstractType.UNITSIZE_32) >>> b == val True :param data: the data encoded in Integer which will be decoded in raw :type data: the current type :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") f = Integer.computeFormat(unitSize, endianness, sign) return struct.pack(f, int(data))
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> print(Integer.decode(23)) b'\\x17' >>> print(Integer.decode(-1, sign=AbstractType.SIGN_UNSIGNED)) Traceback (most recent call last): ... struct.error: ubyte format requires 0 <= number <= 255 >>> print(Integer.decode(-1, sign=AbstractType.SIGN_SIGNED)) b'\\xff' >>> print(Integer.decode(2000000000000000)) Traceback (most recent call last): ... struct.error: byte format requires -128 <= number <= 127 >>> print(Integer.decode(2000000000000000, unitSize=AbstractType.UNITSIZE_64)) b'\\x00\\x07\\x1a\\xfdI\\x8d\\x00\\x00' >>> print(Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE)) b'\\x19\\x00' >>> print(Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG)) b'\\x00\\x19' >>> val = 167749568 >>> a = Integer.decode(val, unitSize=AbstractType.UNITSIZE_32) >>> b = Integer.encode(a, unitSize=AbstractType.UNITSIZE_32) >>> b == val True :param data: the data encoded in Integer which will be decoded in raw :type data: the current type :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") f = Integer.computeFormat(unitSize, endianness, sign) return struct.pack(f, int(data))
def __init__(self, value=None, interval=None, unitSize=None, endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): if unitSize is None: if interval is None: # value handling if value is None: unitSize = AbstractType.defaultUnitSize() else: unitSize = Integer.checkUnitSizeForValue(value, sign) else: # interval handling if isinstance(interval, int): unitSize = Integer.checkUnitSizeForValue(interval, sign) elif len(interval) == 2: unitSizeA = Integer.checkUnitSizeForValue( interval[0], sign) unitSizeB = Integer.checkUnitSizeForValue( interval[1], sign) unitSize = max(unitSizeA, unitSizeB) else: # shouldn't happen, since _checkUnitSizeForValue raises an exception before unitSize = AbstractType.defaultUnitSize() if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray value = TypeConverter.convert(value, Integer, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) if interval is not None: nbBits = int( self._computeNbUnitSizeForInterval(interval, unitSize, sign)) * int(unitSize) else: nbBits = int(unitSize) super(Integer, self).__init__(self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """For the moment its always true because we consider the decimal type to be very similar to the raw type. >>> from netzob.all import * >>> BitArray().canParse(TypeConverter.convert("hello netzob", ASCII, BitArray)) True >>> b = BitArray(nbBits=8) >>> b.canParse(bitarray('01010101')) True >>> b.canParse(bitarray('010101011')) False :param data: the data to check :type data: python raw :return: True if data can be parsed as a BitArray :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if not isinstance(data, bitarray): raise TypeError("Data should be a python raw ({0}:{1})".format( data, type(data))) if len(data) == 0: return False (nbMinBits, nbMaxBits) = self.size nbBitsData = len(data) if nbMinBits is not None and nbMinBits > nbBitsData: return False if nbMaxBits is not None and nbMaxBits < nbBitsData: return False return True
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """For the moment its always true because we consider the decimal type to be very similar to the raw type. >>> from netzob.all import * >>> BitArray().canParse(TypeConverter.convert("hello netzob", ASCII, BitArray)) True >>> b = BitArray(nbBits=8) >>> b.canParse(bitarray('01010101')) True >>> b.canParse(bitarray('010101011')) False :param data: the data to check :type data: python raw :return: True if data can be parsed as a BitArray :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if not isinstance(data, bitarray): raise TypeError("Data should be a python raw ({0}:{1})".format( data, type(data))) if len(data) == 0: return False (nbMinBits, nbMaxBits) = self.size nbBitsData = len(data) if nbMinBits is not None and nbMinBits > nbBitsData: return False if nbMaxBits is not None and nbMaxBits < nbBitsData: return False return True
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the BitArray. It also converts python Raw to a BitArray >>> from netzob.all import * >>> from netzob.Model.Vocabulary.Types.BitArray import BitArray >>> BitArray.encode(Integer.decode(20)) bitarray('00010100') :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in BitArray :rtype: :class:`netzob.Model.Vocabulary.Types.BitArray.BitArray` :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") if endianness == AbstractType.ENDIAN_BIG: endian = 'big' elif endianness == AbstractType.ENDIAN_LITTLE: endian = 'little' else: raise ValueError("Invalid endianness value") if isinstance(data, bytes): norm_data = data elif isinstance(data, str): norm_data = bytes(data, "utf-8") elif isinstance(data, Raw): return data.value b = bitarray(endian=endian) b.frombytes(norm_data) return b
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Decode the specified IPv4 data into its raw representation. >>> from netzob.all import * >>> print(IPv4.decode("127.0.0.1")) b'\\x7f\\x00\\x00\\x01' """ if data is None: raise TypeError("Data cannot be None") ip = IPv4() if not ip.canParse(data): raise TypeError("Data is not a valid IPv4, cannot decode it.") ip = IPAddress(data) return ip.packed
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Decode the specified IPv4 data into its raw representation. >>> from netzob.all import * >>> print(IPv4.decode("127.0.0.1")) b'\\x7f\\x00\\x00\\x01' """ if data is None: raise TypeError("Data cannot be None") ip = IPv4() if not ip.canParse(data): raise TypeError("Data is not a valid IPv4, cannot decode it.") ip = IPAddress(data) return ip.packed
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the BitArray. >>> from netzob.all import * >>> from netzob.Model.Vocabulary.Types.BitArray import BitArray >>> BitArray.encode(Integer.decode(20)) bitarray('00010100') :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in BitArray :rtype: :class:`netzob.Model.Vocabulary.Types.BitArray.BitArray` :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") if endianness == AbstractType.ENDIAN_BIG: endian = 'big' elif endianness == AbstractType.ENDIAN_LITTLE: endian = 'little' else: raise ValueError("Invalid endianness value") if isinstance(data, bytes): norm_data = data elif isinstance(data, str): norm_data = bytes(data, "utf-8") b = bitarray(endian=endian) b.frombytes(norm_data) return b
def decode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the specified data in python raw format. >>> from netzob.all import * >>> import os >>> # Generate 1024 random bytes >>> randomData = os.urandom(1024) >>> # Convert to hexastring >>> hex = TypeConverter.convert(randomData, Raw, HexaString) >>> print(len(hex)) 2048 >>> # Convert back to byte and verify we didn't lost anything >>> raw = TypeConverter.convert(hex, HexaString, Raw) >>> print(raw == randomData) True :param data: the data encoded in hexaString (str) which will be decoded in raw :type data: str :keyword unitSize: the unit size of the specified data :type unitSize: :class:`netzob.Model.Vocabulary.Types.UnitSize.UnitSize` :keyword endianness: the endianness of the specified data :type endianness: :class:`netzob.Model.Vocabulary.Types.Endianness.Endianness` :keyword sign: the sign of the specified data :type sign: :class:`netzob.Model.Vocabulary.Types.Sign.Sign` :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") if len(data) % 2 == 1: data = '0' + data return binascii.unhexlify(data)
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Computes if specified data can be parsed as raw which is always the case if the data is at least 1 length and aligned on a byte. >>> from netzob.all import * >>> Raw().canParse(TypeConverter.convert("hello netzob", ASCII, BitArray)) True The ascii table is defined from 0 to 127: >>> Raw().canParse(TypeConverter.convert(128, Integer, BitArray, src_sign=AbstractType.SIGN_UNSIGNED)) True :param data: the data to check :type data: python raw :return: True if data can be parsed as a Raw which is always the case (if len(data)>0) :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") if len(data) == 0: return False if len(data) % 8 != 0: return False if self.alphabet is not None: data_set = set(data) for element in data_set: if element not in self.alphabet: return False return True
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Encodes the specified data into an IPAddress object :param data: the data to encode into an IPAddress :type data: str or raw bytes (BBBB) :return: the encoded IPAddress """ if isinstance(data, (str, int)): try: ip = IPAddress(data) if ip is not None and ip.version == 4 and not ip.is_netmask(): return ip except: pass try: structFormat = ">" if endianness == AbstractType.ENDIAN_BIG: structFormat = ">" if not sign == AbstractType.SIGN_SIGNED: structFormat += "bbbb" else: structFormat += "BBBB" quads = list(map(str, struct.unpack(structFormat, data))) strIP = '.'.join(quads) ip = IPAddress(strIP) if ip is not None and ip.version == 4 and not ip.is_netmask(): return ip except Exception as e: raise TypeError( "Impossible encode {0} into an IPv4 data ({1})".format( data, e))
def __init__(self, value=None, interval=None, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray interval = value value = TypeConverter.convert( value, Integer, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) else: value = None if interval is not None: nbBits = int( self._computeNbUnitSizeForInterval(interval, unitSize, sign)) * int(unitSize) else: nbBits = int(unitSize) super(Integer, self).__init__( self.__class__.__name__, value, nbBits, unitSize=unitSize, endianness=endianness, sign=sign)
def __init__(self, value=None, network=None, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Builds an IPv4 domain with optional constraints. :parameter value: specify a constraints over the expected value. :type value: an str, an IPAddress or an int which can be parsed as an IPv4 (ex. "192.168.0.10") :parameter network: if no value is specified (None), a constraints over the network the parsed IP belongs can be specified with this parameter (ex. "192.168.0.0/24") :type network: an str or an IPAddress which can be parsed as a network IPv4 """ if value is not None and not isinstance(value, bitarray): from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray value = TypeConverter.convert( value, IPv4, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=unitSize, dst_endianness=endianness, dst_sign=sign) self.network = network super(IPv4, self).__init__( self.__class__.__name__, value, 32, unitSize=unitSize, endianness=endianness, sign=sign)
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to an HexaString >>> from netzob.all import * >>> import os >>> # Generate 4096 random bytes >>> randomData = os.urandom(4096) >>> # Convert to hexastring >>> hex = TypeConverter.convert(randomData, Raw, HexaString) >>> print(len(hex)) 8192 >>> # Convert back to byte and verify we didn't lost anything >>> raw = TypeConverter.convert(hex, HexaString, Raw) >>> print(raw == randomData) True :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in Hexa String :rtype: python str :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") return binascii.hexlify(data)
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the ASCII. >>> from netzob.all import * >>> raw = ASCII.decode("hello zoby!") >>> print(ASCII.encode(raw)) hello zoby! :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") res = "" for ordElt in data: try: res += chr(ordElt) except: raise ValueError( "The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16)" ) return res
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Encodes the specified data into an IPAddress object :param data: the data to encode into an IPAddress :type data: str or raw bytes (BBBB) :return: the encoded IPAddress """ if isinstance(data, (str, int)): try: ip = IPAddress(data) if ip is not None and ip.version == 4 and not ip.is_netmask(): return ip except: pass try: structFormat = ">" if endianness == AbstractType.ENDIAN_BIG: structFormat = ">" if not sign == AbstractType.SIGN_SIGNED: structFormat += "bbbb" else: structFormat += "BBBB" quads = list(map(str, struct.unpack(structFormat, data))) strIP = '.'.join(quads) ip = IPAddress(strIP) if ip is not None and ip.version == 4 and not ip.is_netmask(): return ip except Exception as e: raise TypeError("Impossible encode {0} into an IPv4 data ({1})". format(data, e))
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the Integer. >>> from netzob.all import * >>> raw = Integer.decode(23) >>> print(Integer.encode(raw)) 23 >>> raw = Integer.decode(1200, unitSize=AbstractType.UNITSIZE_16) >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16)) 1200 >>> raw = Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE) >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG))) 6400 >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE))) 25 >>> print(Integer.encode(b'\\xcc\\xac\\x9c\\x0c\\x1c\\xacL\\x1c,\\xac', unitSize=AbstractType.UNITSIZE_8)) -395865088909314208584756 >>> raw = b'\\xcc\\xac\\x9c' >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG)) 10210476 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_BIG)) 13413532 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_LITTLE)) 10267852 :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") perWordFormat = Integer.computeFormat(unitSize, endianness, sign) nbWords = int(len(data) * 8 / int(unitSize)) # Check whether the input data matches unitSize. If not take # precautions to able to pad it with null bytes later. padding_nullbytes = 0 rest = (len(data) * 8) % int(unitSize) if rest != 0: nbWords += 1 padding_nullbytes = (int(unitSize) - rest) / 8 finalValue = 0 iWord = 0 start = 0 end = nbWords inc = 1 if endianness == AbstractType.ENDIAN_BIG: end = 0 start = nbWords inc = -1 for i in range(start, end, inc): # Extract the portion that represents the current word startPos = int(iWord * int(unitSize) / 8) endPos = int(iWord * int(unitSize) / 8 + int(unitSize) / 8) wordData = data[startPos:endPos] # Pad with null bytes to statisfy the unitSize. if padding_nullbytes > 0 and i == (end - inc): if endianness == AbstractType.ENDIAN_BIG: wordData = b'\x00' * int(padding_nullbytes) + wordData elif endianness == AbstractType.ENDIAN_LITTLE: wordData += b'\x00' * int(padding_nullbytes) else: raise ValueError( "Invalid endianness value: {0}".format(endianness)) unpackedWord = struct.unpack(perWordFormat, wordData)[0] unpackedWord = unpackedWord << int(unitSize) * iWord finalValue = finalValue + unpackedWord iWord += 1 return finalValue
def convert(data, sourceType, destinationType, src_unitSize=AbstractType.defaultUnitSize(), src_endianness=AbstractType.defaultEndianness(), src_sign=AbstractType.defaultSign(), dst_unitSize=AbstractType.defaultUnitSize(), dst_endianness=AbstractType.defaultEndianness(), dst_sign=AbstractType.defaultSign()): """Encode data provided as a sourceType to a destinationType. To convert an ASCII to its binary (bitarray) representation >>> from netzob.all import * >>> data = "That's an helloworld!" >>> bin = TypeConverter.convert(data, ASCII, BitArray) >>> print(bin) bitarray('010101000110100001100001011101000010011101110011001000000110000101101110001000000110100001100101011011000110110001101111011101110110111101110010011011000110010000100001') >>> data == TypeConverter.convert(bin, BitArray, ASCII) True To convert a raw data to its decimal representation and then to its ASCII representation >>> data = b'\\x23' >>> decData = TypeConverter.convert(data, Raw, Integer) >>> print(decData) 35 >>> print(TypeConverter.convert(decData, Integer, ASCII)) # You can also play with the unitSize to convert multiple ascii in a single high value decimal >>> TypeConverter.convert("5", ASCII, Integer) 53 >>> print(TypeConverter.convert("zoby", ASCII, Integer)) 2036494202 >>> print(TypeConverter.convert("zoby", ASCII, Integer, dst_unitSize=AbstractType.UNITSIZE_32)) 2054120057 It also works for 'semantic' data like IPv4s >>> TypeConverter.convert("192.168.0.10", IPv4, Integer, dst_sign=AbstractType.SIGN_UNSIGNED) 167815360 >>> TypeConverter.convert("127.0.0.1", IPv4, BitArray) bitarray('01111111000000000000000000000001') >>> TypeConverter.convert(167815360, Integer, IPv4, src_unitSize=AbstractType.UNITSIZE_32, src_sign=AbstractType.SIGN_UNSIGNED) IPAddress('10.0.168.192') :param sourceType: the data source type :type sourceType: :class:`type` :param destinationType: the destination type :type destinationType: :class:`type` :keyword src_unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type src_unitSize: str :keyword src_endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type src_endianness: str :keyword src_sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type src_sign: str :keyword dst_unitSize: the unitsize of the expected result. Values must be one of AbstractType.UNITSIZE_* :type dst_unitSize: str :keyword dst_endianness: the endianness of the expected result. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type dst_endianness: str :keyword dst_sign: the sign of the expected result. Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type dst_sign: str :raise: TypeError if parameter not valid """ # is the two formats supported ? if sourceType not in TypeConverter.supportedTypes(): raise TypeError( "The source type ({0}) is not supported".format(sourceType)) if destinationType not in TypeConverter.supportedTypes(): raise TypeError( "The destination type ({0}) is not supported".format( destinationType)) if data is None: raise TypeError("Data cannot be None") # Do we have a specific source to destination encoding function if (sourceType, destinationType) in list( TypeConverter.__directEncoding().keys()): func = TypeConverter.__directEncoding()[(sourceType, destinationType)] return func(data, src_unitSize, src_endianness, src_sign, dst_unitSize, dst_endianness, dst_sign) else: # Convert from source to raw if sourceType is not Raw: binData = sourceType.decode(data, unitSize=src_unitSize, endianness=src_endianness, sign=src_sign) else: binData = data # Convert from raw to Destination if destinationType is not Raw: outputData = destinationType.encode(binData, unitSize=dst_unitSize, endianness=dst_endianness, sign=dst_sign) else: outputData = binData return outputData
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the Integer. >>> from netzob.all import * >>> raw = Integer.decode(23) >>> print(Integer.encode(raw)) 23 >>> raw = Integer.decode(1200, unitSize=AbstractType.UNITSIZE_16) >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16)) 1200 >>> raw = Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE) >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG))) 6400 >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE))) 25 >>> print(Integer.encode(b'\\xcc\\xac\\x9c\\x0c\\x1c\\xacL\\x1c,\\xac', unitSize=AbstractType.UNITSIZE_8)) -395865088909314208584756 >>> raw = b'\\xcc\\xac\\x9c' >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG)) 10210476 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_BIG)) 13413532 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_LITTLE)) 10267852 :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") perWordFormat = Integer.computeFormat(unitSize, endianness, sign) nbWords = int(len(data) * 8 / int(unitSize)) # Check whether the input data matches unitSize. If not take # precautions to able to pad it with null bytes later. padding_nullbytes = 0 rest = (len(data) * 8) % int(unitSize) if rest != 0: nbWords += 1 padding_nullbytes = (int(unitSize) - rest) / 8 finalValue = 0 iWord = 0 start = 0 end = nbWords inc = 1 if endianness == AbstractType.ENDIAN_BIG: end = 0 start = nbWords inc = -1 for i in range(start, end, inc): # Extract the portion that represents the current word startPos = int(iWord * int(unitSize) / 8) endPos = int(iWord * int(unitSize) / 8 + int(unitSize) / 8) wordData = data[startPos:endPos] # Pad with null bytes to statisfy the unitSize. if padding_nullbytes > 0 and i == (end - inc): if endianness == AbstractType.ENDIAN_BIG: wordData = b'\x00' * int(padding_nullbytes) + wordData elif endianness == AbstractType.ENDIAN_LITTLE: wordData += b'\x00' * int(padding_nullbytes) else: raise ValueError( "Invalid endianness value: {0}".format(endianness)) unpackedWord = struct.unpack(perWordFormat, wordData)[0] unpackedWord = unpackedWord << int(unitSize) * iWord finalValue = finalValue + unpackedWord iWord += 1 return finalValue
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Computes if specified data can be parsed as an IPv4 with the predefined constraints. >>> from netzob.all import * >>> ip = IPv4() >>> ip.canParse("192.168.0.10") True >>> ip.canParse("198.128.0.100") True >>> ip.canParse("256.0.0.1") False >>> ip.canParse("127.0.0.1") True >>> ip.canParse("127.0.0.-1") False >>> ip.canParse("::") False >>> ip.canParse("0.0.0.0") False And with some constraints over the expected IPv4: >>> ip = IPv4("192.168.0.10") >>> ip.canParse("192.168.0.10") True >>> ip.canParse("192.168.1.10") False >>> ip.canParse(3232235530) True >>> ip = IPv4("167.20.14.20") >>> ip.canParse(3232235530) False >>> ip.canParse(3232235530) False or with contraints over the expected network the ipv4 belongs to: >>> ip = IPv4(network="192.168.0.0/24") >>> ip.canParse("192.168.0.10") True >>> ip.canParse("192.168.1.10") False :param data: the data to check :type data: python raw :return: True if data can be parsed as a Raw which is always the case (if len(data)>0) :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") try: ip = IPv4.encode(data, unitSize=unitSize, endianness=endianness, sign=sign) if ip is None or ip.version != 4 or ip.is_netmask(): return False except: return False try: if self.value is not None: from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray return self.value == TypeConverter.convert( data, IPv4, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=self.unitSize, dst_endianness=self.endianness, dst_sign=self.sign) elif self.network is not None: return ip in self.network except: return False return True
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """This method convert the python raw data to the Integer. >>> from netzob.all import * >>> raw = Integer.decode(23) >>> print(Integer.encode(raw)) 23 >>> raw = Integer.decode(1200, unitSize=AbstractType.UNITSIZE_16) >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16)) 1200 >>> raw = Integer.decode(25, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE) >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG))) 6400 >>> print(repr(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_LITTLE))) 25 >>> print(Integer.encode(b'\\xcc\\xac\\x9c\\x0c\\x1c\\xacL\\x1c,\\xac', unitSize=AbstractType.UNITSIZE_8, sign=AbstractType.SIGN_SIGNED)) -247119785962690400474196 # raw is interpreted as b'\xcc\xac\x00\x9c' by encode and # as big endian with unit size 16 as (0x009c << 16) + 0xccac = 10276012 >>> raw = b'\\xcc\\xac\\x9c' >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_16, endianness=AbstractType.ENDIAN_BIG)) 13413532 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_BIG)) 13413532 >>> print(Integer.encode(raw, unitSize=AbstractType.UNITSIZE_32, endianness=AbstractType.ENDIAN_LITTLE)) 10267852 :param data: the data encoded in python raw which will be encoded in current type :type data: python raw :keyword unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type unitSize: str :keyword endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type endianness: str :keyword sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type sign: str :return: data encoded in python raw :rtype: python raw :raise: TypeError if parameters are not valid. """ if data is None: raise TypeError("data cannot be None") perWordFormat = Integer.computeFormat(unitSize, endianness, sign) nbWords = int(len(data) * 8 / int(unitSize)) # Check whether the input data matches unitSize. If not take # precautions to able to pad it with null bytes to statisfy the unitSize. padding_nullbytes = 0 rest = (len(data) * 8) % int(unitSize) if rest != 0: nbWords += 1 padding_nullbytes = (int(unitSize) - rest) / 8 if endianness == AbstractType.ENDIAN_BIG: data = b'\x00' * int(padding_nullbytes) + data elif endianness == AbstractType.ENDIAN_LITTLE: data += b'\x00' * int(padding_nullbytes) else: raise ValueError( "Invalid endianness value: {0}".format(endianness)) finalValue = 0 start = 0 end = nbWords for i in range(start, end): # Extract the portion that represents the current word startPos = i * int(unitSize) // 8 endPos = startPos + int(unitSize) // 8 wordData = data[startPos:endPos] unpackedWord = struct.unpack(perWordFormat, wordData)[0] if endianness == AbstractType.ENDIAN_LITTLE: wordShift = i else: wordShift = nbWords - i - 1 unpackedWord = unpackedWord << (int(unitSize) * wordShift) finalValue = finalValue + unpackedWord return finalValue
def convert(data, sourceType, destinationType, src_unitSize=None, src_endianness=AbstractType.defaultEndianness(), src_sign=AbstractType.defaultSign(), dst_unitSize=None, dst_endianness=AbstractType.defaultEndianness(), dst_sign=AbstractType.defaultSign()): """Encode data provided as a sourceType to a destinationType. To convert an ASCII to its binary (bitarray) representation >>> from netzob.all import * >>> data = "That's an helloworld!" >>> bin = TypeConverter.convert(data, ASCII, BitArray) >>> print(bin) bitarray('010101000110100001100001011101000010011101110011001000000110000101101110001000000110100001100101011011000110110001101111011101110110111101110010011011000110010000100001') >>> data == TypeConverter.convert(bin, BitArray, ASCII) True To convert a raw data to its decimal representation and then to its ASCII representation >>> data = b'\\x23' >>> decData = TypeConverter.convert(data, Raw, Integer) >>> print(decData) 35 >>> print(TypeConverter.convert(decData, Integer, ASCII)) # Conversion to and from Integer detects the output unitsize depending on the input: >>> TypeConverter.convert(b'\\x0b\\x00', Raw, Integer) 2816 >>> TypeConverter.convert(11, Integer, Raw) b'\\x0b' >>> TypeConverter.convert(b'\\xa0\\x0b', Raw, Integer, dst_sign=AbstractType.SIGN_SIGNED) -24565 >>> TypeConverter.convert(-24565, Integer, Raw, src_sign=AbstractType.SIGN_SIGNED) b'\\xa0\\x0b' >>> TypeConverter.convert(0, Integer, Raw) b'\\x00' >>> TypeConverter.convert(b'\\x00\\x00\\x00', Raw, Integer) Traceback (most recent call last): ... TypeError: Unsupported autodetected Integer target UnitSize. Valid UnitSizes are 8, 16, 32 and 64 bits. You can also play with the unitSize to convert multiple ascii in a single high value decimal >>> TypeConverter.convert("5", ASCII, Integer) 53 >>> print(TypeConverter.convert("zoby", ASCII, Integer)) 2054120057 >>> print(TypeConverter.convert("zoby", ASCII, Integer, dst_unitSize=AbstractType.UNITSIZE_32)) 2054120057 It also works for 'semantic' data like IPv4s >>> TypeConverter.convert("192.168.0.10", IPv4, Integer, dst_sign=AbstractType.SIGN_UNSIGNED) 3232235530 >>> TypeConverter.convert("127.0.0.1", IPv4, BitArray) bitarray('01111111000000000000000000000001') >>> TypeConverter.convert(167815360, Integer, IPv4, src_unitSize=AbstractType.UNITSIZE_32, src_sign=AbstractType.SIGN_UNSIGNED) IPAddress('10.0.168.192') To check Integer conversion consistency >>> f = Field(b'some') >>> v = f.domain.currentValue.tobytes() >>> try: ... v_hex = v.hex() ... except AttributeError: ... import codecs # Python <= 3.4: 'bytes' object has no attribute 'hex' ... v_hex = codecs.encode(v, 'hex_codec').decode('ascii') >>> '0x'+v_hex == hex(TypeConverter.convert(f.domain.currentValue, BitArray, Integer)) True :param sourceType: the data source type :type sourceType: :class:`type` :param destinationType: the destination type :type destinationType: :class:`type` :keyword src_unitSize: the unitsize to consider while encoding. Values must be one of AbstractType.UNITSIZE_* :type src_unitSize: str :keyword src_endianness: the endianness to consider while encoding. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type src_endianness: str :keyword src_sign: the sign to consider while encoding Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type src_sign: str :keyword dst_unitSize: the unitsize of the expected result. Values must be one of AbstractType.UNITSIZE_* :type dst_unitSize: str :keyword dst_endianness: the endianness of the expected result. Values must be AbstractType.ENDIAN_BIG or AbstractType.ENDIAN_LITTLE :type dst_endianness: str :keyword dst_sign: the sign of the expected result. Values must be AbstractType.SIGN_SIGNED or AbstractType.SIGN_UNSIGNED :type dst_sign: str :raise: TypeError if parameter not valid """ # Use defaultUnitSize for all types except Integer if dst_unitSize is None and destinationType is not Integer: dst_unitSize = AbstractType.defaultUnitSize() if src_unitSize is None and sourceType is not Integer: src_unitSize = AbstractType.defaultUnitSize() # is the two formats supported ? if sourceType not in TypeConverter.supportedTypes(): raise TypeError( "The source type ({0}) is not supported".format(sourceType)) if destinationType not in TypeConverter.supportedTypes(): raise TypeError("The destination type ({0}) is not supported". format(destinationType)) if data is None: raise TypeError("Data cannot be None") # Do we have a specific source to destination encoding function if (sourceType, destinationType ) in list(TypeConverter.__directEncoding().keys()): func = TypeConverter.__directEncoding()[(sourceType, destinationType)] return func(data, src_unitSize, src_endianness, src_sign, dst_unitSize, dst_endianness, dst_sign) else: # Convert from source to raw if sourceType is not Raw: if sourceType is Integer and src_unitSize is None: src_unitSize = Integer.checkUnitSizeForValue(data,src_sign) binData = sourceType.decode( data, unitSize=src_unitSize, endianness=src_endianness, sign=src_sign) else: binData = data # Convert from raw to Destination if destinationType is not Raw: if destinationType is Integer and dst_unitSize is None: nbUnits = len(binData) if nbUnits == 8: dst_unitSize = AbstractType.UNITSIZE_64 elif nbUnits == 4: dst_unitSize = AbstractType.UNITSIZE_32 elif nbUnits == 2: dst_unitSize = AbstractType.UNITSIZE_16 elif nbUnits == 1: dst_unitSize = AbstractType.UNITSIZE_8 else: raise TypeError("Unsupported autodetected Integer target UnitSize. Valid UnitSizes are 8, 16, 32 and 64 bits.") outputData = destinationType.encode( binData, unitSize=dst_unitSize, endianness=dst_endianness, sign=dst_sign) else: outputData = binData return outputData
def encode(data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): return data
def canParse(self, data, unitSize=AbstractType.defaultUnitSize(), endianness=AbstractType.defaultEndianness(), sign=AbstractType.defaultSign()): """Computes if specified data can be parsed as an IPv4 with the predefined constraints. >>> from netzob.all import * >>> ip = IPv4() >>> ip.canParse("192.168.0.10") True >>> ip.canParse("198.128.0.100") True >>> ip.canParse("256.0.0.1") False >>> ip.canParse("127.0.0.1") True >>> ip.canParse("127.0.0.-1") False >>> ip.canParse("::") False >>> ip.canParse("0.0.0.0") False And with some constraints over the expected IPv4: >>> ip = IPv4("192.168.0.10") >>> ip.canParse("192.168.0.10") True >>> ip.canParse("192.168.1.10") False >>> ip.canParse(3232235530) True >>> ip = IPv4("167.20.14.20") >>> ip.canParse(3232235530) False >>> ip.canParse(3232235530) False or with contraints over the expected network the ipv4 belongs to: >>> ip = IPv4(network="192.168.0.0/24") >>> ip.canParse("192.168.0.10") True >>> ip.canParse("192.168.1.10") False :param data: the data to check :type data: python raw :return: True if data can be parsed as a Raw which is always the case (if len(data)>0) :rtype: bool :raise: TypeError if the data is None """ if data is None: raise TypeError("data cannot be None") try: ip = IPv4.encode( data, unitSize=unitSize, endianness=endianness, sign=sign) if ip is None or ip.version != 4 or ip.is_netmask(): return False except: return False try: if self.value is not None: from netzob.Model.Vocabulary.Types.TypeConverter import TypeConverter from netzob.Model.Vocabulary.Types.BitArray import BitArray return self.value == TypeConverter.convert( data, IPv4, BitArray, src_unitSize=unitSize, src_endianness=endianness, src_sign=sign, dst_unitSize=self.unitSize, dst_endianness=self.endianness, dst_sign=self.sign) elif self.network is not None: return ip in self.network except: return False return True