【白帽子學習筆記13】DVWA 存儲型XSS(跨站點腳本攻擊)
文章目錄
什麼是存儲型XSS
- 長時間存儲在服務器端
- 每次用戶訪問都會被執行javascript腳本
DVWA實戰
安全級別:LOW
1、源碼解析:
<?php
if( isset( $_POST[ 'btnSign' ] ) ) {
// 獲取輸入
$message = trim( $_POST[ 'mtxMessage' ] );
$name = trim( $_POST[ 'txtName' ] );
// 過濾信息
$message = stripslashes( $message );
$message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
// Sanitize name input
$name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
// Update database
$query = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
//mysql_close();
}
?>
函數分析:
1、 trim(string,charlist):移除字符串兩側的空白字符或其他預定義字符,預定義字符包括、\t、\n、\x0B、\r以及空格,可選參數charlist支持添加額外需要刪除的字符;
2、mysql_real_escape_string(string,connection):對字符串中的特殊符號(\x00,\n,\r,\,‘,“,\x1a)進行轉義;
3、stripslashes(string):刪除字符串中的反斜槓。
源碼並沒有對name參數和message參數進行XSS方面的過濾和檢查,並且數據儲存在數據集中,屬於明顯的XSS漏洞;
2、漏洞利用
可以看到當用戶訪問這個頁面的時候也就觸發了,這個js代碼,利用這個漏洞我們還可以完成Cookie獲取,重定向啊等操作
安全級別:Medium
源碼解析:
<?php
if( isset( $_POST[ 'btnSign' ] ) ) {
// Get input
$message = trim( $_POST[ 'mtxMessage' ] );
$name = trim( $_POST[ 'txtName' ] );
// Sanitize message input
$message = strip_tags( addslashes( $message ) );
$message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
// 對message進行html轉義
$message = htmlspecialchars( $message );
// Sanitize name input
$name = str_replace( '<script>', '', $name );
$name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
// Update database
$query = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
//mysql_close();
}
?>
- message參數使用了htmlspecialchars函數進行編碼,因此無法再通過message參數注入XSS代碼
- 對於name參數,這裏是基於黑名單的思想,使用str_replace函數將輸入中的
<script>
刪除,把script腳本當作字符串來處理,仍然存在存儲型的XSS
破解方法
1、大小寫混合<ScRipt>alert("XSS")</ScripT>
2、script嵌套
, <sc<script>ript>
安全級別:High
-
對message參數使用了htmlspecialchars函數進行編碼,因此無法再通過message參數注入XSS代碼;
-
對於name參數,High級別的代碼使用preg_replace() 函數用於正則表達式的搜索和替換,將script前後相關的內容都替換爲空,使得雙寫繞過、大小寫混淆繞過不再有效;(正則表達式中i表示不區分大小寫)
-
雖然在name參數中無法使用
<script>
標籤注入XSS代碼,但是可以通過img、body等標籤事件或者iframe等標籤的src注入惡意的js代碼。
安全等級Impossiable
- 當安全級別爲impossible時,對name、message參數均使用了htmlspecialchars函數進行編碼,因此無法再通過name、message參數注入XSS代碼,不能實現存儲型XSS攻擊。