65.9K
CodeProject 正在变化。 阅读更多。
Home

通过 IIS 保护 HTTP 响应头

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (5投票s)

2018 年 1 月 2 日

CPOL

2分钟阅读

viewsIcon

49515

可用于响应头的选项。

引言

正确管理 HTTP 响应头可以提高网站的安全性,并使其更难被攻破。通常,HTTP 头包含服务器与网页内容一起发送的 string 的名称-值对。这些头是发送给客户端浏览器的安全策略,可以根据头中施加的策略实现更安全的浏览。

内容安全策略 (CSP)

CSP 允许您限制特定网站上的资源加载。应用正确的头值可以降低从不同域注入恶意代码的风险。以下是一些 CSP 头部的常见名称/值。

Content-Security-Policy : default-src 'self'; 
(Allow everything but only from the same origin)
Content-Security-Policy : script-src 'self';  (Only Allow Scripts from the same origin)
Content-Security-Policy : script-src 'self' 
www.google-analytics.com ajax.googleapis.com; 
(Allow Google Analytics, Google AJAX CDN and Same Origin)
Content-Security-Policy : default-src https: 
(Allow any assets to be loaded over https from any origin)

更多参考资料

让我们看看如何在 IIS 上添加名称-值对。

添加所需的名称值对。

X-Frame-Options

X-Frame 选项可用于指示浏览器是否允许 iframe。 简单来说,攻击者可以将您的网站在他们的网站上用作 iframe 的宿主。 可以通过 XFO 头来防止这种情况。

X-Frame-Options: DENY (Site can be iframed)
X-Frame-Options: SAMEORIGIN 
(The page can only be displayed in a frame on the same origin as the page itself)
X-Frame-Options: ALLOW-FROM https://example.com/ 
(The page can only be displayed in a frame on the example.com)

更多参考资料

严格传输安全 (Strict-Transport-Security)

HTTP Strict-Transport-Security (HSTS) 强制浏览器仅通过 https 而不是 http 进行通信。 假设您之前有一个需要强制使用 https 的 http 书签。

Strict-Transport-Security: max-age=31536000; includeSubDomains 
(Policy will enforce TLS on your site for one year, including subdomains)

X-XSS-Protection

此标头用于防止 XSS 攻击,该攻击会删除跨站点脚本注入中的不安全部分。

X-XSS-Protection: 0; (Disable the protection)
X-XSS-Protection: 1;mode=block (Enable the protection)

更多参考资料

公钥固定 (Public-Key-Pins)

HPKP 是一种可以在 HTTP 响应上配置的安全功能,以防止伪造的证书。 创建 Base64 密钥后,它将如下所示(密钥为示例)。

Public-Key-Pins : 'pin-sha256="X3pGTSOuJeEVw989IJ/cEtXUEmy52zs1TZQrU06KUKg="; 
\ pin-sha256="MHJYVThihUrJcxW6wcqyOISTXIsInsdj3xK8QrZbHec="; 
\ pin-sha256="isi41AizREkLvvft0IRW4u3XMFR2Yg7bvrF7padyCJg="; 
\ max-age=10; includeSubdomains';

注意

如果定义不正确,您的网站将无法访问。 对于实时测试,请使用较小的 max-age。 确保包含备份密钥。 切勿使用单个密钥。

以下文章介绍了如何从已安装的证书生成密钥

在续订证书时,使用 Base64 提取公钥,删除之前标头中的一个密钥,并包含新的密钥。

Referrer-Policy

When someone clicks on a links and landed on target, 
target can determine where is origin. Referrer policy enables control this behavior. 
Find available options.

Referrer-Policy: no-referrer (No referrer information sent over with the request)
Referrer-Policy: no-referrer-when-downgrade 
(The browser will not send the referrer header when navigating from HTTPS to HTTP)
Referrer-Policy: origin 
(Only send the origin of the document as the referrer in all cases.)
Referrer-Policy: origin-when-cross-origin 
(Send a full URL when performing a same-origin request)
Referrer-Policy: same-origin 
(The browser will only set the referrer header on requests to the same origin. 
If the destination is another origin then no referrer information will be sent.)
Referrer-Policy: strict-origin 
(Similar to origin above but will not allow the secure origin to be sent 
on a HTTP request, only HTTPS.)
Referrer-Policy: strict-origin-when-cross-origin 
(The browser will send the full URL to requests to the same origin but only 
send the origin when requests are cross-origin.)
Referrer-Policy: unsafe-url 
(Browser will always send the full URL with any request to any origin.)

X-Content-Type-Options

它防止浏览器从服务器那里嗅探 mime 类型。

X-Content-Type-Options : nosniff

配置 HTTP 响应后,使用在线扫描程序/安全测试程序来测试该网站。

以下是使用 https://securityheaders.io 扫描网站的快照。

历史

  • 2018 年 1 月 2 日:初始版本
© . All rights reserved.