Zonebit

个人的奋斗还是历史的进程?

View the Project on GitHub

16 April 2024

关于xss

by

XSS是什么?

定义一

Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites.

定义二

Cross-site scripting (XSS) is a type of coding vulnerability. It is usually found in web applications. XSS enables attackers to inject malicious into web pages viewed by other users. XSS may allow attackers to bypass access controls such as the same-origin policy may. This is one of the most common vulnerabilities found accordingly with OWASP Top 10. Symantec in its annual threat report found that XSS was the number two vulnerability found on web servers.

原理?

将用户输入作为代码执行,最终导致请求到恶意脚本

类型?

XSS 攻击可以分为 3 类:存储型(持久型)、反射型(非持久型)、DOM 型。

目的?

获取Cookie、会话令牌或其他敏感信息。重写HTML页面的内容。

发现?

代码安全检视

Cross-site scripting faws can be difcult to identify and remove from a web application. The best practice to search for faws is to perform an intense code review and search for all places where user input through a HTTP request could possibly make its way into the HTML output.

检视要点

  1. untrusted data不要以HTML或者javascript的形式传输
  2. 当数据从服务器传输到客户端的时候,untrusted data必须经过编码。不要假定服务器的数据一定是安全的,最好每次使用前都检查数据
  3. untrusted data可能是通过以下api被引入DOM

    a. Node.textContent

    b. document.createTextNode

    c. Element.setAttribute (second parameter only)

  4. HTML标签,比如img、iframe、bgsound等都可能被用来传输恶意js
  5. eval
  6. setTimeout, setInterval第一个参数
  7. on***方法绑定事件回调
  8. location的href声明javascript协议
  9. appendTo()、prependTo()、insertAfter()、insertBefore()
  10. v-html,v-bind

v-bind示例

<body>
<div id="app">
  <a v-bind:href="message">aaa</a>
  <video src="#" v-bind:onclick="message" v-bind:onerror="message">bbb</video>
  <img src="1" v-bind:onerror="message" />
  <form v-bind:action="message"><input type="submit" />ccc</form>
  <input type="text" v-bind:onchange="message"></input>
  <img src="img/HBuilder.png" v-bind:onload="message"/>
</div>
 <script>
 new Vue({
   el: '#app',
   data: {
     message: 'javascript:alert(4)'
     }
  })
  </script>
</body>

穷举是不可能的,需要把握原则

  1. 用户输入
  2. 浏览器api可以接收用户输入的入口
  3. 接收字符串形式的代码并执行的函数

js框架

针对js框架需要持续关注框架的CVE,并确保框架一直保持在最新的稳定版本

使用扫描工具

通过扫描工具,扫描工具并不能穷尽XSS漏洞,所以代码检视依然很重要,当然代码检视也不能排查出所有的漏洞,但是这是一种基于现有风险的纵深防御方式,是一种最佳实践

OWASP也提供了Zed Attack Proxy(ZAP)工具

防御方式?

微软的Anti-XSS库(面向.net)

参考

https://developer.mozilla.org/zh-CN/docs/Glossary/Cross-site_scripting

开放式 Web 应用安全项目(OWASP)https://owasp.org/www-community/attacks/xss/#

tags: