#1
刘邦
· 2026-05-30 08:26:12
stalltrix
随便水一贴,WebRTC问题都是老话题了,其他地方都说了很多次。但是感觉大多数都混淆了一个概念,远程WebRTC以及本地WebRTC泄露。
本地WebRTC泄露
这个就是获取到本地的局域网的IP地址(本机IP),比如下面这种:
```
192.168.x.x
10.x.x.x
172.16.x.x
```
WebRTC实际上是peer2peer通信的,双方建立,规范就是交换本地地址。实际上是本地网卡地址,不过如果有NAT实际上会使用实际地址,本地地址用不上,但是也交换泄露出去了。
这种情况下,可能会被搜集本地设备隐私信息,于是很多国外隐私扩展都做了本地WebRTC泄露。后来甚至浏览器厂商也下场做这件事,比如:
obfuscate LAN addresses by mDNS: Firefox,Chromium
远程WebRTC泄露
这种是泄露本机的出网IP地址(公网IP),实际上的原理就是通过 STUN 服务器查询得到公网出口IP,STUN 服务器就知道你的公网IP。
这种情况下其实并不算什么隐私泄露,因为你访问网站,也会提供IP地址,WebRTC也提供,正常情况下是一样的。
于是国外的很多隐私扩展都认为这种不算WebRTC泄露,阻拦可能影响部分应用的P2P通信
但是!
如果是浏览器设置了代理,那么就会变得不一样了。准确来说,很多代理无法正确代理设计目标为P2P通信的WebRTC。
于是出现一个情况,请求服务器得到的是代理IP。WebRTC请求的得到是却是真实IP。这种情况下就是“远程WebRTC泄露真实IP地址”
给个简单的本地油猴脚本,使用STUN 获取IP地址,打开example.com就能使用。会打印STUN返回的IP地址,如果没有输出任何IP地址就是没有泄露
```js
// ==UserScript==
// @name WebRTC Leak Test
// @namespace http://tampermonkey.net/
// @version 2026-05-30
// @description try to take over the world!
// @author You
// @match https://example.com/
// @icon https://www.google.com/s2/favicons?sz=64&domain=example.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
document.documentElement.innerHTML = `
<html>
<head>
<title>WebRTC Leak Test</title>
<style>
body{
background:#111;
color:#0f0;
font-family:monospace;
padding:40px;
}
input,button{
padding:8px;
margin:5px;
background:#000;
color:#0f0;
border:1px solid #0f0;
}
#result{
margin-top:20px;
white-space:pre-wrap;
}
</style>
</head>
<body>
<h2>WebRTC Leak Test</h2>
<div>
STUN Server:
<input id="stun" style="width:320px">
<button id="save">Save</button>
</div>
<button id="test">Start Test</button>
<div id="result"></div>
</body>
</html>
`;
let stunServer = localStorage.getItem("webrtc_stun") || "stun:stun.l.google.com:19302";
document.getElementById("stun").value = stunServer;
document.getElementById("save").onclick = () => {
stunServer = document.getElementById("stun").value.trim();
localStorage.setItem("webrtc_stun", stunServer);
alert("Saved");
};
document.getElementById("test").onclick = startTest;
function startTest(){
const result = document.getElementById("result");
result.textContent = "Testing WebRTC...\n";
const seen = new Set();
const pc = new RTCPeerConnection({
iceServers:[{urls:stunServer}]
});
pc.createDataChannel("test");
pc.createOffer().then(o=>pc.setLocalDescription(o));
pc.onicecandidate = e=>{
if(!e.candidate) return;
const cand = e.candidate.candidate;
const ipMatch = cand.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9:]+\.local|[a-f0-9:]+)/i);
const typeMatch = cand.match(/typ ([a-z]+)/);
if(!ipMatch) return;
const ip = ipMatch[1];
const type = typeMatch ? typeMatch[1] : "unknown";
const key = ip + type;
if(seen.has(key)) return;
seen.add(key);
result.textContent += "IP: " + ip + " | type: " + type + "\n";
};
setTimeout(()=>{
result.textContent += "\nTest finished.";
},4000);
}
})();
```
其实有很多WebRTC泄露检查网站可以用的,但是如果信不过第三方的话,上面这个浏览器本地检查方法也可以用。这种唯一知道你IP的只有STUN服务器。
很多解决WebRTC泄露的方法其实就两个:1.禁用WebRTC,2.开tun代理WebRTC(代理udp)。
大多数原理其实大差不差的就这两个。很多浏览器扩展或者指纹浏览器,所谓解决WebRTC泄露不外乎就是禁用WebRTC。