• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP利用str_replace防注入的方法_php技巧

php 搞代码 4年前 (2022-01-26) 45次浏览 已收录 0个评论

PHP各种过滤字符函数

   <?php<BR>    /**<BR>    * 安全过滤函数<BR>    *<BR>    * @param $string<BR>    * @return string<BR>    */<BR>    function safe_replace($string) {<BR>    $string = str_replace('%20','',$string);<BR>    $string = str_replace('%27','',$string);<BR>    $string = str_replace('%2527','',$string);<BR>    $string = str_replace('*','',$string);<BR>    $string = str_replace('"','"',$string);<BR>    $string = str_replace("'",'',$string);<BR>    $string = str_replace('"','',$string);<BR>    $string = str_replace(';','',$string);<BR>    $string = str_replace('<','<',$string);<BR>    $string = str_replace('>','>',$string);<BR>    $string = str_replace("{",'',$string);<BR>    $string = str_replace('}','',$string);<BR>    $string = str_replace('','',$string);<BR>    return $string;<BR>    }<BR>    ?></P><P><BR>    <?php<BR>    /**<BR>    * 返回经addslashes处理过的字符串或数组<BR>    * @param $string 需要处理的字符串或数组<BR>    * @return mixed<BR>    */<BR>    function new_addslashes($string) {<BR>    if(!is_array($string)) return addslashes($string);<BR>    foreach($string as $key => $val) $string[$key] = new_addslashes($val);<BR>    return $string;<BR>    }<BR>    ?></P><P><BR>    <?php<BR>    //对请求的字符串进行安全处理<BR>    /*<BR>    $safestep<BR>    0 为不处理,<BR>    1 为禁止不安全HTML内容(javascript等),<BR>    2 完全禁止HTML内容,并替换部份不安全字符串(如:eval(、union、CONCAT(、--、等)<BR>    */<BR>    function StringSafe($str, $safestep=-1){<BR>    $safestep = ($safestep > -1) ? $safestep : 1;<BR>    if($safestep == 1){<BR>    $str = preg_replace("#script:#i", "script:", $str);<BR>    $str = preg_replace("#]*>#isU", '', $str);<BR>    $str = preg_replace("#[ ]{1,}#", ' ', $str);<BR>    return $str;<BR>    }else if($safestep == 2){<BR>    $str = addslashes(htmlspecialchars(stripslashes($str)));<BR>    $str = preg_replace("#eval#i", 'eval', $str);<BR>    $str = preg_replace("#union#i", 'union', $str);<BR>    $str = preg_replace("#concat#i", 'concat', $str);<BR>    $str = preg_replace("#--#", '--', $str);<BR>    $str = preg_replace("#[ ]{1,}#", ' ', $str);<BR>    return $str;<BR>    }else{<BR>    return $str;<BR>    }<BR>    }<BR>    ?></P><P><BR>    <?php<BR>       /**<BR>        +----------------------------------------------------------<BR>        * 输出安全的html,用于过滤危险代码<BR>        +----------------------------------------------------------<BR>        * @access public<BR>        +----------------------------------------------------------<BR>        * @param string $text 要处理的字符串<BR>        * @param mixed $tags 允许的标签列表,如 table|td|th|td<BR>        +----------------------------------------------------------<BR>        * @return string<BR>        +----------------------------------------------------------<BR>        */<BR>       static public function safeHtml($text, $tags = null)<BR>       {<BR>           $text =  trim($text);<BR>           //完全过滤注释<BR>           $text = preg_replace('/<!---ecms -ecms -ecms ?.*-->/','',$text);<BR>           //完全过滤动态代码<BR>           $text =  preg_replace('/<?|?'.'>/','',$text);<BR>           //完全过滤js<BR>           $text = preg_replace('/<script?.*/script>/','',$text);<BR>           $text =  str_replace('[','[',$text);<BR>           $text = str_replace(']',']',$text);<BR>           $text =  str_replace('|','|',$text);<BR>           //过滤换行符<BR>           $text = preg_replace('/ ? /','',$text);<BR>           //br<BR>           $text =  preg_replace('/<br>/i','[br]',$text);<BR>           $text = preg_replace('/([br]s*){10,}/i','[br]',$text);<BR>           //过滤危险的属性,如:过滤on事件lang js<BR>           while(preg_match('/(<]+/i',$text,$mat)){<BR>               $text=str_replace($mat[0],$mat[1],$text);<BR>           }<BR>           while(preg_match('/(<]*)/i',$text,$mat)){<BR>               $text=str_replace($mat[0],$mat[1].$mat[3],$text);<BR>           }<BR>           if( empty($allowTags) ) { $allowTags = self::$htmlTags['allow']; }<BR>           //允许的HTML标签<BR>           $text =  preg_replace('//i','[12]',$text);<BR>           //过滤多余html<BR>           if ( empty($banTag) ) { $banTag = self::$htmlTags['ban']; }<BR>           $text =  preg_replace('//i','',$text);<BR>           //过滤合法的html标签<BR>           while(preg_match('/[^><]*/i',$text,$mat)){<BR>               $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);<BR>           }<BR>           //转换引号<BR>           while(preg_match('/([[^[]]*=s*)("|')([^2=[]]+)2([^[]]*])/i',$text,$mat)){<BR>               $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);<BR>           }<BR>           //空属性转换<BR>  <i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong>         $text =  str_replace('''','||',$text);<BR>           $text = str_replace('""','||',$text);<BR>           //过滤错误的单个引号<BR>           while(preg_match('/[[^[]]*("|')[^[]]*]/i',$text,$mat)){<BR>               $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);<BR>           }<BR>           //转换其它所有不合法的 <BR>           $text =  str_replace('<','<',$text);<BR>           $text = str_replace('>','>',$text);<BR>           $text = str_replace('"','"',$text);<BR>           //反转换<BR>           $text =  str_replace('[','<',$text);<BR>           $text =  str_replace(']','>',$text);<BR>           $text =  str_replace('|','"',$text);<BR>           //过滤多余空格<BR>           $text =  str_replace('  ',' ',$text);<BR>           return $text;<BR>       }<BR>    ?></P><P><BR>    <?php<BR>    function RemoveXSS($val) { <BR>       // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed <BR>       // this prevents some character re-spacing such as  <BR>       // note that you have to handle splits with , , and later since they *are* allowed in some          // inputs <BR>       $val = preg_replace('/([x00-x08,x0b-x0c,x0e-x19])/', '', $val); <BR>       // straight replacements, the user should never need these since they're normal characters <BR>       // this prevents like  <BR>       $search = 'abcdefghijklmnopqrstuvwxyz'; <BR>       $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; <BR>       $search .= '1234567890!@#$%^&*()'; <BR>       $search .= '~`";:?+/={}[]-_|''; <BR>       for ($i = 0; $i < strlen($search); $i++) { <BR>           // ;? matches the ;, which is optional <BR>           // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars <BR>           // @ @ search for the hex values <BR>           $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val);//with a ; <BR>           // @ @ 0{0,7} matches '0' zero to seven times <BR>           $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ; <BR>       } <BR>       // now the only remaining whitespace attacks are , , and  <BR>       $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base'); <BR>       $ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'); <BR>       $ra = array_merge($ra1, $ra2); <BR>       $found = true; // keep replacing as long as the previous round replaced something <BR>       while ($found == true) { <BR>           $val_before = $val; <BR>           for ($i = 0; $i < sizeof($ra); $i++) { <BR>               $pattern = '/'; <BR>               for ($j = 0; $j < strlen($ra[$i]); $j++) { <BR>                   if ($j > 0) { <BR>                       $pattern .= '('; <BR>                       $pattern .= '(&#[xX]0{0,8}([9ab]);)'; <BR>                       $pattern .= '|'; <BR>                       $pattern .= '|(&#0{0,8}([9|10|13]);)'; <BR>                       $pattern .= ')*'; <BR>                   } <BR>                   $pattern .= $ra[$i][$j]; <BR>               } <BR>               $pattern .= '/i'; <BR>               $replacement = substr($ra[$i], 0, 2).''.substr($ra[$i], 2); // add in  to nerf the tag <BR>               $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags <BR>               if ($val_before == $val) { <BR>                   // no replacements were made, so exit the loop <BR>                   $found = false; <BR>               } <BR>           } <BR>       } <BR>       return $val; <BR>    }<BR>    ?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP利用str_replace防注入的方法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址