def test_modify_tcp_header_shortcut(self): """ Tests injection of a packet with a modified tcp header using shortcutted send """ fake_port = random_free_port() fake_addr = "10.10.10.10" srv_port = self.server.server_address[1] srv_addr = self.server.server_address[0] text = "Hello World!" client = FakeTCPClient((fake_addr, fake_port), text.encode("UTF-8")) client_thread = threading.Thread(target=client.send) f = "tcp.DstPort == %d or tcp.SrcPort == %d" % (fake_port, srv_port) with Handle(filter=f, priority=1000) as handle: # Initialize the fake tcp client client_thread.start() while True: packet = handle.receive() if packet.meta.is_outbound(): if packet.dst_port == fake_port: packet.dst_port = srv_port packet.dst_addr = srv_addr if packet.src_port == srv_port: packet.src_port = fake_port packet.src_addr = fake_addr handle.send(packet) if hasattr(client, "response") and client.response: break client_thread.join(timeout=10) self.assertEqual(text.upper(), client.response.decode("UTF-8"))
def test_modify_tcp_header(self): """ Tests injection of a packet with a modified tcp header """ fake_port = random_free_port() srv_port = self.server.server_address[1] text = "Hello World!" client = FakeTCPClient(("127.0.0.1", fake_port), text.encode("UTF-8")) client_thread = threading.Thread(target=client.send) f = "tcp.DstPort == %d or tcp.SrcPort == %d" % (fake_port, srv_port) with Handle(filter=f, priority=1000) as handle: # Initialize the fake tcp client client_thread.start() while True: raw_packet, meta = handle.recv() packet = handle.driver.parse_packet(raw_packet) if meta.is_outbound(): if packet.dst_port == fake_port: packet.dst_port = srv_port if packet.src_port == srv_port: packet.src_port = fake_port packet = handle.driver.update_packet_checksums(packet) handle.send((packet.raw, meta)) if hasattr(client, "response") and client.response: break client_thread.join(timeout=10) self.assertEqual(text.upper(), client.response.decode("UTF-8"))
def test_modify_tcp_payload(self): """ Tests injection of a TCP packet with modified payload """ text = "Hello world! ZZZZ" new_text = "Hello World!" # Initialize the fake tcp client client = FakeTCPClient(self.server.server_address, text.encode("UTF-8"), ipv6=True) client_thread = threading.Thread(target=client.send) f = "outbound and tcp.DstPort == %s and tcp.PayloadLength > 0" % self.server.server_address[ 1] with Handle(filter=f) as handle: client_thread.start() raw_packet, metadata = handle.recv() if metadata.is_outbound(): packet = handle.driver.parse_packet(raw_packet) self.assertEqual(text.encode("UTF-8"), packet.payload) packet.payload = new_text.encode("UTF-8") raw_packet = packet.raw handle.send((raw_packet, metadata)) client_thread.join(timeout=10) self.assertEqual(new_text.upper(), client.response.decode("UTF-8"))
def test_syn_tcp_options(self): """ Tests the right capturing of tcp options field """ text = "Hello World!" client = FakeTCPClient(self.server.server_address, text.encode("UTF-8"), ipv6=True) client_thread = threading.Thread(target=client.send) with Handle(filter="tcp.DstPort == %d" % self.server.server_address[1]) as handle: client_thread.start() packet = handle.receive() assert packet.tcp_hdr.Syn == 1 assert hexlify(packet.tcp_hdr.Options) == b"0204ffc30103030801010402"
def test_pass_through(self): """ Tests capture and reinjection of data """ srv_port = self.server.server_address[1] text = "Hello World!" client = FakeTCPClient(self.server.server_address, text.encode("UTF-8"), ipv6=True) client_thread = threading.Thread(target=client.send) with Handle(filter="tcp.DstPort == %d and tcp.PayloadLength > 0" % srv_port) as handle: client_thread.start() handle.send(handle.receive()) client_thread.join(timeout=10) assert text.upper() == client.response.decode()
def test_syn_tcp_options(self): """ Tests the right capturing of tcp options field """ text = "Hello World!" client = FakeTCPClient(self.server.server_address, text.encode("UTF-8")) client_thread = threading.Thread(target=client.send) with Handle(filter="tcp.DstPort == %d" % self.server.server_address[1]) as handle: client_thread.start() packet = handle.receive() self.assertEqual(packet.tcp_hdr.Syn, 1) self.assertEqual(hexlify(packet.tcp_hdr.Options), b"0204ffd70103030801010402") client_thread.join(timeout=10)
def setUp(self): super(WinDivertAsyncTestCase, self).setUp() # Initialize the fake tcp server self.server = FakeTCPServerIPv4(("127.0.0.1", 0), EchoUpperTCPHandler) filter = "outbound and tcp.DstPort == %d and tcp.PayloadLength > 0" % self.server.server_address[1] self.driver = WinDivert() self.driver.register() self.handle = self.driver.open_handle(filter=filter) self.server_thread = threading.Thread(target=self.server.serve_forever) self.server_thread.start() # Initialize the fake tcp client self.text = "Hello World!" self.client = FakeTCPClient(self.server.server_address, self.text.encode("UTF-8")) self.client_thread = threading.Thread(target=self.client.send) self.client_thread.start()
def test_pass_through_mtu_size(self): """ Tests sending a packet bigger than mtu """ srv_port = self.server.server_address[1] text = b"#" * (Defaults.PACKET_BUFFER_SIZE + 1) client = FakeTCPClient(("127.0.0.1", srv_port), text) client_thread = threading.Thread(target=client.send) f = "tcp.DstPort == {0} or tcp.SrcPort == {0} and tcp.PayloadLength > 0".format(srv_port) with Handle(filter=f, priority=1000) as handle: # Initialize the fake tcp client client_thread.start() while True: handle.send(handle.receive()) if hasattr(client, "response") and client.response: break client_thread.join(timeout=10) assert text == client.response