理论
CSP(Content Security Policy):即内容安全策略。点击这里有详细的介绍。
不过简单了解下就是指:开发者在开发过程中设置了一个类似于白名单的策略,要信任某个页面,哪些外部资源可以执行,哪些不可以,我们只需要配置规则,如何拦截由浏览器自己来实现。这可以从根本上防御XSS,如果CSP配置的好,可以从根本上杜绝XSS,相较于XSS漏洞,XSS更侧重于不能做哪些事,而CSP是只有哪些事可以做。
通常有两种方式来开启 CSP,一种是设置 HTTP 首部中的 Content-Security-Policy,另一种 是设置 meta 标签的方式 ,当启用CSP后,不符合CSP的外部资源会被阻止加载。
低级
查看源码
<?php
$headerCSP = "Content-Security-Policy: script-src 'self' https://pastebin.com hastebin.com www.toptal.com example.com code.jquery.com https://ssl.google-analytics.com ;"; // allows js from self, pastebin.com, hastebin.com, jquery and google analytics.
header($headerCSP);
# These might work if you can't create your own for some reason
# https://pastebin.com/raw/R570EE00
# https://www.toptal.com/developers/hastebin/raw/cezaruzeka
?>
<?php
if (isset ($_POST['include'])) {
$page[ 'body' ] .= "
<script src='" . $_POST['include'] . "'></script>
";
}
$page[ 'body' ] .= '
<form name="csp" method="POST">
<p>You can include scripts from external sources, examine the Content Security Policy and enter a URL to include here:</p>
<input size="50" type="text" name="include" value="" id="include" />
<input type="submit" value="Include" />
</form>
';
从http头部也可以得到允许什么访问。

本文介绍了Content-Security-Policy(CSP)的概念,它是用来防御XSS攻击的一种机制,通过设定白名单策略限制页面可执行的外部资源。CSP可以通过HTTP头部或meta标签启用,并通过示例展示了不同级别的CSP配置,包括禁止所有内联脚本和使用nonce保护内联脚本执行。此外,文章还讨论了JSONP的使用以及如何构造POC绕过CSP限制。
最低0.47元/天 解锁文章
688

被折叠的 条评论
为什么被折叠?



