什麼XSS攻擊?PHP防止XSS攻擊函數

XSS 全稱爲 Cross Site Scripting,用戶在表單中有意或無心輸入一些惡意字符,從而破壞頁面的表現!javascript

看看常見的惡意字符XSS 輸入:php

1.XSS 輸入一般包含 JavaScript 腳本,如彈出惡意警告框:<script>alert("XSS");</script>html

2.XSS 輸入也多是 HTML 代碼段,譬如:java

(1).網頁不停地刷新 <meta http-equiv="refresh" content="0;">安全

(2).嵌入其它網站的連接 <iframe src=http://xxxx width=250 height=250></iframe>xss

 

防止XSS攻擊測試路徑:
什麼XSS攻擊?PHP防止XSS攻擊函數
其實有不少測試XSS攻擊的工具:

  • Paros proxy (http://www.parosproxy.org)
  • Fiddler (http://www.fiddlertool.com/fiddler)
  • Burp proxy (http://www.portswigger.net/proxy/)
  • TamperIE (http://www.bayden.com/dl/TamperIESetup.exe)

對於PHP開發者來講,如何去防範XSS攻擊呢?( php防止xss攻擊的函數

能夠用以下函數:

<?PHP
/**
 * @blog http://www.phpddt.com
 * @param $string
 * @param $low 安全別級低
 */
function clean_xss(&$string, $low = False)
{
if (! is_array ( $string ))
{
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low)
{
return True;
}
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
$no = '/%0[0-8bcef]/';
$string = preg_replace ( $no, '', $string );
$no = '/%1[0-9a-f]/';
$string = preg_replace ( $no, '', $string );
$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
$string = preg_replace ( $no, '', $string );
return True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key )
{
clean_xss ( $string [$key] );
}
}
//just a test
$str = 'phpddt.com<meta http-equiv="refresh" content="0;">';
clean_xss($str); //若是你把這個註釋掉,你就知道xss攻擊的厲害了
echo $str;
?>
 
經過clean_xss()就過濾了惡意內容!
相關文章
相關標籤/搜索