Skip to content

Instantly share code, notes, and snippets.

@0xDE57
Last active January 17, 2026 11:29
Show Gist options
  • Select an option

  • Save 0xDE57/7d43ea879c0b12af82c80f142f269796 to your computer and use it in GitHub Desktop.

Select an option

Save 0xDE57/7d43ea879c0b12af82c80f142f269796 to your computer and use it in GitHub Desktop.
misc mitmproxy testing. wipe UA, print headers, kill connection, inject error
from mitmproxy import http
''' dns intercept not working... why?
from mitmproxy import dns
def request(flow: dns.DNSFlow):
print(f"DNS Request: {flow.request.qname}")
def response(flow: dns.DNSFlow):
print(f"DNS Response: {flow.response.qname}")
'''
printHeader = True
printRequestContent = False
printResponseContent = False
clearUserAgent = True
injectError = False
injectReplaceKeyWord = False
killConnection = False
def request(flow: http.HTTPFlow) -> None:
if printHeader:
ua = flow.request.headers["User-Agent"]
print('user: ' + ua)
print(f"Response:\n{flow.request.headers}")
if printRequestContent:
print(flow.request.content)
if killConnection:
print(f"killed request to: {flow.request.pretty_url} ({flow.request.host })")
flow.kill()
return
if clearUserAgent:
flow.request.headers["User-Agent"] = ""
flow.request.headers["x-requested-with"] = ""
# https://issues.chromium.org/issues/40626445
# https://android-developers.googleblog.com/2023/02/improving-user-privacy-by-requiring-opt-in-to-send-x-requested-wih-header-from-webview.html
def response(flow: http.HTTPFlow) -> None:
response_body = flow.response.get_text()
if printResponseContent:
print(f"Response: {flow.response.status_code}")
try:
print(f"Response Body:\n{response_body}")
except UnicodeDecodeError:
print("Failed to decode response body as text.")
if injectReplaceKeyWord:
keyword = "the"
replacement = "REPLACED!" #demo
modified_body = response_body.replace(keyword, replacement)
flow.response.set_text(modified_body)
if injectError:
flow.response.status_code = 400
@0xDE57
Copy link
Author

0xDE57 commented Jan 11, 2026

SSLKEYLOGFILE="$PWD/.mitmproxy/sslkeylogfile.txt" mitmweb -p 9090 --ssl-insecure --scripts /home/user/Desktop/
mitmproxyIntecept.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment