博主辛苦了,我要打赏银两给博主,犒劳犒劳站长。
【摘要】把自己仅仅知道的一个xss攻击方式分享一下,后期遇到新的攻击方式再补充。
XSS:Cross Site Script,本来简写是css,但为了区别层级样式表的css,因此在安全领域叫做"XSS"。
XSS攻击通常是指黑客通过"HTML注入"篡改了网页,插入了恶意的脚本,从而在用户浏览网页时,控制用户浏览器的一种攻击。
// 第一种攻击:在输入框中输入
<script>alert('xss');</script> // 弹窗攻击
<meta http-equiv="refresh" content="0;" url=""> // 不断的刷新当前页面
<iframe src='http://www.baidu.com' FRAMEBORDER=0 width=1000 height=900 /> // 嵌入网页
// 解决方法:使用htmlspecialchars()转换成HTML实体。
// 把 '&' 转义成 '&'
// 把 '"' 转义成 '"'
// 把 "'" 转义成 ''' (或者 ')
// 把 '<' 转义成 '<'
// 把 '>' 转义成 '>'
// <script>alert('xss');</script> 就变成了:<script>alert('xss');</script>
一个比较全的防xss攻击函数:
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 ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $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] );
}
}
版权归 马富天个人博客 所有
本文标题:《PHP中xss攻击与防御》
本文链接地址:http://www.mafutian.com/77.html
转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^
顶0
踩0
第 1 楼 成航先森 2016-04-29 11:37:15 Noord-HollandAmsterdam
评论审核未开启 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
||