Skip to content

Full Toolkit for Next-Level Domain Analysis

Add IP filtering to prevent DNS-based SSRF attacks 🛡️⛔ (#81)

Felix Wotschofskye90adfdbfb34
diff --git a/lib/resolvers/authoritative.ts b/lib/resolvers/authoritative.ts
index bb27aaa..6489e74 100644
--- a/lib/resolvers/authoritative.ts
+++ b/lib/resolvers/authoritative.ts
@@ -19,6 +19,7 @@ import {
type RecordType,
type ResolverResponse,
} from './base';
+import { isPublicIp } from './ip-filter';
type DnsResponse = {
packet: Packet;
@@ -334,16 +335,31 @@ export class AuthoritativeResolver extends DnsResolver {
return { records, trace: fullTrace };
}
- // TODO Support IPv6
+ // We must validate referrals before following them: an attacker controlling
+ // a delegated zone can otherwise point our recursive query at loopback or
+ // RFC1918 addresses and turn this resolver into an SSRF primitive.
const redirects = [
...(response.authorities || []),
...(response.additionals || []),
- ].filter((answer) => answer.type === 'A' || answer.type === 'NS');
+ ].filter(
+ (answer): answer is StringAnswer =>
+ answer.type === 'A' || answer.type === 'NS',
+ );
if (redirects.length) {
+ // Only trust glue A records that match a delegated NS hostname
+ // (in-bailiwick) and resolve to a publicly routable IP address.
+ const delegatedNsNames = new Set(
+ redirects
+ .filter((r) => r.type === 'NS')
+ .map((r) => r.data.toLowerCase()),
+ );
const aRedirects = redirects.filter(
- (redirect) => redirect.type === 'A',
- ) as StringAnswer[];
+ (redirect) =>
+ redirect.type === 'A' &&
+ delegatedNsNames.has(redirect.name.toLowerCase()) &&
+ isPublicIp(redirect.data),
+ );
if (aRedirects.length) {
return this.fetchRecords({
domain,
@@ -357,9 +373,7 @@ export class AuthoritativeResolver extends DnsResolver {
});
}
- const nsRedirects = redirects.filter(
- (redirect) => redirect.type === 'NS',
- ) as StringAnswer[];
+ const nsRedirects = redirects.filter((redirect) => redirect.type === 'NS');
if (nsRedirects.length) {
const { records: aRecords, trace: subTrace } = await this.fetchRecords({
domain: nsRedirects[0].data,
@@ -367,14 +381,17 @@ export class AuthoritativeResolver extends DnsResolver {
depth: depth + 1,
});
- if (!aRecords.length) {
+ // The resolved NS address is attacker-controlled (they own the zone
+ // and can set any A record); filter to public IPs before using it.
+ const publicARecords = aRecords.filter((r) => isPublicIp(r.data));
+ if (!publicARecords.length) {
throw new Error(`Bad redirects for ${domain}`);
}
return this.fetchRecords({
domain,
recordType,
- nameserver: aRecords[0].data,
+ nameserver: publicARecords[0].data,
trace: [
...trace,
`${recordType} ${domain} @ ${usedNameserver} (${protocol}) -> redirect to ${nsRedirects.map((r) => r.data).join(', ')}`,
diff --git a/lib/resolvers/ip-filter.test.ts b/lib/resolvers/ip-filter.test.ts
new file mode 100644
index 0000000..cf918f6
--- /dev/null
+++ b/lib/resolvers/ip-filter.test.ts
@@ -0,0 +1,123 @@
+import { describe, expect, it } from 'vitest';
+
+import { isPublicIp } from './ip-filter';
+
+describe('isPublicIp', () => {
+ describe('invalid input', () => {
+ it.each([
+ '',
+ 'not-an-ip',
+ '256.0.0.1',
+ '1.2.3',
+ '1.2.3.4.5',
+ 'gggg::',
+ '::g',
+ ])('returns false for %j', (input) => {
+ expect(isPublicIp(input)).toBe(false);
+ });
+ });
+
+ describe('IPv4 private/reserved ranges', () => {
+ it.each([
+ // Unspecified / "this network"
+ '0.0.0.0',
+ '0.1.2.3',
+ // Private RFC1918
+ '10.0.0.1',
+ '10.255.255.255',
+ '172.16.0.1',
+ '172.31.255.255',
+ '192.168.0.1',
+ '192.168.255.255',
+ // Loopback
+ '127.0.0.1',
+ '127.1.2.3',
+ // Link-local
+ '169.254.0.1',
+ '169.254.169.254',
+ // Carrier-grade NAT
+ '100.64.0.1',
+ '100.127.255.254',
+ // Documentation / TEST-NET
+ '192.0.2.1',
+ '198.51.100.1',
+ '203.0.113.1',
+ // Benchmarking
+ '198.18.0.1',
+ '198.19.255.254',
+ // IETF protocol assignments
+ '192.0.0.1',
+ // Multicast
+ '224.0.0.1',
+ '239.255.255.255',
+ // Reserved / broadcast
+ '240.0.0.1',
+ '255.255.255.255',
+ ])('rejects %s', (ip) => {
+ expect(isPublicIp(ip)).toBe(false);
+ });
+ });
+
+ describe('IPv4 public addresses', () => {
+ it.each([
+ '1.1.1.1',
+ '8.8.8.8',
+ '9.9.9.9',
+ '198.41.0.4', // a.root-servers.net
+ '192.5.6.30', // a.gtld-servers.net
+ '172.15.255.255', // just outside private range
+ '172.32.0.1', // just outside private range
+ '192.167.255.255', // just outside private range
+ '100.63.255.255', // just outside CGN range
+ '100.128.0.0', // just outside CGN range
+ '169.253.255.255', // just outside link-local
+ '169.255.0.0', // just outside link-local
+ '223.255.255.255', // just below multicast
+ ])('accepts %s', (ip) => {
+ expect(isPublicIp(ip)).toBe(true);
+ });
+ });
+
+ describe('IPv6 private/reserved ranges', () => {
+ it.each([
+ // Unspecified
+ '::',
+ // Loopback
+ '::1',
+ // IPv4-mapped private addresses
+ '::ffff:127.0.0.1',
+ '::ffff:10.0.0.1',
+ '::ffff:192.168.1.1',
+ // Unique-local
+ 'fc00::1',
+ 'fd12:3456:789a::1',
+ // Link-local
+ 'fe80::1',
+ 'febf:ffff::1',
+ // Multicast
+ 'ff00::1',
+ 'ff02::1',
+ // Discard prefix
+ '100::1',
+ // Documentation
+ '2001:db8::1',
+ '2001:0db8:1234::',
+ // Well-known NAT64
+ '64:ff9b::1.2.3.4',
+ ])('rejects %s', (ip) => {
+ expect(isPublicIp(ip)).toBe(false);
+ });
+ });
+
+ describe('IPv6 public addresses', () => {
+ it.each([
+ '2001:4860:4860::8888', // Google DNS
+ '2606:4700:4700::1111', // Cloudflare DNS
+ '2620:fe::fe', // Quad9
+ '::ffff:8.8.8.8', // IPv4-mapped public
+ 'fbff::1', // just below fc00::/7
+ ])('accepts %s', (ip) => {
+ expect(isPublicIp(ip)).toBe(true);
+ });
+ });
+});
diff --git a/lib/resolvers/ip-filter.ts b/lib/resolvers/ip-filter.ts
new file mode 100644
index 0000000..3196a27
--- /dev/null
+++ b/lib/resolvers/ip-filter.ts
@@ -0,0 +1,46 @@
+import { BlockList, isIP } from 'node:net';
+
+const blocked = new BlockList();
+
+// IPv4 reserved / non-routable ranges
+blocked.addSubnet('0.0.0.0', 8); // "this network"
+blocked.addSubnet('10.0.0.0', 8); // private
+blocked.addSubnet('100.64.0.0', 10); // carrier-grade NAT (RFC 6598)
+blocked.addSubnet('127.0.0.0', 8); // loopback
+blocked.addSubnet('169.254.0.0', 16); // link-local
+blocked.addSubnet('172.16.0.0', 12); // private
+blocked.addSubnet('192.0.0.0', 24); // IETF protocol assignments
+blocked.addSubnet('192.0.2.0', 24); // TEST-NET-1 (documentation)
+blocked.addSubnet('192.168.0.0', 16); // private
+blocked.addSubnet('198.18.0.0', 15); // benchmarking
+blocked.addSubnet('198.51.100.0', 24); // TEST-NET-2 (documentation)
+blocked.addSubnet('203.0.113.0', 24); // TEST-NET-3 (documentation)
+blocked.addSubnet('224.0.0.0', 4); // multicast
+blocked.addSubnet('240.0.0.0', 4); // reserved (incl. 255.255.255.255 broadcast)
+
+// IPv6 reserved / non-routable ranges
+blocked.addAddress('::', 'ipv6'); // unspecified
+blocked.addAddress('::1', 'ipv6'); // loopback
+blocked.addSubnet('64:ff9b::', 96, 'ipv6'); // well-known NAT64
+blocked.addSubnet('100::', 64, 'ipv6'); // discard prefix
+blocked.addSubnet('2001:db8::', 32, 'ipv6'); // documentation
+blocked.addSubnet('fc00::', 7, 'ipv6'); // unique-local
+blocked.addSubnet('fe80::', 10, 'ipv6'); // link-local
+blocked.addSubnet('ff00::', 8, 'ipv6'); // multicast
+
+/**
+ * Returns true only for IP addresses that are public/routable on the Internet.
+ * Rejects loopback, RFC1918 private, link-local, multicast, documentation,
+ * unspecified, and other reserved ranges in both IPv4 and IPv6.
+ *
+ * Used to prevent attacker-controlled DNS referrals from forcing the
+ * authoritative resolver to send DNS traffic to internal hosts.
+ */
+export const isPublicIp = (ip: string): boolean => {
+ const version = isIP(ip);
+ if (!version) return false;
+ if (version === 4) return !blocked.check(ip, 'ipv4');
+ // For IPv6, also apply IPv4 policy to catch IPv4-mapped addresses like
+ // ::ffff:10.0.0.1; BlockList.check coerces them to their IPv4 form.
+ return !blocked.check(ip, 'ipv6') && !blocked.check(ip, 'ipv4');
+};