How to make a network request inside a Mitmproxy addon
Asked Answered
S

1

1

Let's say I have an addon that modifies a response from specific endpoints, like the one provided in the documentation example: https://docs.mitmproxy.org/stable/api/events.html#event-hooks

Within that response function I want to make an http(s) request to an unrelated endpoint. To make http(s) requests I use the requests library. Problem is, if I specify nothing but the url in the requests get/post method, I get ValueError: check_hostname requires server_hostname error. After researching this I got the impression that the underlying urllib3 library requires extra proxy information. But if I try to provide proxies object that looks like:

proxies={
    'http': f'http://127.0.0.1:{port}',
    'https': f'https://127.0.0.1:{port}',
}

I get the following error instead: requests.exceptions.ProxyError: HTTPSConnectionPool(host='<hostname>', port=443): Max retries exceeded with url: <endpoint> (Caused by ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1114: The handshake operation timed out')))

I assume that any request made inside addon scripts goes through mitmproxy itself, where it successfully dies. Hence, I was wondering how to properly make http requests within mitmproxy addons?


OS: Windows 10
Mitmproxy version: 6.0.2

Sigler answered 10/9, 2021 at 18:50 Comment(2)
If you want to make a request to an unrelated endpoint why do you want to let the request pass Mitmproxy? Wouldn't it make more sense to directly connect to the endpoint without any proxy?Subaltern
I do not want to let the unrelated request pass mitmproxy. It just happens because the proxy is activated on system level, so all requests, by default, go through it.Sigler
E
2

Make a file Named: multiproxy.py

from mitmproxy import http
from mitmproxy.connection import Server
from mitmproxy.net.server_spec import ServerSpec

class multiproxy:

    def __init__(self):
        self.num = 0
        
    def request(self, flow) -> None:
        address = ("target-ip-2", [target-port-2])
        flow.server_conn = Server(flow.server_conn.address)
        flow.server_conn.via = ServerSpec("http", address)
        
addons = [multiproxy()]

Run mitmproxy with this addon:

./mitmproxy --set block_global=false --ssl-insecure -s multiproxy.py
Electrodynamics answered 3/5, 2022 at 1:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.