
Demo: https://diafygi.github.io/webrtc-ips/
데모: https://diafygi.github.io/webrtc-ips/
Firefox와 Chrome은 사용자의 로컬 및 공용 IP 주소를 반환하는 STUN 서버 요청을 허용하는 WebRTC를 구현했습니다. 이러한 요청 결과는 javascript에서 사용할 수 있으므로 이제 javascript에서 사용자의 로컬 및 공용 IP 주소를 얻을 수 있습니다. 이 데모는 그 구현 예시입니다.
추가적으로, 이러한 STUN 요청은 일반적인 XMLHttpRequest 절차 외부에서 이루어지므로 개발자 콘솔에 표시되지 않으며 AdBlockPlus나 Ghostery와 같은 플러그인으로 차단할 수도 없습니다. 이는 광고주가 와일드카드 도메인으로 STUN 서버를 설정할 경우 이러한 유형의 요청을 온라인 추적에 사용할 수 있게 만듭니다.
다음은 STUN 요청을 수행하는 주석이 달린 데모 함수입니다. 이를 Firefox 또는 Chrome 개발자 콘솔에 복사하여 붙여넣고 테스트를 실행할 수 있습니다.
//get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {};
//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;
//bypass naive webrtc blocking using an iframe
if(!RTCPeerConnection){
//NOTE: you need to have an iframe in the page right above the script tag
//
//
//<script>...getIPs called in here...
//
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate){
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_regex.exec(candidate)[1];
//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
//listen for candidate events
pc.onicecandidate = function(ice){
//skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);
};
//create a bogus data channel
pc.createDataChannel("");
//create an offer sdp
pc.createOffer(function(result){
//trigger the stun server request
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
//wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});