php防注入和XSS攻擊通用過濾

對(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

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

  2. 弱類型的腳本語言必須保證類型和期望的一致

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

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

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

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

  7. 除去不必要的 HTML 注釋

  8. 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í)候htmlentitieshtmlspecialchars沒有區(qū)別,都可以達(dá)到目的.但是,中文情況下,

htmlentities卻會(huì)轉(zhuǎn)化所有的html代碼,連同里面的它無法識(shí)別的中文字符也給轉(zhuǎn)化了。

htmlentitieshtmlspecialchars這兩個(gè)函數(shù)對(duì) '之類的字符串支持不好,都不能轉(zhuǎn)化, 所以用htmlentitieshtmlspecialchars轉(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('&amp;','&lt;','&gt;'),$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]);
        }
     }
   }
}
?>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 對(duì)網(wǎng)站發(fā)動(dòng)XSS攻擊的方式有很多種,僅僅使用php的一些內(nèi)置過濾函數(shù)是對(duì)付不了的,即使你將filter_var,m...
    dreamer_lk閱讀 7,787評(píng)論 0 6
  • (一)XSS跨站腳本攻擊 參考:XSS站腳本攻擊 (1)XSS簡(jiǎn)介 XSS攻擊全稱跨站腳本攻擊(Cross Sit...
    肆意咯咯咯閱讀 3,729評(píng)論 4 3
  • DVWA實(shí)踐 Web漏洞原理 1. DVWA環(huán)境搭建 Warmpserver+DVWA 2. Brute Forc...
    JasonChiu17閱讀 3,952評(píng)論 0 19
  • 之前積累了XSS 有一段時(shí)間,因?yàn)槟壳伴_始了一件有趣的工程,需要整合非常多的知識(shí),其中Web 安全這一塊出現(xiàn)最多的...
    刀背藏身閱讀 9,561評(píng)論 0 16
  • 說到商業(yè)創(chuàng)新,從0到1,必須提到的一個(gè)人就是彼得·蒂爾,畢業(yè)于斯坦福法學(xué)院。他是paypal的創(chuàng)始人、Fac...
    A小蝸牛閱讀 773評(píng)論 2 0

友情鏈接更多精彩內(nèi)容