-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathproxy.py
327 lines (264 loc) · 10.8 KB
/
proxy.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
"""
@author: FBK CyberSecurity [ by Sergey Migalin & Andrey Skuratov]
@contact: https://fbkcs.ru
@license Apache License, Version 2.0
Copyright (C) 2018
"""
import socket
import select
import time
import sys
import argparse
import json
import re
import signal
import struct
from socketserver import ThreadingMixIn, TCPServer, StreamRequestHandler
IP_DOMAIN_REGEX = re.compile(r"^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$|^localhost$|^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
PORT_REGEX = re.compile(r"^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
buffer_size = 4096
delay = 0.000001
clients = {}
client_id = None
SOCKS_VERSION = 5
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
class SocksProxy(StreamRequestHandler):
"""
Socks Proxy class
"""
last_address = 0
last_port = 0
def handle(self):
header = self.connection.recv(2)
version, count_of_methods = struct.unpack("!BB", header)
assert version == SOCKS_VERSION
assert count_of_methods > 0
methods = self.get_available_methods(count_of_methods)
if 0 not in set(methods):
self.server.close_request(self.request)
return
self.connection.sendall(struct.pack("!BB", SOCKS_VERSION, 0))
version, cmd, _, address_type = struct.unpack("!BBBB", self.connection.recv(4))
assert version == SOCKS_VERSION
if address_type == 1:
address = socket.inet_ntoa(self.connection.recv(4))
elif address_type == 3:
domain_length = ord(self.connection.recv(1)[0])
address = self.connection.recv(domain_length)
target_port = struct.unpack('!H', self.connection.recv(2))[0]
try:
if cmd == 1:
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((args.dns , int(args.dns_port)))
print('Forwarding data to {}:{}'.format(args.dns, args.dns_port))
bind_address = remote.getsockname()
print('Accepting connection from %s:%s' % self.client_address)
print('Connecting to %s:%s' % (address, target_port))
remote.send(b'\x02RESET:' + bytes(client_id.zfill(2), 'utf-8') + b':\n')
time.sleep(1)
remote.send(b'\x01' + bytes('{}:{}:{}:\n'.format(client_id.zfill(2), str(address), str(target_port)), 'utf-8'))
else:
self.server.close_request(self.request)
addr = struct.unpack("!I", socket.inet_aton(bind_address[0]))[0]
port = bind_address[1]
reply = struct.pack("!BBBBIH", SOCKS_VERSION, 0, 0, address_type,
addr, port)
except Exception as err:
print(err)
reply = self.generate_failed_reply(address_type, 5)
self.connection.sendall(reply)
if reply[1] == 0 and cmd == 1:
self.exchange_loop(self.connection, remote)
self.server.close_request(self.request)
def get_available_methods(self, n):
methods = []
for i in range(n):
methods.append(ord(self.connection.recv(1)))
return methods
@staticmethod
def generate_failed_reply(address_type, error_number):
return struct.pack("!BBBBIH", SOCKS_VERSION, error_number, 0, address_type, 0, 0)
@staticmethod
def exchange_loop(client, remote):
while True:
r, w, e = select.select([client, remote], [], [])
if client in r:
data = client.recv(4096)
if (data):
if remote.send(b'\x03'+data) <= 0:
break
if remote in r:
data = remote.recv(4096)
if client.send(data) <= 0:
break
class Forward:
"""
Forwarder class
"""
def __init__(self):
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(self, host, port):
try:
self.forward.connect((host, port))
return self.forward
except Exception as e:
print(e)
return False
class Proxy:
"""
Proxy class
"""
input_list = []
socket_list = []
channel = {}
def __init__(self, host, port):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((host, port))
self.server.listen(200)
def main_loop(self):
self.input_list.append(self.server)
while 1:
time.sleep(delay)
ss = select.select
inputready, outputready, exceptready = ss(self.input_list, [], [])
for self.s in inputready:
if self.s == self.server:
self.on_accept()
break
self.data = self.s.recv(buffer_size)
if len(self.data) == 0:
self.on_close()
break
else:
self.on_recv()
def on_accept(self):
forward = Forward().start(forward_to[0], forward_to[1])
print('\nForwarding data to {}:{}'.format(forward_to[0], forward_to[1]))
global client_id
global ip
global port
forward.send(b'\x02RESET:' + bytes(client_id.zfill(2), 'utf-8') + b':\n')
time.sleep(1)
clientsock, clientaddr = self.server.accept()
if forward:
print ("Accepting connection from ", clientaddr)
forward.send(b'\x01' + bytes('{}:{}:{}:\n'.format(client_id.zfill(2), str(ip), str(port)), 'utf-8'))
self.socket_list.append(clientsock)
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forward] = clientsock
else:
print("Can't establish connection with remote server.")
print("Closing connection with client side", clientaddr)
clientsock.close()
def on_close(self):
global client_id
global ip
global port
global clients
print(self.s.getpeername(), "has disconnected")
clients = {}
self.socket_list.remove(self.s)
self.input_list.remove(self.s)
self.input_list.remove(self.channel[self.s])
out = self.channel[self.s]
self.channel[out].close()
self.channel[self.s].close()
del self.channel[out]
del self.channel[self.s]
def on_recv(self):
if self.s in self.socket_list:
time.sleep(timeout)
data = b'\x03' + self.data
else:
data = self.data
self.channel[self.s].send(data)
def handler(signum, frame):
sys.exit(1)
if __name__ == '__main__':
print('________________ __________ _________ ')
print('___/__ __/__/ /_____ ______________/ /______________/ __ \________________ ______ __')
print('_____/ /____/ __ \ / / /_ __ \ __ /_ _ \_ ___/_/ /_/ /_/ ___// __ \_ |/_/_ / / /')
print('____/ /____/ / / / /_/ /_ / / / /_/ / / __/ / __/ ____/_/ / / /_/ /_> < _ /_/ / ')
print('___/_/____/_/ /_/\__,_/ /_/ /_/\__,_/__\___//_/ __/_/ /_/ \____//_/|_| _\__, / ')
print(' /____/ ')
parser = argparse.ArgumentParser(add_help=True, usage='%(prog)s [options]', description="Proxy for Thunder DNS")
parser.add_argument("--clients", help="Get list of avaliable clients", nargs='?', const=True)
parser.add_argument("--dns", required=True, help="Address of your DNS server", default=9091)
parser.add_argument("--dns_port", default="9091", help="Port to connect to your DNS server")
parser.add_argument("--target", help="Address where you want to connect", default='')
parser.add_argument("--target_port", help="Port to connecnt to target IP", default='')
parser.add_argument("--client", help="Client id", default='')
parser.add_argument("--send_timeout", help="Timeout in seconds before sending message", default=0.2)
parser.add_argument("--localport", help="Local port to connect application", default=9090)
parser.add_argument("--socks5", help="Use sock5 mode", nargs='?', const=True)
args, leftovers = parser.parse_known_args()
if not IP_DOMAIN_REGEX.match(args.dns):
print('Invalid DNS')
exit(0)
if not PORT_REGEX.match(args.dns_port):
print('Invalid DNS port')
exit(0)
forward_to = (args.dns, int(args.dns_port))
try:
timeout = float(args.send_timeout)
except ValueError:
print('Timeout value must be int or float')
exit(0)
if args.clients is not None:
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((args.dns, int(args.dns_port)))
soc.send(b'\x00GETCLIENTS\n')
clients = json.loads(soc.recv(buffer_size).decode('utf-8'))
print('Clients available:')
for el in range (0, len(clients['clients'])):
print(str(el + 1) + '. ' + clients['clients'][el])
exit(0)
if not IP_DOMAIN_REGEX.match(args.target) and not args.socks5:
print('Invalid TARGET')
exit(0)
if not args.socks5:
ip = args.target
if not PORT_REGEX.match(args.target_port) and not args.socks5:
print('Invalid TARGET port')
exit(0)
if not args.socks5:
port = int(args.target_port)
if not re.match(r'^[a-zA-Z0-9]{1,100}$', args.client):
print('Invalid id')
exit(0)
try:
int(args.client)
client_id = args.client
except ValueError:
print("Client id must be integer")
exit(0)
try:
if not (0 < int(args.localport) < 65535):
print('Invalid port number!')
exit(0)
except (TypeError, ValueError):
print('Parameter localport must be possitive integer')
exit(0)
signal.signal(signal.SIGTERM, handler)
print('All right! After establishing connection, we will work with client ', client_id)
if args.socks5:
print('SOCKS5 mode enabled!')
try:
with ThreadingTCPServer(('127.0.0.1', int(args.localport)), SocksProxy) as server:
server.serve_forever()
except KeyboardInterrupt:
print("Ctrl C - Stopping server")
server.shutdown()
else:
server = Proxy('', int(args.localport) )
try:
server.main_loop()
except KeyboardInterrupt:
print("Ctrl C - Stopping server")
sys.exit(1)