-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsock5server.py
More file actions
100 lines (86 loc) · 3.3 KB
/
sock5server.py
File metadata and controls
100 lines (86 loc) · 3.3 KB
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
#coding=utf-8
import socket
import select
import socketserver
import logging
import json
logging.basicConfig(filename='logger.log', level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
#打开配置文件
with open('config.json', 'rb') as f:
config = json.load(f)
port = int(config['port'])
class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
class Socks5Server(socketserver.StreamRequestHandler):
def handle_tcp(self, client, remote):
try:
fds = [client,remote]
while True:
r,w,e = select.select(fds,[],[],5)
if client in r:
cli_data = client.recv(1024 * 100)
if len(cli_data) <= 0:
break
result = send_all(remote, cli_data)
if result < len(cli_data):
logging.warn("Failed pipping all data to target!!!")
break
if remote in r:
remote_data = remote.recv(1024 * 100)
if len(remote_data) <= 0:
break
result = send_all(client, remote_data)
if result < len(remote_data):
logging("Failed pipping all data to client!!!")
break
except Exception as e:
logging.error(e)
finally:
client.close()
remote.close()
def handle(self):
client = self.request
ver,methods = client.recv(1),client.recv(1)
methods = client.recv(ord(methods))
client.send(b'\x05\x00')
ver,cmd,rsv,atype = client.recv(1),client.recv(1),client.recv(1),client.recv(1)
if ord(cmd) is not 1:
client.close()
return
# 判断是否支持atype,目前不支持IPv6
# 比特流转化成整型 big表示编码为大端法,
if ord(atype) == 1:
# IPv4
remote_addr = socket.inet_ntoa(client.recv(4))
remote_port = int.from_bytes(client.recv(2), 'big')
elif ord(atype) == 3:
# 域名
addr_len = int.from_bytes(client.recv(1), byteorder = 'big')
remote_addr = client.recv(addr_len)
remote_port = int.from_bytes(client.recv(2), byteorder = 'big')
else:
#不支持则关闭连接
client.close()
return
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.info('[+] %s:%dConnect to --> %s:%d' % (self.client_address[0], self.client_address[1], remote_addr, remote_port))
remote.connect((remote_addr, remote_port))
reply = b"\x05\x00\x00\x01" + socket.inet_aton("0.0.0.0") + (2222).to_bytes(2, byteorder = 'big')
client.send(reply)
self.handle_tcp(client,remote)
def send_all(sock, data):
bytes_sent = 0
while True:
r = sock.send(data[bytes_sent:])
if r < 0:
return r
bytes_sent += r
if bytes_sent == len(data):
return bytes_sent
try:
server = ThreadingTCPServer(('', port), Socks5Server)
logging.info('[+] Lintening on port:%d' % port)
server.serve_forever()
except Exception as e:
logging.error(e)