1.xss攻擊(sql注入)
攻擊者向有XSS漏洞的網(wǎng)站中輸入惡意的HTML代碼氧敢,當(dāng)用戶瀏覽該網(wǎng)站時(shí)日戈,這段HTML代碼會(huì)自動(dòng)執(zhí)行,從而達(dá)到攻擊的目的孙乖。如浙炼,盜取用戶Cookie信息、破壞頁(yè)面結(jié)構(gòu)的圆、重定向到其它網(wǎng)站等鼓拧。只要存在能提供輸入的表單并且沒(méi)做安全過(guò)濾或過(guò)濾不徹底半火,都有可能存在XSS漏洞越妈。
比較常見(jiàn)的惡意字符XSS輸入:
1.XSS 輸入通常包含 JavaScript 腳本,如彈出惡意警告框:<script>alert("XSS");</script>
2.XSS 輸入也可能是 HTML 代碼段钮糖,譬如:
(1).網(wǎng)頁(yè)不停地刷新 <meta http-equiv="refresh" content="0;">
(2).嵌入其它網(wǎng)站的鏈接 <iframe src=http://xxxx width=250 height=250></iframe>
除了通過(guò)正常途徑輸入XSS攻擊字符外梅掠,還可以繞過(guò)JavaScript校驗(yàn)酌住,通過(guò)修改請(qǐng)求達(dá)到XSS攻擊的目的
image.png
如何預(yù)防xss攻擊:
<?PHP
/**
* @param $string
* @param $low 安全別級(jí)低
*/
function clean_xss(&$string, $low = False) {
if (! is_array($string)) {
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low) return True;
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "http://" ), '', $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 True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key ) {
clean_xss ( $string [$key] );
}
}
// just a test
$str = 'codetc.com<meta http-equiv="refresh" content="0;">';
clean_xss($str); //如果你把這個(gè)注釋掉,你就知道xss攻擊的厲害了
echo $str;