什麼XSS***?PHP防止XSS***函數

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

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

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

2.XSS 輸入也可能是 HTML 代碼段,譬如:

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

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


防止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***的函數)

可以用如下函數:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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()就過濾了惡意內容!          
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章