【应用安全】安全相关的HTTP HEADERS

X-XSS-Protection

X-XSS-Protection为浏览器针对XSS攻击的防御机制。
配置如下:

0 禁用过滤器。
1 启用过滤器。 如果检测到跨站点脚本攻击,为了停止攻击,浏览器将清理页面。
1; mode = block启用过滤器。 当检测到XSS攻击时,浏览器将阻止页面的呈现,而不是清理页面。
1; report=http://domain/url 过滤器已启用。 浏览器将清理页面并报告违例。 这是一个使用CSP违规报告的Chromium功能,可将详细信息发送到您选择的URI。

1)没有设置X-XSS-Protection
服务端代码:

<?php
echo $_GET['id'];
?>

请求URL:http://192.168.192.120:8080/xss.php?id=1<img/src=x οnerrοr=alert(1)>

可以看到Console打印信息如下:

The XSS Auditor refused to execute a script in 'http://192.168.192.120:8080/xss.php?id=1%3Cimg/src=x%20onerror=alert(1)%3E' because its source code was found within the request. The auditor was enabled as the server sent neither an 'X-XSS-Protection' nor 'Content-Security-Policy' header.

2)设置X-XSS-Protection:0
服务端代码:

<?php
header("X-XSS-Protection:0");
echo $_GET['id'];
?>

会触发XSS

3)设置X-XSS-Protection:1; mode = block
服务端代码:

<?php
header("X-XSS-Protection:1;mode = block");
echo $_GET['id'];
?>

发现页面为空,连1也没有打印出来

X-Frame-Options

X-Frame-Options,是为了减少点击劫持(Clickjacking)而引入的一个响应头。
这个响应头支持三种配置:

DENY:不允许被任何页面嵌入;
SAMEORIGIN:不允许被本域以外的页面嵌入;
ALLOW-FROM uri:不允许被指定的域名以外的页面嵌入(Chrome现阶段不支持);

如果某页面被不被允许的页面以<iframe>或<frame>的形式嵌入,IE会显示类似于“此内容无法在框架中显示”的提示信息,Chrome和Firefox都会在控制台打印信息。由于嵌入的页面不会加载,这就减少了点击劫持的发生。
1)设置X-Frame-Options:deny
http://192.168.192.120:8080/xss.php代码

<iframe src=http://192.168.192.120:8080/123.php></iframe>
http://192.168.192.120:8080/123.php代码
<?php header("X-Frame-Options:deny"); ?>
123456

可以看到Console输出如下:

Refused to display 'http://192.168.192.120:8080/123.php' in a frame because it set 'X-Frame-Options' to 'deny'.

2)设置X-Frame-Options:SAMEORIGIN
http://192.168.192.120:8080/xss.php代码

<iframe src=http://192.168.192.120:8080/123.php></iframe>

http://192.168.192.120:8080/123.php代码

<?php header("X-Frame-Options:SAMEORIGIN"); ?>
123456

3)X-Frame-Options: allow-from http://192.168.192.120:8080/xss.php
http://192.168.192.120:8080/xss.php代码

<iframe src=http://10.59.0.248/1.php></iframe>

http://10.59.0.248/1.php代码

<?php header("X-Frame-Options:allow-from http://192.168.192.120:8080/xss.php"); ?>
123456

Chrome会忽略该条配置,console输出如下:

Invalid 'X-Frame-Options' header encountered when loading 'http://10.59.0.248/1.php': 'allow-from http://192.168.192.120:8080/xss.php' is not a recognized directive. The header will be ignored.

Firefox下可以生效。

X-Content-Type-Options

互联网上的资源有各种类型,通常浏览器会根据响应头的Content-Type字段来分辨它们的类型。例如:”text/html”代表html文档,”image/png”是PNG图片,”text/css”是CSS样式文档。然而,有些资源的Content-Type是错的或者未定义,浏览器会自动判断文档类型。例如:

test.php内容如下:

<script>alert(1)</Script>
<?php
header("Content-Type:text/plain");
?>

在IE8中访问发现JS可以执行:

但是在Chrome和Firefox中不会执行。
IE8里面新增了一个HTTP请求数据包header的属性X-Content-Type-Options。
可以通过使用X-Content-Type-Options:nosniff 选项来关闭IE的文档类型自动判断功能。
修改test.php内容,并添加header(“X-Content-Type-Options: nosniff”); 结果如下:

发现不会自动判断文档类型,并没有执行JS。

Strict-transport-security

HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式。
要使用HSTS,只需要在你的HTTPS网站响应头中,加入下面这行:

strict-transport-security: max-age=16070400; includeSubDomains

includeSubDomains是可选的,用来指定是否作用于子域名。支持HSTS的浏览器遇到这个响应头,会把当前网站加入HSTS列表,然后在max-age指定的秒数内,当前网站所有请求都会被重定向为https。即使用户主动输入http://或者不输入协议部分,都将重定向到https://地址。
Chrome内置了一个HSTS列表,默认包含Google、Paypal、Twitter、Linode等等服务。我们也可以在Chrome输入chrome://net-internals/#hsts,进入HSTS管理界面。在这个页面,你可以增加/删除/查询HSTS记录。
拿www.alipay.com测试下:
首先我们在chrome://net-internals/#hsts查询下www.alipay.com

然后我们访问http://www.alipay.com

返回了301跳转,Tengine重定向到了https://www.alipay.com,然后查看这个请求的返回包

可以看到返回了HSTS头,Strict-Transport-Security:max-age=31536000
在chrome://net-internals/#hsts查询下www.alipay.com

也就是在接下来的一年内,所有www.alipay.com的访问都将被重定向到https
然后我再次访问http://www.alipay.com

可以看到,一个307 响应码,这是chrome浏览器的内部转换,将http转换成https后再发送请求。

参考文章:
http://www.mottoin.com/93711.html

总会有不期而遇的温暖. 和生生不息的希望。
原文地址:https://www.cnblogs.com/devi1/p/13486215.html