代理  mitmproxy/mitmdump/mitmweb抓取websocket包并解码输出已关闭评论
11月 292019



mitmproxy/mitmdump/mitmweb命令行支持 -s  脚本文件.py,此脚本文件(addons)支持的Events事件见


mitmproxy 自带的addons实例文件可参考:



#!mitmdump -s

import mitmproxy.addonmanager

import mitmproxy.connections

import mitmproxy.http

import mitmproxy.log

import mitmproxy.tcp

import mitmproxy.websocket

import mitmproxy.proxy.protocol

class SniffWebSocket:

    def __init__(self):


    # Websocket lifecycle

    def websocket_handshake(self, flow: mitmproxy.http.HTTPFlow):


            Called when a client wants to establish a WebSocket connection. The

            WebSocket-specific headers can be manipulated to alter the

            handshake. The flow object is guaranteed to have a non-None request



    def websocket_start(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has commenced.


    def websocket_message(self, flow: mitmproxy.websocket.WebSocketFlow):


            Called when a WebSocket message is received from the client or

            server. The most recent message will be flow.messages[-1]. The

            message is user-modifiable. Currently there are two types of

            messages, corresponding to the BINARY and TEXT frame types.


        for flow_msg in flow.messages:

            packet = flow_msg.content

            from_client = flow_msg.from_client

            print(“[” + (“Sended” if from_client else “Reveived”) + “]: decode the packet here: %r…” % packet)

    def websocket_error(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has had an error.


        print(“websocket_error, %r” % flow)

    def websocket_end(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has ended.


addons = [




使用命令行 mitmproxy或mitmdump或mitmweb 加 -s如: mitmweb -s 即可以看到websocket解码的输出了.

实际使用中我一般使用mitmweb -s 方式,因为这样在web浏览器里可以窗口化显示http/https的详细输入输出,而在终端运行的窗口可以看到websocket的信息。



在使用中发现在我的ios 13.1的iphone机器上无法抓到websocket的解码内容,但android的机器可以,目前还没有找到好的方法,有知道的朋友可以分享下