我想模拟/演示CSRF攻击。使用Django,我设置了一个站点(a.com),一个url(/ foo /)不启用反CSRF的东西。
首先,我可以无需登录即可访问此URL。我可以看到POST请求(见下面的代码)成功完成。 (实际上,我遇到了一个no Access-Control-Allow-Origin
问题,文档#main
内容没有更新。但帖子是200状态完成。即钱转移到我的帐户:)。
所以我做了一次成功的CSRF攻击。
然后我将a.com/foo/
包裹在login_required()
中,这意味着,您需要先在网站a.com上登录才能在a.com/foo/
页面中执行操作。
1)我在a.com的一个Chrome标签上登录。
2)然后我在同一个Chrome窗口中打开另一个Tab并打开localhost:8000
(见下文)。
当我运行JS函数时,POST开始获得302状态(重定向到a.com
的登录页面而不是之前的200 OK状态)。
为什么?如果是这种情况,CSRF攻击如何对另一个域的登录所需页面有用/可能?
用于执行伪造请求的JS代码(来自localhost:8000等,不同的域):
function hack_it() {
var http = new XMLHttpRequest();
var url = 'https://a.com/foo/';
var params = 'name=hacker&amount=200';
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
document.getElementById("main").innerHTML = http.responseText;
}
}
http.send(params);
}
是的,对于需要登录的页面,可以进行CSRF攻击。
目前,您的AJAX请求未发送会话cookie,因此您将被重定向到登录页面。
要使您的演示攻击起作用,您需要将withCredentials
设置为true
。
var http = new XMLHttpRequest();
http.withCredentials = true;
...