国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

整理php防注入和XSS攻擊通用過(guò)濾

2024-05-04 23:39:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

現(xiàn)在很多網(wǎng)站都存在跨站腳本攻擊漏洞,讓黑客有機(jī)可乘.跨站攻擊很容易就可以構(gòu)造,而且非常隱蔽,不易被查覺(jué)(通常盜取信息后馬上跳轉(zhuǎn)回原頁(yè)面)。如何攻擊,在此不作介紹,主要談?wù)勅绾畏婪丁?/p>

對(duì)網(wǎng)站發(fā)動(dòng)XSS攻擊的方式有很多種,僅僅使用php的一些內(nèi)置過(guò)濾函數(shù)是對(duì)付不了的,即使你將filter_var,mysql_real_escape_string,htmlentities,htmlspecialchars,strip_tags這些函數(shù)都使用上了也不一定能保證絕對(duì)的安全。

那么如何預(yù)防 XSS 注入?主要還是需要在用戶數(shù)據(jù)過(guò)濾方面得考慮周全,在這里不完全總結(jié)下幾個(gè) Tips

1. 假定所有的用戶輸入數(shù)據(jù)都是“邪惡”的

2. 弱類(lèi)型的腳本語(yǔ)言必須保證類(lèi)型和期望的一致

3. 考慮周全的正則表達(dá)式

4. strip_tags、htmlspecialchars 這類(lèi)函數(shù)很好用

5. 外部的 Javascript 不一定就是可靠的

6. 引號(hào)過(guò)濾必須要重點(diǎn)注意

7. 除去不必要的 HTML 注釋

8. Exploer 求你放過(guò)我吧……

方法一,利用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沒(méi)有區(qū)別,都可以達(dá)到目的.但是,中文情況下, htmlentities卻會(huì)轉(zhuǎn)化所有的html代碼,連同里面的它無(wú)法識(shí)別的中文字符也給轉(zhuǎn)化了。

htmlentities和htmlspecialchars這兩個(gè)函數(shù)對(duì) '之類(lèi)的字符串支持不好,都不能轉(zhuǎn)化, 所以用htmlentities和htmlspecialchars轉(zhuǎn)化的字符串只能防止XSS攻擊,不能防止SQL注入攻擊.

所有有打印的語(yǔ)句如echo,print等 在打印前都要使用htmlentities() 進(jìn)行過(guò)濾,這樣可以防止Xss,注意中文要寫(xiě)出htmlentities($name,ENT_NOQUOTES,GB2312) 。

方法二,什么也不多說(shuō)我們給一個(gè)函數(shù)

例子

 

 
  1. function xss_clean($data){ 
  2. // Fix &entity\n; 
  3. $data=str_replace(array('&','<','>'),array('&amp;','&lt;','&gt;'),$data); 
  4. $data=preg_replace('/(&#*\w+)[\x00-\x20]+;/u','$1;',$data); 
  5. $data=preg_replace('/(&#x*[0-9A-F]+);*/iu','$1;',$data); 
  6. $data=html_entity_decode($data,ENT_COMPAT,'UTF-8'); 
  7. // Remove any attribute starting with "on" or xmlns 
  8. $data=preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu','$1>',$data); 
  9. // Remove javascript: and vbscript: protocols 
  10. $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); 
  11. $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); 
  12. $data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u','$1=$2nomozbinding...',$data); 
  13. // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> 
  14. $data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i','$1>',$data); 
  15. $data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i','$1>',$data); 
  16. $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); 
  17. // Remove namespaced elements (we do not need them) 
  18. $data=preg_replace('#</*\w+:\w[^>]*+>#i','',$data); 
  19. do{// Remove really unwanted tags 
  20. $old_data=$data
  21. $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); 
  22. }while($old_data!==$data); 
  23. // we are done... 
  24. return $data

方法三:

 

 
  1. <?php 
  2. //php防注入和XSS攻擊通用過(guò)濾.  
  3. //by qq:831937 
  4. $_GET && SafeFilter($_GET); 
  5. $_POST && SafeFilter($_POST); 
  6. $_COOKIE && SafeFilter($_COOKIE); 
  7.  
  8. function SafeFilter (&$arr)  
  9.  
  10. $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/'); 
  11.  
  12. if (is_array($arr)) 
  13. foreach ($arr as $key => $value)  
  14. if (!is_array($value)) 
  15. if (!get_magic_quotes_gpc()) //不對(duì)magic_quotes_gpc轉(zhuǎn)義過(guò)的字符使用addslashes(),避免雙重轉(zhuǎn)義。 
  16. $value = addslashes($value); //給單引號(hào)(')、雙引號(hào)(")、反斜線(/)與 NUL(NULL 字符)加上反斜線轉(zhuǎn)義 
  17. $value = preg_replace($ra,'',$value); //刪除非打印字符,粗暴式過(guò)濾xss可疑字符串 
  18. $arr[$key] = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 標(biāo)記并轉(zhuǎn)換為 HTML 實(shí)體 
  19. else 
  20. SafeFilter($arr[$key]); 
  21. ?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丽江市| 资中县| 石家庄市| 垫江县| 高淳县| 扎鲁特旗| 克拉玛依市| 锡林郭勒盟| 杭锦后旗| 尤溪县| 泗阳县| 淮阳县| 泸西县| 穆棱市| 安吉县| 开封市| 遵义县| 奉节县| 淅川县| 定远县| 宜川县| 兴山县| 泰和县| 寿阳县| 上栗县| 阿克陶县| 长顺县| 遵义县| 崇义县| 普兰县| 新巴尔虎右旗| 桦甸市| 东台市| 成都市| 双峰县| 普格县| 阳谷县| 仙居县| 资中县| 嵊泗县| 娄底市|