-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathasyncproxy.py
150 lines (104 loc) · 3.7 KB
/
asyncproxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
Name: asyncproxy.py
Version: v1.0.0 (23/06/2017)
Date: 23/06/2017
Created By: Antonin Beaujeant
Description: AsyncProxy is a custom asynchronous proxy skeleton based on https://gist.github.com/majek/1662475. More info: https://blog.keyidentity.com/2017/06/27/pwnadventure3-asynchronous-proxy-in-python/
Example:
$ python asyncproxy.py 192.168.1.10
"""
import argparse
import asyncore
import socket
import struct
import time
import re
def parse(p_out):
"""Contains all modifications to apply on the communication between the client and the server
"""
return (p_in, p_out, p_next)
# ------------------------------------------------
# ------------------------------------------------
class FixedDispatcher(asyncore.dispatcher):
"""https://docs.python.org/2/library/asyncore.html#asyncore.dispatcher"""
def handle_error(self):
print "What happened?"
raise
class Sock(FixedDispatcher):
write_buffer = ''
def readable(self):
return not self.other.write_buffer
def handle_read(self):
self.other.write_buffer += self.recv(4096*4)
def handle_write(self):
if self.write_buffer:
(p_in, p_out, p_next) = parse(self.write_buffer)
self.other.write_buffer += p_in
sent = self.send(p_out)
self.write_buffer = self.write_buffer[sent:]
self.write_buffer = p_next
def handle_close(self):
print ' [-] %i -> %i (closed)' % \
(self.getsockname()[1], self.getpeername()[1])
self.close()
if self.other.other:
self.other.close()
self.other = None
class ProxServer(FixedDispatcher):
def __init__(self, src_port, dst_host, dst_port):
self.src_port = src_port
self.dst_host = dst_host
self.dst_port = dst_port
self.client = None
self.server = None
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(('0.0.0.0', src_port))
print ' [*] Listening on port %i' % src_port
self.listen(5)
def handle_accept(self):
pair = self.accept()
if not pair:
return
left, addr = pair
try:
right = socket.create_connection((self.dst_host, self.dst_port))
except socket.error, e:
if e.errno is not errno.ECONNREFUSED: raise
print ' [!] Connection refused (%s:%i)' % \
(self.dst_host, self.dst_port)
left.close()
else:
print ' [+] %i:%i > %i:%i' % \
(addr[1], self.src_port,
right.getsockname()[1], self.dst_port)
self.client = Sock(left)
self.server = Sock(right)
self.client.other = self.server
self.server.other = self.client
def close(self):
print ' [*] Closed %i ==> %i' % \
(self.src_port, self.dst_port)
self.client.close()
self.server.close()
asyncore.dispatcher.close(self)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Asynchronous proxy')
parser.add_argument('-h', "--host", required=True, help='Target server IP/hostname')
args = parser.parse_args()
while True:
# Change port here
master = ProxServer(3333, args.host, 3333)
game = ProxServer(3000, args.host, 3000)
game2 = ProxServer(3001, args.host, 3001)
print "Proxy ready..."
try:
asyncore.loop()
except Exception as e:
print e
master.close()
game.close()
game2.close()
time.sleep(0.5)