TRR (DoH) RFC1918 addresses protection bypass using IPv4-mapped address
Firefox's Trusted Recursive Resolver blocks private (RFC1918) addresses returned by DNS to stop LAN-targeting attacks such as DNS rebinding. The reporter found that an IPv4-mapped IPv6 address returned by the DoH resolver is not recognised as local, so the browser still connects to the private IPv4 address.
Originally disclosed on Mozilla
- Fixed in 14 days
- Paid in 20 days
- Disclosed after 14 days
Summary
When using Firefox with Trusted Recursive Resolver (TRR), private IP addresses are
rejected by default (network.trr.allow-rfc1918=false). This protection can prevent
browser-based attacks of machines located on the LAN such as DNS-rebinding attacks.
However, this protection can be bypassed by using a IPv4-mapped IPV6 address
(eg. ::ffff:192.168.1.254).
This has been tested with Firefox Nightly 84.0a1 (2020-10-21) (64 bits) on Debian testing.
Example
Let's create some domain names records:
$ dig @1.1.1.1 +short A wat4.urdhr.fr
::ffff:192.168.1.254
$ dig @1.1.1.1 +short AAAA wat6.urdhr.fr
::ffff:192.168.1.254
Let's check the are actually resolved by the DoH resolver we are using:
import dns.query
import dns.message
resolver = "https://cloudflare-dns.com/dns-query"
dns.query.https(dns.message.make_query("wat4.urdhr.fr", "A"), resolver).answer
# => [<DNS wat4.urdhr.fr. IN A RRset: [<192.168.1.254>]>]
dns.query.https(dns.message.make_query("wat6.urdhr.fr", "AAAA"), resolver).answer
# => [<DNS wat6.urdhr.fr. IN AAAA RRset: [<::ffff:192.168.1.254>]>]
When using TRR and network.trr.allow-rfc1918=false,
-
http://wat4.urdhr.fr is not reachable in TRR mode because the private IP address is rejected by Firefox;
-
http://wat6.urdhr.fr however, is reachable (assuming a web server is running on this IP address and port).
As far as I understand, the process is as follow:
- the name is resolved to IPv6 ffff:192.168.1.254;
- this IPv6 address is not considered a private IP address by
NetAddr::IsIPAddrLocal(); - this IPv6 address generates a
AF_INET6address inDOHresp::Add(); - this
AF_INET6is passed to kernel; - kernel connects to 192.168.1.256.
Code snippets
NetAddr derivation from DNS bytes:
nsresult DOHresp::Add(uint32_t TTL, unsigned char* dns, unsigned int index,
uint16_t len, bool aLocalAllowed) {
NetAddr addr;
if (4 == len) {
// IPv4
addr.inet.family = AF_INET;
addr.inet.port = 0; // unknown
addr.inet.ip = ntohl(get32bit(dns, index));
} else if (16 == len) {
// IPv6
addr.inet6.family = AF_INET6;
addr.inet6.port = 0; // unknown
addr.inet6.flowinfo = 0; // unknown
addr.inet6.scope_id = 0; // unknown
for (int i = 0; i < 16; i++, index++) {
addr.inet6.ip.u8[i] = dns[index];
}
} else {
return NS_ERROR_UNEXPECTED;
}
if (addr.IsIPAddrLocal() && !aLocalAllowed) {
return NS_ERROR_FAILURE;
}
// While the DNS packet might return individual TTLs for each address,
// we can only return one value in the AddrInfo class so pick the
// lowest number.
if (mTtl < TTL) {
mTtl = TTL;
}
if (LOG_ENABLED()) {
char buf[128];
addr.ToStringBuffer(buf, sizeof(buf));
LOG(("DOHresp:Add %s\n", buf));
}
mAddresses.AppendElement(addr);
return NS_OK;
}
Checking if IP address is private:
bool NetAddr::IsIPAddrLocal() const {
const NetAddr* addr = this;
// IPv4 RFC1918 and Link Local Addresses.
if (addr->raw.family == AF_INET) {
uint32_t addr32 = ntohl(addr->inet.ip);
if (addr32 >> 24 == 0x0A || // 10/8 prefix (RFC 1918).
addr32 >> 20 == 0xAC1 || // 172.16/12 prefix (RFC 1918).
addr32 >> 16 == 0xC0A8 || // 192.168/16 prefix (RFC 1918).
addr32 >> 16 == 0xA9FE) { // 169.254/16 prefix (Link Local).
return true;
}
}
// IPv6 Unique and Link Local Addresses.
if (addr->raw.family == AF_INET6) {
uint16_t addr16 = ntohs(addr->inet6.ip.u16[0]);
if (addr16 >> 9 == 0xfc >> 1 || // fc00::/7 Unique Local Address.
addr16 >> 6 == 0xfe80 >> 6) { // fe80::/10 Link Local Address.
return true;
}
}
// Not an IPv4/6 local address.
return false;
}
Tests
These tests should be able to check the expected behavior (in test_trr.js):
// verify RFC1918 IPv4-mapped address from the server is rejected
add_task(async function test7() {
dns.clearCache(true);
Services.prefs.setIntPref("network.trr.mode", 3); // TRR-only
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${h2Port}/doh?responseIP=::ffff.192.168.0.1`
);
let [, , inStatus] = await new DNSListener(
"rfc1918.example.com",
undefined,
false
);
Assert.ok(
!Components.isSuccessCode(inStatus),
`${inStatus} should be an error code`
);
});
// verify RFC1918 IPv4-mapped address from the server is fine when told so
add_task(async function test8() {
dns.clearCache(true);
Services.prefs.setIntPref("network.trr.mode", 3); // TRR-only
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${h2Port}/doh?responseIP=::ffff.192.168.0.1`
);
Services.prefs.setBoolPref("network.trr.allow-rfc1918", true);
await new DNSListener("rfc1918.example.com", "192.168.0.1");
});
Additional Considerations
It would probably make sense to be able to reject the following networks as well:
- 0.0.0.0/24
- 127.0.0.0/8
- ::1/128
These can be used for DNS-rebinding attacks on the local machine.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the local-address check only inspects the IPv6 prefix of the address and does not recognise IPv4-mapped IPv6 addresses as private IPv4 addresses.
Impact
A malicious DNS answer could make the browser reach devices on the local network, enabling DNS-rebinding-style attacks that the RFC1918 protection was meant to stop.
What the attacker needed
Firefox with TRR enabled; allow-rfc1918 set to false; Attacker controls DNS answers; A service on the LAN target
The fix
The local-address check should treat IPv4-mapped IPv6 addresses as their IPv4 equivalent and also reject loopback and unspecified ranges.
How it was found
- Publish DNS records that return an IPv4-mapped IPv6 address pointing at a private IPv4 address.
- Configure Firefox to use TRR with private addresses disallowed.
- Visit the domain whose A record is the private IPv4 address; it is blocked.
- Visit the domain whose AAAA record is the IPv4-mapped address; the browser connects to the private address.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Firefox has a privacy feature that sends DNS lookups to a remote resolver over HTTPS. To stop a malicious DNS answer from pointing the browser at devices on your home network, Firefox refuses answers that are private addresses. The reporter showed that writing the same private address in a different IPv6 format slips past that check, so the browser still connects to the local device.
What is SSRF?
The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.