2015-08-31 11:43:30 +00:00
|
|
|
from __future__ import (absolute_import, print_function, division)
|
2015-08-27 23:51:13 +00:00
|
|
|
import collections
|
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
|
2015-07-15 21:19:01 +00:00
|
|
|
|
2015-08-27 16:31:15 +00:00
|
|
|
from netlib import certutils, tcp
|
2015-07-15 21:19:01 +00:00
|
|
|
from netlib.http import authentication
|
2015-08-30 13:27:29 +00:00
|
|
|
from netlib.tcp import Address, sslversion_choices
|
2015-07-15 21:19:01 +00:00
|
|
|
|
2015-08-27 16:31:15 +00:00
|
|
|
from .. import utils, platform
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
CONF_BASENAME = "mitmproxy"
|
2014-11-15 03:14:08 +00:00
|
|
|
CA_DIR = "~/.mitmproxy"
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2015-08-26 13:12:04 +00:00
|
|
|
# We manually need to specify this, otherwise OpenSSL may select a non-HTTP2 cipher by default.
|
|
|
|
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=apache-2.2.15&openssl=1.0.2&hsts=yes&profile=old
|
|
|
|
DEFAULT_CLIENT_CIPHERS = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2015-08-27 23:51:13 +00:00
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
class HostMatcher(object):
|
2015-08-27 23:51:13 +00:00
|
|
|
def __init__(self, patterns=tuple()):
|
2014-10-18 16:29:35 +00:00
|
|
|
self.patterns = list(patterns)
|
|
|
|
self.regexes = [re.compile(p, re.IGNORECASE) for p in self.patterns]
|
|
|
|
|
|
|
|
def __call__(self, address):
|
2015-08-30 00:27:38 +00:00
|
|
|
if not address:
|
|
|
|
return False
|
2014-10-18 16:29:35 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-08-27 23:51:13 +00:00
|
|
|
ServerSpec = collections.namedtuple("ServerSpec", "scheme address")
|
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class ProxyConfig:
|
2014-11-15 03:14:08 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host='',
|
|
|
|
port=8080,
|
|
|
|
cadir=CA_DIR,
|
|
|
|
clientcerts=None,
|
|
|
|
no_upstream_cert=False,
|
|
|
|
body_size_limit=None,
|
2015-08-27 23:51:13 +00:00
|
|
|
mode="regular",
|
2014-11-15 03:14:08 +00:00
|
|
|
upstream_server=None,
|
|
|
|
authenticator=None,
|
2015-08-27 23:51:13 +00:00
|
|
|
ignore_hosts=tuple(),
|
|
|
|
tcp_hosts=tuple(),
|
2015-09-10 22:00:00 +00:00
|
|
|
http2=False,
|
|
|
|
rawtcp=False,
|
2015-12-11 16:09:02 +00:00
|
|
|
ciphers_client=DEFAULT_CLIENT_CIPHERS,
|
2015-03-02 13:35:50 +00:00
|
|
|
ciphers_server=None,
|
2015-08-27 23:51:13 +00:00
|
|
|
certs=tuple(),
|
2015-08-27 16:37:16 +00:00
|
|
|
ssl_version_client="secure",
|
|
|
|
ssl_version_server="secure",
|
2015-06-29 17:32:57 +00:00
|
|
|
ssl_verify_upstream_cert=False,
|
2015-08-27 23:51:13 +00:00
|
|
|
ssl_verify_upstream_trusted_cadir=None,
|
|
|
|
ssl_verify_upstream_trusted_ca=None,
|
2014-11-15 03:14:08 +00:00
|
|
|
):
|
2014-09-08 21:34:43 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2015-03-02 13:35:50 +00:00
|
|
|
self.ciphers_client = ciphers_client
|
|
|
|
self.ciphers_server = ciphers_server
|
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
|
2015-08-27 16:31:15 +00:00
|
|
|
self.mode = mode
|
2015-08-27 23:51:13 +00:00
|
|
|
if upstream_server:
|
|
|
|
self.upstream_server = ServerSpec(upstream_server[0], Address.wrap(upstream_server[1]))
|
|
|
|
else:
|
|
|
|
self.upstream_server = None
|
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)
|
2015-09-10 22:00:00 +00:00
|
|
|
self.http2 = http2
|
|
|
|
self.rawtcp = rawtcp
|
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)
|
2015-05-30 00:03:28 +00:00
|
|
|
self.certstore = certutils.CertStore.from_store(
|
|
|
|
self.cadir,
|
2015-08-27 16:31:15 +00:00
|
|
|
CONF_BASENAME
|
|
|
|
)
|
2014-04-29 12:54:11 +00:00
|
|
|
for spec, cert in certs:
|
|
|
|
self.certstore.add_cert_file(spec, cert)
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2015-08-27 23:51:13 +00:00
|
|
|
self.openssl_method_client, self.openssl_options_client = \
|
|
|
|
sslversion_choices[ssl_version_client]
|
|
|
|
self.openssl_method_server, self.openssl_options_server = \
|
|
|
|
sslversion_choices[ssl_version_server]
|
2015-07-15 21:19:01 +00:00
|
|
|
|
2015-06-29 17:32:57 +00:00
|
|
|
if ssl_verify_upstream_cert:
|
|
|
|
self.openssl_verification_mode_server = SSL.VERIFY_PEER
|
|
|
|
else:
|
|
|
|
self.openssl_verification_mode_server = SSL.VERIFY_NONE
|
2015-08-27 23:51:13 +00:00
|
|
|
self.openssl_trusted_cadir_server = ssl_verify_upstream_trusted_cadir
|
|
|
|
self.openssl_trusted_ca_server = ssl_verify_upstream_trusted_ca
|
|
|
|
|
|
|
|
|
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
|
2015-08-27 23:51:13 +00:00
|
|
|
mode, upstream_server = "regular", 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:
|
2015-08-27 23:51:13 +00:00
|
|
|
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:
|
2015-05-30 00:03:28 +00:00
|
|
|
return parser.error(
|
|
|
|
"Transparent, SOCKS5, reverse and upstream proxy mode "
|
2015-08-27 23:51:13 +00:00
|
|
|
"are mutually exclusive. Read the docs on proxy modes to understand why."
|
|
|
|
)
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
if options.clientcerts:
|
|
|
|
options.clientcerts = os.path.expanduser(options.clientcerts)
|
2015-12-29 16:48:02 +00:00
|
|
|
if not os.path.exists(options.clientcerts):
|
2014-03-09 20:51:24 +00:00
|
|
|
return parser.error(
|
2015-12-29 16:48:02 +00:00
|
|
|
"Client certificate path does not exist: %s" % options.clientcerts
|
2015-08-27 23:51:13 +00:00
|
|
|
)
|
2014-03-09 20:51:24 +00:00
|
|
|
|
2015-08-27 23:51:13 +00:00
|
|
|
if options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd:
|
2015-08-31 15:29:14 +00:00
|
|
|
|
2015-11-14 20:55:02 +00:00
|
|
|
if options.transparent_proxy:
|
|
|
|
return parser.error("Proxy Authentication not supported in transparent mode.")
|
|
|
|
|
2015-08-31 15:29:14 +00:00
|
|
|
if options.socks_proxy:
|
|
|
|
return parser.error(
|
|
|
|
"Proxy Authentication not supported in SOCKS mode. "
|
|
|
|
"https://github.com/mitmproxy/mitmproxy/issues/738"
|
|
|
|
)
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
if options.auth_singleuser:
|
|
|
|
if len(options.auth_singleuser.split(':')) != 2:
|
2015-05-30 00:03:28 +00:00
|
|
|
return parser.error(
|
2015-08-27 23:51:13 +00:00
|
|
|
"Invalid single-user specification. Please use the format username:password"
|
|
|
|
)
|
2014-03-09 20:51:24 +00:00
|
|
|
username, password = options.auth_singleuser.split(':')
|
2015-07-15 21:19:01 +00:00
|
|
|
password_manager = authentication.PassManSingleUser(username, password)
|
2014-03-09 20:51:24 +00:00
|
|
|
elif options.auth_nonanonymous:
|
2015-07-15 21:19:01 +00:00
|
|
|
password_manager = authentication.PassManNonAnon()
|
2014-03-09 20:51:24 +00:00
|
|
|
elif options.auth_htpasswd:
|
|
|
|
try:
|
2015-07-15 21:19:01 +00:00
|
|
|
password_manager = authentication.PassManHtpasswd(
|
2015-05-30 00:03:28 +00:00
|
|
|
options.auth_htpasswd)
|
|
|
|
except ValueError as v:
|
2014-03-09 20:51:24 +00:00
|
|
|
return parser.error(v.message)
|
2015-07-15 21:19:01 +00:00
|
|
|
authenticator = authentication.BasicProxyAuth(password_manager, "mitmproxy")
|
2014-03-09 20:51:24 +00:00
|
|
|
else:
|
2015-07-15 21:19:01 +00:00
|
|
|
authenticator = authentication.NullProxyAuth(None)
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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,
|
2014-10-18 16:29:35 +00:00
|
|
|
ignore_hosts=options.ignore_hosts,
|
|
|
|
tcp_hosts=options.tcp_hosts,
|
2015-09-10 22:00:00 +00:00
|
|
|
http2=options.http2,
|
|
|
|
rawtcp=options.rawtcp,
|
2014-08-08 02:43:44 +00:00
|
|
|
authenticator=authenticator,
|
2015-03-02 13:35:50 +00:00
|
|
|
ciphers_client=options.ciphers_client,
|
|
|
|
ciphers_server=options.ciphers_server,
|
2015-08-27 23:51:13 +00:00
|
|
|
certs=tuple(certs),
|
2014-12-15 11:46:13 +00:00
|
|
|
ssl_version_client=options.ssl_version_client,
|
|
|
|
ssl_version_server=options.ssl_version_server,
|
2015-06-29 17:32:57 +00:00
|
|
|
ssl_verify_upstream_cert=options.ssl_verify_upstream_cert,
|
2015-08-27 23:51:13 +00:00
|
|
|
ssl_verify_upstream_trusted_cadir=options.ssl_verify_upstream_trusted_cadir,
|
|
|
|
ssl_verify_upstream_trusted_ca=options.ssl_verify_upstream_trusted_ca
|
2015-09-10 22:00:00 +00:00
|
|
|
)
|