suukii / fe-interview

前端面试题整理
5 stars 0 forks source link

说一下点击劫持 #24

Open suukii opened 4 years ago

suukii commented 4 years ago

READING

LAB


点击劫持是一种视觉欺骗的攻击手段,攻击者将需要攻击的网站通过 iframe 的方式嵌入自己的网页中,一般是设为一个透明元素,或者伪装成另外一个元素,诱导用户点击。

对于这种攻击方式,一般推荐以下两种防御方式:

JS 防御

对于某些低版本的浏览器,只能通过 JS 的方式来防御点击劫持。

<head>
  <style id="click-jack">
    html {
      display: none !important;
    }
  </style>
</head>
<body>
  <script>
    if (self == top) {
      var style = document.getElementById("click-jack");
      document.body.removeChild(style);
    } else {
      top.location = self.location;
    }
  </script>
</body>

CSP frame-ancestors

用于指定哪些源可以通过 <iframe> 等方式来展示当前页面,可以指定一或多个源。

Content-Security-Policy: frame-ancestors <source> <source>;

e.g.

Content-Security-Policy: frame-ancestors 'none';

Content-Security-Policy: frame-ancestors 'self' https://www.example.org;

X-Frame-Options

X-Frame-Options 是一个 HTTP 响应头,用来控制浏览器是否渲染 <frame><iframe> 中的内容,可以设置的值有 3 个: