mitmproxy2swagger

repository·master·Indexed 27 days ago

https://github.com/alufers/mitmproxy2swagger

A tool for automatically converting mitmproxy captures or browser HAR files into OpenAPI 3.0 specifications. It allows developers to reverse-engineer REST APIs by observing traffic, featuring a CLI for processing flow files and HAR archives, custom path parameter detection via regex, and utilities for inferring OpenAPI schemas from captured request and response data.

Tokens
2.4K
Snippets
1
Records
18
Agent score
93%

What's inside mitmproxy2swagger

  1. Convert mitmproxy captures to OpenAPI 3.0

    master

    To reverse-engineer a REST API from mitmproxy traffic, follow these steps:

    1. Capture Traffic: Use mitmweb or mitmproxy to capture the desired HTTP traffic. Ensure your client is configured to use the proxy.
    2. Save Flow: Save the captured traffic to a flow file (e.g., via the "File" > "Save" menu in mitmweb).
    3. First Pass: Run the tool to generate an initial schema with path templates.
    4. Edit Schema: Open the generated schema file. You will see paths prefixed with ignore:. Remove the ignore: prefix from the paths you want to include in the final specification and adjust parameters as needed.
    5. Second Pass: Run the tool again on the same schema file to generate the actual endpoint descriptions. Use the --examples or --headers flags to include extra data (use caution with sensitive information).
  2. Convert HAR files to OpenAPI 3.0

    master

    You can process HAR (HTTP Archive) files exported from browser DevTools. mitmproxy2swagger automatically detects HAR files.

    1. Open browser DevTools and go to the Network tab.
    2. Click the Export HAR button.
    3. Run mitmproxy2swagger using the HAR file as the input -i argument.
  3. Install mitmproxy2swagger

    master

    You can install mitmproxy2swagger using pip, pip3, or by building the Docker image from the source repository.

    $ pip install mitmproxy2swagger
    # or
    $ pip3 install mitmproxy2swagger
    # or via Docker
    $ git clone git@github.com:alufers/mitmproxy2swagger.git
    $ cd mitmproxy2swagger
    $ docker build -t mitmproxy2swagger .
  4. Use x-path-templates for suggested endpoints

    master

    When running the tool, it identifies new paths and adds them to the x-path-templates array in the output YAML file. Some suggested paths are prefixed with ignore: to prevent them from being automatically converted into full endpoints immediately.

    To turn a suggested template into a real endpoint in your Swagger file, remove the ignore: prefix from the path string in the x-path-templates section of the YAML file.

  5. mitmproxy2swagger CLI Reference

    master

    The CLI tool accepts the following arguments to process traffic captures:

    • -i, --input: Path to the mitmproxy flow file or HAR file.
    • -o, --output: Path to the output OpenAPI schema file. If the file exists, it will be extended/merged.
    • -p, --prefix: The base URL of the API you wish to reverse-engineer (e.g., https://api.example.com/v1).
    • --examples: (Optional) Adds example data to requests and responses. Warning: May include sensitive data.
    • --headers: (Optional) Adds header data to requests and responses. Warning: May include sensitive data.
  6. Process HAR entries with HarCaptureReader

    master

    The HarCaptureReader class is used to parse and iterate through requests captured in a HAR file.

    Initialization: HarCaptureReader(file_path: str, progress_callback=None)

    • file_path: Path to the .har file.
    • progress_callback: An optional function called with a float (0.0 to 1.0) representing the current progress through the file.

    Methods:

    • captured_requests() -> Iterator[HarFlowWrapper]: Yields HarFlowWrapper objects for each entry in the HAR log.
  7. Identify mitmproxy dump files using mitmproxy_dump_file_huristic

    master

    The mitmproxy_dump_file_huristic function provides a heuristic score to determine if a file is a valid mitmproxy dump file. It checks for keywords in the filename and inspects the file content for specific patterns (like status_code or regular) and non-printable characters.

    A higher score indicates a higher probability that the file is a mitmproxy flow file.

  8. Extract request and response data with HarFlowWrapper

    master

    The HarFlowWrapper class wraps individual HAR entries (flows) and provides methods to extract request and response details.

    Request Methods:

    • get_url(): Returns the request URL.
    • get_matching_url(prefix): Returns the URL if it starts with the provided prefix, otherwise returns None.
    • get_method(): Returns the HTTP method (e.g., GET, POST).
    • get_request_headers(): Returns a dictionary where keys are header names and values are lists of header strings.
    • get_request_body(): Returns the request body text, or None if not present.

    Response Methods:

    • get_response_status_code(): Returns the HTTP status code.
    • get_response_reason(): Returns the HTTP status text.
    • get_response_headers(): Returns a dictionary where keys are header names and values are lists of header strings.
    • get_response_body(): Returns the response body text. If the content is base64 encoded, it automatically decodes it to a string.
  9. Convert headers to OpenAPI format with `request_to_headers` and `response_to_headers`

    master

    These functions map raw header dictionaries to OpenAPI-compliant structures.

    • request_to_headers(headers, add_example=False): Converts a dictionary of headers into a list of parameter objects with in: "header". If add_example is True, it includes the first value of the header as an example field.
    • response_to_headers(headers): Converts a dictionary of headers into an object where each key maps to a description and a schema (detecting number or string).
  10. Identify HAR archives with har_archive_heuristic

    master

    Use the har_archive_heuristic(file_path: str) -> int function to determine if a file is a valid HAR (HTTP Archive) file. It returns an integer score based on several checks:

    • File extension .har (+25)
    • File content is printable ASCII (+25)
    • File starts with { (+23)
    • Presence of Chrome or Firefox export markers (+15)
    • Presence of the "entries" key (+15)
    • Presence of the "version" key (+15)
  11. Use MitmproxyCaptureReader to iterate over captured requests

    master

    The MitmproxyCaptureReader class is used to read and stream http.HTTPFlow objects from a mitmproxy log file. You can provide an optional progress_callback function that receives a float (0.0 to 1.0) representing the current progress of the file reading.

    Use the captured_requests() method to get an iterator of MitmproxyFlowWrapper objects. Note that flows without a response are skipped and a warning is printed to stdout.