竹木人

----------------隐感存理,去伪存真,做个安分的守候者

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

网站终于上线了,用360网站安全扫描神器扫描了下:

xss,sql漏洞是主要的:

sql漏洞主要是因为一个整型变量引起,未过滤

用一下方法过滤掉:$XXX=intval($XXX);

xss漏洞,主要是一些输入变量未过滤,用了一个开源的过滤方法:(不过本身到我这有问题,做了相应修改,搞定了):

转载的方法,特分享如下:

View Code
<?php
// 数据过滤
// 参数:$str - 要过滤的数据
function dFilter($str)
{
    if(strlen($str)==0)
        return $str;
    $str= htmlspecialchars($str);            // html特殊标记转义
    //$str =  strAddslashes($str);            // 添加转义
    $str = str_replace("%", "\%", $str);    // '%'转义
    //$str = nl2br($str);                        // 回车转换
    
    // 过滤一些危险字符(串),\',select,from,where,insert,update,delete,union,into,count,load_file,outfile,drop
    $str = str_replace("\'", "", $str);
    $str = str_replace("select", "s_e_l_e_c_t", $str);
    $str = str_replace("from", "f_r_o_m", $str);
    $str = str_replace("where", "w_h_e_r_e", $str);
    $str = str_replace("insert", "i_n_s_e_r_t", $str);
    $str = str_replace("update", "u_p_d_a_t_e", $str);
    $str = str_replace("delete'", "d_e_l_e_t_e", $str);
    $str = str_replace("union", "u_n_i_o_n", $str);
    $str = str_replace("into", "i_n_t_o", $str);
    $str = str_replace("count'", "c_o_u_n_t", $str);
    $str = str_replace("load_file", "l_o_a_d_f_i_l_e", $str);
    $str = str_replace("outfile", "o_u_t_f_i_l_e", $str);
    $str = str_replace("drop", "d_r_o_p", $str);
    $str = str_replace("<script>", "s_c_r_i_p_t", $str);
    $str = str_replace("</script>", "s_c_r_i_p_t", $str);
    $str = preg_replace( "@<script(.*?)</script>@is", "", $str );
    $str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
    $str = preg_replace( "@<style(.*?)</style>@is", "", $str );
    //echo $str;
    return $str;
}

// 获取参数
// 参数:$pName=参数名称 $pType=参数类型
function Param($pName,$pType='str')
{
    // 获取参数
    if($pName=='')
       return $pType=='str'? '':-1;
    $strParam= trim($pName);
    
    // 过滤参数
    $strParam=dFilter($strParam);
    
    // 判断参数类型
    if($pType=="int") // 如果需要的是整型
    {
      if(is_numeric($strParam))
        return (int)$strParam;
      else
        return -1;
    }
    if($pType=="float") // 如果需要的是浮点型
    {
      if(is_numeric($strParam))
        return (float)$strParam;
      else
        return -1;
    }
    if($pType=="str") // 如果需要的是字符串
      return $strParam;
}


// 干掉xss关键字
function RemoveXSS($val) {  
    // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed  
    // this prevents some character re-spacing such as <java\0script>  
    // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs  
    $val = preg_replace('/([\x00-\x09\x0a-\x0c\x0e-\x19])/', '', $val);
    
    // straight replacements, the user should never need these since they're normal characters  
    // this prevents like <IMG SRC=@avascript:alert('XSS')>  
    $search = 'abcdefghijklmnopqrstuvwxyz'; 
    $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $search .= '1234567890!@#$%^&*()'; 
    $search .= '~`";:?+/={}[]-_|\'\\<></>';
    $search .= 'script'; 
    for ($i = 0; $i < strlen($search); $i++) { 
       // ;? matches the ;, which is optional 
       // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars 
       // @ @ search for the hex values 
       $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ; 
       // @ @ 0{0,7} matches '0' zero to seven times  
       $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ; 
    } 
     
    // now the only remaining whitespace attacks are \t, \n, and \r 
    $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', /*'xml', 'blink', 'link', 'style',*/ 'script',/* 'embed',*/ 'object', 'iframe', 'frame', 'frameset', 'ilayer',/* 'layer',*/ 'bgsound', 'title', 'base','behaviour'); 
    $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); 
    
    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{0,8}([9|10|13]);)'; 
            $pattern .= ')*'; 
         } 
         $pattern .= $ra[$i][$j]; 
       } 
       $pattern .= '/i';

       $replacement = substr($ra[$i], 0, 2).'[notallow]'.substr($ra[$i], 2); // add in <> to nerf the tag  
       $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags  
           
     }  
    return $val;  
 }

?>

具体用法就不说了,自己看喽~~~

posted on 2012-05-24 14:01  竹木人  阅读(581)  评论(0编辑  收藏  举报