定义
- 点击劫持是一种视觉欺骗的攻击手段。
- 攻击者将需要攻击的网站通过frame嵌套的方式嵌入自己的网页中,并将 iframe设置为透明,在页面中透出一个按钮诱导用户点击。
模拟攻击
- 登录http://localhost:4000/clickjacking.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>点击劫持</title>
<style>
iframe { opacity: 0; }
</style>
</head>
<body>
<button>最新动态</button>
<img src="pic.jpg" alt="">
<iframe src="http://localhost:3000" frameborder="0" scrolling="no">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello</title>
</head>
<body>
<form action="http://localhost:3000/updateText" method="POST">添加评论<input type="text" name="text" value="评论" /></form>
<script></script>
</body>
</html>
</iframe>
</body>
</html>
防范手段
- X-FRAME-OPTIONS是一个HTTP响应头,在现代浏览器有一个很好的支持。这个HTTP响应头就是为了防御用iframe嵌套的点击劫持攻击
- 该响应头有三个值可选
// DENY,表示页面不允许通过 iframe的方式展示
// SAMEOR|GN,表示页面可以在相同域名下通过 iframe的方式展示
// ALLOW-FROM,表示页面可以在指定来源的 iframe中展示
app.use(async (ctx, next) => {
await next()
ctx.set('X-FRAME-OPTIONS', 'DENY')
})
- self代表本身,top代表页面top
- 当通过iframe的方式加载页面时,攻击者的网页直接不显示所有内容了
<head>
<style id="click-jack">
html {
display: none !important;
}
</style>
</head>
<script>
if (self == top) {
var style = document.getElementById('click-jack')
document.removeChild(style)
} else {
top.location = self.location
}
</script>