幾個常見的漏洞(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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM