proxy for udp packet allows packets to pass through, but does not change their source ip address

Viewed 490

I'm trying to route my outbound udp packets through an external proxy server (not expecting traffic to return atm). The traffic makes it to the destination server, however the source ip address in the packets is my client's ip address rather than the external proxy's.

What am I doing wrong?

from scapy.all import *
import socks, socket

sent = False

def intercept(pkt):
        if pkt.haslayer(UDP):
                s = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM)
                s.setproxy(socks.PROXY_TYPE_SOCKS5, "proxy-server-ip", 19012, True)
                dst_addr = pkt[IP].dst
                global sent
                if not sent:
                        dst_port = pkt[UDP].dport
                        address = (dst_addr, dst_port)
                        s.sendto(bytes("test", 'utf-8'), address)
                        sent = True
def main():
        sniff(iface='ens3', prn=intercept)

if __name__ == '__main__':
        main()

The way I'm checking the udp packets are with a small client/server configuration.

Client:

import socket
import sys

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('server-ip', 10000)
message = 'This is the message.  It will be repeated.'

try:

    # Send data
    print >>sys.stderr, 'sending "%s"' % message
    sent = sock.sendto(message, server_address)

    # Receive response
    print >>sys.stderr, 'waiting to receive'
    data, server = sock.recvfrom(4096)
    print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

Server:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('0.0.0.0', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

while True:
    print >>sys.stderr, '\nwaiting to receive message'
    data, address = sock.recvfrom(4096)

    print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
    print >>sys.stderr, data

    if data:
        sent = sock.sendto(data, address)
        print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)

0 Answers
Related