Ejemplo n.º 1
0
    def test_integer_enum(self):
        structure = Structure((
            IM("a", "uint8", enum_class=_FooEnum),
            IM("b", "uint16", enum_class=_FooEnum),
            IM("c", "uint32", enum_class=_FooEnum),
            IM("d", "uint32", enum_class=_FooEnum, strict_enum=True),
        ))

        orig_values = {
            "a": _FooEnum.Foo,
            "b": _FooEnum.Moo,
            "c": _FooEnum.Bar,
            "d": 1234,
        }
        with self.assertRaises(InvalidInputException):
            data = structure.pack(orig_values)

        orig_values["d"] = _FooEnum.Moo
        data = structure.pack(orig_values)
        db = DataBuffer(data)
        decoded_values = structure.unpack(db)
        self.assertEqual(orig_values, decoded_values)

        orig_values["b"] = 1
        data = structure.pack(orig_values)
        db = DataBuffer(data)
        decoded_values = structure.unpack(db)
        self.assertEqual(orig_values, decoded_values)
Ejemplo n.º 2
0
    def test_expect_fixed_value_error_fixed(self):
        structure = Structure((IM("inner_data", "fixed[12 34]"), ))
        binary_encoding = DataBuffer(bytes.fromhex("12 34"))
        structure.unpack(binary_encoding)

        binary_encoding = DataBuffer(bytes.fromhex("12 35"))
        with self.assertRaises(UnexpectedFixedValueException):
            structure.unpack(binary_encoding)
Ejemplo n.º 3
0
 def test_simple(self):
     db = DataBuffer()
     db += bytes.fromhex("11 22 33")
     db.append(bytes.fromhex("44 55"))
     self.assertEqual(db.get(1), bytes.fromhex("11"))
     self.assertEqual(db.get(3), bytes.fromhex("22 33 44"))
     with self.assertRaises(DataBufferException):
         db.get(3)
     self.assertEqual(db.get(1), bytes.fromhex("55"))
     with self.assertRaises(DataBufferException):
         db.get(1)
Ejemplo n.º 4
0
    def test_opaque_unpacking(self):
        structure = Structure((IM("data", "opaque8"), ))
        self.assertEqual(structure.unpack(DataBuffer(b"\x06foobar")),
                         {"data": b"foobar"})

        structure = Structure((IM("data", "opaque16"), ))
        self.assertEqual(
            structure.unpack(DataBuffer(b"\x00\x09foobar123trail")),
            {"data": b"foobar123"})

        structure = Structure((IM("data", "opaque24"), ))
        self.assertEqual(
            structure.unpack(DataBuffer(b"\x00\x00\x0cfoobar123321blubb")),
            {"data": b"foobar123321"})
Ejemplo n.º 5
0
    def test_client_hello_helper(self):
        chh = ClientHelloHelper(allow_tls_compression=True)

        client_hello = chh.create("foobar.com")
        serialized = ClientHelloPkt.pack(client_hello)
        deserialized = ClientHelloPkt.unpack(DataBuffer(serialized))
        self.assertEqual(client_hello, deserialized)
Ejemplo n.º 6
0
 def test_position_unchanged(self):
     # Either unpacking works and offset is advanced or it remains the same
     # after unpacking, no halfways things.
     db = DataBuffer.fromhex("00 aa ab cd 11 22")
     db.offset = 1
     with self.assertRaises(DeserializationException):
         self._BASE_STRUCT.unpack(db)
     self.assertEqual(db.offset, 1)
Ejemplo n.º 7
0
 def test_basic_unpacking(self):
     self.assertEqual(
         self._BASE_STRUCT.unpack(DataBuffer.fromhex("aa ab cd 11 22 33")),
         {
             "first": 0xaa,
             "second": 0xabcd,
             "third": 0x112233,
         })
Ejemplo n.º 8
0
 def test_trailing_data_error(self):
     structure = Structure((IM("inner_data",
                               "opaque8",
                               inner=Structure([
                                   IM("value", "uint16"),
                               ])), ))
     binary_encoding = bytes.fromhex("04 11 11 22 22")
     with self.assertRaises(IncompleteUnpackingException):
         structure.unpack(DataBuffer(binary_encoding))
Ejemplo n.º 9
0
    def _assert_encoding_decoding(self, structure, values, binary_encoding):
        # Test encoding
        data = structure.pack(values)
        self.assertEqual(data, binary_encoding)

        # Test decoding
        db = DataBuffer(data)
        decoded_values = structure.unpack(db)
        self.assertEqual(values, decoded_values)
Ejemplo n.º 10
0
 def _decode_hook(self):
     self._payload = None
     if self.asn1 is None:
         return
     raw_data = bytes(self.asn1)
     try:
         self._payload = SignedCertificateTimestampList.unpack(
             DataBuffer(raw_data))
     except DeserializationException:
         pass
Ejemplo n.º 11
0
 def test_array_unpacking(self):
     structure = Structure((
         IM("int1", "uint8"),
         IM("data", "array[6, ab]"),
         IM("int2", "uint8"),
     ))
     self.assertEqual(structure.unpack(DataBuffer(b"\xaafoobar\xbb")), {
         "data": b"foobar",
         "int1": 0xaa,
         "int2": 0xbb
     })
Ejemplo n.º 12
0
 def _unpack(self, databuffer):
     length = self._length_field.unpack(databuffer)
     data = databuffer.get(length)
     if (self._string_encoding is not None):
         data = data.decode(self._string_encoding)
     elif self._inner is not None:
         db = DataBuffer(data)
         if not self._contains_array:
             data = self._inner.unpack(db)
         else:
             result_array = []
             while db.remaining > 0:
                 pre_unpack = db.remaining
                 result_array.append(self._inner.unpack(db))
                 if pre_unpack == db.remaining:
                     raise InfiniteUnpackingException(
                         "Inner unpacking of %s consumed no data, still %d bytes remaining. Breaking infinite loop."
                         % (str(self._inner), db.remaining))
             data = result_array
         if (db.remaining > 0) and (not self._allow_trailing_data):
             raise IncompleteUnpackingException(
                 "%s unpacking still has %d bytes of trailing, non-consumed data left."
                 % (self.typename, db.remaining))
     return data
Ejemplo n.º 13
0
    def test_client_hello(self):
        """Trying to exactly replicate a ClientHello packet we sniffed using
		Wireshark. Serialization and deserialization."""

        reference_packet = bytes.fromhex(
            "1603010076010000720301ba616e70c10cd842eacbf26cca4bfc7e736ce718b18f90becff766fb7cff045e00000ac00ac014c009c01300ff0100003f0000001700150000126a6f68616e6e65732d62617565722e636f6d000b000403000102000a000c000a001d0017001e00190018002300000016000000170000"
        )

        (tls_version_recordlayer,
         tls_version_handshake) = TLSVersion.ProtocolTLSv1_0.value
        client_hello = {
            "payload": {
                "handshake_protocol_version":
                tls_version_handshake,
                "client_random":
                bytes.fromhex(
                    "ba616e70c10cd842eacbf26cca4bfc7e736ce718b18f90becff766fb7cff045e"
                ),
                "session_id":
                bytes.fromhex(""),
                "cipher_suites": [
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
                    CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                    CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                    CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
                ],
                "compression_methods": [
                    CompressionMethod.Null,
                ],
                "extensions": [
                    (TLSExtensionServerNameIndication,
                     TLSExtensionServerNameIndication.create(
                         "johannes-bauer.com")),
                    (TLSExtensionECPointFormats, {
                        "content": {
                            "point_formats": [
                                ECPointFormats.uncompressed,
                                ECPointFormats.ansiX962_compressed_prime,
                                ECPointFormats.ansiX962_compressed_char2,
                            ],
                        },
                    }),
                    (TLSExtensionSupportedGroups, {
                        "content": {
                            "groups": [
                                SupportedGroups.X25519,
                                SupportedGroups.secp256r1,
                                SupportedGroups.X448,
                                SupportedGroups.secp521r1,
                                SupportedGroups.secp384r1,
                            ],
                        },
                    }),
                    (TLSExtensionFlag, {
                        "extension_id": ExtensionType.session_ticket
                    }),
                    (TLSExtensionFlag, {
                        "extension_id": ExtensionType.encrypt_then_mac
                    }),
                    (TLSExtensionFlag, {
                        "extension_id": ExtensionType.extended_master_secret
                    }),
                ]
            },
        }
        serialized_client_hello = ClientHelloPkt.pack(client_hello)

        record_layer_packet = {
            "content_type": ContentType.Handshake,
            "record_layer_version": tls_version_recordlayer,
            "payload": serialized_client_hello,
        }
        serialized_record_layer_packet = RecordLayerPkt.pack(
            record_layer_packet)
        self.assertEqual(reference_packet, serialized_record_layer_packet)

        deserialized_record_layer = RecordLayerPkt.unpack(
            DataBuffer(reference_packet))
        self.assertEqual(record_layer_packet, deserialized_record_layer)

        deserialized_client_hello = ClientHelloPkt.unpack(
            DataBuffer(record_layer_packet["payload"]))
        self.assertEqual(deserialized_client_hello, client_hello)
Ejemplo n.º 14
0
 def test_inited(self):
     db = DataBuffer(b"ABC")
     self.assertEqual(db.get(3), bytes.fromhex("41 42 43"))
Ejemplo n.º 15
0
 def test_position_advanced(self):
     db = DataBuffer.fromhex("aa ab cd 11 22 33")
     self._BASE_STRUCT.unpack(db)
     self.assertEqual(db.offset, 6)