几个常见的漏洞(xss攻击 cookie未清除 nginx信息泄露)与处理方法

项目在安全检查中发现很多问题,要求整改,其中就有最常见的xss攻击

漏洞描述

渗透测试人员检测到网站筛选框均存在反射型跨站脚本攻击,例如:

"><script>alert(1)</script>

漏洞建议

  1. 对用户输入的数据进行严格过滤,包括但不限于以下字符及字符串
    Javascript script src img onerror { } ( ) < > = , . ; : " ' # ! / * \
  2. 使用一个统一的规则和库做输出编码
最好前后端一起处理,在前端传到后端前进行一次检测过滤和处理
在后端也必须要过滤处理,
字符串处理函数:htmlspecialchars(),strip_tags(),trim(), 防sql注入 addslashes()
几个xss过滤的函数
tp的xss过滤
function remove_xss($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-\x08,\x0b-\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 .= '~`";:?+/={}[]-_|\'\\';
   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');
   $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; // keep replacing as long as the previous round replaced something
   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{0,8}([9|10|13]);)';
               $pattern .= ')*';
            }
            $pattern .= $ra[$i][$j];
         }
         $pattern .= '/i';
         $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
         $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
         if ($val_before == $val) {
            // no replacements were made, so exit the loop
            $found = false;
         }
      }
   }
   return $val;
}
一个简单的过滤函数
function clean_xss($string, $low = False)
{
        $string = trim ( $string );
        $string = strip_tags ( $string );
        $string = htmlspecialchars ( $string );
        if ($low)
        {
                return True;
        }
        $string = str_ireplace ( array ('"', "\\", "'", "/", "..", "../", "./", "//", 'Javascript', 'script', 'src', 'img', 'onerror', '{', '}', '(', ')', '<', '>', '=', ',', '.', ';', ':', '#', '!', '*'), '', $string );
        $no = '/%0[0-8bcef]/';
        $string = preg_replace ( $no, '', $string );
        $no = '/%1[0-9a-f]/';
        $string = preg_replace ( $no, '', $string );
        $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
        $string = preg_replace ( $no, '', $string );

        return $string;
}
数字的要用intval()来处理

还有如果返回的数据中有url(分页),而url里也有这js代码的话,一定要再过滤一次,这样就可以保证不会弹窗了

$url_string = trim(htmlspecialchars($url_string));

用户退出登录后,其cookie未失效处理。

Cookie: domain_login=xxxxx; ci_session=b9eah6aaaidddapt42otqaorkhj4har0g9qthf0

漏洞建议

  • 用户退出登录,cookie失效。清除所有的session和cookie数据
  • 还有要确认session_id 登陆的这一次要和下一次要不一样,如果一样的话,黑客就可以拿着这个session_id去登陆进去了
CI 要清除session和cookie
$this->load->library('session');
$this->load->helper('cookie');

$this->session->unset_userdata('xxx');
delete_cookie('xxx');

//session_destroy();
$this->session->sess_destroy();

nginx信息泄露


要不让版本号外显

在nginx的主配置文件nginx.conf中的http中加上  server_tokens off;
http {
    server_tokens off;
}
posted @ 2019-11-07 16:46  段佳伟  阅读(2117)  评论(0编辑  收藏  举报