【问题标题】:How to create a proxy that can decode SSL traffic?如何创建可以解码 SSL 流量的代理?
【发布时间】:2016-12-03 12:12:53
【问题描述】:

我正在编写一个代理,它可以捕获在我的 selenium 测试中发出的请求。在硒中我使用了这个

host = '10.203.9.156'
profile  = webdriver.FirefoxProfile()
myProxy = "localhost:8899"

proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(proxy=proxy)

接受客户端请求的代理部分

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
###
ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, keyfile = ??, certfile = ???, server_side=True)
###
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.hostname, self.port))
self.socket.listen(self.backlog)
while True:
    conn, addr = self.socket.accept()
    logger.debug('Accepted connection %r at address %r' % (conn, addr))
    self.handle(conn,addr)

这是与服务器建立连接的部分

self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
###
ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, keyfile = ??, certfile = ???, server_side=True)
###
self.conn.connect((self.addr[0], self.addr[1]))

我可以访问服务器。我的问题是客户端请求接受部分和转发到服务器的部分应该是什么,在###之间,这将允许我以人类可读的格式捕获流量?我不太擅长证书。欢迎任何帮助。

【问题讨论】:

标签: python selenium ssl encryption proxy


【解决方案1】:

样板

SSL 是一种在两方之间提供端到端加密通信的协议,每一方都拥有私钥/公钥对中的一个密钥。通常是浏览器和网络服务器。

在正常情况下,两个端点之间的任何设备都无法解密通信。

但是,可以使用代理服务器对通信进行解密和重新加密,从而允许拦截和解密,这就是您的情况。但是,它确实需要将额外的证书添加到客户端计算机上的受信任证书存储中(通过软件管理系统自动或由用户手动)。

解决您的问题

总体而言,您正在创建“中间人”类型的代理,这意味着传递给代理服务器的每个请求都应再次解密和加密,而客户端应具有匹配的 SSL 私钥。 尝试使用 mitmproxy/libmproxy 库。

查看可能的 proxy.py 解决方案:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from libmproxy import controller, proxy
import os, sys, re, datetime, json

class RequestHacks:
  @staticmethod
  def example_com (msg):
    # tamper outgoing requests for https://example.com/api/v2
    if ('example.org' in msg.host) and ('action=login' in msg.content):
      fake_lat, fake_lng = 25.0333, 121.5333
      tampered = re.sub('lat=([\d.]+)&lng=([\d.]+)', 'lat=%s&lng=%s' % (fake_lat, fake_lng), msg.content)
      msg.content = tampered
      print '[RequestHacks][Example.com] Fake location (%s, %s) sent when logging in' % (fake_lat, fake_lng)


class ResponseHacks:
  @staticmethod
  def example_org (msg):
    # simple substitution for https://example.org/api/users/:id.json
    if 'example.org' in msg.request.host:
      regex = re.compile('/api/users/(\d+).json')
      match = regex.search(msg.request.path)
      if match and msg.content:
        c = msg.replace(''private_data_accessible':false', ''private_data_accessible':true')
        if c > 0:
          user_id = match.groups()[0]
          print '[ResponseHacks][Example.org] Private info of user #%s revealed' % user_id

  @staticmethod
  def example_com (msg):
    # JSON manipulation for https://example.com/api/v2
    if ('example.com' in msg.request.host) and ('action=user_profile' in msg.request.content):
      msg.decode() # need to decode the message first
      data = json.loads(msg.content) # parse JSON with decompressed content
      data['access_granted'] = true
      msg.content = json.dumps(data) # write back our changes
      print '[ResponseHacks][Example.com] Access granted of user profile #%s' % data['id']

  @staticmethod
  def example_net (msg):
    # Response inspection for https://example.net
    if 'example.net' in msg.request.host:
      data = msg.get_decoded_content() # read decompressed content without modifying msg
      print '[ResponseHacks][Example.net] Respones: %s' % data


class InterceptingMaster (controller.Master):
  def __init__ (self, server):
    controller.Master.__init__(self, server)

  def run (self):
    while True:
      try:
        controller.Master.run(self)
      except KeyboardInterrupt:
        print 'KeyboardInterrupt received. Shutting down'
        self.shutdown()
        sys.exit(0)
      except Exception:
        print 'Exception catched. Intercepting proxy restarted'
        pass

  def handle_request (self, msg):
    timestamp = datetime.datetime.today().strftime('%Y/%m/%d %H:%M:%S')
    client_ip = msg.client_conn.address[0]
    request_url = '%s://%s%s' % (msg.scheme, .msg.host, msg.path)
    print '[%s %s] %s %s' % (timestamp, client_ip, msg.method, request_url)

    RequestHacks.example_com(msg)
    msg.reply()

  def handle_response (self, msg):
    ResponseHacks.example_org(msg)
    ResponseHacks.example_com(msg)
    ResponseHacks.example_net(msg)
    msg.reply()


def main (argv):
  config = proxy.ProxyConfig(
    cacert = os.path.expanduser('./mitmproxy.pem'),
  )
  server = proxy.ProxyServer(config, 8080)
  print 'Intercepting Proxy listening on 8080'
  m = InterceptingMaster(server)
  m.run()

if __name__ == '__main__':
  main(sys.argv)

【讨论】:

  • 按照您的建议使用 mitmproxy 后的有趣问题。它解释了安装(或成为 CA 机构)。但是我什么都没做,当我通过 mitmproxy 代理时,我能够看到所有的 SSL 流量。这对我的系统有什么影响?
  • @user1429322:这可能意味着您已将 Selenium/Firefox 设置为忽略 here 中描述的证书错误。
猜你喜欢
  • 2019-05-29
  • 2015-06-02
  • 2014-10-25
  • 2014-08-05
  • 2019-05-17
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多