2015-07-11 07:51:30 +02:00
|
|
|
#!/usr/bin/env python
|
2013-09-22 05:37:33 +02:00
|
|
|
|
|
|
|
"""Minimal non-feature complete socks proxy"""
|
2018-06-12 11:43:13 +02:00
|
|
|
from __future__ import print_function
|
2013-09-22 05:37:33 +02:00
|
|
|
|
|
|
|
import socket
|
|
|
|
from struct import pack, unpack
|
|
|
|
import threading
|
|
|
|
import sys
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
# Python 3 renamed SocketServer to socketserver
|
|
|
|
try:
|
|
|
|
from socketserver import StreamRequestHandler, ThreadingTCPServer
|
2018-10-20 00:05:18 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
from SocketServer import StreamRequestHandler, ThreadingTCPServer
|
|
|
|
|
|
|
|
|
2014-01-14 10:07:39 +01:00
|
|
|
def debug(s):
|
2019-08-02 16:07:52 +02:00
|
|
|
print('socks.py: ', s)
|
|
|
|
sys.stdout.flush()
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2014-01-14 10:07:39 +01:00
|
|
|
|
2014-01-21 10:46:03 +01:00
|
|
|
def error(s):
|
2019-08-02 16:07:52 +02:00
|
|
|
print('socks.py, ERROR: ', s)
|
|
|
|
sys.stdout.flush()
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2014-01-21 10:46:03 +01:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
class MyTCPServer(ThreadingTCPServer):
|
|
|
|
allow_reuse_address = True
|
2013-10-03 07:44:38 +02:00
|
|
|
|
|
|
|
def handle_timeout(self):
|
2019-08-02 16:07:52 +02:00
|
|
|
raise Exception('socks.py: timeout')
|
2013-10-03 07:44:38 +02:00
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
CLOSE = object()
|
|
|
|
|
|
|
|
VERSION = '\x05'
|
|
|
|
NOAUTH = '\x00'
|
|
|
|
USERPASS = '\x02'
|
|
|
|
CONNECT = '\x01'
|
2015-06-02 03:14:52 +02:00
|
|
|
UDP_ASSOCIATE = '\x03'
|
2013-09-22 05:37:33 +02:00
|
|
|
IPV4 = '\x01'
|
|
|
|
IPV6 = '\x04'
|
|
|
|
DOMAIN_NAME = '\x03'
|
|
|
|
SUCCESS = '\x00'
|
|
|
|
|
|
|
|
password = None
|
|
|
|
username = None
|
|
|
|
allow_v4 = False
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
def send(dest, msg):
|
|
|
|
if msg == CLOSE:
|
2018-06-12 11:43:13 +02:00
|
|
|
try:
|
|
|
|
dest.shutdown(socket.SHUT_WR)
|
2018-10-20 00:05:18 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
pass
|
2013-09-22 05:37:33 +02:00
|
|
|
dest.close()
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return dest.sendall(msg)
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
def recv(source, buffer):
|
|
|
|
data = source.recv(buffer)
|
|
|
|
if data == '':
|
|
|
|
return CLOSE
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
def forward(source, dest, name):
|
|
|
|
while True:
|
|
|
|
data = recv(source, 4000)
|
|
|
|
if data == CLOSE:
|
|
|
|
send(dest, CLOSE)
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('%s hung up' % name)
|
2013-09-22 05:37:33 +02:00
|
|
|
return
|
2015-06-21 18:41:50 +02:00
|
|
|
# debug('Forwarding (%d) %r' % (len(data), data))
|
2013-09-22 05:37:33 +02:00
|
|
|
send(dest, data)
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
def spawn_forwarder(source, dest, name):
|
|
|
|
t = threading.Thread(target=forward, args=(source, dest, name))
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
class SocksHandler(StreamRequestHandler):
|
|
|
|
"""Highly feature incomplete SOCKS 5 implementation"""
|
|
|
|
|
|
|
|
def close_request(self):
|
|
|
|
self.server.close_request(self.request)
|
|
|
|
|
|
|
|
def read(self, n):
|
|
|
|
data = ''
|
|
|
|
while len(data) < n:
|
|
|
|
extra = self.rfile.read(n)
|
|
|
|
if extra == '':
|
|
|
|
raise Exception('Connection closed')
|
|
|
|
data += extra
|
|
|
|
return data
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
# IMRPOVEMENT: Report who requests are from in logging
|
|
|
|
# IMPROVEMENT: Timeout on client
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Connection - authenticating')
|
2013-09-22 05:37:33 +02:00
|
|
|
version = self.read(1)
|
|
|
|
|
|
|
|
if allow_v4 and version == '\x04':
|
|
|
|
cmd = self.read(1)
|
2015-06-02 03:14:52 +02:00
|
|
|
if cmd != CONNECT and cmd != UDP_ASSOCIATE:
|
|
|
|
error('Only supports connect and udp-associate method not (%r) closing' % cmd)
|
2013-09-22 05:37:33 +02:00
|
|
|
self.close_request()
|
|
|
|
return
|
|
|
|
|
|
|
|
raw_dest_port = self.read(2)
|
|
|
|
dest_port, = unpack('>H', raw_dest_port)
|
|
|
|
|
|
|
|
raw_dest_address = self.read(4)
|
|
|
|
dest_address = '.'.join(map(str, unpack('>4B', raw_dest_address)))
|
|
|
|
|
|
|
|
user_id = ''
|
|
|
|
c = self.read(1)
|
|
|
|
while c != '\0':
|
|
|
|
user_id += c
|
|
|
|
c = self.read(1)
|
|
|
|
|
|
|
|
outbound_sock = socket.socket(socket.AF_INET)
|
2018-06-12 11:43:13 +02:00
|
|
|
out_address = socket.getaddrinfo(dest_address, dest_port)[0][4]
|
2014-01-16 09:40:28 +01:00
|
|
|
debug("Creating forwarder connection to %s:%d" % (out_address[0], out_address[1]))
|
2013-09-22 05:37:33 +02:00
|
|
|
outbound_sock.connect(out_address)
|
|
|
|
|
|
|
|
self.send_reply_v4(outbound_sock.getsockname())
|
|
|
|
|
|
|
|
spawn_forwarder(outbound_sock, self.request, 'destination')
|
|
|
|
forward(self.request, outbound_sock, 'client')
|
|
|
|
return
|
|
|
|
|
|
|
|
if version != '\x05':
|
|
|
|
error('Wrong version number (%r) closing...' % version)
|
|
|
|
self.close_request()
|
|
|
|
return
|
|
|
|
|
|
|
|
nmethods = ord(self.read(1))
|
|
|
|
method_list = self.read(nmethods)
|
|
|
|
|
|
|
|
global password
|
|
|
|
global username
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
if password is None and NOAUTH in method_list:
|
2013-09-22 05:37:33 +02:00
|
|
|
self.send_no_auth_method()
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Authenticated (no-auth)')
|
2013-09-22 05:37:33 +02:00
|
|
|
elif USERPASS in method_list:
|
|
|
|
self.send_user_pass_auth_method()
|
|
|
|
auth_version = self.read(1)
|
|
|
|
if auth_version != '\x01':
|
|
|
|
error('Wrong sub-negotiation version number (%r) closing...' % version)
|
|
|
|
self.close_request()
|
|
|
|
return
|
|
|
|
usr_len = ord(self.read(1))
|
|
|
|
usr_name = self.read(usr_len)
|
|
|
|
pwd_len = ord(self.read(1))
|
|
|
|
pwd = self.read(pwd_len)
|
|
|
|
|
|
|
|
if usr_name != username or pwd != password:
|
|
|
|
error('Invalid username or password')
|
|
|
|
self.close_request()
|
|
|
|
return
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Authenticated (user/password)')
|
2013-09-22 05:37:33 +02:00
|
|
|
self.send_authenticated()
|
|
|
|
else:
|
|
|
|
error('Server only supports NOAUTH and user/pass')
|
|
|
|
self.send_no_method()
|
|
|
|
return
|
|
|
|
|
|
|
|
# If we were authenticating it would go here
|
|
|
|
version, cmd, zero, address_type = self.read(4)
|
|
|
|
if version != '\x05':
|
|
|
|
error('Wrong version number (%r) closing...' % version)
|
|
|
|
self.close_request()
|
2015-06-02 03:14:52 +02:00
|
|
|
elif cmd != CONNECT and cmd != UDP_ASSOCIATE:
|
|
|
|
error('Only supports connect and udp-associate method not (%r) closing' % cmd)
|
2013-09-22 05:37:33 +02:00
|
|
|
self.close_request()
|
|
|
|
elif zero != '\x00':
|
|
|
|
error('Mangled request. Reserved field (%r) is not null' % zero)
|
|
|
|
self.close_request()
|
|
|
|
|
|
|
|
if address_type == IPV4:
|
|
|
|
raw_dest_address = self.read(4)
|
|
|
|
dest_address = '.'.join(map(str, unpack('>4B', raw_dest_address)))
|
|
|
|
elif address_type == IPV6:
|
|
|
|
raw_dest_address = self.read(16)
|
2018-06-12 11:43:13 +02:00
|
|
|
dest_address = ":".join([hex(x)[2:] for x in unpack('>8H', raw_dest_address)])
|
2013-09-22 05:37:33 +02:00
|
|
|
elif address_type == DOMAIN_NAME:
|
|
|
|
dns_length = ord(self.read(1))
|
|
|
|
dns_name = self.read(dns_length)
|
|
|
|
dest_address = dns_name
|
|
|
|
else:
|
|
|
|
error('Unknown addressing (%r)' % address_type)
|
|
|
|
self.close_request()
|
|
|
|
|
|
|
|
raw_dest_port = self.read(2)
|
|
|
|
dest_port, = unpack('>H', raw_dest_port)
|
|
|
|
|
|
|
|
if address_type == IPV6:
|
|
|
|
outbound_sock = socket.socket(socket.AF_INET6)
|
|
|
|
else:
|
|
|
|
outbound_sock = socket.socket(socket.AF_INET)
|
2013-11-03 09:15:51 +01:00
|
|
|
try:
|
2018-06-12 11:43:13 +02:00
|
|
|
out_address = socket.getaddrinfo(dest_address, dest_port)[0][4]
|
|
|
|
except Exception as e:
|
2019-08-02 16:07:52 +02:00
|
|
|
error('%s' % e)
|
2013-11-03 09:15:51 +01:00
|
|
|
return
|
2015-06-02 03:14:52 +02:00
|
|
|
|
|
|
|
if cmd == UDP_ASSOCIATE:
|
2018-06-12 11:43:13 +02:00
|
|
|
debug("no UDP support yet, closing")
|
|
|
|
return
|
2015-06-02 03:14:52 +02:00
|
|
|
|
2014-01-16 09:40:28 +01:00
|
|
|
debug("Creating forwarder connection to %s:%d" % (out_address[0], out_address[1]))
|
2014-01-14 10:07:39 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
outbound_sock.connect(out_address)
|
2018-06-12 11:43:13 +02:00
|
|
|
except Exception as e:
|
2019-08-02 16:07:52 +02:00
|
|
|
error('%s' % e)
|
2014-01-14 10:07:39 +01:00
|
|
|
return
|
2013-09-22 05:37:33 +02:00
|
|
|
|
|
|
|
if address_type == IPV6:
|
|
|
|
self.send_reply6(outbound_sock.getsockname())
|
|
|
|
else:
|
|
|
|
self.send_reply(outbound_sock.getsockname())
|
|
|
|
|
|
|
|
spawn_forwarder(outbound_sock, self.request, 'destination')
|
2014-02-05 10:38:32 +01:00
|
|
|
try:
|
|
|
|
forward(self.request, outbound_sock, 'client')
|
2018-06-12 11:43:13 +02:00
|
|
|
except Exception as e:
|
2019-08-02 16:07:52 +02:00
|
|
|
error('%s' % e)
|
2013-09-22 05:37:33 +02:00
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
def send_reply_v4(self, xxx_todo_changeme):
|
|
|
|
(bind_addr, bind_port) = xxx_todo_changeme
|
2013-09-22 05:37:33 +02:00
|
|
|
self.wfile.write('\0\x5a\0\0\0\0\0\0')
|
|
|
|
self.wfile.flush()
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
def send_reply(self, xxx_todo_changeme1):
|
|
|
|
(bind_addr, bind_port) = xxx_todo_changeme1
|
2013-09-22 05:37:33 +02:00
|
|
|
bind_tuple = tuple(map(int, bind_addr.split('.')))
|
|
|
|
full_address = bind_tuple + (bind_port,)
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Setting up forwarding port %r' % (full_address,))
|
2013-09-22 05:37:33 +02:00
|
|
|
msg = pack('>cccc4BH', VERSION, SUCCESS, '\x00', IPV4, *full_address)
|
|
|
|
self.wfile.write(msg)
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
def send_reply6(self, xxx_todo_changeme2):
|
|
|
|
(bind_addr, bind_port, unused1, unused2) = xxx_todo_changeme2
|
|
|
|
bind_tuple = tuple([int(x, 16) for x in bind_addr.split(':')])
|
2013-09-22 05:37:33 +02:00
|
|
|
full_address = bind_tuple + (bind_port,)
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Setting up forwarding port %r' % (full_address,))
|
2013-09-22 05:37:33 +02:00
|
|
|
msg = pack('>cccc8HH', VERSION, SUCCESS, '\x00', IPV6, *full_address)
|
|
|
|
self.wfile.write(msg)
|
|
|
|
|
|
|
|
def send_no_method(self):
|
|
|
|
self.wfile.write('\x05\xff')
|
|
|
|
self.close_request()
|
|
|
|
|
|
|
|
def send_no_auth_method(self):
|
|
|
|
self.wfile.write('\x05\x00')
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
def send_user_pass_auth_method(self):
|
|
|
|
self.wfile.write('\x05\x02')
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
def send_authenticated(self):
|
|
|
|
self.wfile.write('\x01\x00')
|
|
|
|
self.wfile.flush()
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2013-09-22 05:37:33 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2019-08-02 16:07:52 +02:00
|
|
|
debug('starting socks.py %s' % " ".join(sys.argv))
|
2013-09-22 05:37:33 +02:00
|
|
|
listen_port = 8002
|
|
|
|
i = 1
|
|
|
|
while i < len(sys.argv):
|
|
|
|
if sys.argv[i] == '--username':
|
2018-06-12 11:43:13 +02:00
|
|
|
username = sys.argv[i + 1]
|
2013-09-22 05:37:33 +02:00
|
|
|
i += 1
|
|
|
|
elif sys.argv[i] == '--password':
|
2018-06-12 11:43:13 +02:00
|
|
|
password = sys.argv[i + 1]
|
2013-09-22 05:37:33 +02:00
|
|
|
i += 1
|
|
|
|
elif sys.argv[i] == '--port':
|
2018-06-12 11:43:13 +02:00
|
|
|
listen_port = int(sys.argv[i + 1])
|
2013-09-22 05:37:33 +02:00
|
|
|
i += 1
|
|
|
|
elif sys.argv[i] == '--allow-v4':
|
|
|
|
allow_v4 = True
|
|
|
|
else:
|
2018-06-12 11:43:13 +02:00
|
|
|
if sys.argv[i] != '--help':
|
|
|
|
debug('unknown option "%s"' % sys.argv[i])
|
2013-09-22 05:37:33 +02:00
|
|
|
print('usage: socks.py [--username <user> --password <password>] [--port <listen-port>]')
|
2019-08-02 16:07:52 +02:00
|
|
|
sys.stdout.flush()
|
2013-09-22 05:37:33 +02:00
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
|
|
|
|
2014-01-14 10:07:39 +01:00
|
|
|
debug('Listening on port %d...' % listen_port)
|
2013-09-22 05:37:33 +02:00
|
|
|
server = MyTCPServer(('localhost', listen_port), SocksHandler)
|
2014-02-05 10:38:32 +01:00
|
|
|
server.timeout = 190
|
2013-10-03 07:44:38 +02:00
|
|
|
while True:
|
|
|
|
server.handle_request()
|