PHP利用str_replace()函数防注入
str_replace()函数的使用就是用来替换指定字符了,那么我们正好可以利用它这一点来过滤敏感字符以太到防注入的效果,下面我来给大家总结了一些方法,给大家分享一下.
PHP各种过滤字符函数,代码如下:
- <?php
-
-
-
-
-
-
- function safe_replace($string) {
- $string = str_replace('%20','',$string);
- $string = str_replace('%27','',$string);
- $string = str_replace('%2527','',$string);
- $string = str_replace('*','',$string);
- $string = str_replace('"','"',$string);
- $string = str_replace("'",'',$string);
- $string = str_replace('"','',$string);
- $string = str_replace(';','',$string);
- $string = str_replace('<','<',$string);
- $string = str_replace('>','>',$string);
- $string = str_replace("{",'',$string);
- $string = str_replace('}','',$string);
- $string = str_replace('','',$string);
- return $string;
- }
- ?>
-
-
- <?php
-
-
-
-
-
- function new_addslashes($string) {
- if(!is_array($string)) return addslashes($string);
- foreach($string as $key => $val) $string[$key] = new_addslashes($val);
- return $string;
- }
- ?>
-
-
- <?php
-
-
-
-
-
-
-
- function StringSafe($str, $safestep=-1){
- $safestep = ($safestep > -1) ? $safestep : 1;
- if($safestep == 1){
- $str = preg_replace("#script:#i", "script:", $str);
- $str = preg_replace("#<[/]{0,1}(link|meta|ifr|fra|scr)[^>]*>#isU", '', $str);
- $str = preg_replace("#[ ]{1,}#", ' ', $str);
- return $str;
- }else if($safestep == 2){
- $str = addslashes(htmlspecialchars(stripslashes($str)));
- $str = preg_replace("#eval#i", 'eval', $str);
- $str = preg_replace("#union#i", 'union', $str);
- $str = preg_replace("#concat#i", 'concat', $str);
- $str = preg_replace("#--#", '--', $str);
- $str = preg_replace("#[ ]{1,}#", ' ', $str);
- return $str;
- }else{
- return $str;
- }
- }
- ?>
-
-
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
- static public function safeHtml($text, $tags = null)
- {
- $text = trim($text);
-
- $text = preg_replace('/<!--?.*-->/','',$text);
-
- $text = preg_replace('/<?|?'.'>/','',$text);
-
- $text = preg_replace('/<script?.*/script>/','',$text);
- $text = str_replace('[','[',$text);
- $text = str_replace(']',']',$text);
- $text = str_replace('|','|',$text);
-
- $text = preg_replace('/ ? /','',$text);
-
- $text = preg_replace('/<br(s/)?'.'>/i','[br]',$text);
- $text = preg_replace('/([br]s*){10,}/i','[br]',$text);
-
- while(preg_match('/(<[^><]+)(lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
- $text=str_replace($mat[0],$mat[1],$text);
- }
- while(preg_match('/(<[^><]+)(window.|javascript:|js:|about:|file:|document.|vbs:|cookie)([^><]*)/i',$text,$mat)){
- $text=str_replace($mat[0],$mat[1].$mat[3],$text);
- }
- if( emptyempty($allowTags) ) { $allowTags = self::$htmlTags['allow']; }
-
- $text = preg_replace('/<('.$allowTags.')( [^><[]]*)>/i','[12]',$text);
-
- if ( emptyempty($banTag) ) { $banTag = self::$htmlTags['ban']; }
- $text = preg_replace('/</?('.$banTag.')[^><]*>/i','',$text);
-
- while(preg_match('/<([a-z]+)[^><[]]*>[^><]*</1>/i',$text,$mat)){
- $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
- }
-
- while(preg_match('/([[^[]]*=s*)("|')([^2=[]]+)2([^[]]*])/i',$text,$mat)){
- $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
- }
-
- $text = str_replace('''','||',$text);
- $text = str_replace('""','||',$text);
-
- while(preg_match('/[[^[]]*("|')[^[]]*]/i',$text,$mat)){
- $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
- }
-
- $text = str_replace('<','<',$text);
- $text = str_replace('>','>',$text);
- $text = str_replace('"','"',$text);
-
- $text = str_replace('[','<',$text);
- $text = str_replace(']','>',$text);
- $text = str_replace('|','"',$text);
-
- $text = str_replace(' ',' ',$text);
- return $text;
- }
- ?>
-
-
- <?php
- function RemoveXSS($val) {
-
-
-
- $val = preg_replace('/([x00-x08,x0b-x0c,x0e-x19])/', '', $val);
-
-
- $search = 'abcdefghijklmnopqrstuvwxyz';
- $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $search .= '1234567890!@#$%^&*()';
- $search .= '~`";:?+/={}[]-_|'';
- for ($i = 0; $i < strlen($search); $i++) {
-
-
-
- $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val);
-
- $val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val);
- }
-
- $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
- $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');
- $ra = array_merge($ra1, $ra2);
- $found = true;
- while ($found == true) {
- $val_before = $val;
- for ($i = 0; $i < sizeof($ra); $i++) {
- $pattern = '/';
- for ($j = 0; $j < strlen($ra[$i]); $j++) {
- if ($j > 0) {
- $pattern .= '(';
- $pattern .= '(&#[xX]0{0,8}([9ab]);)';
- $pattern .= '|';
- $pattern .= '|(�{0,8}([9|10|13]);)';
- $pattern .= ')*';
- }
- $pattern .= $ra[$i][$j];
- }
- $pattern .= '/i';
- $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
- $val = preg_replace($pattern, $replacement, $val);
- if ($val_before == $val) {
-
- $found = false;
- }
- }
- }
- return $val;
- }
- ?>
|