点击劫持

点击劫持是一种视觉欺骗的攻击手段。

嵌套一个iframe,然后将 iframe 设置为透明。在页面中透出一个按钮诱导用户点击。

防御方法有2种:

1. X-FRAME-OPTIONS

  通过 Response Header 设置,表示哪些情况下才允许使用 iframe 展示自己

X-Frame-Options: deny
X-Frame-Options: sameorigin
X-Frame-Options: allow-from https://example.com/

  deny 表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许

  sameorigin 表示该页面可以在相同域名页面的 frame 中展示

  allow-from uri表示该页面可以在指定来源的 frame 中展示。

2. 古老的方法

  js 判断,当 top !== self 的时候,证明本页面被嵌套在 iframe了

<head>
  <style id="click-jack">
    html {
      display: none !important;
    }
  </style>
</head>
<body>
  <script>
    if (self == top) {
      // 没有被嵌套,则把display none 的样式去掉
      var style = document.getElementById('click-jack')
      document.body.removeChild(style)
    } else {
      top.location = self.location
    }
  </script>
</body>
原文地址:https://www.cnblogs.com/amiezhang/p/11456947.html