import sys import os import socket from threading import Thread import select class Proxy(Thread): BUFF_SIZE = 65535 # Used to keep track of "lines" prnt_id_client = 0 prnt_id_server = 0 def __init__(self, listen, target): Thread.__init__(self) self.listen = listen self.target = target self.clients = [] def run(self): client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) client.bind(self.listen) # Listen #master.listen(5) target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) target.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) target.connect(self.target) # Server #target. #client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #client.connect(self.listen) client.setblocking(0) target.setblocking(0) while True: (c, _, _) = select.select([client],[],[],0) if c: c_data, c_addr = client.recvfrom(self.BUFF_SIZE) #print("[C] data:{} addr:{}".format(c_data, c_addr)) if c_data: """ if c_data[:2] == b"\x08\x1e": print("Editing port check: {}".format(c_data.hex())) c_data = b"\x08\x1e\x77\xda" """ print("[C] -> {}".format(c_data)) with open("logs/{}-client-raw.log".format(LISTEN[1]), "a") as f: f.write("({}) {} \r\n".format(self.prnt_id_client, c_data)) with open("logs/{}-client-hex.log".format(LISTEN[1]), "a") as f: f.write("({}) {} \r\n".format(self.prnt_id_client, c_data.hex())) with open("logs/{}-all-raw.log".format(LISTEN[1]), "a") as f: f.write("[C] -> ({}) {} \r\n".format(self.prnt_id_client, c_data)) with open("logs/{}-all-hex.log".format(LISTEN[1]), "a") as f: f.write("[C] -> ({}) {} \r\n".format(self.prnt_id_client, c_data.hex())) target.sendto(c_data, self.target) self.prnt_id_client += 1 (s, _, _) = select.select([target],[],[],0) if s: s_data = target.recv(self.BUFF_SIZE) #print("[S] data:{}".format(s_data)) if s_data: print("[C] <- {}".format(s_data)) with open("logs/{}-server-raw.log".format(LISTEN[1]), "a") as f: f.write("({}) {} \r\n".format(self.prnt_id_server, s_data)) with open("logs/{}-server-hex.log".format(LISTEN[1]), "a") as f: f.write("({}) {} \r\n".format(self.prnt_id_server, s_data.hex())) with open("logs/{}-all-raw.log".format(LISTEN[1]), "a") as f: f.write("[C] <- ({}) {} \r\n".format(self.prnt_id_server, s_data)) with open("logs/{}-all-hex.log".format(LISTEN[1]), "a") as f: f.write("[C] <- ({}) {} \r\n".format(self.prnt_id_server, s_data.hex())) client.sendto(s_data, c_addr) self.prnt_id_server += 1 client.close() if len(sys.argv) < 4: print("Missing arguments.") print("Usage: $ python proxy.py [proxy_port] [server_ip] [server_port]") print("Sample: $ python proxy.py 7777 192.168.2.1 7777") exit() LISTEN = ("0.0.0.0", int(sys.argv[1])) TARGET = (sys.argv[2], int(sys.argv[3])) ##while True: if not os.path.exists("logs"): os.makedirs("logs") # Delete old log files open("logs/{}-client-raw.log".format(LISTEN[1]), "w").close() open("logs/{}-client-hex.log".format(LISTEN[1]), "w").close() open("logs/{}-server-raw.log".format(LISTEN[1]), "w").close() open("logs/{}-server-hex.log".format(LISTEN[1]), "w").close() open("logs/{}-all-raw.log".format(LISTEN[1]), "w").close() open("logs/{}-all-hex.log".format(LISTEN[1]), "w").close() proxy = Proxy(LISTEN, TARGET) proxy.start() proxy.join() print("Error?")