Search code examples
pythonmitmproxy

How to filter a request for a host with mitmproxy addon if its hostname match a regex?


I wanna implement an addon which filters and remove all request for a set of domains. All domains must match with the following regex:

a-.+\.xxxx\.com

I do not know how to get a request hostname:

if re.match("a-.+\.xxxx\.com", flow.request.hostname):
     # Do something

Solution

  • In the mitmproxy, the following class is used to encapsulate request information:

    mitmproxy.net.http.Request
    

    And the contractor of the Request class is:

    def __init__(
            self,
            host: str,
            port: int,
            method: bytes,
            scheme: bytes,
            authority: bytes,
            path: bytes,
            http_version: bytes,
            headers: Union[Headers, Tuple[Tuple[bytes, bytes], ...]],
            content: Optional[bytes],
            trailers: Union[None, Headers, Tuple[Tuple[bytes, bytes], ...]],
            timestamp_start: float,
            timestamp_end: Optional[float],
    ):
    

    So, somewhere in the platform the host and port is passed to the class instance.

    The implementation uses a data class to store data:

    @dataclass
    class RequestData(message.MessageData):
        host: str
        port: int
        method: bytes
        scheme: bytes
        authority: bytes
        path: bytes
    

    Therefor, to access the request hostname:

    if re.match("a-.+\.xxxx\.com", flow.request.data.host):
         # Do something
    

    Following method added directly into the Request class:

    A getter

    @property
    def host(self) -> str:
    

    A setter

    @host.setter
    def host(self, val: Union[str, bytes]) -> None:
    

    So the following code is acceptable too:

    if re.match("a-.+\.xxxx\.com", flow.request.host):
         # Do something