mitmproxy/netlib/socks.py

127 lines
3.5 KiB
Python
Raw Normal View History

2014-06-25 12:30:42 +00:00
import socket
import struct
from array import array
2014-06-25 18:31:28 +00:00
from . import tcp, utils
2014-06-25 12:30:42 +00:00
class SocksError(Exception):
def __init__(self, code, message):
super(SocksError, self).__init__(message)
self.code = code
2014-06-25 18:31:28 +00:00
2014-06-25 12:30:42 +00:00
class VERSION:
SOCKS4 = 0x04
SOCKS5 = 0x05
class CMD:
CONNECT = 0x01
BIND = 0x02
UDP_ASSOCIATE = 0x03
class ATYP:
IPV4_ADDRESS = 0x01
DOMAINNAME = 0x03
IPV6_ADDRESS = 0x04
2014-06-25 18:31:28 +00:00
2014-06-25 12:30:42 +00:00
class REP:
SUCCEEDED = 0x00
GENERAL_SOCKS_SERVER_FAILURE = 0x01
CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02
NETWORK_UNREACHABLE = 0x03
HOST_UNREACHABLE = 0x04
CONNECTION_REFUSED = 0x05
TTL_EXPIRED = 0x06
COMMAND_NOT_SUPPORTED = 0x07
ADDRESS_TYPE_NOT_SUPPORTED = 0x08
2014-06-25 18:31:28 +00:00
2014-06-25 12:30:42 +00:00
class METHOD:
NO_AUTHENTICATION_REQUIRED = 0x00
GSSAPI = 0x01
USERNAME_PASSWORD = 0x02
NO_ACCEPTABLE_METHODS = 0xFF
class ClientGreeting(object):
__slots__ = ("ver", "methods")
def __init__(self, ver, methods):
self.ver = ver
self.methods = methods
@classmethod
def from_file(cls, f):
2014-06-25 18:31:28 +00:00
ver, nmethods = struct.unpack("!BB", f.read(2))
2014-06-25 12:30:42 +00:00
methods = array("B")
2014-06-25 18:31:28 +00:00
methods.fromstring(f.read(nmethods))
2014-06-25 12:30:42 +00:00
return cls(ver, methods)
def to_file(self, f):
2014-06-25 18:31:28 +00:00
f.write(struct.pack("!BB", self.ver, len(self.methods)))
f.write(self.methods.tostring())
2014-06-25 12:30:42 +00:00
class ServerGreeting(object):
__slots__ = ("ver", "method")
def __init__(self, ver, method):
self.ver = ver
self.method = method
@classmethod
def from_file(cls, f):
2014-06-25 18:31:28 +00:00
ver, method = struct.unpack("!BB", f.read(2))
2014-06-25 12:30:42 +00:00
return cls(ver, method)
def to_file(self, f):
2014-06-25 18:31:28 +00:00
f.write(struct.pack("!BB", self.ver, self.method))
2014-06-25 12:30:42 +00:00
2014-06-25 18:31:28 +00:00
class Message(object):
__slots__ = ("ver", "msg", "atyp", "addr")
2014-06-25 12:30:42 +00:00
2014-06-25 18:31:28 +00:00
def __init__(self, ver, msg, atyp, addr):
2014-06-25 12:30:42 +00:00
self.ver = ver
2014-06-25 18:31:28 +00:00
self.msg = msg
2014-06-25 12:30:42 +00:00
self.atyp = atyp
2014-06-25 18:31:28 +00:00
self.addr = addr
2014-06-25 12:30:42 +00:00
@classmethod
def from_file(cls, f):
2014-06-25 18:31:28 +00:00
ver, msg, rsv, atyp = struct.unpack("!BBBB", f.read(4))
2014-06-25 12:30:42 +00:00
if rsv != 0x00:
raise SocksError(REP.GENERAL_SOCKS_SERVER_FAILURE,
"Socks Request: Invalid reserved byte: %s" % rsv)
if atyp == ATYP.IPV4_ADDRESS:
2014-06-25 18:31:28 +00:00
host = utils.inet_ntop(socket.AF_INET, f.read(4)) # We use tnoa here as ntop is not commonly available on Windows.
2014-06-25 12:30:42 +00:00
use_ipv6 = False
elif atyp == ATYP.IPV6_ADDRESS:
2014-06-25 18:31:28 +00:00
host = utils.inet_ntop(socket.AF_INET6, f.read(16))
2014-06-25 12:30:42 +00:00
use_ipv6 = True
elif atyp == ATYP.DOMAINNAME:
2014-06-25 18:31:28 +00:00
length, = struct.unpack("!B", f.read(1))
2014-06-25 12:30:42 +00:00
host = f.read(length)
use_ipv6 = False
else:
raise SocksError(REP.ADDRESS_TYPE_NOT_SUPPORTED,
"Socks Request: Unknown ATYP: %s" % atyp)
2014-06-25 18:31:28 +00:00
port, = struct.unpack("!H", f.read(2))
addr = tcp.Address((host, port), use_ipv6=use_ipv6)
return cls(ver, msg, atyp, addr)
2014-06-25 12:30:42 +00:00
def to_file(self, f):
2014-06-25 18:31:28 +00:00
f.write(struct.pack("!BBBB", self.ver, self.msg, 0x00, self.atyp))
2014-06-25 12:30:42 +00:00
if self.atyp == ATYP.IPV4_ADDRESS:
2014-06-25 18:31:28 +00:00
f.write(utils.inet_pton(socket.AF_INET, self.addr.host))
2014-06-25 12:30:42 +00:00
elif self.atyp == ATYP.IPV6_ADDRESS:
2014-06-25 18:31:28 +00:00
f.write(utils.inet_pton(socket.AF_INET6, self.addr.host))
2014-06-25 12:30:42 +00:00
elif self.atyp == ATYP.DOMAINNAME:
2014-06-25 18:31:28 +00:00
f.write(struct.pack("!B", len(self.addr.host)))
f.write(self.addr.host)
2014-06-25 12:30:42 +00:00
else:
raise SocksError(REP.ADDRESS_TYPE_NOT_SUPPORTED, "Unknown ATYP: %s" % self.atyp)
2014-06-25 18:31:28 +00:00
f.write(struct.pack("!H", self.addr.port))