DEV Community

Cover image for Detecting WebRTC IP leaks in 3 seconds: ICE candidate harvesting, mDNS obfuscation and the regexes we regret
404 vpn team
404 vpn team

Posted on

Detecting WebRTC IP leaks in 3 seconds: ICE candidate harvesting, mDNS obfuscation and the regexes we regret

Our WebRTC leak test is about 60 lines of JavaScript. This is a walkthrough of those lines, the RFCs behind them, and the edge cases we know it gets wrong.
Snippets are lightly simplified from the production code.

The threat model in one paragraph

A page can create an RTCPeerConnection, point it at a STUN server and read the ICE candidates the browser gathers, all without a permission prompt. A server-reflexive (srflx) candidate carries the public transport address the STUN server observed in your Binding request (RFC 8489). If the VPN client doesn't capture that UDP flow, the observed address is your ISP-assigned IP, not the tunnel exit. That's the "leak".

The harvesting loop

const pc = new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] });
pc.createDataChannel(''); // no m-line, no gathering: we need at least one transceiver or channel
pc.onicecandidate = (event) => {
if (!event.candidate) return; // null candidate = end-of-candidates (trickle ICE done)
const cand = event.candidate.candidate; // raw "candidate:..." SDP attribute line
// classify (below)
};
pc.createOffer().then((offer) => pc.setLocalDescription(offer)).catch(() => {});
Notes from the trenches:

  • createDataChannel('') is load-bearing. Without a data channel or a transceiver the offer has no media section and the ICE agent never starts gathering.- setLocalDescription is what actually kicks off gathering; createOffer alone does nothing observable.- We don't parse the candidate grammar (RFC 8839 ยง5.1). We regex it. More on that regret later.## mDNS: the browser already lies to us (good) Since ~2019, Chromium and WebKit replace host candidates' private IPs with a random UUID.local hostname (draft-ietf-mmusic-mdns-ice-candidates). So a host candidate looks like: candidate:1 1 udp 2122260223 4f1d9a6e-....-....-....-............local 54321 typ host We detect it and report it as "local, hidden by mDNS", not as a leak: const MDNS_RE = /[a-f0-9-]{36}.local/i; // 36 = canonical UUID string length if (MDNS_RE.test(cand)) { addRow(cand.match(MDNS_RE)[0], 'local (hidden by mDNS)'); return; } ## Classifying IPv4: RFC 1918 and friends const IPV4_RE = /\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b/g;

function isPrivate(ip) {
return /^10./.test(ip) // RFC 1918
|| /^192.168./.test(ip) // RFC 1918
|| /^172.(1[6-9]|2\d|3[0-1])./.test(ip) // RFC 1918, 172.16.0.0/12
|| /^127./.test(ip) // loopback
|| /^169.254./.test(ip); // link-local, RFC 3927
}
Anything that matches the IPv4 regex and isn't private gets labelled "public IP" and flips the verdict to found.

The 3-second timeout

setTimeout(() => { pc.close(); /* verdict */ }, 3000);
Why not wait for the null end-of-candidates event? Because on some networks STUN Binding transactions retransmit per RFC 8489's RTO schedule and gathering can take noticeably longer. Three seconds is a UX trade-off: almost every srflx candidate we've seen arrives well inside that window.

Known limitations (a.k.a. the regret section)

  • CGNAT is misclassified. 100.64.0.0/10 (RFC 6598) isn't in isPrivate, so a carrier-grade NAT address would show up as "public". It's not your globally routable IP, but it's also not nothing.- IPv6 candidates aren't classified. We declare an IPV6_RE but only run IPV4_RE over candidates. A leaky IPv6 srflx would currently pass silently. (We have a separate IPv6 leak test, but this one should handle it too.)- Regex over grammar. An IPv4-looking token anywhere in the candidate line counts. In practice the raddr related-address field of an srflx candidate also contains an IP, which is fine for leak detection but sloppy.- The private regex is prefix-based, so 10.foo would match. It can't happen in a valid candidate, but it's the kind of thing that makes reviewers twitch.- No TURN. We only test STUN. A relay candidate would reveal the TURN server, not you, so it's out of scope.## How to read the result
  • VPN off, public IP shown: expected.- VPN on, the VPN exit IP shown: fine.- VPN on, your ISP IP shown: leak. Fix it in the VPN client (full tunnel, UDP included) first; browser flags second.Disclosure: I work on 404 VPN. The tool is free, no sign-up, runs client-side: https://404vpn.io/en/tools/webrtc-leak-test. PRs-in-spirit welcome: if you'd fix one of the limitations above differently, tell me how in the comments.

Top comments (0)