Skip to content

Commit 7093e50

Browse files
Add example addon for DNS flows (mitmproxy#7973)
* add example addon for DNS flows * [autofix.ci] apply automated fixes * fix mypy --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a358d28 commit 7093e50

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
([#7933](https://github.com/mitmproxy/mitmproxy/pull/7933), @caiquejjx, @mhils)
2121
- Fix various issues in infer_content_encoding
2222
([#7928](https://github.com/mitmproxy/mitmproxy/pull/7928), @xu-cheng)
23+
- Add example addon to spoof DNS responses.
24+
([#7973](https://github.com/mitmproxy/mitmproxy/pull/7973), @mhils)
2325
- Gracefully handle decoding of raw binary payloads that previously caused
2426
"Raw cannot decode" or "failed to parse as JSON" errors
2527
([#7940](https://github.com/mitmproxy/mitmproxy/pull/7940), @AdityaPatadiya)

examples/addons/dns-simple.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Spoof DNS responses.
3+
4+
In this example, we fiddle with IPv6 (AAAA) records:
5+
- For example.com, `::1` is returned.
6+
(domain is hosted on localhost)
7+
- For example.org, an NXDOMAIN error is returned.
8+
(domain does not exist)
9+
- For all other domains, return a non-error response without any records.
10+
(domain exists, but has no IPv6 configured)
11+
"""
12+
13+
import ipaddress
14+
import logging
15+
16+
from mitmproxy import dns
17+
18+
19+
def dns_request(flow: dns.DNSFlow) -> None:
20+
q = flow.request.question
21+
if q and q.type == dns.types.AAAA:
22+
logging.info(f"Spoofing IPv6 records for {q.name}...")
23+
if q.name == "example.com":
24+
flow.response = flow.request.succeed(
25+
[
26+
dns.ResourceRecord(
27+
name="example.com",
28+
type=dns.types.AAAA,
29+
class_=dns.classes.IN,
30+
ttl=dns.ResourceRecord.DEFAULT_TTL,
31+
data=ipaddress.ip_address("::1").packed,
32+
)
33+
]
34+
)
35+
elif q.name == "example.org":
36+
flow.response = flow.request.fail(dns.response_codes.NXDOMAIN)
37+
else:
38+
flow.response = flow.request.succeed([])

0 commit comments

Comments
 (0)