php如何防止xss攻击
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);