在不同环境下添加X-Frame-Options保护的方法

首先我们来看一段关于框架劫持的安全说明。

The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a<frame>, <iframe> or <object> . Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.

Using X-Frame-Options
There are three possible values for X-Frame-Options:

DENY
The page cannot be displayed in a frame, regardless of the site attempting to do so.
SAMEORIGIN
The page can only be displayed in a frame on the same origin as the page itself.
ALLOW-FROM uri
The page can only be displayed in a frame on the specified origin.
In other words, if you specify DENY, not only will attempts to load the page in a frame fail when loaded from other sites, attempts to do so will fail when loaded from the same site. On the other hand, if you specify SAMEORIGIN, you can still use the page in a frame as long as the site including it in a frame is the same as the one serving the page.

文章里写到,在未设置相关的框架保护下,容易导致页面劫持,但是设置了框架的保护后并采用上述的三种保护方法中的任一种后,安全漏洞就得到了缓解,但是不同的平台有不同的方法。

Apache:

在配置文件中增加:

Header always append X-Frame-Options SAMEORIGIN

即可

Nginx:

同样在配置文件中增加:

add_header X-Frame-Options SAMEORIGIN

即可。

Tomcat:

这个比较特别,需要在页面里指定,当然也可以在web.xml里配,但是相当地复杂。

如在每个页面里增加:

response.addHeader( "X-Frame-Options", "SAMEORIGIN" )

xml的配置请看:https://www.owasp.org/index.php/Clickjacking_Protection_for_Java_EE。

IIS:

增加配置:

<system.webServer>
...

<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" value="SAMEORIGIN" />
</customHeaders>
</httpProtocol>

...
</system.webServer>

即可。

HAProxy:

增加:

rspadd X-Frame-Options:\ SAMEORIGIN

即可。

好了,请开始配置吧。

参考内容:

https://www.owasp.org/index.php/Clickjacking_Protection_for_Java_EE
https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options
http://stackoverflow.com/questions/6666423/overcoming-display-forbidden-by-x-frame-options
相关推荐:
Notepad++是一款免费的源代码编辑器,并且可以替代系统自带的记事本的功能,同时也支持各类流行的计算机编程语言。编辑器可以运行在微软的windows系统环境下,它的使用是受GPL许可证保护的,支持的语言:C …
opcache.revalidate_freq 这个选项用于设置缓存的过期时间(单位是秒),当这个时间达到后,opcache会检查你的代码是否改变,如果改变了PHP会重新编译它,生成新的opcode,并且更新缓存。 值为“0”表示每次 …
Win10以及之前的Win7/Win8/Win8.1在有密码的情况下会在登录画面要求用户输入密码,这是出于安全考虑。可有时候我们也许不想要输入密码,但又不想直接去掉密码。 有没有什么办法让Win10自动登录呢? 当然有 …
其实就是换行符的系统默认常量。 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性。 如: <?php echo PHP_EOL; //windows平台相当于 echo "\r\n"; …
微软称这是一种进程缓存技术,有点类似安卓,看似进程驻留,但是不影响你泡新妹子的,当你运行的软件或应用需求更多的内存时会自动地释放原来那些长时间不用的驻留进程。 所以不用怕,不过呢,考虑到有洁癖 …
拿起手机扫一扫即可带走我!