- 发现自己在描述较大的事情——比如时间跨度较长的大学生活——总是会出现跳跃,不能很好地让别人连贯地理解我所表述的。
月份:2015年4月
Web安全之X-Frame-Options
为了避免网页被 frame 或者 iframe 嵌套,可以设置 Frame-Options 禁用。
Frame-Options 目前还未完全成为标准,仍需要使用 X-Frame-Options。
X-Frame-Options 值列表
DENY | 拒绝在 frame 或 iframe 中加载网页 |
SAMEORIGIN | 允许在同域 frame 和 iframe 中加载网页 |
ALLOW-FROM uri | 允许在指定域下的 frame 和 iframe 中加载网页,支持并不完整 |
例如在 www.main.com frame-options.php 中增加 X-Frame-Options header,然后在 www.third.com frame.php 中增加 iframe,指向 frame-options.php,效果如下:
代码示例:
www.main.com
// 文件 frame-options.php <?php header('X-Frame-Options: DENY');?> You cannot see this page.
www.third.com
// 文件 frame.php <iframe src="http://www.main.com/frame-options.php"></iframe>
Web安全之CSRF
CSRF 全称是 Cross-Site Request Forgery,指攻击者盗用了用户的身份,以用户的身份发送恶意请求。
CSRF 攻击流程
- User 登录网站 A(存在 CSRF 攻击漏洞的网站)
- User 在没有网站 A 的情况下,使用同一个浏览器访问危险网站 B
- 网站 B 模拟 User,发送恶意的 GET 或者 POST 请求(通过 iframe、js 等)到网站 A。比如让网站 A 删除用户数据
- 网站 A 在没有防范的情况下,破坏或者泄露了用户数据
网站 A 防范 CSRF 攻击的简单方式是在用户提交表单时,增加一个令牌验证。
令牌验证
在网站 A 增加简单令牌
<?php function gen_csrf_token() { session_start(); return md5(session_id()); } function verify_csrf_token($token) { session_start(); return $token = md5(session_id()); } ?> <form action="/user/update" method="post"> <input name="csrf_token" value="<?=gen_csrf_token()?>"/> <input name="sex" value="man"/> <input type="submit"/> </form>
当表单提交时,执行 verify_csrf_token 判断是否令牌是否正确,继而认为不是第三方网站伪造的请求。
HTTP_REFERER 验证
验证 http_referer 也能够从一定程度上降低 CSRF 攻击风险。现代浏览器是不允许修改 HTTP_REFERER 头信息的。
Web安全之CSP
CSP 全称是 Content Security Policy,控制浏览器在加载网络资源时,告知浏览器哪些资源是可信的、安全的。可以有效地降低和杜绝 XSS 攻击。
如何产生 XSS 攻击,可以参考 http://blog.zhengxianjun.com/2015/web-security-xss/。
有时候即便整个网站增加了 XSS 防注入,也可能在迭代过程中存在遗漏。
浏览器不能区分哪些资源是安全的,一旦下载之后就会执行。(有些浏览器可以识别简单的 XSS 注入)。
CSP 就是为了告诉浏览器哪些资源是可信任的、安全的,通过白名单去加载网络资源和执行。这样即使网页存在 XSS 注入,出现损失的可能性也会大大地降低。
比如:
Content-Security-Policy: script-src https://s.fiaox.com
那么浏览器只会信任来自 s.zhengxianjun.com 的脚本,这样即使网站被注入了第三方站点脚本或者内联,也不会执行,那 XSS 攻击也就失效了。
代码示例:
<?php header('Content-Security-Policy: script-src https://s.fiaox.com');?> <script type="text/javascript" src="https://code.jquery.com/jquery.js"></script> // 支持 CSP 则不会加载来自 jquery.com 的脚本
以上代码分别在 IE、Chrome、Firefox 中的效果。
在 IE10 中,似乎没有支持 CSP,jquery.js 被正常加载和执行。这是因为早期 CSP 没有规范,IE 识别 X-Content-Security-Policy,但并不是完整支持。
Chrome 正确识别了 CSP,并且说明了原因。
Firefox 正确识别了 CSP,并且说明了原因。
CSP 是帮助浏览器创建了一个网络资源白名单。
CSP 规则列表
Content-Security-Policy: script-src 'self' s.zhengxianjun.com;style-src 'self';img-src 'self'
- 多个指令之间用英文分号隔开;
- 指令的值不是域名时需要引号引起来;
- 指令重复以第一个为准。
指令 | 说明 |
default-src | 定义资源默认加载策略 |
connect-src | 定义 Ajax、WebSocket 等加载策略 |
font-src | 定义 Font 加载策略 |
frame-src | 定义 Frame 加载策略 |
img-src | 定义图片加载策略 |
media-src | 定义 <audio>、<video> 等引用资源加载策略 |
object-src | 定义 <applet>、<embed>、<object> 等引用资源加载策略 |
script-src | 定义 JS 加载策略 |
style-src | 定义 CSS 加载策略 |
sandbox | 值为 allow-forms,对资源启用 sandbox |
report-uri | 值为 /report-uri,提交日志 |
以上指令除了 sandbox、report-uri 是比较特殊的值,其余指令可以接收 ‘self’、data、域名 等值。
指令示例
示例 | 说明 |
script-src | 允许任何资源 |
script-src ‘none’ | 不允许任何资源 |
script-src ‘self’ | 允许同源资源(协议、域名、端口均相同) |
script-src ‘unsafe-eval’ | 允许动态脚本,如 eval、setTimeout、new Function |
script-src ‘unsafe-inline’ | 允许内联资源 |
script-src s.zhengxianjun.com | 允许某个域名下的资源 |
script-src *.zhengxianjun.com | 允许子域名下的资源 |
script-src https | 允许 https 资源 |
script-src https://zhengxianjun.com | 允许某个域名下的 https 资源 |
img-src data | 允许 data: 协议资源 |
report-uri 会接收到一个 post 请求,包含 CSP 阻止的详细信息。
虽然 CSP 能够有效降低 XSS 攻击,但是不要指望它可以完全杜绝 XSS 攻击。
为了让产品能够顺利过渡到 CSP,可以通过 Content-Security-Policy-Report-Only 先收集 CSP 报告,根据报告修正被阻止的正确资源,再启用 CSP。使用了 Content-Security-Policy-Report-Only 则一定要配置 report-uri。
示例:
Content-Security-Policy-Report-Only: script-src 'self'; report-uri https://www.zhengxianjun.com/csp/report
Web安全之P3P
P3P 全称是 The Platform for Privacy Preferences,控制用户在在浏览网页时,如果网页中包含第三方站点资源,是否接受第三方站点设置的 cookie。
举个例子:
user 访问 www.main.com, 该站点加载了第三方网站 www.third.com 的网页(通过iframe)或者静态资源(css, js, img),是否接受 www.third.com 设置的 cookie。
默认情况下不允许保存第三方 cookie,假如第三方站点在 header 中设置了 P3P 相关信息,则可能允许跨域访问隐私数据。
IE 浏览器隐私设置:
Chrome 浏览器隐私设置:
配合这两幅截图说明第三方 cookie 在 IE、Chrome 中是否有效。两幅截图显示 IE 阻止第三方 cookie,Chrome 接受第三方 cookie。
代码示例:
www.main.com setcookie
// file http://www.main.com/setcookie.php <script src="http://www.main.com/setcookie.php"></script> <!-- <iframe src="http://www.main.com/setcookie.php"></iframe> -->
www.third.com setcookie
// header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); setcookie('p3p_cookie_key', 'time_' . time(), time() + 60, '/', '.third.com');
www.third.com getcookie
// file http://www.third.com/getcookie.php echo 'p3p_cookie_key:' . $_COOKIE['p3p_cookie_key'];
访问 http://www.main.com/setcookie.php,网页会通过 iframe 或者 script 访问 http://www.third.com/setcookie.php, 此时浏览器不一定接受来自 www.third.com 的 cookie。也就是立即访问 http://www.third.com/getcookie.php,可能获取不到 cookie 信息。
当 www.third.com setcookie.php 输出 P3P 头信息时:
- 访问 http://www.main.com/setcookie.php
- 访问 http://www.third.com/getcookie.php 可以获取到 cookie p3p_cookie_key
网页上所有资源浏览器都可以访问,但出于安全考虑,浏览器对此进行了限制,产生一些安全词汇。
P3P Header is present [引用]
CP=”CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR” | |
Compact Policy token is present. A trailing ‘o’ means opt-out, a trailing ‘i’ means opt-in. | |
CURa | Information is used to complete the activity for which it was provided. |
ADMa | Information may be used for the technical support of the Web site and its computer system. |
DEVa | Information may be used to enhance, evaluate, or otherwise review the site, service, product, or market. |
PSAo | Information may be used to create or build a record of a particular individual or computer that is tied to a pseudonymous identifier, without tying identified data (such as name, address, phone number, or email address) to the record. This profile will be used to determine the habits, interests, or other characteristics of individuals for purpose of research, analysis and reporting, but it will not be used to attempt to identify specific individuals. |
PSDo | Information may be used to create or build a record of a particular individual or computer that is tied to a pseudonymous identifier, without tying identified data (such as name, address, phone number, or email address) to the record. This profile will be used to determine the habits, interests, or other characteristics of individuals to make a decision that directly affects that individual, but it will not be used to attempt to identify specific individuals. |
OUR | We share information with ourselves and/or entities acting as our agents or entities for whom we are acting as an agent. |
BUS | Info is retained under a service provider’s stated business practices. Sites MUST have a retention policy that establishes a destruction time table. The retention policy MUST be included in or linked from the site’s human-readable privacy policy. |
UNI | Non-financial identifiers, excluding government-issued identifiers, issued for purposes of consistently identifying or recognizing the individual. These include identifiers issued by a Web site or service. |
PUR | Information actively generated by the purchase of a product or service, including information about the method of payment. |
INT | Data actively generated from or reflecting explicit interactions with a service provider through its site — such as queries to a search engine, or logs of account activity. |
DEM | Data about an individual’s characteristics — such as gender, age, and income. |
STA | Mechanisms for maintaining a stateful session with a user or automatically recognizing users who have visited a particular site or accessed particular content previously — such as HTTP cookies. |
PRE | Data about an individual’s likes and dislikes — such as favorite color or musical tastes. |
COM | Information about the computer system that the individual is using to access the network — such as the IP number, domain name, browser type or operating system. |
NAV | Data passively generated by browsing the Web site — such as which pages are visited, and how long users stay on each page. |
OTC | Other types of data not captured by the above definitions. |
NOI | Web Site does not collected identified data. |
DSP | The privacy policy contains DISPUTES elements. |
COR | Errors or wrongful actions arising in connection with the privacy policy will be remedied by the service. |