示例#1
0
    def test_ip_payload(self):
        ethernet_header_hex = IP_PAYLOAD_TEST_CONTEXT["ETHERNET_HEADER"]
        ip_payload_hex = IP_PAYLOAD_TEST_CONTEXT["IP_PAYLOAD"]
        packet_hex = ethernet_header_hex + ip_payload_hex

        packet = EthernetPacket(dest_mac="00:7e:95:02:61:42",
                                source_mac="e0:d5:5e:21:b0:cb")
        packet_bytes = packet.to_bytes()
        self.assertEqual(ethernet_header_hex, packet_bytes.hex())

        ip_packet = IpPacket(source_addr_str="127.0.0.1",
                             dest_addr_str="8.8.8.7",
                             identification=49101,
                             flags=IpFragmentationFlags.from_int(0),
                             ttl=255,
                             protocol=0)
        self.assertEqual(ip_payload_hex, ip_packet.to_bytes().hex())

        ethernet_ip = packet / ip_packet
        self.assertEqual(packet_hex, ethernet_ip.to_bytes().hex())
        self.assertEqual(ethernet_ip,
                         EthernetPacket.from_bytes(bytes.fromhex(packet_hex)))
        self.assertTrue(EthernetPacket in ethernet_ip)
        self.assertTrue(IpPacket in ethernet_ip)
        self.assertEqual(ip_packet, ethernet_ip[IpPacket])
示例#2
0
    def test_to_bytes(self):
        tcp_packet1 = TcpPacket(
            source_port=59700,
            dest_port=443,
            sequence_number=1407506493,
            ack_number=3676709599,
            flags=TcpControlBits(ack=True),
            win_size=501,
            urg_pointer=0,
            options=TcpOptions([
                TcpOptions.NOP, TcpOptions.NOP,
                (TcpOptions.TIMESTAMPS, [3252488245, 365238493])
            ]),
        )
        tcp_packet1 = IpPacket(source_addr_str="192.168.1.32",
                               dest_addr_str="35.160.240.60") / tcp_packet1
        self.__test_tcp_packet(PACKET_DUMP_1, tcp_packet1)

        tcp_packet2 = TcpPacket(source_port=44134,
                                dest_port=443,
                                sequence_number=2302261952,
                                ack_number=1291093731,
                                flags=TcpControlBits(ack=True),
                                win_size=496)
        tcp_packet2 = IpPacket(source_addr_str="10.10.128.44",
                               dest_addr_str="40.101.18.242") / tcp_packet2
        self.__test_tcp_packet(PACKET_DUMP_2, tcp_packet2)

        tcp_packet3 = TcpPacket(
            source_port=43480,
            dest_port=2193,
            sequence_number=703011338,
            ack_number=2915529351,
            flags=TcpControlBits(ack=True, psh=True),
            win_size=22461,
            options=TcpOptions([
                TcpOptions.NOP, TcpOptions.NOP,
                (TcpOptions.TIMESTAMPS, [1314978149, 3029537658])
            ]),
        ) / bytearray.fromhex(PACKET_DUMP_3_PAYLOAD)
        tcp_packet3 = IpPacket(source_addr_str="10.10.128.44",
                               dest_addr_str="10.10.144.153") / tcp_packet3
        self.__test_tcp_packet(PACKET_DUMP_3, tcp_packet3)

        tcp_packet4 = TcpPacket(
            source_port=59058,
            dest_port=443,
            sequence_number=746641599,
            ack_number=1952224292,
            flags=TcpControlBits(ack=True, psh=True),
            win_size=2483,
            options=TcpOptions([
                TcpOptions.NOP, TcpOptions.NOP,
                (TcpOptions.TIMESTAMPS, [4044761679, 555562620])
            ]),
        ) / bytearray.fromhex(PACKET_DUMP_4_PAYLOAD)
        tcp_packet4 = IpPacket(source_addr_str="192.168.1.32",
                               dest_addr_str="93.186.225.198") / tcp_packet4
        self.__test_tcp_packet(PACKET_DUMP_4, tcp_packet4)
示例#3
0
    def test_udp_ip_payload(self):
        ethernet_header_hex = UDP_IP_PAYLOAD_TEST_CONTEXT["ETHERNET_HEADER"]
        ip_payload_hex = UDP_IP_PAYLOAD_TEST_CONTEXT["IP_PAYLOAD"]
        udp_payload_hex = UDP_IP_PAYLOAD_TEST_CONTEXT["UDP_PAYLOAD"]
        app_payer_payload = UDP_IP_PAYLOAD_TEST_CONTEXT["APP_LAYER_PAYLOAD"]
        packet_hex = (ethernet_header_hex + ip_payload_hex + udp_payload_hex +
                      app_payer_payload)

        ethernet_packet = EthernetPacket(dest_mac="0c:84:dc:a6:bf:c1",
                                         source_mac="34:da:b7:87:d5:34")
        self.assertEqual(ethernet_header_hex, ethernet_packet.to_bytes().hex())

        ip_packet = IpPacket(source_addr_str="86.57.135.193",
                             dest_addr_str="192.168.1.32",
                             dscp=IpDiffServiceValues.CS2,
                             identification=53262,
                             flags=IpFragmentationFlags(),
                             ttl=252,
                             protocol=socket.IPPROTO_UDP)

        udp_packet = UdpPacket(
            dest_port=39237,
            source_port=443) / bytes.fromhex(app_payer_payload)

        packet = ethernet_packet / ip_packet / udp_packet
        self.assertEqual(packet_hex, packet.to_bytes().hex())
        self.assertEqual(packet,
                         EthernetPacket.from_bytes(bytes.fromhex(packet_hex)))
        self.assertTrue(EthernetPacket in packet)
        self.assertTrue(IpPacket in packet)
        self.assertTrue(UdpPacket in packet)
        self.assertFalse(TcpPacket in packet)
示例#4
0
    def test_to_bytes(self):
        udp_packet1 = UdpPacket(
            source_port=55380,
            dest_port=53
        ) / bytes.fromhex(PACKET_DUMP_1_PAYLOAD)
        udp_packet1 = IpPacket(
            source_addr_str="192.168.1.32",
            dest_addr_str="192.168.1.1",
            protocol=socket.IPPROTO_UDP
        ) / udp_packet1
        self.__test_udp_packet(PACKET_DUMP_1, udp_packet1)

        udp_packet2 = UdpPacket(
            source_port=53349,
            dest_port=1194
        ) / bytes.fromhex(PACKET_DUMP_2_PAYLOAD)
        udp_packet2 = IpPacket(
            source_addr_str="192.168.1.32",
            dest_addr_str="217.38.170.114",
            protocol=socket.IPPROTO_UDP
        ) / udp_packet2
        self.__test_udp_packet(PACKET_DUMP_2, udp_packet2)
示例#5
0
    def test_packet_creation_with_invalid_fields(self):
        # pass too long payload
        invalid_ip_packet = IpPacket(
            source_addr_str="10.10.128.44",
            dest_addr_str="216.58.209.14",
        ) / bytearray(65535)
        self.assertRaises(ValueError, invalid_ip_packet.to_bytes)

        # pass too long Identification field
        self.assertRaises(ValueError,
                          IpPacket,
                          source_addr_str="10.10.128.44",
                          dest_addr_str="216.58.209.14",
                          identification=pow(2, 16))

        # pass too long Fragment Offset field
        self.assertRaises(ValueError,
                          IpPacket,
                          source_addr_str="10.10.128.44",
                          dest_addr_str="216.58.209.14",
                          fragment_offset=pow(2, 13))
示例#6
0
    def test_tcp_ip_payload(self):
        ethernet_header_hex = TCP_IP_PAYLOAD_TEST_CONTEXT["ETHERNET_HEADER"]
        ip_payload_hex = TCP_IP_PAYLOAD_TEST_CONTEXT["IP_PAYLOAD"]
        tcp_payload_hex = TCP_IP_PAYLOAD_TEST_CONTEXT["TCP_PAYLOAD"]
        packet_hex = ethernet_header_hex + ip_payload_hex + tcp_payload_hex

        ethernet_packet = EthernetPacket(dest_mac="e0:d5:5e:21:b0:cb",
                                         source_mac="00:7e:95:02:61:42")
        self.assertEqual(ethernet_header_hex, ethernet_packet.to_bytes().hex())

        ip_packet = IpPacket(source_addr_str="3.123.217.208",
                             dest_addr_str="10.10.128.44",
                             identification=11150,
                             flags=IpFragmentationFlags(df=True),
                             ttl=47,
                             protocol=socket.IPPROTO_TCP)
        tcp_packet = TcpPacket(
            source_port=443,
            dest_port=55978,
            sequence_number=2555500760,
            ack_number=1254966751,
            flags=TcpControlBits(ack=True),
            win_size=10,
            options=TcpOptions([
                TcpOptions.NOP, TcpOptions.NOP,
                (TcpOptions.TIMESTAMPS, [654701382, 3921945890])
            ]),
        )

        packet = ethernet_packet / ip_packet / tcp_packet
        self.assertEqual(packet_hex, packet.to_bytes().hex())
        self.assertEqual(packet,
                         EthernetPacket.from_bytes(bytes.fromhex(packet_hex)))
        self.assertTrue(EthernetPacket in packet)
        self.assertTrue(IpPacket in packet)
        self.assertTrue(TcpPacket in packet)
示例#7
0
    def test_to_bytes(self):
        ip_packet_1 = IpPacket(source_addr_str="192.168.1.8",
                               dest_addr_str="126.12.14.67",
                               flags=IpFragmentationFlags(),
                               identification=39434,
                               protocol=TEST_PROTO_TYPE) / bytes([0x58] * 5)
        hex_dump_1 = ip_packet_1.to_bytes().hex()
        self.assertEqual(PACKET_DUMP_1, hex_dump_1)
        self.assertEqual(ip_packet_1,
                         IpPacket.from_bytes(ip_packet_1.to_bytes()))

        ip_packet_2 = IpPacket(source_addr_str="192.168.1.8",
                               dest_addr_str="8.8.8.8",
                               dscp=IpDiffServiceValues.EF,
                               flags=IpFragmentationFlags(),
                               identification=29320)
        hex_dump_2 = ip_packet_2.to_bytes().hex()
        self.assertEqual(PACKET_DUMP_2, hex_dump_2)
        self.assertEqual(ip_packet_2,
                         IpPacket.from_bytes(ip_packet_2.to_bytes()))

        ip_packet_3 = IpPacket(source_addr_str="192.168.1.8",
                               dest_addr_str="8.8.8.8",
                               dscp=IpDiffServiceValues.EF,
                               ecn=IpEcnValues.CE,
                               flags=IpFragmentationFlags(),
                               identification=55463)
        hex_dump_3 = ip_packet_3.to_bytes().hex()
        self.assertEqual(PACKET_DUMP_3, hex_dump_3)
        self.assertEqual(ip_packet_3,
                         IpPacket.from_bytes(ip_packet_3.to_bytes()))