您好,登錄后才能下訂單哦!
小編給大家分享一下php中curl帶有csrf-token驗證模擬提交的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
通常為了安全會在表單里加入一個隨機的token值來防止csrf攻擊。
要想模擬提交有token驗證的網(wǎng)站其實也不難。
1.通過正則獲取token
2.帶上獲取到的token模擬提交
下面是一個成功的例子
目錄結(jié)構(gòu)
│ form.php –需要模擬的表單 │ getForm.php – 模擬提交程序 │ post.php –表單驗證程序 │ └─cookie – cookie存放目錄
getForm.php
<?php $cookie_file = './cookie/'.time().'.cookie'; $str = getResponse('http://a.curl.com:81/form.php',[],$cookie_file); setcookie("PHPSESSID", "vc0heoa6lfsi3gger54pkns152"); preg_match('/<input name="token" type="hidden" value="(.*)"/U', $str, $match); $post['token'] = $match[1]; $post['name'] = '3333333'; $post['password'] = '12121213'; print_r(getResponse('http://a.curl.com:81/post.php', $post, $cookie_file)); function getResponse($url, $data=[], $cookie_file='', $timeout = 3) { if(empty($cookie_file)) { $cookie_file = '.cookie'; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, "https://www.baidu.com"); //構(gòu)造來路 curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36"); if(!empty($data)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);// 取cookie的參數(shù)是 curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file); //發(fā)送cookie curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); try { $handles = curl_exec($ch); curl_close($ch); return $handles; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } unlink($cookie_file); }
form.php
<?php session_start(); $_SESSION['token'] = md5($_SERVER['REQUEST_TIME']); $_SESSION['time'] = date("Y-m-d H:i:s"); session_write_close(); //echo $_SESSION['auth']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> </head> <body> <form action="post.php" method="post"> <p><input name="name" type="text"></p> <p><input name="password" type="password"></p> <p><input name="token" type="hidden" value="<?php echo $_SESSION['token']?>"></p> <p><input type="submit"></p> </form> </body> </html>
post.php
<?php session_start(); if(empty($_POST['token'])) { exit ("token is empty!"); } if(empty($_SESSION['token'])) { exit ("session is empty"); } if($_POST['token'] != $_SESSION['token']) { exit ("token "); } else { unset($_SESSION['token']); } echo PHP_EOL; echo "pass"; print_r($_REQUEST); echo PHP_EOL; print_r($_SERVER);
以上是“php中curl帶有csrf-token驗證模擬提交的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。