對(duì)網(wǎng)站發(fā)動(dòng)XSS攻擊的方式有很多種,僅僅使用php的一些內(nèi)置過濾函數(shù)是對(duì)付不了的,即使你將
filter_var,mysql_real_escape_string,htmlentities,htmlspecialchars,strip_tags這些函數(shù)都使用上了也不一定能保證絕對(duì)的安全。
那么如何預(yù)防 XSS 注入?主要還是需要在用戶數(shù)據(jù)過濾方面得考慮周全,在這里不完全總結(jié)下幾個(gè) Tips
假定所有的用戶輸入數(shù)據(jù)都是“邪惡”的
弱類型的腳本語言必須保證類型和期望的一致
考慮周全的正則表達(dá)式
strip_tags、htmlspecialchars 這類函數(shù)很好用
外部的 Javascript 不一定就是可靠的
引號(hào)過濾必須要重點(diǎn)注意
除去不必要的 HTML 注釋
Exploer 求你放過我吧……
方法一,利用php htmlentities函數(shù)
例子
php防止XSS跨站腳本攻擊的方法:是針對(duì)非法的HTML代碼包括單雙引號(hào)等,使用htmlspecialchars()函數(shù) 。
在使用htmlspecialchars()函數(shù)的時(shí)候注意第二個(gè)參數(shù), 直接用htmlspecialchars($string) 的話,第二個(gè)參數(shù)默認(rèn)是ENT_COMPAT,函數(shù)默認(rèn)只是轉(zhuǎn)化雙引號(hào)(“), 不對(duì)單引號(hào)(‘)做轉(zhuǎn)義.
所以,htmlspecialchars函數(shù)更多的時(shí)候要加上第二個(gè)參數(shù), 應(yīng)該這樣用:
htmlspecialchars(string,ENT_QUOTES).當(dāng)然,如果需要不轉(zhuǎn)化如何的引號(hào),用htmlspecialchars(string,ENT_NOQUOTES).
另外, 盡量少用htmlentities,
在全部英文的時(shí)候htmlentities和htmlspecialchars沒有區(qū)別,都可以達(dá)到目的.但是,中文情況下,
htmlentities卻會(huì)轉(zhuǎn)化所有的html代碼,連同里面的它無法識(shí)別的中文字符也給轉(zhuǎn)化了。
htmlentities和htmlspecialchars這兩個(gè)函數(shù)對(duì) '之類的字符串支持不好,都不能轉(zhuǎn)化, 所以用htmlentities和htmlspecialchars轉(zhuǎn)化的字符串只能防止XSS攻擊,不能防止SQL注入攻擊.
所有有打印的語句如echo,print等 在打印前都要使用htmlentities() 進(jìn)行過濾,這樣可以防止Xss,注意中文要寫出htmlentities($name,ENT_NOQUOTES,GB2312) 。
方法二,什么也不多說我們給一個(gè)函數(shù)
例子
function xss_clean($data){
// Fix &entity\n;
$data=str_replace(array('&','<','>'),array('&','<','>'),$data);
$data=preg_replace('/(&#*\w+)[\x00-\x20]+;/u','$1;',$data);
$data=preg_replace('/(&#x*[0-9A-F]+);*/iu','$1;',$data);
$data=html_entity_decode($data,ENT_COMPAT,'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data=preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu','$1>',$data);
// Remove javascript: and vbscript: protocols
$data=preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2nojavascript...',$data);
$data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2novbscript...',$data);
$data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u','$1=$2nomozbinding...',$data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i','$1>',$data);
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i','$1>',$data);
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu','$1>',$data);
// Remove namespaced elements (we do not need them)
$data=preg_replace('#</*\w+:\w[^>]*+>#i','',$data);
do{// Remove really unwanted tags
$old_data=$data;
$data=preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i','',$data);
}while($old_data!==$data);
// we are done...
return $data;
}
方法三:
<?php
//php防注入和XSS攻擊通用過濾.
//by qq:831937
$_GET && SafeFilter($_GET);
$_POST && SafeFilter($_POST);
$_COOKIE && SafeFilter($_COOKIE);
function SafeFilter (&$arr)
{
$ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/','/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/','/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/','/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/','/onmouseout/','/onmouseover/','/onmouseup/','/onunload/');
if (is_array($arr))
{
foreach ($arr as $key => $value)
{
if (!is_array($value))
{
if (!get_magic_quotes_gpc())//不對(duì)magic_quotes_gpc轉(zhuǎn)義過的字符使用addslashes(),避免雙重轉(zhuǎn)義。
{
$value = addslashes($value); //給單引號(hào)(')、雙引號(hào)(")、反斜線(\)與NUL(NULL字符)加上反斜線轉(zhuǎn)義
}
$value = preg_replace($ra,'',$value); //刪除非打印字符,粗暴式過濾xss可疑字符串
$arr[$key] = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 標(biāo)記并轉(zhuǎn)換為HTML實(shí)體
}
else
{
SafeFilter($arr[$key]);
}
}
}
}
?>