Пример #1
0
    def redirect_udp_request(self, packet: pydivert.Packet):
        # Probably didn't need to duplicate this, since it works on the IP layer, but just in case... - N3X
        # print(" * Redirect client -> server to proxy")
        # print(f"{packet.src_addr}:{packet.src_port} -> {packet.dst_addr}:{packet.dst_port}")
        client = (packet.src_addr, packet.src_port)

        self.client_server_map[client] = (packet.dst_addr, packet.dst_port)

        # We do need to inject to an external IP here, 127.0.0.1 does not work.
        if packet.address_family == socket.AF_INET:
            assert self.ipv4_address
            packet.dst_addr = self.ipv4_address
        elif packet.address_family == socket.AF_INET6:
            if not self.ipv6_address:
                self.ipv6_address = get_local_ip6(packet.src_addr)
            assert self.ipv6_address
            packet.dst_addr = self.ipv6_address
        else:
            raise RuntimeError("Unknown address family")
        packet.dst_port = self.proxy_port
        packet.direction = pydivert.consts.Direction.INBOUND

        # We need a handle on the NETWORK layer. the local handle is not guaranteed to exist,
        # so we use the response handle.
        self.response.windivert.send(packet)
Пример #2
0
    def redirect_response(self, packet: pydivert.Packet):
        """
        If the proxy responds to the client, let the client believe the target server sent the
        packets.
        """
        # print(" * Adjust proxy -> client")
        client = (packet.dst_addr, packet.dst_port)
        try:
            packet.src_addr, packet.src_port = self.client_server_map[client]
        except KeyError:
            print(f"Warning: Previously unseen connection from proxy to {client}")
        else:
            packet.recalculate_checksums()

        self.response.windivert.send(packet, recalculate_checksum=False)
Пример #3
0
    def redirect_response(self, packet: pydivert.Packet):
        """
        If the proxy responds to the client, let the client believe the target server sent the
        packets.
        """
        # print(" * Adjust proxy -> client")
        client = (packet.dst_addr, packet.dst_port)
        try:
            packet.src_addr, packet.src_port = self.client_server_map[client]
        except KeyError:
            print(f"Warning: Previously unseen connection from proxy to {client}")
        else:
            packet.recalculate_checksums()

        self.response.windivert.send(packet, recalculate_checksum=False)
Пример #4
0
    def _request(self, packet: pydivert.Packet):
        # print(" * Redirect client -> server to proxy")
        # print("%s:%s -> %s:%s" % (packet.src_addr, packet.src_port, packet.dst_addr, packet.dst_port))
        client = (packet.src_addr, packet.src_port)
        server = (packet.dst_addr, packet.dst_port)

        if client in self.client_server_map:
            self.client_server_map.move_to_end(client)
        else:
            while len(self.client_server_map) > self.connection_cache_size:
                self.client_server_map.popitem(False)
            self.client_server_map[client] = server

        packet.dst_addr, packet.dst_port = self.proxy_addr, self.proxy_port
        packet.direction = pydivert.consts.Direction.INBOUND

        # Use any handle that's on the NETWORK layer - request_local may be
        # unavailable.
        self.response_handle.send(packet)
Пример #5
0
    def _request(self, packet: pydivert.Packet):
        # print(" * Redirect client -> server to proxy")
        # print("%s:%s -> %s:%s" % (packet.src_addr, packet.src_port, packet.dst_addr, packet.dst_port))
        client = (packet.src_addr, packet.src_port)
        server = (packet.dst_addr, packet.dst_port)

        if client in self.client_server_map:
            self.client_server_map.move_to_end(client)
        else:
            while len(self.client_server_map) > self.connection_cache_size:
                self.client_server_map.popitem(False)
            self.client_server_map[client] = server

        packet.dst_addr, packet.dst_port = self.proxy_addr, self.proxy_port
        packet.direction = pydivert.consts.Direction.INBOUND

        # Use any handle thats on the NETWORK layer - request_local may be
        # unavailable.
        self.response_handle.send(packet)
Пример #6
0
    def redirect_request(self, packet: pydivert.Packet):
        # print(" * Redirect client -> server to proxy")
        # print(f"{packet.src_addr}:{packet.src_port} -> {packet.dst_addr}:{packet.dst_port}")
        client = (packet.src_addr, packet.src_port)

        self.client_server_map[client] = (packet.dst_addr, packet.dst_port)

        # We do need to inject to an external IP here, 127.0.0.1 does not work.
        if packet.address_family == socket.AF_INET:
            assert self.ipv4_address
            packet.dst_addr = self.ipv4_address
        elif packet.address_family == socket.AF_INET6:
            if not self.ipv6_address:
                self.ipv6_address = get_local_ip6(packet.src_addr)
            assert self.ipv6_address
            packet.dst_addr = self.ipv6_address
        else:
            raise RuntimeError("Unknown address family")
        packet.dst_port = self.proxy_port
        packet.direction = pydivert.consts.Direction.INBOUND

        # We need a handle on the NETWORK layer. the local handle is not guaranteed to exist,
        # so we use the response handle.
        self.response.windivert.send(packet)