2016-07-28 54 views
1

我正在寫一個代理,可以捕獲我的硒測試中提出的請求。在硒我用的是接受客戶端請求如何創建可以解碼SSL流量的代理?

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) 

而這正是該連接twith服務器

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])) 

我曾訪問所作的部分原因

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) 

代理部分服務器。我的問題是什麼應該是客戶端請求接受部分的一部分,並在###之間將其轉發給服務器,這將允許我以可讀的格式捕獲流量?證書不是很好。任何幫助將受到歡迎。

回答

2

樣板

SSL是提供雙方各具有在專用/公共密鑰對中的鍵中的一個之間的端至端的加密通信的協議。通常是一個瀏覽器和一個Web服務器。

在正常情況下,兩個端點之間的任何設備都無法解密通信。

然而,有可能使用代理服務器來解密和重新加密通信,從而允許您的情況下進行攔截和解密。但是,它確實需要向客戶機上的可信證書存儲區添加附加證書(自動通過軟件管理系統或由用戶手動添加)。

解決您的問題

總體要創建「中間人」類型的代理,這意味着傳遞到代理服務器的每個請求都應該被解密並再次被加密,而客戶端應該有匹配的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) 
+0

我用mitmproxy如你所說後,有趣的問題。它解釋瞭如何安裝(或成爲CA權威機構)。但是我沒有做到這一點,並且當我通過mitmproxy進行代理時,我能夠看到所有SSL流量。這對我的系統有什麼影響? – user1429322

+0

@ user1429322:這可能意味着您已經設置Selenium/Firefox來忽略證書錯誤,如[在此描述](http://stackoverflow.com/questions/16879566/how-to-disable-firefoxs-untrusted-connection-warning - 使用 - 硒)。 –