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
| import sys import select import socket import struct from socketserver import StreamRequestHandler, ThreadingTCPServer
from sokt_control.oplist import Opblklist
SOCKS_VERSION = 5 HOST = "127.0.0.1" PORT = 8081
class SocksProxy(StreamRequestHandler): def handle(self):
""" 一、客户端认证请求 +----+----------+----------+ |VER | NMETHODS | METHODS | +----+----------+----------+ | 1 | 1 | 1~255 | +----+----------+----------+ """ print('Accepting connection from {}'.format(self.client_address)) header = self.connection.recv(2) version, Nmethods = struct.unpack("!BB", header) assert version == SOCKS_VERSION, 'SOCKS版本错误' assert Nmethods > 0 methods = self.get_available_methods(Nmethods) if 0 not in set(methods): self.server.close_request(self.request) return """ 二、服务端回应认证 +----+--------+ |VER | METHOD | +----+--------+ | 1 | 1 | +----+--------+ """ self.connection.sendall(struct.pack("!BB", SOCKS_VERSION, 0))
""" 三、客户端连接请求(连接目的网络) +----+-----+-------+------+----------+----------+ |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT | +----+-----+-------+------+----------+----------+ | 1 | 1 | 1 | 1 | Variable | 2 | +----+-----+-------+------+----------+----------+ """ version, cmd, _, address_type = struct.unpack("!BBBB", self.connection.recv(4)) assert version == SOCKS_VERSION, 'socks版本错误' if address_type == 1: address = socket.inet_ntoa(self.connection.recv(4)) elif address_type == 3: domain_length = self.connection.recv(1)[0] address = self.connection.recv(domain_length) elif address_type == 4: addr_ip = self.connection.recv(16) address = socket.inet_ntop(socket.AF_INET6, addr_ip) else: self.server.close_request(self.request) return port = struct.unpack('!H', self.connection.recv(2))[0]
print("src_host: ", self.client_address[0]) dsthost = address.decode("UTF-8") if(port!=80): dsthost = dsthost+":"+str(port) split_url = dsthost.split("/", 2) if len(split_url) > 2: dsthost = split_url[2] print("dst_host: ", dsthost) opblklist = Opblklist('./sokt_control/blklist.txt') blklist = opblklist.show() if dsthost in blklist: print("dst_host:{} is in blklist, conn is not supported!".format(dsthost)) else:
""" 四、服务端回应连接 +----+-----+-------+------+----------+----------+ |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | +----+-----+-------+------+----------+----------+ | 1 | 1 | 1 | 1 | Variable | 2 | +----+-----+-------+------+----------+----------+ """ try: if cmd == 1: remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) remote.connect((address, port)) bind_address = remote.getsockname() print('Connected to {} {}'.format(address, port)) 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, 1, addr, port) except Exception as 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) return def get_available_methods(self, n): """ 检查是否支持该验证方式 """ methods = [] for i in range(n): methods.append(ord(self.connection.recv(1))) return methods def generate_failed_reply(self, address_type, error_number): """ 生成连接失败的回复包 """ return struct.pack("!BBBBIH", SOCKS_VERSION, error_number, 0, address_type, 0, 0) def VerifyAuth(self): """ 校验用户名和密码 """ version = ord(self.connection.recv(1)) assert version == SOCKS_VERSION, 'SOCKS版本错误' username_len = ord(self.connection.recv(1)) username = self.connection.recv(username_len).decode('utf-8') password_len = ord(self.connection.recv(1)) password = self.connection.recv(password_len).decode('utf-8') self.username = "leo" self.password = "1234" if username == self.username and password == self.password: response = struct.pack("!BB", version, 0) self.connection.sendall(response) return True response = struct.pack("!BB", version, 0xFF) self.connection.sendall(response) self.server.close_request(self.request) return False
def exchange_loop(self, client, remote): """ 交换数据 """ while True: r, w, e = select.select([client, remote], [], []) if client in r: data = client.recv(4096) if remote.send(data) <= 0: break if remote in r: data = remote.recv(4096) if client.send(data) <= 0: break
def my_socks5_proxy_server(): with ThreadingTCPServer((HOST, PORT), SocksProxy) as server: print("SOCKS5 proxy server is running on {} : {} ...".format(HOST, PORT)) server.serve_forever()
if __name__ == '__main__': if sys.argv[1:]: PORT = int(sys.argv[1]) my_socks5_proxy_server()
|