mitmproxy2swagger

alufers活跃维护

抓取 mitmproxy 流量后自动逆向生成 OpenAPI/Swagger 文档,省去手写接口定义的麻烦,适合分析没有文档的 REST API。

Automagically reverse-engineer REST APIs via capturing traffic

0.15.0发布于 2026-05-25 11:38 UTC9,704star +11 / 7 天384fork同步于 2026-11-10 11:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install mitmproxy2swagger
mitmproxy2swagger -i <path_to_mitmptoxy_flow> -o <path_to_output_schema> -p <api_prefix>
硬件要求按 README 说明

README 未说明,仅要求 python3 和 pip3,可纯 CPU 运行

有 Docker 镜像
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

把 mitmproxy 抓包或浏览器 HAR 自动转成 OpenAPI 3.0 规范,用来逆向 REST API

抓取 mitmproxy 流量后自动逆向生成 OpenAPI/Swagger 文档,省去手写接口定义的麻烦,适合分析没有文档的 REST API。

核心能力

  • 解析 mitmproxy flow 文件生成 OpenAPI 3.0 规范
  • 支持浏览器 DevTools 导出的 HAR 文件并自动识别
  • 用 x-path-templates 的 ignore 前缀人工筛选要生成的端点
  • 两遍式流程,第二遍才生成端点描述并可合并多次抓包
  • 可选 --examples 和 --headers 把示例与请求头写入规范

容易踩的坑

需先自行用 mitmweb 抓包并保存 flow 文件,工具本身不抓流量;第一遍生成的路径带 ignore: 前缀,必须手动删掉再跑第二遍才会生成端点;--examples 和 --headers 会把 token、密码等敏感数据写进规范,慎用

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本0.15.0
  1. 0.15.0uv migration, dependency updates, bugfixes最新正式版2026-05-25 · 2026-05-25 11:38 UTC
    • Python 3.14 is the default version used in CI and docker container
    • Python 3.12 is the minimum supported Python version
    • Upgraded mitmproxy from 11.0.2 to 12.2.3
    • Fix UnicodeEncodeError when saving swagger file on Windows — thanks @MrTheFall (#212)
    • Fix flow files being misdetected as HAR when first bytes contain only printable characters (e.g. certificates) — thanks @kristiandueholm (#215)
    • Fix path parameters being dropped when using --headers flag — thanks @matgoebl (#216)
    • Fix AttributeError: 'str' object has no attribute 'decode' when processing HAR files with form-encoded bodies — thanks @sarah-github (#174)
    • Fix request headers never being included in parameters for HAR files (missing return in get_request_headers)
    • Fix request headers and query parameters not both appearing when using --headers with HAR files

    Internals:

    • Bumped pytest, pytest-asyncio, pytest-cov, openapi-spec-validator to latest versions
    • migrate the project from poetry to uv
    • use ruff for linting
    • migrate from pre-commit to prek
  2. 0.14.0Mitmproxy 11 support2024-12-12 · 2024-12-12 13:25 UTC
    查看更新说明

    Bumped mitmproxy dependency to 11

  3. 0.13.00.13.0: Msgpack support2024-01-03 · 2024-01-03 13:27 UTC
    查看更新说明
    • Add msgpack support
    • Update dependencies
  4. 0.12.0Update mitmproxy to 10.1.62023-12-28 · 2023-12-28 17:34 UTC
    查看更新说明

    Update other dependencies

  5. 0.11.02023-08-21 · 2023-08-21 12:28 UTC
    查看更新说明
    • Update mitmproxy to 1.0.0
    • Update other dependencies

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    mitmproxy2swagger

    PyPI version Arch Linux repository

    https://user-images.githubusercontent.com/5400940/168086818-c48f60ab-3f95-42eb-b435-c8b1a6326b81.mp4

    A tool for automatically converting mitmproxy captures to OpenAPI 3.0 specifications. This means that you can automatically reverse-engineer REST APIs by just running the apps and capturing the traffic.


    🆕 NEW!

    Added support for processing HAR exported from the browser DevTools. See Usage - HAR for more details.


    Installation

    First you will need python3 and pip3.

    $ pip install mitmproxy2swagger
    # ... or ...
    $ pip3 install mitmproxy2swagger
    # ... or ...
    $ git clone [email protected]:alufers/mitmproxy2swagger.git
    $ cd mitmproxy2swagger
    $ docker build -t mitmproxy2swagger .
    

    Then clone the repo and run mitmproxy2swagger as per examples below.

    Usage

    Mitmproxy

    To create a specification by inspecting HTTP traffic you will need to:

    1. Capture the traffic by using the mitmproxy tool. I personally recommend using mitmweb, which is a web interface built-in to mitmproxy.

      $ mitmweb
      Web server listening at http://127.0.0.1:8081/
      Proxy server listening at http://*:9999
      ...
      

      IMPORTANT

      To configure your client to use the proxy exposed by mitm proxy, please consult the mitmproxy documentation for more information.

    2. Save the traffic to a flow file.

      In mitmweb you can do this by using the "File" menu and selecting "Save":

      A screenshot showing the location of the "Save" option in the "File" menu

    3. Run the first pass of mitmproxy2swagger:

      $ mitmproxy2swagger -i <path_to_mitmptoxy_flow> -o <path_to_output_schema> -p <api_prefix>
      # ... or ...
      $ docker run -it -v $PWD:/app mitmproxy2swagger mitmproxy2swagger -i <path_to_mitmptoxy_flow> -o <path_to_output_schema> -p <api_prefix>
      

      Please note that you can use an existing schema, in which case the existing schema will be extended with the new data. You can also run it a few times with different flow captures, the captured data will be safely merged.

      <api_prefix> is the base url of the API you wish to reverse-engineer. You will need to obtain it by observing the requests being made in mitmproxy.

      For example if an app has made requests like these:

      https://api.example.com/v1/login
      https://api.example.com/v1/users/2
      https://api.example.com/v1/users/2/profile
      

      The likely prefix is https://api.example.com/v1.

    4. Running the first pass should have created a section in the schema file like this:

      x-path-templates:
        # Remove the ignore: prefix to generate an endpoint with its URL
        # Lines that are closer to the top take precedence, the matching is greedy
        - ignore:/addresses
        - ignore:/basket
        - ignore:/basket/add
        - ignore:/basket/checkouts
        - ignore:/basket/coupons/attach/{id}
        - ignore:/basket/coupons/attach/104754
      

      You should edit the schema file with a text editor and remove the ignore: prefix from the paths you wish to be generated. You can also adjust the parameters appearing in the paths.

    5. Run the second pass of mitmproxy2swagger:

      $ mitmproxy2swagger -i <path_to_mitmptoxy_flow> -o <path_to_output_schema> -p <api_prefix> [--examples]
      # ... or ...
      $ docker run -it -v $PWD:/app mitmproxy2swagger mitmproxy2swagger -i <path_to_mitmptoxy_flow> -o <path_to_output_schema> -p <api_prefix> [--examples]
      

      Run the command a second time (with the same schema file). It will pick up the edited lines and generate endpoint descriptions.

      Please note that mitmproxy2swagger will not overwrite existing endpoint descriptions, if you want to overwrite them, you can delete them before running the second pass.

      Passing --examples will add example data to requests and responses. Take caution when using this option, as it may add sensitive data (tokens, passwords, personal information etc.) to the schema. Passing --headers will add headers data to requests and responses. Take caution when using this option, as it may add sensitive data (tokens, passwords, personal information etc.) to the schema.

    HAR

    1. Capture and export the traffic from the browser DevTools.

      In the browser DevTools, go to the Network tab and click the "Export HAR" button.

      A screenshot showing where the export har button is located

    2. Continue the same way you would do with the mitmproxy dump. mitmproxy2swagger will automatically detect the HAR file and process it.

    Example output

    See the examples. You will find a generated schema there and an html file with the generated documentation (via redoc-cli).

    See the generated html file.

    Development and contributing

    This project uses:

    • uv for dependency management
    • prek for code formatting and linting
    • pytest for unit testing

    To install the dependencies:

    uv sync
    

    Run linters:

    uv run prek run --all-files
    

    Install prek hooks:

    uv run prek install
    

    Run tests:

    uv run pytest
    

    Run tests with coverage:

    uv run pytest --cov=mitmproxy2swagger
    

    License

    MIT