存儲型XSSphp
攻擊者事先將惡意代碼上傳或儲存到漏洞服務器中,只要受害者瀏覽包含此惡意代碼的頁面就會執行惡意代碼。這就意味着只要訪問了這個頁面的訪客,都有可能會執行這段惡意腳本,所以儲存型XSS的危害會更大。由於存儲型XSS的代碼存在於網頁的代碼中,能夠說是永久型的。存儲型 XSS 通常出如今網站留言、評論、博客日誌等交互處,惡意腳本存儲到客戶端或者服務端的數據庫中。html
DVWA中存儲型XSS分四個等級, 分別是low、middle、high、impossiblemysql
首先有些標籤限制了輸入長度,能夠修改標籤裏面maxlength屬性修改輸入限制sql
如:數據庫
Low:服務器
先觀察源碼cookie
這個級別對輸入進行了部分特殊字符校驗,沒有對script標籤校驗。使用mysqli_real_escape_string轉義部分特殊字符,影響\x00、\n、\r、\、'、"、\x1a。dom
能夠看到最終是存儲到mysql數據庫中函數
1 <?php 2 3 if( isset( $_POST[ 'btnSign' ] ) ) { 4 // Get input 5 $message = trim( $_POST[ 'mtxMessage' ] ); 6 $name = trim( $_POST[ 'txtName' ] ); 7 8 // Sanitize message input 9 $message = stripslashes( $message ); 10 // mysqli_real_escape_string轉義部分特殊字符,影響\x00、\n、\r、\、'、"、\x1a 11 $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)) ? "" : "")); 12 13 // Sanitize name input 14 // mysqli_real_escape_string轉義部分特殊字符,影響\x00、\n、\r、\、'、"、\x1a 15 $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)) ? "" : "")); 16 17 // Update database 18 $query = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 19 $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>' ); 20 21 //mysql_close(); 22 } 23 24 ?>
實踐: 也就是說能夠放心使用script注入代碼網站
如注入代碼: <script>alert(document.domain)</script> 獲取域名
Middle:
老規矩先看源碼:
其中message修改了3點:
一、strip_tags()函數剝去字符串中的HTML、XML以及PHP的標籤,但容許使用標籤。
二、addslashes() 函數返回在預約義字符(單引號、雙引號、反斜槓、NULL)以前添加反斜槓的字符串。
三、htmlspecialchars()函數是使用來把一些預約義的字符轉換爲HTML實體,&轉換爲&, "轉換爲", <轉換爲<>, 轉換爲>
而 name 只是簡單的對<script>標籤作了限制,存在注入點
1 <?php 2 3 if( isset( $_POST[ 'btnSign' ] ) ) { 4 // Get input 5 $message = trim( $_POST[ 'mtxMessage' ] ); 6 $name = trim( $_POST[ 'txtName' ] ); 7 8 // Sanitize message input 9 // 函數剝去字符串中的HTML、XML以及PHP的標籤,但容許使用標籤。addslashes() 函數返回在預約義字符(單引號、雙引號、反斜槓、NULL)以前添加反斜槓的字符串。 10 $message = strip_tags( addslashes( $message ) ); 11 $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)) ? "" : "")); 12 // &轉換爲&, "轉換爲", <轉換爲<>, 轉換爲> 13 $message = htmlspecialchars( $message ); 14 15 // Sanitize name input 16 // 簡單過濾了<script>標籤,存在注入點 17 $name = str_replace( '<script>', '', $name ); 18 $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)) ? "" : "")); 19 20 // Update database 21 $query = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 22 $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>' ); 23 24 //mysql_close(); 25 } 26 27 ?>
實踐:
一、將name標籤中maxlength屬性改大(原10個字符長度, 修改成100)
二、構造js代碼<scr<script>ipt>alert(document.cookie)</script> 或者<ScriPt>alert(document.cookie)</script>, 多重標籤或大小寫繞過
High:
老規矩先看源碼:
message 的代碼與middle級別一致,沒作修改
name 也對<script>標籤嚴格過濾,可是其餘標籤沒作過濾。畢竟其餘標籤也是能夠寫js的
1 <?php 2 3 if( isset( $_POST[ 'btnSign' ] ) ) { 4 // Get input 5 $message = trim( $_POST[ 'mtxMessage' ] ); 6 $name = trim( $_POST[ 'txtName' ] ); 7 8 // Sanitize message input 9 // 函數剝去字符串中的HTML、XML以及PHP的標籤,但容許使用標籤。 10 $message = strip_tags( addslashes( $message ) ); 11 $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)) ? "" : "")); 12 // &轉換爲&, "轉換爲", <轉換爲<>, 轉換爲> 13 $message = htmlspecialchars( $message ); 14 15 // Sanitize name input 16 // 對script作了嚴格校驗 17 $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $name ); 18 $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)) ? "" : "")); 19 20 // Update database 21 $query = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 22 $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>' ); 23 24 //mysql_close(); 25 } 26 27 ?>
一、將name標籤中maxlength屬性改大(原10個字符長度, 修改成100)
二、構造標籤代碼<img > <iframe>等, 例如:<img src=1 onerror="alert(document.cookie)" style="display:none" /> 和<iframe src=1 onload="alert(document.cookie)" style="display:none" />。 style中display:none 爲不顯示,能夠下降被發現的機率。
Impossible:
查看源碼:
message和name的校驗代碼與middle級別修改一致。暫沒法注入。
若是有哪位老兄想到了方法,還請不吝在評論留言。