Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import os
- from threading import Thread
- import parser_part9 as parser#ขาดอันนี้อะรอให้ถึงบ้านก่อนนะจะ
- import importlib
- import socket
- iphamachi="เปลียนเป็นip hamachi"
- localserverip="เปลียนเป็น ipserverนะจะ"
- # Part 9
- # Developing a TCP Network Proxy - Pwn Adventure 3
- # https://www.youtube.com/watch?v=iApNzWZG-10
- class Proxy2Server(Thread):
- def __init__(self, host, port):
- super(Proxy2Server, self).__init__()
- self.game = None # game client socket not known yet
- self.port = port
- self.host = host
- self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.server.connect((host, port))
- # run in thread
- def run(self):
- while True:
- data = self.server.recv(8)
- if data:
- #print "[{}] <- {}".format(self.port, data[:100].encode('hex'))
- try:
- importlib.reload(parser)
- parser.parse(data, self.port, 'server')
- except Exception as e:
- print('server[{}]'.format(self.port)), e
- # forward to client
- self.game.sendall(data)
- class Game2Proxy(Thread):
- def __init__(self, host, port):
- super(Game2Proxy, self).__init__()
- self.server = None # real server socket not known yet
- self.port = port
- self.host = host
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind((host, port))
- sock.listen(1)
- # waiting for a connection
- self.game, addr = sock.accept()
- class Proxy(Thread):
- def __init__(self, from_host, to_host, from_port, to_port):
- super(Proxy, self).__init__()
- self.from_host = from_host
- self.to_host = to_host
- self.from_port = from_port
- self.to_port = to_port
- self.running = False
- def run(self):
- while True:
- print("[proxy({})]) setting up".format(self.from_port))
- self.g2p = Game2Proxy(self.from_host, self.from_port) # waiting for a client
- self.p2s = Proxy2Server(self.to_host, self.to_port)
- print("[proxy({})]) connection established".format(self.to_port))
- self.g2p.server = self.p2s.server
- self.p2s.game = self.g2p.game
- self.running = True
- self.g2p.start()
- self.p2s.start()
- servers=[]
- _upnp_server = Proxy(iphamachi, localserverip, 8000,8000)
- _upnp_server.start()
- servers.append(_upnp_server)
- while True:
- try:
- cmd = input('$ ')
- if cmd[:4] == 'quit':
- os._exit(0)
- if cmd[:7] == "injectc":
- for server in servers:
- if server.running:
- server.g2p.client.sendall(cmd[8:])
- pass
- if cmd[:7] == "injects":
- for server in servers:
- if server.running:
- server.p2s.server.sendall(cmd[8:])
- pass
- except Exception as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement