2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2015-07-03 00:47:12 +00:00
|
|
|
from netlib import socks, tcp
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2014-06-25 21:25:49 +00:00
|
|
|
class ProxyError(Exception):
|
|
|
|
def __init__(self, code, message, headers=None):
|
2014-08-31 14:52:26 +00:00
|
|
|
super(ProxyError, self).__init__(message)
|
2014-06-25 21:25:49 +00:00
|
|
|
self.code, self.headers = code, headers
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyServerError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
class ProxyMode(object):
|
|
|
|
http_form_in = None
|
|
|
|
http_form_out = None
|
|
|
|
|
2014-10-08 23:58:54 +00:00
|
|
|
def get_upstream_server(self, client_conn):
|
2014-03-10 04:11:51 +00:00
|
|
|
"""
|
|
|
|
Returns the address of the server to connect to.
|
2014-09-08 12:32:42 +00:00
|
|
|
Returns None if the address needs to be determined on the protocol level (regular proxy mode)
|
2014-03-10 04:11:51 +00:00
|
|
|
"""
|
2014-09-08 12:32:42 +00:00
|
|
|
raise NotImplementedError() # pragma: nocover
|
2014-03-10 04:11:51 +00:00
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.__class__.__name__.replace("ProxyMode", "").lower()
|
2014-03-10 04:11:51 +00:00
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2014-03-10 04:11:51 +00:00
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
"""
|
|
|
|
Allow comparisions with "regular" etc.
|
|
|
|
"""
|
|
|
|
if isinstance(other, ProxyMode):
|
|
|
|
return self is other
|
|
|
|
else:
|
|
|
|
return self.name == other
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
|
|
|
class RegularProxyMode(ProxyMode):
|
|
|
|
http_form_in = "absolute"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
2014-10-08 23:58:54 +00:00
|
|
|
def get_upstream_server(self, client_conn):
|
2014-09-08 12:32:42 +00:00
|
|
|
return None
|
2014-03-10 04:11:51 +00:00
|
|
|
|
|
|
|
|
2015-06-20 12:43:50 +00:00
|
|
|
class SpoofMode(ProxyMode):
|
2015-06-18 14:53:27 +00:00
|
|
|
http_form_in = "relative"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
|
|
|
def get_upstream_server(self, client_conn):
|
|
|
|
return None
|
|
|
|
|
2015-06-20 12:43:50 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return "spoof"
|
|
|
|
|
2015-06-18 14:53:27 +00:00
|
|
|
|
2015-06-20 15:51:56 +00:00
|
|
|
class SSLSpoofMode(ProxyMode):
|
|
|
|
http_form_in = "relative"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
2015-07-03 00:47:12 +00:00
|
|
|
def __init__(self, sslport):
|
2015-06-20 15:51:56 +00:00
|
|
|
self.sslport = sslport
|
|
|
|
|
|
|
|
def get_upstream_server(self, client_conn):
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return "sslspoof"
|
|
|
|
|
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
class TransparentProxyMode(ProxyMode):
|
|
|
|
http_form_in = "relative"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
2014-03-10 04:11:51 +00:00
|
|
|
def __init__(self, resolver, sslports):
|
|
|
|
self.resolver = resolver
|
|
|
|
self.sslports = sslports
|
|
|
|
|
2014-10-08 23:58:54 +00:00
|
|
|
def get_upstream_server(self, client_conn):
|
2014-08-10 14:10:49 +00:00
|
|
|
try:
|
2014-10-08 23:58:54 +00:00
|
|
|
dst = self.resolver.original_addr(client_conn.connection)
|
2015-05-30 00:03:28 +00:00
|
|
|
except Exception as e:
|
2014-08-10 14:10:49 +00:00
|
|
|
raise ProxyError(502, "Transparent mode failure: %s" % str(e))
|
2014-03-10 04:11:51 +00:00
|
|
|
|
|
|
|
if dst[1] in self.sslports:
|
|
|
|
ssl = True
|
|
|
|
else:
|
|
|
|
ssl = False
|
|
|
|
return [ssl, ssl] + list(dst)
|
|
|
|
|
|
|
|
|
2014-10-08 23:58:54 +00:00
|
|
|
class Socks5ProxyMode(ProxyMode):
|
|
|
|
http_form_in = "relative"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
|
|
|
def __init__(self, sslports):
|
|
|
|
self.sslports = sslports
|
|
|
|
|
|
|
|
def get_upstream_server(self, client_conn):
|
|
|
|
try:
|
|
|
|
# Parse Client Greeting
|
2015-07-03 00:47:12 +00:00
|
|
|
client_greet = socks.ClientGreeting.from_file(client_conn.rfile, fail_early=True)
|
|
|
|
client_greet.assert_socks5()
|
2014-10-08 23:58:54 +00:00
|
|
|
if socks.METHOD.NO_AUTHENTICATION_REQUIRED not in client_greet.methods:
|
|
|
|
raise socks.SocksError(
|
|
|
|
socks.METHOD.NO_ACCEPTABLE_METHODS,
|
|
|
|
"mitmproxy only supports SOCKS without authentication"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Send Server Greeting
|
|
|
|
server_greet = socks.ServerGreeting(
|
|
|
|
socks.VERSION.SOCKS5,
|
|
|
|
socks.METHOD.NO_AUTHENTICATION_REQUIRED
|
|
|
|
)
|
|
|
|
server_greet.to_file(client_conn.wfile)
|
|
|
|
client_conn.wfile.flush()
|
|
|
|
|
|
|
|
# Parse Connect Request
|
|
|
|
connect_request = socks.Message.from_file(client_conn.rfile)
|
2015-07-03 00:47:12 +00:00
|
|
|
connect_request.assert_socks5()
|
2014-10-08 23:58:54 +00:00
|
|
|
if connect_request.msg != socks.CMD.CONNECT:
|
|
|
|
raise socks.SocksError(
|
|
|
|
socks.REP.COMMAND_NOT_SUPPORTED,
|
|
|
|
"mitmproxy only supports SOCKS5 CONNECT."
|
|
|
|
)
|
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
# We do not connect here yet, as the clientconnect event has not
|
|
|
|
# been handled yet.
|
2014-10-08 23:58:54 +00:00
|
|
|
|
|
|
|
connect_reply = socks.Message(
|
|
|
|
socks.VERSION.SOCKS5,
|
|
|
|
socks.REP.SUCCEEDED,
|
2015-07-03 00:47:12 +00:00
|
|
|
connect_request.atyp,
|
2015-05-30 00:03:28 +00:00
|
|
|
# dummy value, we don't have an upstream connection yet.
|
2015-07-03 00:47:12 +00:00
|
|
|
connect_request.addr
|
2014-10-08 23:58:54 +00:00
|
|
|
)
|
|
|
|
connect_reply.to_file(client_conn.wfile)
|
|
|
|
client_conn.wfile.flush()
|
|
|
|
|
|
|
|
ssl = bool(connect_request.addr.port in self.sslports)
|
|
|
|
return ssl, ssl, connect_request.addr.host, connect_request.addr.port
|
|
|
|
|
2015-07-03 00:47:12 +00:00
|
|
|
except (socks.SocksError, tcp.NetLibError) as e:
|
2015-07-24 15:48:27 +00:00
|
|
|
raise ProxyError(502, "SOCKS5 mode failure: %s" % repr(e))
|
2014-10-08 23:58:54 +00:00
|
|
|
|
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
class _ConstDestinationProxyMode(ProxyMode):
|
|
|
|
def __init__(self, dst):
|
|
|
|
self.dst = dst
|
|
|
|
|
2014-10-08 23:58:54 +00:00
|
|
|
def get_upstream_server(self, client_conn):
|
2014-09-08 12:32:42 +00:00
|
|
|
return self.dst
|
|
|
|
|
|
|
|
|
|
|
|
class ReverseProxyMode(_ConstDestinationProxyMode):
|
|
|
|
http_form_in = "relative"
|
|
|
|
http_form_out = "relative"
|
|
|
|
|
|
|
|
|
|
|
|
class UpstreamProxyMode(_ConstDestinationProxyMode):
|
|
|
|
http_form_in = "absolute"
|
|
|
|
http_form_out = "absolute"
|
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class Log:
|
2014-03-13 00:22:12 +00:00
|
|
|
def __init__(self, msg, level="info"):
|
2014-03-13 00:04:45 +00:00
|
|
|
self.msg = msg
|
2015-05-30 00:03:28 +00:00
|
|
|
self.level = level
|