Dav*_*der 14 javascript xss content-security-policy
虽然使用CSP的目的略有不同(沙盒),但我意识到一个非常简单的自动点击链接似乎绕过了相对严格的CSP.我所描述的是以下内容:
内容安全政策:
default-src 'none'; script-src 'unsafe-inline';
Run Code Online (Sandbox Code Playgroud)
和身体:
<a href="http://www.google.com">test</a>
<script>
document.querySelector("a").click();
</script>
Run Code Online (Sandbox Code Playgroud)
显然,在真正的攻击中,您首先将cookie信息包含在href
字段中,并可能将其包装在隐藏的自嵌入iframe中,或者让域重定向到您来自的位置(可能使用其他url参数,从而创建一种XMLHttpRequest绕过connect-src
),但这个基本的例子确实显示了问题.
有没有办法用CSP(仍然允许执行Javascript)来阻止这种情况?
显然,使用其他一些导航方法也可以做同样的事情.我之所以特别询问这种方法的原因实际上与我的次要目标有关,而不是XSS漏洞.无论哪种方式,对任何和所有真正的解决方案开放.
由于所有的困惑,即使没有,这仍然可以适用script-src: 'unsafe-inline'
.想象一下这个名为的文件api.ext
print URLParameters.method
[...]
Run Code Online (Sandbox Code Playgroud)
然后可以调用此文件api.ext?method=<script src='api.ext?method=alert("test")//'></script><!--
(除了您需要额外的URL编码和东西,这只是为了得到重点).找到这样的漏洞是很困难的,它们很少见,但connect-src
似乎存在这样的事情,以防止信息泄露,即使在这些情况下也是如此.
这不太可能是一种令人满意的方法 - 显然它不是基于 CSP - 但如果您确实必须防止此类攻击,它可能是您唯一的选择。在使用类似的东西之前,请确保确实没有办法禁用内联脚本(这应该涵盖大多数攻击)。此外,您应该将您的反馈发送到 public-webappsec@w3.org 邮件列表,主题为 [CSP2]。
这是我的(不完整的)想法:
function guardMethods(clazz, methodNames, urlGetter, allowFilter, reportViolation) {
var prototype = clazz.prototype;
methodNames.forEach(function (methodName) {
var originalMethod = prototype[methodName];
if (originalMethod) {
Object.defineProperty(prototype, methodName, {
value: function () {
var url = urlGetter.apply(this, arguments) || '';
if (allowFilter(url)) {
return originalMethod.apply(this, arguments);
} else {
reportViolation(url);
}
}
});
}
})
}
function allowFilter(url) {
// todo: implement
}
function reportViolation(url) {
console.error('Redirection prevented:', url);
}
guardMethods(HTMLAnchorElement, ['click', 'dispatchEvent', 'fireEvent'], function () {return this.href}, allowFilter, reportViolation);
Run Code Online (Sandbox Code Playgroud)
您必须为 location、location.href、window.open 和其他允许重定向到其他页面的函数/属性/事件实现类似的防护。如果你只错过一个,那么你仍然很脆弱。CSP 本身可以覆盖表单、XHR 和大多数其他资源。据我所知,原型破解在某些较旧的浏览器中不起作用。
再次强调,我不建议使用这个。您犯错误或者它在某些浏览器中不起作用或者添加可用于重定向的新 API 的可能性太高了。
归档时间: |
|
查看次数: |
930 次 |
最近记录: |