-
Notifications
You must be signed in to change notification settings - Fork 19
/
ServerHandler.py
613 lines (551 loc) · 21.6 KB
/
ServerHandler.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#ServerHanlder.py
import sys
import select
import socket
import random
import urlparse
import ServerInfo
import ServerConfig
import ServerSocks
import SocketServer
import BaseHTTPServer
ra = lambda text: text.decode('ascii', 'ignore')
sets = ServerConfig.Sets()
logs = False
def ServerUpdate():
global sets
sets = ServerConfig.Sets()
def LogWindow(flag = False):
global logs
logs = flag
class QueryHandler():
def __init__(self, command = '', path = '/', headers = {}, https = False, phost = '', pport = 0):
self.command = command
self.path = path
self.headers = headers
self.https = https
self.phost = phost
self.pport = pport
def get_path(self, path):
if '/' in path:
host, path = path.split('/', 1)
path = '/%s' % path
else:
host = path
path = '/'
fport = False
if self.https:
port = 443
else:
port = 80
if ':' in host:
_host, _port = host.rsplit(':', 1)
try:
port = int(_port)
host = _host
fport = True
except:
pass
return (fport,
host,
port,
path)
def get_query(self):
if self.https:
url = 'https://%s/' % self.path
else:
url = self.path
url_scm, _, _, _, _, _ = urlparse.urlparse(url)
if len(sets.FQUERY.split('/')) > 2:
cgi_http = 'http/'
if cgi_http in sets.FQUERY.lower():
url_cgi = url.split(cgi_http)
if len(url_cgi) > 1:
url = '%s://%s' % (url_scm, url_cgi.pop())
else:
url = url.replace(sets.FQUERY, '')
if len(sets.MQUERY.split('/')) > 2:
url = url.replace(sets.MQUERY, '')
if len(sets.BQUERY.split('/')) > 2:
url = url.replace(sets.BQUERY, '')
url_len = len(url_scm) + 3
url_path = url[url_len:]
if sets.CQUERY:
cquery_list = sets.CQUERY.split('|')
for cquery in cquery_list:
try:
old, new = cquery.split('>')
url_path = url_path.replace(old, new)
except:
pass
fport, host, port, path = self.get_path('%s%s' % (sets.FQUERY, url_path))
advhost = host
if fport and not sets.RPORT:
path = '%s:%s%s%s%s' % (host,
port,
sets.MQUERY,
path,
sets.BQUERY)
else:
path = '%s%s%s%s' % (host,
sets.MQUERY,
path,
sets.BQUERY)
fport, host, port, path = self.get_path(path)
if self.https:
fport = True
path = '%s:%s' % (host, port)
elif self.phost and self.pport or sets.ADMODE:
if sets.RQUERY:
if sets.MQUERY.startswith('/'):
path = '%s%s%s' % (url[:url_len], sets.RQUERY, path)
else:
path = '%s%s%s%s' % (url[:url_len],
sets.RQUERY,
sets.MQUERY,
path)
elif fport and not sets.RPORT:
path = '%s%s:%s%s' % (url[:url_len],
host,
port,
path)
else:
path = '%s%s%s' % (url[:url_len], host, path)
else:
_, path = path.split('/', 1)
path = '/%s' % path
cur_header = 'proxy-connection'
if cur_header in self.headers and not self.phost and not self.pport:
del self.headers[cur_header]
cur_header = 'connection'
if not self.https and not sets.PTYPE:
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = 'close'
cur_header = 'host'
if cur_header in self.headers:
del self.headers[cur_header]
if fport and not sets.RPORT and not self.https:
self.headers[cur_header] = '%s:%s' % (host, port)
else:
self.headers[cur_header] = host
if sets.RQUERY:
cur_header = 'host'
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = sets.RQUERY
cur_header = 'x-online-host'
if cur_header in self.headers:
del self.headers[cur_header]
if fport and not self.https:
self.headers[cur_header] = '%s:%s' % (host, port)
else:
self.headers[cur_header] = '%s' % host
if sets.ADMODE:
cur_header = 'host'
if cur_header in self.headers:
if sets.RQUERY:
del self.headers[cur_header]
self.headers[cur_header] = '%s' % sets.RQUERY
cur_header = 'x-online-host'
if cur_header in self.headers:
del self.headers[cur_header]
if fport and not self.https:
self.headers[cur_header] = '%s:%s' % (advhost, port)
else:
self.headers[cur_header] = '%s' % advhost
elif self.phost and self.pport:
del self.headers[cur_header]
advhost = advhost.replace(sets.FQUERY, '').replace(sets.MQUERY, '').replace(sets.BQUERY, '')
if fport and not self.https:
self.headers[cur_header] = '%s:%s' % (advhost, port)
else:
self.headers[cur_header] = '%s' % advhost
if sets.CUSHDR0 and not sets.VALHDR0:
cur_header = sets.CUSHDR0.lower()
if cur_header in self.headers:
del self.headers[cur_header]
if sets.CUSHDR0 and sets.VALHDR0:
cur_header = sets.CUSHDR0.lower()
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = sets.VALHDR0
if sets.CUSHDR1 and not sets.VALHDR1:
cur_header = sets.CUSHDR1.lower()
if cur_header in self.headers:
del self.headers[cur_header]
if sets.CUSHDR1 and sets.VALHDR1:
cur_header = sets.CUSHDR1.lower()
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = sets.VALHDR1
if sets.CUSHDR2 and not sets.VALHDR2:
cur_header = sets.CUSHDR2.lower()
if cur_header in self.headers:
del self.headers[cur_header]
if sets.CUSHDR2 and sets.VALHDR2:
cur_header = sets.CUSHDR2.lower()
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = sets.VALHDR2
if sets.CUSHDR3 and not sets.VALHDR3:
cur_header = sets.CUSHDR3.lower()
if cur_header in self.headers:
del self.headers[cur_header]
if sets.CUSHDR3 and sets.VALHDR3:
cur_header = sets.CUSHDR3.lower()
if cur_header in self.headers:
del self.headers[cur_header]
self.headers[cur_header] = sets.VALHDR3
if sets.RPORT:
cur_port = ':%s' % port
path = path.replace(cur_port, '')
cur_list = ('host', 'x-online-host')
for cur_header in cur_list:
if cur_header in self.headers and ':' in self.headers[cur_header]:
rhost, _ = self.headers[cur_header].split(':')
del self.headers[cur_header]
self.headers[cur_header] = rhost
header = self.headers
uahdr = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
cur_header = 'user-agent'
if cur_header in self.headers:
uahdr = self.headers[cur_header]
self.del_garbage()
return (path,
header,
uahdr,
host,
port,
advhost)
def del_garbage(self):
del self.command
del self.path
del self.headers
del self.https
del self.phost
del self.pport
class ProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def log_message(self, format, *args):
pass
def __getattr__(self, item):
if item.startswith('do_'):
return self.do_COMMAND
def do_COMMAND(self):
self.get_urlcheck()
self.get_headercheck()
self.get_recv_headers()
self.get_proxy()
query = QueryHandler(self.command, self.path, self.headers, self.https, self.phost, self.pport)
self.path, self.headers, self.uahdr, self.host, self.port, self.advhost = query.get_query()
self.get_newline()
self.get_requestline()
self.get_injectline()
self.get_send_inject()
self.get_send_headers()
soc = self.proxy_sock()
try:
if self.connect_to(soc, self.host, self.port, self.advhost):
data = ra('%s%s' % (self.get_injectline(), self.newline)).encode('hex')
for header, value in self.headers.items():
data += ra('%s: %s%s' % (str(header).title(), value, self.newline)).encode('hex')
post_header = 'content-length'
if post_header in self.headers:
data += ra(self.newline).encode('hex')
data += self.rfile.read(int(self.headers[post_header])).encode('hex')
data += ra(self.newline).encode('hex')
data += ra('%s%s' % (self.newline, self.get_send_end())).encode('hex')
data = data.decode('hex')
while data:
byte = soc.send(data)
data = data[byte:]
self.get_response_data(soc)
self.send_connection_close(soc)
self.del_garbage()
except socket.error as msg:
self.send_connection_error(msg)
self.send_connection_close(soc)
return
except:
return
def do_CONNECT(self):
if sets.RHTTPS:
self.get_urlcheck()
self.get_headercheck()
self.get_recv_headers()
self.get_proxy()
query = QueryHandler(self.command, self.path, self.headers, self.https, self.phost, self.pport)
self.path, self.headers, self.uahdr, self.host, self.port, self.advhost = query.get_query()
self.get_newline()
self.get_requestline()
self.get_injectline()
self.get_send_inject()
self.get_send_headers()
soc = self.proxy_sock()
try:
if self.connect_to(soc, self.host, self.port, self.advhost):
data = '%s 200 Connection Established\r\nProxy-Agent: %s/%s' % (self.request_version, ServerInfo.Info('name').get_info().replace(' ', ''), ServerInfo.Info('ver').get_info()[:3])
self.send_response_data('%s\r\n' % data)
self.send_response_data('\r\n')
self.get_response_header(data)
self.get_response_data(soc)
self.send_connection_close(soc)
self.del_garbage()
except socket.error as msg:
self.send_connection_error(msg)
self.send_connection_close(soc)
return
except:
return
else:
self.send_connection_error((501, 'method not allowed'))
self.connection.close()
return
def get_urlcheck(self):
self.https = False
if self.command == 'CONNECT':
self.https = True
def get_headercheck(self):
header_check = {}
for header, value in self.headers.items():
if header.find('\t') == -1 and header.find('\t') == -1:
header_check[str(header).lower()] = value
self.headers = header_check
def get_proxy(self):
self.phost = ''
self.pport = 0
self.puser = None
self.ppass = None
if ':' in sets.PHOST and not sets.PPORT:
plist = sets.PHOST.split('>')
count = len(plist)
while 1:
count -= 1
if count >= 0:
plist = plist[random.randint(0, len(plist) - 1)]
if '@' in plist and plist:
try:
self.puser, self.ppass = plist.split('@')[1].split(':')
plist = plist.split('@')[0]
except:
pass
if ':' in plist and plist:
try:
self.phost, self.pport = plist.split(':')
self.pport = int(self.pport)
except:
pass
break
else:
break
elif sets.PHOST and sets.PPORT:
self.phost, self.pport = sets.PHOST, sets.PPORT
return
def proxy_sock(self):
if sets.IQUERY and self.https or self.https:
data = ra('%s%s' % (self.get_injectline(), self.newline))
for header, value in self.headers.items():
data += ra('%s: %s%s' % (str(header).title(), value, self.newline))
soc = ServerSocks.socksocket(headers=data, newline=self.newline)
else:
soc = ServerSocks.socksocket(newline=self.newline)
if self.phost and self.pport:
soc.setproxy(sets.PTYPE, self.phost, self.pport, rdns=True, username=self.puser, password=self.puser, useragent=self.uahdr)
return soc
def connect_to(self, soc, host, port, advhost):
try:
if sets.ADMODE:
host, port = advhost, port
soc.setblocking(1)
soc.connect((host, port))
return 1
except socket.error as msg:
self.send_connection_error(msg)
self.send_connection_close(soc)
return 0
except:
return 0
def get_newline(self):
self.newline = ['\r\n', '\n'][sets.ILINE]
def get_requestline(self):
if sets.RHTTP == 1:
self.request_version = 'HTTP/1.0'
elif sets.RHTTP == 2:
self.request_version = 'HTTP/1.1'
self.requestline = '%s %s %s' % (self.command, self.path, self.request_version)
def get_injectline(self):
if sets.IQUERY:
meth = ['HEAD',
'GET',
'POST',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE',
'PUT'][sets.IMETHOD]
if '/' in sets.IQUERY:
host, path = sets.IQUERY.split('/', 1)
path = '/%s' % path
else:
host = sets.IQUERY
path = '/'
if self.phost and self.pport or sets.ADMODE:
path = 'http://%s%s' % (host, path)
self.splitline = self.newline * 3
if sets.ISPLIT:
self.splitline = self.newline * sets.ISPLIT
self.injectline = '%s %s HTTP/1.1%sHost: %s%s' % (meth,
path,
self.newline,
host, self.splitline)
return '%s%s' % (self.injectline, self.requestline)
else:
return self.requestline
def get_send_end(self):
if sets.IQUERY:
return self.newline
else:
return ''
def get_recv_headers(self):
self.send_connection_logger('+++Receive Request+++\r\nFrom Address - %s:%s\r\n%s\r\n' % (self.client_address[0], self.client_address[1], self.requestline))
for header, value in self.headers.items():
self.send_connection_logger('%s: %s\r\n' % (str(header).title(), value))
self.send_connection_logger('\r\n')
def get_send_inject(self):
if sets.IQUERY:
self.send_connection_logger('+++Send Inject+++\r\n')
if self.phost and self.pport:
self.send_connection_logger('Using Proxy - %s:%s\r\n' % (self.phost, self.pport))
elif sets.ADMODE:
self.send_connection_logger('Using Host - %s:%s\r\n' % (self.advhost, self.port))
else:
self.send_connection_logger('Using Server - %s:%s\r\n' % (self.host, self.port))
for inject in self.splitline[0].split(self.newline):
self.send_connection_logger('%s' % inject)
self.send_connection_logger('\r\n')
def get_send_headers(self):
self.send_connection_logger('+++Send Request+++\r\n')
if self.phost and self.pport:
self.send_connection_logger('Using Proxy - %s:%s\r\n' % (self.phost, self.pport))
elif sets.ADMODE:
self.send_connection_logger('Using Host - %s:%s\r\n' % (self.advhost, self.port))
else:
self.send_connection_logger('Using Server - %s:%s\r\n' % (self.host, self.port))
self.send_connection_logger('%s\r\n' % self.requestline)
for header, value in self.headers.items():
self.send_connection_logger('%s: %s\r\n' % (str(header).title(), value))
self.send_connection_logger('\r\n')
def find_double_newline(self, data):
pos1 = data.find('\n\r\n')
if pos1 >= 0:
pos1 += 3
pos2 = data.find('\n\n')
if pos2 >= 0:
pos2 += 2
if pos1 >= 0:
if pos2 >= 0:
return min(pos1, pos2)
else:
return pos1
else:
return pos2
def get_data_splitter(self, data):
if data.split('\r\n\r\n')[0].split(' ')[0] in ('HTTP/0.9', 'HTTP/1.0', 'HTTP/1.1'):
return 1
else:
return 0
def get_response_header(self, data):
if not self.https:
index = self.find_double_newline(data)
if index >= 0:
data = str(data[:index].split('\r\n\r\n')[0])
if self.get_data_splitter(data):
self.send_connection_logger('+++Receive Response+++\r\n%s\r\n' % data)
self.send_connection_logger('\r\n')
elif self.get_data_splitter(data):
self.send_connection_logger('+++Receive Response+++\r\n%s\r\n' % data)
self.send_connection_logger('\r\n')
def get_response_data(self, soc):
iw = [self.connection, soc]
ow = []
count = 0
timeout = 0
while 1:
timeout += 1
ins, _, exs = select.select(iw, ow, iw, 3)
if exs:
break
if ins:
for resp in ins:
try:
data = resp.recv(sets.SBUFF)
if data:
if resp is soc:
if sets.IQUERY:
if self.get_data_splitter(data):
count += 1
if not self.https:
if count % 2 == 0:
count = 0
self.get_response_header(data)
self.send_response_data(data)
else:
for idata in data.split('\r\n\r\n'):
if count == 1 and not idata.startswith('HTTP/'):
self.send_response_data(idata)
else:
self.get_response_header(data)
self.send_response_data(data)
else:
while data:
byte = soc.send(data)
data = data[byte:]
timeout = 0
else:
break
except:
break
if timeout == sets.TIMEOUT:
break
def send_response_data(self, data):
self.wfile.write(data)
def send_connection_close(self, soc):
soc.close()
self.connection.close()
def send_connection_error(self, msg, page = True):
try:
code, message = msg
except:
self.send_connection_error((501, 'unknown error'))
message = str(message).capitalize()
self.send_connection_logger('+++Connection Error+++\r\n')
self.send_connection_logger('%s: %s\r\n\r\n' % (str(code), message))
if page:
self.send_error(502, '%s.' % message)
def send_connection_logger(self, data):
if logs:
sys.stderr.write(data)
def del_garbage(self):
del self.https
del self.path
del self.headers
del self.uahdr
del self.host
del self.port
del self.advhost
del self.newline
del self.requestline
del self.injectline
del self.phost
del self.pport
del self.puser
del self.ppass
class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def handle_error(self, request, client_address):
pass
class HTTPProxyService():
def __init__(self):
self.httpd = ThreadingHTTPServer((sets.LHOST, sets.LPORT), ProxyHandler)
self.httpd.allow_reuse_address = True
def serve_forever(self):
self.httpd.serve_forever()