Stored Cross Site Scripting (XSS)
跨站腳本為了避免與css混淆,稱之為XSS,這是存盤型的XSS漏洞,這是XSS漏洞中危害性最高的一種
利用網站沒有對用戶提交資料進行轉義處理或者過濾不足的缺點,進而添加一些代碼,嵌入到web頁面中去,使別的用戶訪問都會執行相應的嵌入代碼,
而代碼所執行可以是Js腳本,危害多大可以說取決于腳本寫的有多精妙,一般可以盜取用戶身份資訊,或者執行惡意代碼等,
存盤型又稱為持久型跨站點腳本,它一般發生在XSS攻擊向量(一般指XSS攻擊代碼)存盤在網站資料庫,當一個頁面被用戶打開的時候執行,每當用戶打開瀏覽器,腳本執行,持久的XSS相比非持久性XSS攻擊危害性更大,因為每當用戶打開頁面,查看內容時腳本將自動執行,谷歌的orkut曾經就遭受到XSS,
?Low
<?php if( isset( $_POST[ 'btnSign' ] ) ) { // Get input $message = trim( $_POST[ 'mtxMessage' ] ); $name = trim( $_POST[ 'txtName' ] ); // Sanitize message input $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(); } ?>
看看幾個函式的用法
trim(string,charlist)移除string字符兩側的預定義字符
引數 描述 string 必需,規定要檢查的字串,
charlist 可選,規定從字串中洗掉哪些字符
stripslashes(tring)去掉string字符的反斜杠\
這里只是對資料庫中作了防護,但是卻沒有XSS防護,我們嘗試輸入
payload:<script>alert('xss')</script>

注入成功,有意思的是我現在正常留言也會出現彈窗,也就是說攻擊代碼注入了資料庫
每次有用戶正常使用都會執行XSS代碼,如果攻擊者做的精妙,那么用戶在不知覺中資訊就被盜取了

我們看一下資料庫里面,果然這些資料已經存盤在資料庫中了

還是刪掉插入的資料

?Meduim
<?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 = 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(); } ?>
strip_tags()函式剝去字串中的HTML、XML以及PHP的標簽
string 必需,規定要檢查的字串,
allow 可選,規定允許的標簽,這些標簽不會被洗掉,
PHP addslashes() 函式
addslashes() 函式回傳在預定義字符之前添加反斜杠的字串,
預定義字符是:
單引號(')
雙引號(")
反斜杠(\)
NULL

htmlspecialchars(string,flags,character-set,double_encode)
把預定義的字符轉換為 HTML 物體
預定義的字符是:
& (和號)成為 &
“ (雙引號)成為 "
‘ (單引號)成為 ‘
< (小于)成為 <
> (大于)成為 >
因為使用了strip_tages 和htmlspecialchars函式對于message變數幾乎過濾了所有的XSS,但是對于name變數作的過濾就不嚴格了
只是過濾了<script>標簽,可以利用

但是這里應該是輸入有限制,我們在審查元素里可以調大一些或者用burp抓包直接改

payload:<<script>script>alert('xss')</script>

這里也可以用其他標簽來寫了,比如img body等.
?High
<?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 = htmlspecialchars( $message ); // Sanitize name input $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $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(); } ?>
看原始碼這里也只是用正則匹配完全過濾掉name的script標簽而已
paylaod:<img src=https://www.cnblogs.com/byErichas/p/1 one rror=alert('xss')>

攻擊成功!!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/285635.html
標籤:其他
