Skip to main content

No project description provided

Project description

mitmproxy2swagger

Install and run with m2s instead. It is for specific purposes, and might be not reverse-engineer compliant. For pure reverse engineering, please refer to alufers/mitmproxy2swagger. Some fixes and custom features:

  • fix headers ecluding path or query params
  • fix legacy path persisting with --suppress-params
  • --x-request-headers to exclude specific request headers
  • --x-response-headers to exclude specific response headers
  • --no-request-header to exclude all request headers
  • --no-response-header to exclude all response headers
  • --populate-new to allow populating document for the new endpoint immediately
  • --x-methods to exclude the request with specific methods
  • --overwrite to overwrite the whole swagger file
  • --add-tags to add tags automatically based on endpoint discrepancies
  • --tags-json to add tags according to JSON mapping
  • --title to specify document title i.e. info.title
  • rough support for Basic/Bearer Autorization

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 git@github.com: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 here.

Development and contributing

This project uses:

To install the dependencies:

poetry install

Run linters:

pre-commit run --all-files

Install pre-commit hooks:

pre-commit install

Run tests:

poetry run pytest

Run tests with coverage:

poetry run pytest --cov=mitmproxy2swagger

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m2s-0.14.7.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m2s-0.14.7-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file m2s-0.14.7.tar.gz.

File metadata

  • Download URL: m2s-0.14.7.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for m2s-0.14.7.tar.gz
Algorithm Hash digest
SHA256 00a4001b8226ef33cae523aa8588f7009bf36d883e1f6a7d31b783693ea7b05a
MD5 15892baf06455ccdfb0dc9dbc81c6da0
BLAKE2b-256 781d1eca6b34f9b84628e1fcfff26efb7d29d78c8a64dddb8f094f0f4a06153f

See more details on using hashes here.

File details

Details for the file m2s-0.14.7-py3-none-any.whl.

File metadata

  • Download URL: m2s-0.14.7-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for m2s-0.14.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0ae525d5110d97c1ef0ab4de08a6fb513922e379e403bcb354394029b2d73ef6
MD5 c43fb73b17b588826152df58d744188f
BLAKE2b-256 fbd89a5ea7247eb91840ef6cec39a415d276a3c19b0fd22ada3c0ed3c949fb01

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page