This repository has been archived on 2020-03-29. You can view files and clone it, but cannot push or open issues or pull requests.
sampy_archive/proxy.py

109 lines
3.7 KiB
Python
Raw Normal View History

2018-09-24 11:37:21 +02:00
import sys
2018-09-28 22:13:01 +02:00
import os
2018-09-24 11:37:21 +02:00
import socket
from threading import Thread
import select
def format_hex(hex_str):
return " ".join([hex_str[i:i+2] for i in range(0, len(hex_str), 2)])
2018-09-24 11:37:21 +02:00
class Proxy(Thread):
BUFF_SIZE = 65535
2018-09-28 22:40:10 +02:00
# Used to keep track of "lines"
prnt_id_client = 0
prnt_id_server = 0
2018-09-24 11:37:21 +02:00
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)
2018-09-28 21:58:12 +02:00
#print("[C] data:{} addr:{}".format(c_data, c_addr))
2018-09-24 11:37:21 +02:00
if c_data:
2018-09-28 21:58:12 +02:00
"""
2018-09-24 11:37:21 +02:00
if c_data[:2] == b"\x08\x1e":
print("Editing port check: {}".format(c_data.hex()))
c_data = b"\x08\x1e\x77\xda"
2018-09-28 21:58:12 +02:00
"""
print("[C] -> {}".format(c_data))
with open("logs/{}-client-raw.log".format(LISTEN[1]), "ab") as f:
f.write("({}) ".format(self.prnt_id_client).encode() + c_data + b"\r\n")
2018-09-28 22:13:01 +02:00
with open("logs/{}-client-hex.log".format(LISTEN[1]), "a") as f:
f.write("({}) {} \r\n".format(self.prnt_id_client, format_hex(c_data.hex())))
with open("logs/{}-all-raw.log".format(LISTEN[1]), "ab") as f:
f.write("[C] -> ({}) ".format(self.prnt_id_client).encode() + c_data + b"\r\n")
2018-09-28 22:13:01 +02:00
with open("logs/{}-all-hex.log".format(LISTEN[1]), "a") as f:
f.write("[C] -> ({}) {} \r\n".format(self.prnt_id_client, format_hex(c_data.hex())))
2018-09-24 11:37:21 +02:00
target.sendto(c_data, self.target)
2018-09-28 22:40:10 +02:00
self.prnt_id_client += 1
2018-09-24 11:37:21 +02:00
(s, _, _) = select.select([target],[],[],0)
if s:
s_data = target.recv(self.BUFF_SIZE)
2018-09-28 21:58:12 +02:00
#print("[S] data:{}".format(s_data))
2018-09-24 11:37:21 +02:00
if s_data:
2018-09-28 21:58:12 +02:00
print("[C] <- {}".format(s_data))
with open("logs/{}-server-raw.log".format(LISTEN[1]), "ab") as f:
f.write("({}) ".format(self.prnt_id_server).encode() + s_data + b"\r\n")
2018-09-28 22:13:01 +02:00
with open("logs/{}-server-hex.log".format(LISTEN[1]), "a") as f:
f.write("({}) {} \r\n".format(self.prnt_id_server, format_hex(s_data.hex())))
with open("logs/{}-all-raw.log".format(LISTEN[1]), "ab") as f:
f.write("[C] <- ({}) ".format(self.prnt_id_server).encode() + s_data + b"\r\n")
2018-09-28 22:13:01 +02:00
with open("logs/{}-all-hex.log".format(LISTEN[1]), "a") as f:
f.write("[C] <- ({}) {} \r\n".format(self.prnt_id_server, format_hex(s_data.hex())))
2018-09-24 11:37:21 +02:00
client.sendto(s_data, c_addr)
2018-09-28 22:40:10 +02:00
self.prnt_id_server += 1
2018-09-24 11:37:21 +02:00
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:
2018-09-28 21:58:12 +02:00
2018-09-28 22:13:01 +02:00
if not os.path.exists("logs"):
os.makedirs("logs")
2018-09-28 21:58:12 +02:00
# Delete old log files
2018-09-28 22:13:01 +02:00
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()
2018-09-28 21:58:12 +02:00
2018-09-24 11:37:21 +02:00
proxy = Proxy(LISTEN, TARGET)
proxy.start()
proxy.join()
print("Error?")