php防止xss跨站腳本攻擊的方法,是針對非法的html代碼包括單雙引號,使用htmlspecialchar()函數。
在使用htmlspecialchar()的時候注意第二個參數,直接用htmlspecialchar($string)的話,第二個參數默認是ENT_COMPAT,函數只是轉義雙引號,不轉義單引號。
所以使用htmlspecialchar函數時盡量加上第二個參數,htmlspecialchar($string,ENT_QUOTES) 轉化單引號和雙引號,如果不需要編譯任何的引號,則使用htmlspecialchar($string,ENT_NOQUOTES)
另外,盡量少使用htmlentities,在全部英文的時候htmlentities和htmlspecialchar沒有區別,都可以達到目的。但是中文情況下,htmlentities卻會轉化所有的html代碼,連同里面的它無法識別的中文符也給轉化了。
htmlentites和htmlspecialchar這兩個函數‘之類的字符串支持不好,都不能轉化,所以用htmlentities和htmlspecialchar轉化的字符串只能防止xss攻擊,不能SQL注入。
所有有打印的語句echo、print等在打印前都要使用htmlentities進行過濾,這樣可以防止xss,注意中文要寫出htmlrntities($name,ENT_NOQUOTES,gb2312)。
(1)網頁不停的刷新 ’<meta http-equiv='refresh' content=0>‘
(2) 嵌入其他網站的鏈接“<iframe src=http://xxx width=250 height=250 > </iframe> ”除了正常途徑輸入xss攻擊字符串外,還可以繞過javascript檢驗,通過修改請求達到xss攻擊的目的
//------------------------------php防注入和XSS攻擊通用過濾-----Start--------------------------------------------// function string_remove_xss($html) { preg_match_all("/\<([^\<]+)\>/is", $html, $ms); $searchs[] = '<'; $replaces[] = '<'; $searchs[] = '>'; $replaces[] = '>'; if ($ms[1]) { $allowtags = 'img|a|font|div|table|tbody|caption|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li|blockquote'; $ms[1] = array_unique($ms[1]); foreach ($ms[1] as $value) { $searchs[] = "<".$value.">"; $value = str_replace('&', '_uch_tmp_str_', $value); $value = string_htmlspecialchars($value); $value = str_replace('_uch_tmp_str_', '&', $value); $value = str_replace(array('\\', '/*'), array('.', '/.'), $value); $skipkeys = 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','javascript','script','eval','behaviour','expression','style','class'); $skipstr = implode('|', $skipkeys); $value = preg_replace(array("/($skipstr)/i"), '.', $value); if (!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) { $value = ''; } $replaces[] = empty($value) ? '' : "<" . str_replace('"', '"', $value) . ">"; } } $html = str_replace($searchs, $replaces, $html); return $html; } //php防注入和XSS攻擊通用過濾 function string_htmlspecialchars($string, $flags = null) { if (is_array($string)) { foreach ($string as $key => $val) { $string[$key] = string_htmlspecialchars($val, $flags); } } else { if ($flags === null) { $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string); if (strpos($string, '&#') !== false) { $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string); } } else { if (PHP_VERSION < '5.4.0') { $string = htmlspecialchars($string, $flags); } else { if (!defined('CHARSET') || (strtolower(CHARSET) == 'utf-8')) { $charset = 'UTF-8'; } else { $charset = 'ISO-8859-1'; } $string = htmlspecialchars($string, $flags, $charset); } } } return $string; } //------------------php防注入和XSS攻擊通用過濾-----End--------------------------------------------//
php中的設置
php5.2 以上版本以支持HttpOnly 參數的設置,同樣也支持httpOnly的設置,在php.ini
session.cookie_htttponly=
設置為1或者true,來開啟全局的cookie的httponly屬性,當然也支持在代碼中來開啟
ini_set('cookie_httponly',1); //或者使用 session_set_cookie_params(0,null,null,null);
Cookie 操作函數setcookie函數和setrawcookie也專門添加了第七個參數來作為httpOnly的選項,開啟方法為:
setcookie('abc','test',null.null.null,null.true);
setrawcookie('abc','test',null,null,null,null,true);