DOM-Based XSS

本文讨论了如何防御DOM型跨站脚本(XSS)攻击,强调了客户端过滤的重要性,并提供了具体的防御措施,包括验证输入数据、硬化客户端代码及实施严格的IPS策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

无论怎样,DOM 的XSS攻击需要小心 再小心,最好能在客户端做一个过滤。

 

这个在服务器端解析时,竟然只能解析一个name?

 

http://www.vulnerable.site/welcome.html?foobar=name=<script>alert(document.cookie)<script>&name=Joe

 

#号后面的某些浏览器传递不到服务器,但是在某些情况下,url解析后,恶意代码可以在客户端执行,如下

http://www.vulnerable.site/welcome.html#name=<script>alert(document.cookie)<script>

var pos=document.URL.indexOf("name=")+5;

document.write(document.URL.substring(pos,document.URL.length));

 

 

 

 

原文:http://www.webappsec.org/projects/articles/071105.shtml

 

1. Avoiding client side document rewriting, redirection, or other sensitive actions, using client side data. Most of these effects can be achieved by using dynamic pages (server side).

2. Analyzing and hardening the client side (Javascript) code. Reference to DOM objects that may be influenced by the user (attacker) should be inspected, including (but not limited to):

  • document.URL
  • document.URLUnencoded
  • document.location (and many of its properties)
  • document.referrer
  • window.location (and many of its properties)

Note that a document object property or a window object property may be referenced syntactically in many ways - explicitly (e.g. window.location), implicitly (e.g.location), or via obtaining a handle to a window and using it (e.g.handle_to_some_window.location).

Special attention should be given to scenarios wherein the DOM is modified, either explicitly or potentially, either via raw access to the HTML or via access to the DOM itself, e.g. (by no means an exhaustive list, there are probably various browser extensions):

  • Write raw HTML, e.g.:
    • document.write(…)
    • document.writeln(…)
    • document.body.innerHtml=…
  • Directly modifying the DOM (including DHTML events), e.g.:
    • document.forms[0].action=… (and various other collections)
    • document.attachEvent(…)
    • document.create…(…)
    • document.execCommand(…)
    • document.body. … (accessing the DOM through the body object)
    • window.attachEvent(…)
  • Replacing the document URL, e.g.:
    • document.location=… (and assigning to location’s href, host and hostname)
    • document.location.hostname=…
    • document.location.replace(…)
    • document.location.assign(…)
    • document.URL=…
    • window.navigate(…)
  • Opening/modifying a window, e.g.:
    • document.open(…)
    • window.open(…)
    • window.location.href=… (and assigning to location’s href, host and hostname)
  • Directly executing script, e.g.:
    • eval(…)
    • window.execScript(…)
    • window.setInterval(…)
    • window.setTimeout(…)

To continue the above example, an effective defense can be replacing the original script part with the following code, which verifies that the string written to the HTML page consists of alphanumeric characters only: 

  <SCRIPT>
  var pos=document.URL.indexOf("name=")+5;
  var name=document.URL.substring(pos,document.URL.length);
  if (name.match(/^[a-zA-Z0-9]$/))
  {
       document.write(name);
  }
  else
  {
        window.alert("Security error");
  }
  </SCRIPT>

Such functionality can (and perhaps should) be provided through a generic library for sanitation of data (i.e. a set of Javascript functions that perform input validation and/or sanitation). The downside is that the security logic is exposed to the attackers - it is embedded in the HTML code. This makes it easier to understand and to attack it. While in the above example, the situation is very simple, in more complex scenarios wherein the security checks are less than perfect, this may come to play.

3. Employing a very strict IPS policy in which, for example, page welcome.html is expected to receive a one only parameter named “name”, whose content is inspected, and any irregularity (including excessive parameters or no parameters) results in not serving the original page, likewise with any other violation (such as an Authorization header or Referer header containing problematic data), the original content must not be served. And in some cases, even this cannot guarantee that an attack will be thwarted.

 

帮我以下的过程具体到每一步做什么:DVWA(Damn Vulnerable Web Application)是一款专门用于安全测试的漏洞百出的 Web 应用程序,借助它能直观地演示跨站脚本攻击(XSS)及其防范措施。以下结合 DVWA 的不同模块来演示第五点中的内容: 演示反射型 XSS 攻击 攻击步骤:打开 DVWA 并登录,将安全等级设置为 “low” 。找到 “Reflected XSS (GET)” 模块,在输入框中输入恶意脚本,比如<script>alert('反射型XSS')</script>,然后点击 “Submit” 按钮。页面会弹出包含 “反射型 XSS” 的警告框,这是因为服务器直接把输入内容反射回页面,未对输入进行过滤,浏览器误将恶意脚本当作正常内容执行。 防范演示:把 DVWA 安全等级提升到 “high”。再次输入同样的恶意脚本并提交,此时页面不会弹出警告框。查看页面源代码,会发现输入的脚本被转义成了普通文本,这是因为高安全等级下,服务器对输入进行了过滤,把特殊字符转换为 HTML 实体,使脚本无法被浏览器执行。 演示存储型 XSS 攻击 攻击步骤:在 DVWA 中选择 “Stored XSS” 模块。在留言输入框输入恶意脚本,例如<script>alert('存储型XSS')</script>,并提交。当其他用户访问该留言页面时,页面会自动弹出包含 “存储型 XSS” 的警告框。这是由于恶意脚本被存储在服务器数据库中,每次页面加载时都会执行。 防范演示:切换到 “high” 安全等级,重新输入恶意脚本提交。刷新页面后,留言内容里的脚本被过滤掉,以纯文本形式显示。这表明高安全等级下,服务器在存储数据前对输入进行了严格过滤,阻止了恶意脚本的存储和执行。 演示 DOM - Based XSS 攻击 攻击步骤:进入 DVWA 的 “DOM - Based XSS” 模块。查看页面源代码,找到与 DOM 操作相关的 JavaScript 代码。在浏览器地址栏的 URL 后添加恶意脚本,如?default=English</script><script>alert('DOM - Based XSS')</script> 。页面加载后会弹出包含 “DOM - Based XSS” 的警告框,这是因为恶意脚本通过修改 URL 参数影响了 DOM 操作,被插入到页面中执行。 防范演示:修改 DVWA 配置文件,提升此模块的安全性。再次输入同样的恶意 URL,页面不会弹出警告框。这是因为改进后的代码对 URL 参数进行了严格验证和过滤,防止恶意脚本通过 DOM 操作被执行。
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值