2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2014-03-09 20:51:24 +00:00
|
|
|
import os
|
2014-08-09 01:03:21 +00:00
|
|
|
import re
|
2014-12-15 11:46:13 +00:00
|
|
|
from OpenSSL import SSL
|
2014-10-18 16:29:35 +00:00
|
|
|
from netlib import http_auth, certutils, tcp
|
2014-09-08 21:34:43 +00:00
|
|
|
from .. import utils, platform, version
|
2014-10-08 23:58:54 +00:00
|
|
|
from .primitives import RegularProxyMode, TransparentProxyMode, UpstreamProxyMode, ReverseProxyMode, Socks5ProxyMode
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
TRANSPARENT_SSL_PORTS = [443, 8443]
|
|
|
|
CONF_BASENAME = "mitmproxy"
|
2014-11-15 03:14:08 +00:00
|
|
|
CA_DIR = "~/.mitmproxy"
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
class HostMatcher(object):
|
|
|
|
def __init__(self, patterns=[]):
|
|
|
|
self.patterns = list(patterns)
|
|
|
|
self.regexes = [re.compile(p, re.IGNORECASE) for p in self.patterns]
|
|
|
|
|
|
|
|
def __call__(self, address):
|
|
|
|
address = tcp.Address.wrap(address)
|
|
|
|
host = "%s:%s" % (address.host, address.port)
|
|
|
|
if any(rex.search(host) for rex in self.regexes):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
return bool(self.patterns)
|
2014-09-08 10:20:40 +00:00
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class ProxyConfig:
|
2014-11-15 03:14:08 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host='',
|
|
|
|
port=8080,
|
|
|
|
server_version=version.NAMEVERSION,
|
|
|
|
cadir=CA_DIR,
|
|
|
|
clientcerts=None,
|
|
|
|
no_upstream_cert=False,
|
|
|
|
body_size_limit=None,
|
|
|
|
mode=None,
|
|
|
|
upstream_server=None,
|
|
|
|
http_form_in=None,
|
|
|
|
http_form_out=None,
|
|
|
|
authenticator=None,
|
|
|
|
ignore_hosts=[],
|
|
|
|
tcp_hosts=[],
|
2015-03-02 02:12:27 +00:00
|
|
|
client_ciphers=None,
|
|
|
|
server_ciphers=None,
|
2014-11-15 03:14:08 +00:00
|
|
|
certs=[],
|
|
|
|
certforward=False,
|
2014-12-15 11:46:13 +00:00
|
|
|
ssl_version_client="secure",
|
|
|
|
ssl_version_server="secure",
|
2014-11-15 03:14:08 +00:00
|
|
|
ssl_ports=TRANSPARENT_SSL_PORTS
|
|
|
|
):
|
2014-09-08 21:34:43 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.server_version = server_version
|
2015-02-28 16:16:31 +00:00
|
|
|
self.client_ciphers = client_ciphers
|
|
|
|
self.server_ciphers = server_ciphers
|
2014-03-09 20:51:24 +00:00
|
|
|
self.clientcerts = clientcerts
|
|
|
|
self.no_upstream_cert = no_upstream_cert
|
|
|
|
self.body_size_limit = body_size_limit
|
2014-08-08 02:43:44 +00:00
|
|
|
|
|
|
|
if mode == "transparent":
|
2014-10-30 16:38:23 +00:00
|
|
|
self.mode = TransparentProxyMode(platform.resolver(), ssl_ports)
|
2014-10-08 23:58:54 +00:00
|
|
|
elif mode == "socks5":
|
2014-10-30 16:38:23 +00:00
|
|
|
self.mode = Socks5ProxyMode(ssl_ports)
|
2014-08-08 02:43:44 +00:00
|
|
|
elif mode == "reverse":
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = ReverseProxyMode(upstream_server)
|
2014-08-08 02:43:44 +00:00
|
|
|
elif mode == "upstream":
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = UpstreamProxyMode(upstream_server)
|
2014-08-08 02:43:44 +00:00
|
|
|
else:
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = RegularProxyMode()
|
|
|
|
|
2014-09-08 21:34:43 +00:00
|
|
|
# Handle manual overrides of the http forms
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode.http_form_in = http_form_in or self.mode.http_form_in
|
|
|
|
self.mode.http_form_out = http_form_out or self.mode.http_form_out
|
2014-08-08 02:43:44 +00:00
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
self.check_ignore = HostMatcher(ignore_hosts)
|
|
|
|
self.check_tcp = HostMatcher(tcp_hosts)
|
2014-03-09 20:51:24 +00:00
|
|
|
self.authenticator = authenticator
|
2014-11-15 03:14:08 +00:00
|
|
|
self.cadir = os.path.expanduser(cadir)
|
|
|
|
self.certstore = certutils.CertStore.from_store(self.cadir, CONF_BASENAME)
|
2014-04-29 12:54:11 +00:00
|
|
|
for spec, cert in certs:
|
|
|
|
self.certstore.add_cert_file(spec, cert)
|
2014-03-11 00:02:10 +00:00
|
|
|
self.certforward = certforward
|
2014-12-15 11:46:13 +00:00
|
|
|
self.openssl_client_method, self.openssl_client_options = version_to_openssl(ssl_version_client)
|
|
|
|
self.openssl_server_method, self.openssl_server_options = version_to_openssl(ssl_version_server)
|
2014-08-30 18:15:19 +00:00
|
|
|
self.ssl_ports = ssl_ports
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
|
2014-12-15 11:46:13 +00:00
|
|
|
sslversion_choices = ("all", "secure", "SSLv2", "SSLv3", "TLSv1", "TLSv1_1", "TLSv1_2")
|
|
|
|
|
|
|
|
|
|
|
|
def version_to_openssl(version):
|
|
|
|
"""
|
|
|
|
Convert a reasonable SSL version specification into the format OpenSSL expects.
|
|
|
|
Don't ask...
|
|
|
|
https://bugs.launchpad.net/pyopenssl/+bug/1020632/comments/3
|
|
|
|
"""
|
|
|
|
if version == "all":
|
|
|
|
return SSL.SSLv23_METHOD, None
|
|
|
|
elif version == "secure":
|
|
|
|
# SSLv23_METHOD + NO_SSLv2 + NO_SSLv3 == TLS 1.0+
|
|
|
|
# TLSv1_METHOD would be TLS 1.0 only
|
|
|
|
return SSL.SSLv23_METHOD, (SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3)
|
|
|
|
elif version in sslversion_choices:
|
|
|
|
return getattr(SSL, "%s_METHOD" % version), None
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid SSL version: %s" % version)
|
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
def process_proxy_options(parser, options):
|
|
|
|
body_size_limit = utils.parse_size(options.body_size_limit)
|
|
|
|
|
2014-03-10 04:11:51 +00:00
|
|
|
c = 0
|
2014-08-08 02:43:44 +00:00
|
|
|
mode, upstream_server = None, None
|
2014-03-09 20:51:24 +00:00
|
|
|
if options.transparent_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-03-09 20:51:24 +00:00
|
|
|
if not platform.resolver:
|
|
|
|
return parser.error("Transparent mode not supported on this platform.")
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "transparent"
|
2014-10-08 23:58:54 +00:00
|
|
|
if options.socks_proxy:
|
|
|
|
c += 1
|
|
|
|
mode = "socks5"
|
2014-03-09 20:51:24 +00:00
|
|
|
if options.reverse_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "reverse"
|
|
|
|
upstream_server = options.reverse_proxy
|
2014-03-13 23:02:00 +00:00
|
|
|
if options.upstream_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "upstream"
|
|
|
|
upstream_server = options.upstream_proxy
|
2014-03-10 04:11:51 +00:00
|
|
|
if c > 1:
|
2014-10-08 23:58:54 +00:00
|
|
|
return parser.error("Transparent, SOCKS5, reverse and upstream proxy mode "
|
2014-09-08 12:32:42 +00:00
|
|
|
"are mutually exclusive.")
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
if options.clientcerts:
|
|
|
|
options.clientcerts = os.path.expanduser(options.clientcerts)
|
|
|
|
if not os.path.exists(options.clientcerts) or not os.path.isdir(options.clientcerts):
|
|
|
|
return parser.error(
|
|
|
|
"Client certificate directory does not exist or is not a directory: %s" % options.clientcerts
|
|
|
|
)
|
|
|
|
|
|
|
|
if (options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd):
|
|
|
|
if options.auth_singleuser:
|
|
|
|
if len(options.auth_singleuser.split(':')) != 2:
|
|
|
|
return parser.error("Invalid single-user specification. Please use the format username:password")
|
|
|
|
username, password = options.auth_singleuser.split(':')
|
|
|
|
password_manager = http_auth.PassManSingleUser(username, password)
|
|
|
|
elif options.auth_nonanonymous:
|
|
|
|
password_manager = http_auth.PassManNonAnon()
|
|
|
|
elif options.auth_htpasswd:
|
|
|
|
try:
|
|
|
|
password_manager = http_auth.PassManHtpasswd(options.auth_htpasswd)
|
|
|
|
except ValueError, v:
|
|
|
|
return parser.error(v.message)
|
|
|
|
authenticator = http_auth.BasicProxyAuth(password_manager, "mitmproxy")
|
|
|
|
else:
|
|
|
|
authenticator = http_auth.NullProxyAuth(None)
|
|
|
|
|
|
|
|
certs = []
|
|
|
|
for i in options.certs:
|
|
|
|
parts = i.split("=", 1)
|
|
|
|
if len(parts) == 1:
|
|
|
|
parts = ["*", parts[0]]
|
|
|
|
parts[1] = os.path.expanduser(parts[1])
|
|
|
|
if not os.path.exists(parts[1]):
|
2014-08-08 02:43:44 +00:00
|
|
|
parser.error("Certificate file does not exist: %s" % parts[1])
|
2014-03-09 20:51:24 +00:00
|
|
|
certs.append(parts)
|
|
|
|
|
2014-10-21 13:08:39 +00:00
|
|
|
ssl_ports = options.ssl_ports
|
|
|
|
if options.ssl_ports != TRANSPARENT_SSL_PORTS:
|
|
|
|
# arparse appends to default value by default, strip that off.
|
|
|
|
# see http://bugs.python.org/issue16399
|
|
|
|
ssl_ports = ssl_ports[len(TRANSPARENT_SSL_PORTS):]
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
return ProxyConfig(
|
2014-09-08 21:34:43 +00:00
|
|
|
host=options.addr,
|
|
|
|
port=options.port,
|
2014-11-15 03:14:08 +00:00
|
|
|
cadir=options.cadir,
|
2014-08-08 02:43:44 +00:00
|
|
|
clientcerts=options.clientcerts,
|
|
|
|
no_upstream_cert=options.no_upstream_cert,
|
|
|
|
body_size_limit=body_size_limit,
|
|
|
|
mode=mode,
|
|
|
|
upstream_server=upstream_server,
|
|
|
|
http_form_in=options.http_form_in,
|
|
|
|
http_form_out=options.http_form_out,
|
2014-10-18 16:29:35 +00:00
|
|
|
ignore_hosts=options.ignore_hosts,
|
|
|
|
tcp_hosts=options.tcp_hosts,
|
2014-08-08 02:43:44 +00:00
|
|
|
authenticator=authenticator,
|
2015-03-02 02:12:27 +00:00
|
|
|
client_ciphers=options.client_ciphers,
|
2015-03-02 02:21:35 +00:00
|
|
|
server_ciphers=options.server_ciphers,
|
2014-08-08 02:43:44 +00:00
|
|
|
certs=certs,
|
|
|
|
certforward=options.certforward,
|
2014-12-15 11:46:13 +00:00
|
|
|
ssl_version_client=options.ssl_version_client,
|
|
|
|
ssl_version_server=options.ssl_version_server,
|
2014-10-21 13:08:39 +00:00
|
|
|
ssl_ports=ssl_ports
|
2014-03-09 20:51:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def ssl_option_group(parser):
|
|
|
|
group = parser.add_argument_group("SSL")
|
|
|
|
group.add_argument(
|
|
|
|
"--cert", dest='certs', default=[], type=str,
|
2014-08-08 02:43:44 +00:00
|
|
|
metavar="SPEC", action="append",
|
2014-10-08 22:49:11 +00:00
|
|
|
help='Add an SSL certificate. SPEC is of the form "[domain=]path". '
|
|
|
|
'The domain may include a wildcard, and is equal to "*" if not specified. '
|
|
|
|
'The file at path is a certificate in PEM format. If a private key is included in the PEM, '
|
|
|
|
'it is used, else the default key in the conf dir is used. '
|
|
|
|
'The PEM file should contain the full certificate chain, with the leaf certificate as the first entry. '
|
|
|
|
'Can be passed multiple times.'
|
2014-03-09 20:51:24 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--client-certs", action="store",
|
|
|
|
type=str, dest="clientcerts", default=None,
|
|
|
|
help="Client certificate directory."
|
|
|
|
)
|
|
|
|
group.add_argument(
|
2015-02-28 16:16:31 +00:00
|
|
|
"--client-ciphers", action="store",
|
|
|
|
type=str, dest="client_ciphers", default=None,
|
2015-03-02 02:49:03 +00:00
|
|
|
help="Client facing SSL cipher specification."
|
2015-02-28 16:16:31 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--server-ciphers", action="store",
|
|
|
|
type=str, dest="server_ciphers", default=None,
|
2015-03-02 02:49:03 +00:00
|
|
|
help="Server facing SSL cipher specification."
|
2014-03-11 00:02:10 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--cert-forward", action="store_true",
|
|
|
|
dest="certforward", default=False,
|
|
|
|
help="Simply forward SSL certificates from upstream."
|
|
|
|
)
|
2014-12-15 11:46:13 +00:00
|
|
|
group.add_argument(
|
|
|
|
"--ssl-version-client", dest="ssl_version_client",
|
|
|
|
default="secure", action="store",
|
|
|
|
choices=sslversion_choices,
|
|
|
|
help="Set supported SSL/TLS version for client connections. "
|
|
|
|
"SSLv2, SSLv3 and 'all' are INSECURE. Defaults to secure."
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--ssl-version-server", dest="ssl_version_server",
|
|
|
|
default="secure", action="store",
|
|
|
|
choices=sslversion_choices,
|
|
|
|
help="Set supported SSL/TLS version for server connections. "
|
|
|
|
"SSLv2, SSLv3 and 'all' are INSECURE. Defaults to secure."
|
|
|
|
)
|
2014-03-13 22:43:26 +00:00
|
|
|
group.add_argument(
|
|
|
|
"--no-upstream-cert", default=False,
|
|
|
|
action="store_true", dest="no_upstream_cert",
|
|
|
|
help="Don't connect to upstream server to look up certificate details."
|
2014-08-30 18:15:19 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
2014-10-21 13:08:39 +00:00
|
|
|
"--ssl-port", action="append", type=int, dest="ssl_ports", default=list(TRANSPARENT_SSL_PORTS),
|
2014-08-30 18:15:19 +00:00
|
|
|
metavar="PORT",
|
|
|
|
help="Can be passed multiple times. Specify destination ports which are assumed to be SSL. "
|
|
|
|
"Defaults to %s." % str(TRANSPARENT_SSL_PORTS)
|
2015-02-28 16:16:31 +00:00
|
|
|
)
|