Mitmproxy2Swagger – Automagically Reverse-Engineer REST APIs Via Capturing Traffic

A tool for automatically converting

  • Run the first pass of 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.

  • 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.

  • Run the second pass of 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.

  • 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.

    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.

    Download Mitmproxy2Swagger

    If you like the site, please consider joining the telegram channel or supporting us on Patreon using the button below.

    Discord

    Original Source