Exemple #1
0
    def test_encrypt_payload(self):
        client = FfClient(FfConfig(ip_address='127.0.0.1', port=8080,
                                   pre_shared_key='testabc', log_level=logging.DEBUG))

        request = FfRequest(version=FfRequest.Version.V1,
                            request_id=123)
        payload = "GET / HTTP/1.1\nHost: google.com\n\n"

        encrypted_payload = client.encrypted_request_payload(request, payload)

        self.assertGreater(len(encrypted_payload), 0)
        self.assertNotEqual(payload, encrypted_payload)

        encryption_mode = [i for i in request.options if i.type ==
                           FfRequest.Option.Type.ENCRYPTION_MODE][0].value
        self.assertEqual(FfRequest.EncryptionMode.AES_256_GCM,
                         encryption_mode[0])

        encryption_iv = [i for i in request.options if i.type ==
                         FfRequest.Option.Type.ENCRYPTION_IV][0].value
        self.assertEqual(12, len(encryption_iv))

        encryption_tag = [i for i in request.options if i.type ==
                          FfRequest.Option.Type.ENCRYPTION_TAG][0].value
        self.assertEqual(16, len(encryption_tag))
Exemple #2
0
    def test_send_get_request(self):
        client = FfClient(
            FfConfig(ip_address='127.0.0.1',
                     port=8080,
                     log_level=logging.DEBUG))

        client.send_request('GET / HTTP/1.1\nHost: google.com\n\n')
Exemple #3
0
    def test_new(self):
        client = FfClient(
            FfConfig(ip_address='127.0.0.1',
                     port=8080,
                     log_level=logging.DEBUG))

        self.assertEqual('127.0.0.1', client.config.ip_address)
        self.assertEqual(8080, client.config.port)
Exemple #4
0
    def test_create_request_packets(self):
        client = FfClient(
            FfConfig(ip_address='127.0.0.1',
                     port=8080,
                     pre_shared_key='testabc',
                     log_level=logging.DEBUG))

        http_request = "POST / HTTP/1.1\nHost: google.com.au\n\nThis is the request body"
        packets = client.create_request_packets(http_request)

        self.assertEqual(1, len(packets))

        packet1_buff = packets[0].payload
        packet1_len = packets[0].length
        ptr = 0

        self.assertEqual(126, packet1_len)

        # Request version
        self.assertEqual(FfRequest.Version.V1,
                         packet1_buff[ptr] << 8 | packet1_buff[ptr + 1])
        ptr += 2

        # Request ID
        self.assertNotEqual(0, (packet1_buff[ptr] << 56
                                | packet1_buff[ptr + 1] << 48
                                | packet1_buff[ptr + 2] << 40
                                | packet1_buff[ptr + 3] << 32
                                | packet1_buff[ptr + 4] << 24
                                | packet1_buff[ptr + 5] << 16
                                | packet1_buff[ptr + 6] << 8
                                | packet1_buff[ptr + 7]))
        ptr += 8

        # Total length
        self.assertEqual(len(http_request), (packet1_buff[ptr] << 24
                                             | packet1_buff[ptr + 1] << 16
                                             | packet1_buff[ptr + 2] << 8
                                             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk offset
        self.assertEqual(0, (packet1_buff[ptr] << 24
                             | packet1_buff[ptr + 1] << 16
                             | packet1_buff[ptr + 2] << 8
                             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk length
        self.assertEqual(len(http_request), (packet1_buff[ptr] << 8
                                             | packet1_buff[ptr + 1]))
        ptr += 2

        # HTTPS option type
        self.assertEqual(FfRequest.Option.Type.HTTPS, packet1_buff[ptr])
        ptr += 1

        # HTTPS option length
        self.assertEqual(1, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # HTTPS option value
        self.assertEqual(1, packet1_buff[ptr])
        ptr += 1

        # Encryption Mode option type
        self.assertEqual(FfRequest.Option.Type.ENCRYPTION_MODE,
                         packet1_buff[ptr])
        ptr += 1

        # Encryption Mode option length
        self.assertEqual(1, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Encryption Mode option value
        self.assertEqual(FfRequest.EncryptionMode.AES_256_GCM,
                         packet1_buff[ptr])
        ptr += 1

        # Encryption IV option type
        self.assertEqual(FfRequest.Option.Type.ENCRYPTION_IV,
                         packet1_buff[ptr])
        ptr += 1

        # Encryption IV option length
        self.assertEqual(12, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Encryption IV option value
        ptr += 12

        # Encryption Tag option type
        self.assertEqual(FfRequest.Option.Type.ENCRYPTION_TAG,
                         packet1_buff[ptr])
        ptr += 1

        # Encryption Tag option length
        self.assertEqual(16, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Encryption Tag option value
        ptr += 16

        # EOL option type
        self.assertEqual(FfRequest.Option.Type.EOL, packet1_buff[ptr])
        ptr += 1

        # EOL option length
        self.assertEqual(0, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Payload
        self.assertNotEqual(bytearray(http_request.encode('utf8')),
                            packet1_buff[ptr:packet1_len])
Exemple #5
0
    def test_create_request_packets(self):
        client = FfClient(
            FfConfig(ip_address='127.0.0.1',
                     port=8080,
                     log_level=logging.DEBUG))

        http_request = "POST / HTTP/1.1\nHost: google.com.au\n\n".ljust(
            2000, '0')
        packets = client.create_request_packets(http_request, https=False)

        payload_options_length = 11 + 3  # Timstamp option + EOL option

        self.assertEqual(2, len(packets))

        packet1_buff = packets[0].payload
        packet1_len = packets[0].length
        ptr = 0

        # -- Packet 1 --

        self.assertEqual(1300, packet1_len)

        # Request version
        self.assertEqual(FfRequest.Version.V1,
                         packet1_buff[ptr] << 8 | packet1_buff[ptr + 1])
        ptr += 2

        # Request ID
        packet1_request_id = (packet1_buff[ptr] << 56
                              | packet1_buff[ptr + 1] << 48
                              | packet1_buff[ptr + 2] << 40
                              | packet1_buff[ptr + 3] << 32
                              | packet1_buff[ptr + 4] << 24
                              | packet1_buff[ptr + 5] << 16
                              | packet1_buff[ptr + 6] << 8
                              | packet1_buff[ptr + 7])
        self.assertNotEqual(0, packet1_request_id)
        ptr += 8

        # Total length
        self.assertEqual(
            len(http_request) + payload_options_length,
            (packet1_buff[ptr] << 24
             | packet1_buff[ptr + 1] << 16
             | packet1_buff[ptr + 2] << 8
             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk offset
        self.assertEqual(0, (packet1_buff[ptr] << 24
                             | packet1_buff[ptr + 1] << 16
                             | packet1_buff[ptr + 2] << 8
                             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk length
        self.assertEqual(1277, (packet1_buff[ptr] << 8
                                | packet1_buff[ptr + 1]))
        ptr += 2

        self.assertEqual(FfRequest.Option.Type.BREAK, packet1_buff[ptr])
        ptr += 1
        # Break option length
        self.assertEqual(0, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Timestamp option type
        self.assertEqual(FfRequest.Option.Type.TIMESTAMP, packet1_buff[ptr])
        ptr += 1
        # Timestamp option length
        self.assertEqual(8, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2
        # Timestamp option value
        self.assertAlmostEqual(time.time(),
                               packet1_buff[ptr] << 56
                               | packet1_buff[ptr + 1] << 48
                               | packet1_buff[ptr + 2] << 40
                               | packet1_buff[ptr + 3] << 32
                               | packet1_buff[ptr + 4] << 24
                               | packet1_buff[ptr + 5] << 16
                               | packet1_buff[ptr + 6] << 8
                               | packet1_buff[ptr + 7],
                               delta=5)
        ptr += 8

        # EOL option type
        self.assertEqual(FfRequest.Option.Type.EOL, packet1_buff[ptr])
        ptr += 1
        # EOL option length
        self.assertEqual(0, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Payload
        self.assertEqual(
            bytearray(http_request.encode('utf8'))[:1277 -
                                                   payload_options_length],
            packet1_buff[ptr:packet1_len])

        # -- Packet 2 --

        packet2_buff = packets[1].payload
        packet2_len = packets[1].length
        ptr = 0

        self.assertEqual(760, packet2_len)

        # Request version
        self.assertEqual(FfRequest.Version.V1,
                         packet2_buff[ptr] << 8 | packet2_buff[ptr + 1])
        ptr += 2

        # Request ID
        packet2_request_id = (packet2_buff[ptr] << 56
                              | packet2_buff[ptr + 1] << 48
                              | packet2_buff[ptr + 2] << 40
                              | packet2_buff[ptr + 3] << 32
                              | packet2_buff[ptr + 4] << 24
                              | packet2_buff[ptr + 5] << 16
                              | packet2_buff[ptr + 6] << 8
                              | packet2_buff[ptr + 7])
        self.assertNotEqual(0, packet2_request_id)
        ptr += 8

        # Total length
        self.assertEqual(
            len(http_request) + payload_options_length,
            (packet2_buff[ptr] << 24
             | packet2_buff[ptr + 1] << 16
             | packet2_buff[ptr + 2] << 8
             | packet2_buff[ptr + 3]))
        ptr += 4

        # Chunk offset
        self.assertEqual(1277, (packet2_buff[ptr] << 24
                                | packet2_buff[ptr + 1] << 16
                                | packet2_buff[ptr + 2] << 8
                                | packet2_buff[ptr + 3]))
        ptr += 4

        # Chunk length
        self.assertEqual(
            len(http_request) - (1277 - payload_options_length),
            (packet2_buff[ptr] << 8
             | packet2_buff[ptr + 1]))
        ptr += 2

        # EOL option type
        self.assertEqual(FfRequest.Option.Type.EOL, packet2_buff[ptr])
        ptr += 1
        # EOL option length
        self.assertEqual(0, packet2_buff[ptr] << 16 | packet2_buff[ptr + 1])
        ptr += 2

        # Payload
        self.assertEqual(
            bytearray(http_request.encode('utf8'))[1277 -
                                                   payload_options_length:],
            packet2_buff[ptr:packet2_len])

        # Request ID's
        self.assertEqual(packet1_request_id, packet2_request_id)
Exemple #6
0
    def test_create_request_packets_for_get_request(self):
        client = FfClient(
            FfConfig(ip_address='127.0.0.1',
                     port=8080,
                     log_level=logging.DEBUG))

        http_request = "GET / HTTP/1.1\nHost: google.com.au\n\n"
        packets = client.create_request_packets(http_request, https=False)

        self.assertEqual(1, len(packets))

        packet1_buff = packets[0].payload
        packet1_len = packets[0].length
        ptr = 0

        self.assertEqual(59, packet1_len)

        # Request version
        self.assertEqual(FfRequest.Version.V1,
                         packet1_buff[ptr] << 8 | packet1_buff[ptr + 1])
        ptr += 2

        # Request ID
        self.assertNotEqual(0, (packet1_buff[ptr] << 56
                                | packet1_buff[ptr + 1] << 48
                                | packet1_buff[ptr + 2] << 40
                                | packet1_buff[ptr + 3] << 32
                                | packet1_buff[ptr + 4] << 24
                                | packet1_buff[ptr + 5] << 16
                                | packet1_buff[ptr + 6] << 8
                                | packet1_buff[ptr + 7]))
        ptr += 8

        # Total length
        self.assertEqual(len(http_request), (packet1_buff[ptr] << 24
                                             | packet1_buff[ptr + 1] << 16
                                             | packet1_buff[ptr + 2] << 8
                                             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk offset
        self.assertEqual(0, (packet1_buff[ptr] << 24
                             | packet1_buff[ptr + 1] << 16
                             | packet1_buff[ptr + 2] << 8
                             | packet1_buff[ptr + 3]))
        ptr += 4

        # Chunk length
        self.assertEqual(len(http_request), (packet1_buff[ptr] << 8
                                             | packet1_buff[ptr + 1]))
        ptr += 2

        # EOL option type
        self.assertEqual(FfRequest.Option.Type.EOL, packet1_buff[ptr])
        ptr += 1

        # EOL option length
        self.assertEqual(0, packet1_buff[ptr] << 16 | packet1_buff[ptr + 1])
        ptr += 2

        # Payload
        self.assertEqual(bytearray(http_request.encode('utf8')),
                         packet1_buff[ptr:packet1_len])