Skip to content

Instantly share code, notes, and snippets.

@clouedoc
Last active December 22, 2024 23:27
Show Gist options
  • Save clouedoc/818e05949717d9cfab9e709b9b05fe7a to your computer and use it in GitHub Desktop.
Save clouedoc/818e05949717d9cfab9e709b9b05fe7a to your computer and use it in GitHub Desktop.
A Mitmproxy script to intercept DNS responses to a given domain.
# This is a Mitmproxy plugin that allows overwriting DNS responses to a given domain.
# Currently, it overwrites requests to thesupermac.local to 10.0.2.2
# There might be some typos -- I'll let this for you to fix; but I can confirm that it works.
# I added some comments to help future users :)
# IMPORTANT: you need to use the Wireguard mode for this to work, as far as I know.
import logging
from ipaddress import IPv4Address
import mitmproxy
from mitmproxy import dns
class OverrideDNS:
def dns_request(self, flow: mitmproxy.dns.DNSFlow):
# The DNS address we want to intercept.
interception_url = "thesupermac.local"
# There are multiple questions in a DNS request. Well, it's possible, but almost no software
# actually does it. So, we consider that if we're matching any of the questions of a given request,
# then, we'll inject our custom answer.
# Do note that I'm using the "in" operator, so "aaathesupermac.local" and "thesupermac.local" will match
# equally since they both contain "thesupermac.local". You could change this to a "==" operator if you
# wanted to be more precise.
if any(interception_url in q.name for q in flow.request.questions):
# flow.request.succeed(...) crafts a DNS response, of type mitmproxy.dns.Message.
# We then set it to flow, which is of type mitmproxy.dns.DNSFlow
flow.response = flow.request.succeed(
[
# In DNS, an "A" answer corresponds to an IPv4 address.
# AAAA, for instance, matches a domain name to an IPv6 address
dns.ResourceRecord.A(
interception_url, IPv4Address("10.0.2.2"), ttl=10
)
]
)
logging.info("Overwrite DNS response") # Log on the console to help you debug the script
flow.comment = "Overwrote by OverrideAP" # Put a comment on the flow on Mitmweb
flow.marked = ":black_nib:" # Show a black pen emoj next to the response on Mitmweb
# make sure to include this line below so that Mitmproxy recognizes your addon
addons = [OverrideDNS()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment