您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)PHP怎么防止XSS跨站腳本攻擊,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
PHP防止XSS跨站腳本攻擊的方法:是針對非法的HTML代碼包括單雙引號等,使用htmlspecialchars()函數(shù) 。
在使用htmlspecialchars()函數(shù)的時候注意第二個參數(shù), 直接用htmlspecialchars($string) 的話,第二個參數(shù)默認(rèn)是ENT_COMPAT,函數(shù)默認(rèn)只是轉(zhuǎn)化雙引號(“), 不對單引號(‘)做轉(zhuǎn)義.
所以,htmlspecialchars函數(shù)更多的時候要加上第二個參數(shù), 應(yīng)該這樣用: htmlspecialchars($string,ENT_QUOTES).當(dāng)然,如果需要不轉(zhuǎn)化任何引號,用htmlspecialchars($string,ENT_NOQUOTES).
另外, 盡量少用htmlentities, 在全部英文的時候htmlentities和htmlspecialchars沒有區(qū)別,都可以達(dá)到目的.但是,中文情況下, htmlentities卻會轉(zhuǎn)化所有的html代碼,連同里面的它無法識別的中文字符也給轉(zhuǎn)化了。
htmlentities和htmlspecialchars這兩個函數(shù)對 '之類的字符串支持不好,都不能轉(zhuǎn)化, 所以用htmlentities和htmlspecialchars轉(zhuǎn)化的字符串只能防止XSS攻擊,不能防止SQL注入攻擊.
所有有打印的語句如echo,print等 在打印前都要使用htmlentities() 進(jìn)行過濾,這樣可以防止Xss,注意中文要寫出htmlentities($name,ENT_NOQUOTES,GB2312) 。
(1).網(wǎng)頁不停地刷新 '<meta http-equiv="refresh" content="0;">'
(2).嵌入其它網(wǎng)站的鏈接 <iframe src=http://xxxx width=250 height=250></iframe> 除了通過正常途徑輸入XSS攻擊字符外,還可以繞過JavaScript校驗(yàn),通過修改請求達(dá)到XSS攻擊的目的.
<?php //php防注入和XSS攻擊通用過濾 $_GET && SafeFilter($_GET); $_POST && SafeFilter($_POST); $_COOKIE && SafeFilter($_COOKIE); function SafeFilter (&$arr) { $ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/' ,'/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/' ,'/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/', '/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/' ,'/onmouseout/','/onmouseover/','/onmouseup/','/onunload/'); if (is_array($arr)) { foreach ($arr as $key => $value) { if (!is_array($value)) { if (!get_magic_quotes_gpc()) //不對magic_quotes_gpc轉(zhuǎn)義過的字符使用addslashes(),避免雙重轉(zhuǎn)義。 { $value = addslashes($value); //給單引號(')、雙引號(")、反斜線(\)與 NUL(NULL 字符) #加上反斜線轉(zhuǎn)義 } $value = preg_replace($ra,'',$value); //刪除非打印字符,粗暴式過濾xss可疑字符串 $arr[$key] = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 標(biāo)記并轉(zhuǎn)換為 HTML 實(shí)體 } else { SafeFilter($arr[$key]); } } } } ?> $str = 'www.90boke.com<meta http-equiv="refresh" content="0;">'; SafeFilter ($str); //如果你把這個注釋掉,提交之后就會無休止刷新 echo $str;
//------------------------------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中的設(shè)置
PHP5.2以上版本已支持HttpOnly參數(shù)的設(shè)置,同樣也支持全局的HttpOnly的設(shè)置,在php.ini中
----------------------------------------------------- session.cookie_httponly = -----------------------------------------------------
設(shè)置其值為1或者TRUE,來開啟全局的Cookie的HttpOnly屬性,當(dāng)然也支持在代碼中來開啟:
<?php ini_set("session.cookie_httponly", 1); // or session_set_cookie_params(0, NULL, NULL, NULL, TRUE); ?>
Cookie操作函數(shù)setcookie函數(shù)和setrawcookie函數(shù)也專門添加了第7個參數(shù)來做為HttpOnly的選項(xiàng),開啟方法為:
<?php setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE); setrawcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE); ?>
關(guān)于PHP怎么防止XSS跨站腳本攻擊就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。