一、客戶檢測報告:
事件URL:
http://127.0.0.1/mgr/login.php?gotopage=%22%3E%3Cinput%20type=%22text%22%20onInput=alert(1)%3E%3Cx=%22
事件類型:
漏洞-KingCms門戶系統存儲型XSS
事件URL:
http://127.0.0.1/mgr/login.php?gotopage=%22%3E%3Cinput%20type=%22text%22%20onInput=alert(1)%3E%3Cx=%22
二、XSS注入分析
檢測方使用的注冊方式(轉碼前):
http://127.0.0.1/mgr/login.php?gotopage="><input type="text" onInput=alert(1)><x="
原理是 利用目標表單頁面上面的一個input 隱藏域:<input name="gotopage" value="${gotopage}"> 實現注入。
三、處理方式 :
方法1:過濾js關鍵字
function xss_clea1($var) { $ra=array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/i','/javascript/i','/vbscript/i','/onload/i','/onunload/i','/onchange/i','/onsubmit/i','/onreset/i','/onselect/i','/onblur/i','/onfocus/i','/onabort/i','/onkeydown/i','/onkeypress/i','/onkeyup/i','/onclick/i','/ondblclick/i','/onmousedown/i','/onmousemove/i','/onmouseout/i','/onmouseover/i','/onmouseup/i','/onunload/i'); $var = preg_replace($ra,'',$var); return htmlspecialchars($var,ENT_QUOTES); }
方法2:轉義特殊符號 及html 關鍵字
function xss_clean2($data){ // Fix &entity\n; $data=str_replace(array('&','<','>'),array('&','<','>'),$data); $data=preg_replace('/(&#*\w+)[\x00-\x20]+;/u','$1;',$data); $data=preg_replace('/(&#x*[0-9A-F]+);*/iu','$1;',$data); $data=html_entity_decode($data,ENT_COMPAT,'UTF-8'); // Remove any attribute starting with "on" or xmlns $data=preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu','$1>',$data); // Remove javascript: and vbscript: protocols $data=preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2nojavascript...',$data); $data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2novbscript...',$data); $data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u','$1=$2nomozbinding...',$data); // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> $data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i','$1>',$data); $data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i','$1>',$data); $data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu','$1>',$data); // Remove namespaced elements (we do not need them) $data=preg_replace('#</*\w+:\w[^>]*+>#i','',$data); do{// Remove really unwanted tags $old_data=$data; $data=preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i','',$data); }while($old_data!==$data); // we are done... return $data; }
四、如何使用:
在接收頁面,用這兩個函數分別過濾所有的參數:
如:
$gotopage = xss_clean1($gotopage);
$gotopage = xss_clean2($gotopage );