您现在的位置是:网站首页 > 点击劫持的危害文章详情
点击劫持的危害
陈川
【
前端安全
】
3139人已围观
4617字
点击劫持的基本原理
点击劫持(Clickjacking)是一种视觉欺骗攻击手段。攻击者通过透明或伪装的iframe层覆盖在看似无害的网页元素上,诱使用户在不知情的情况下点击恶意内容。这种攻击利用了HTML的层叠特性,通常结合CSS的opacity、z-index等属性实现界面伪装。
<!-- 恶意网站代码示例 -->
<style>
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0.01;
z-index: 2;
}
button {
position: absolute;
top: 300px;
left: 600px;
z-index: 1;
}
</style>
<button>点击抽奖</button>
<iframe src="https://victim-site.com/delete-account"></iframe>
主要攻击场景分析
社交媒体操纵
攻击者在虚假页面覆盖"点赞"按钮,用户以为点击的是普通链接,实际触发了社交媒体的关注或分享操作。2015年Twitter就曾爆发大规模点击劫持事件,攻击者利用此手段自动转发恶意内容。
敏感操作劫持
银行网站的交易确认按钮可能被透明iframe覆盖。用户认为自己在确认购物金额,实际点击的是"转账确认"按钮。2018年某欧洲银行客户因此损失超过20万欧元。
权限获取攻击
恶意网站可能覆盖摄像头/麦克风权限请求弹窗。用户点击"播放视频"按钮时,实际授权了设备访问权限。Chrome扩展程序曾发现此类漏洞(CVE-2019-13720)。
技术实现细节
多层iframe嵌套
现代点击劫持常采用多层嵌套结构逃避检测:
<div style="position: relative;">
<iframe src="decoy.html" style="opacity: 0.5;"></iframe>
<iframe src="malicious.html" style="position: absolute; top: 0; left: 0;"></iframe>
</div>
鼠标事件劫持
通过JavaScript增强攻击精准度:
document.addEventListener('mousemove', function(e) {
maliciousIframe.style.left = e.clientX + 'px';
maliciousIframe.style.top = e.clientY + 'px';
});
浏览器特性滥用
某些CSS属性可辅助攻击:
iframe {
pointer-events: none; /* 穿透点击事件 */
transform: scale(0.1); /* 微型化iframe */
clip-path: circle(10px at 50px 50px); /* 局部可见 */
}
防御措施实践
服务端防护
X-Frame-Options响应头是最基础防御:
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
现代浏览器安全策略
Content-Security-Policy提供更灵活控制:
Content-Security-Policy: frame-ancestors 'none';
Content-Security-Policy: frame-ancestors 'self' https://trusted.com;
客户端检测脚本
JavaScript防御方案示例:
if (window !== window.top || window.self !== window.top) {
document.body.style.display = 'none';
top.location = window.self.location;
}
视觉混淆对抗
防止界面元素被精准覆盖:
#sensitive-button {
position: relative;
z-index: 2147483647; /* 最大z-index值 */
transform: rotate(0.5deg); /* 轻微变形 */
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
实际案例分析
Facebook Likejacking事件
2010年大规模攻击利用虚假视频播放按钮覆盖Like按钮。攻击者使用动态定位脚本确保鼠标悬停时iframe跟随移动,点击率高达62%。
Adobe Flash漏洞利用
CVE-2011-2107允许绕过Flash的安全沙箱。结合点击劫持,攻击者可远程控制用户摄像头。Adobe随后紧急发布补丁并弃用相关API。
移动端点击劫持变种
触屏设备上的"Tapjacking"攻击:
<meta name="viewport" content="width=device-width, user-scalable=no">
<style>
iframe {
width: 100px;
height: 100px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
进阶攻击手法
拖放劫持(Drag-and-Drop)
利用浏览器拖放API窃取数据:
document.addEventListener('dragstart', (e) => {
maliciousIframe.contentWindow.postMessage(e.dataTransfer.getData('text'), '*');
});
键盘事件劫持
透明iframe捕获键盘输入:
window.addEventListener('keypress', (e) => {
if (e.target.tagName !== 'INPUT') {
maliciousIframe.contentWindow.dispatchEvent(
new KeyboardEvent('keypress', {key: e.key})
);
}
});
WebAssembly增强攻击
使用WASM实现更精准的事件预测:
// 伪代码示例
EMSCRIPTEN_KEEPALIVE
void trackCursor(int x, int y) {
// 机器学习预测点击位置
}
框架级防护方案
React防护模式
高阶组件实现自动防护:
const withClickjackingProtection = (WrappedComponent) => {
return class extends React.Component {
componentDidMount() {
if (window.self !== window.top) {
document.body.innerHTML = '<h1>Security Alert</h1>';
}
}
render() {
return <WrappedComponent {...this.props} />;
}
};
};
Angular安全模块
@HostListener('window:load')
checkFrameContext() {
if (window.top !== window.self) {
this.router.navigate(['/security-warning']);
}
}
Vue全局混入
Vue.mixin({
mounted() {
if (window.location !== window.parent.location) {
this.$destroy();
document.body.innerHTML = '';
}
}
});
测试与验证方法
自动化检测工具
使用Puppeteer进行模拟测试:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const isVulnerable = await page.evaluate(() => {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
});
console.log(`Clickjacking vulnerability: ${isVulnerable}`);
})();
手动测试流程
- 创建测试HTML文件包含透明iframe
- 尝试覆盖目标页面关键元素
- 验证是否能捕获到点击事件
- 检查目标页面的防护头部
持续集成方案
在构建流程中加入安全检测:
# .gitlab-ci.yml 示例
security_test:
stage: test
script:
- npm run clickjacking-scan
- if [ $? -ne 0 ]; then exit 1; fi
上一篇: 点击劫持的常见攻击方式