mitmproxy_rs

repository·main·Indexed 19 days ago

https://github.com/mitmproxy/mitmproxy_rs

Rust implementation of core mitmproxy components providing high-performance proxying capabilities. It includes WireGuard mode, platform-specific traffic redirection for macOS, Windows, and Linux, and specialized crates for syntax highlighting (mitmproxy-highlight) and content views (mitmproxy-contentviews). The project provides Python bindings via mitmproxy-rs and utilizes technologies such as eBPF for Linux redirection and WinDivert for Windows.

Tokens
24.4K
Snippets
86
Records
125
Agent score
65%

What's inside mitmproxy_rs

  1. Overview of mitmproxy_rs

    main

    mitmproxy_rs contains the Rust-based components of mitmproxy, providing high-performance core logic and specialized proxying modes. Key features include:

    • WireGuard Mode: Enables proxying for any device that can be configured as a WireGuard client.
    • Local Redirect Mode: Allows proxying arbitrary macOS or Windows applications by their name or process ID (PID).
  2. Understand the components of mitmproxy-windows

    main

    The mitmproxy-windows package provides the necessary components to redirect Windows network traffic to mitmproxy. It consists of:

    • windows-redirector.exe: A Rust-based executable that handles traffic redirection to mitmproxy using a Windows named pipe.
    • WinDivert: A vendored copy of the WinDivert library, which is used by the redirector to intercept network packets.
  3. Components of the macOS Redirector

    main

    The macOS transparent interception system consists of three primary components:

    • macos-redirector: The macOS app bundle responsible for setting up and hosting the network extension.
    • network-extension: The system-level extension that performs the actual traffic redirection.
    • ipc: The Inter-Process Communication layer using Protobuf to facilitate communication between the proxy (written in Rust) and the redirector (written in Swift).
  4. Understand the macOS mitmproxy components

    main

    The mitmproxy-macos package provides two primary precompiled binaries for macOS users:

    • macos-certificate-truster.app: A Rust-based helper application used to mark the mitmproxy Certificate Authority (CA) as trusted on the system.
    • Mitmproxy Redirector.app: An app bundle that sets up and hosts the macOS Network System Extension required for traffic redirection.
  5. Why the macOS certificate-truster bundle app is required

    main

    On macOS, automating the process of adding and trusting a certificate is restricted by GUI-based security limitations. The macos-certificate-truster bundle app is used to bypass these restrictions, allowing for automated certificate management.

    Key characteristics:

    • Operates without user interaction or window display.
    • May trigger a system popup asking for permission to unlock the keychain.
    • Designed to ensure smoother automation of the mitmproxy certificate trust process on macOS systems.

    For technical details regarding these security restrictions, refer to the Apple Documentation on SecTrustSettingsSetTrustSettings.

  6. Project Structure and Package Components

    main

    The repository is organized into several specialized packages that extend mitmproxy's capabilities:

    • mitmproxy (in src/): The core Rust crate containing the primary logic.
    • mitmproxy-rs: Python bindings for the Rust crate using PyO3. Available on PyPI.
    • mitmproxy-macos: A macOS Network Extension for transparent traffic interception. Available on PyPI as a binary distribution.
    • mitmproxy-windows: A Windows traffic redirector based on WinDivert. Available on PyPI as a binary distribution.
    • mitmproxy-linux: A Linux traffic redirector based on Aya. Available on PyPI.
    • mitmproxy-contentviews: Pretty-printers for HTTP message bodies.
    • mitmproxy-highlight: Syntax highlighting backend for mitmproxy and mitmdump.
    • mitmproxy-linux-ebpf: The eBPF program used by the Linux redirector.
    • mitmproxy-linux-ebpf-common: Shared data structures between user space and eBPF.
  7. How the macOS transparent interception works

    main

    Transparent interception on macOS is achieved through a coordinated effort between mitmproxy-rs and a macOS app bundle. The process follows these steps:

    1. Setup: mitmproxy-rs calls start_local_redirector, which copies the macos-redirector app bundle into /Applications (required for system extension installation).
    2. Initialization: mitmproxy-rs opens a Unix socket listener and launches the macos-redirector app, passing the Unix socket path as a command-line argument.
    3. Extension Installation: The macos-redirector app installs the system extension and configures the transparent proxy settings.
    4. Control Channel: The OS starts the network-extension, which connects back to mitmproxy-rs via a Unix socket to establish a control channel.
    5. Intercept Configuration: mitmproxy-rs sends an intercept spec (defining which applications to intercept) to the network extension.
    6. Traffic Redirection: The network extension captures TCP/UDP flows. If a flow matches the intercept spec, the extension opens a dedicated Unix socket per flow to mitmproxy-rs. This architecture ensures that each connection has its own buffers and backpressure management.
  8. Access mitmproxy-contentviews from Python

    main
    The mitmproxy-contentviews crate provides various content views for mitmproxy. These views are exposed to the Python layer through the mitmproxy-rs bridge. To use these content views in your Python-based mitmproxy addons or scripts, you must interact with the interface exposed in mitmproxy-rs/src/contentviews.rs.