2012-03-17 52 views
1

我有一個腳本,用於從外部網站下載並顯示網頁。該網站會生成一次性令牌並將其存儲在隱藏的表單字段中,並將相同的令牌放入發送給用戶的Cookie中。在我的第一個捲曲的請求,我存儲的cookie:cURL:兩個單獨的請求,同一會話

$url = 'http://www.example.com/form.php'; 
$host = parse_url($url, PHP_URL_HOST); 
$referer = 'http://' . $host; 
$ip_address = $_SERVER['REMOTE_ADDR']; 

// Give the user a unique cookie file for each request 
$cookie_file = 'cookies/' . sha1($ip_address . $agent . $url) . '.txt'; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HTTPGET, 1); 
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $referer); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
curl_setopt($ch, CURLOPT_COOKIESESSION, false); 

$file = curl_exec($ch); 

這工作正常,並正確創建的cookie文件,當我打開它,它有它的一次性令牌,和它匹配令牌隱藏在表單字段中。

當我提交表單,我再拍捲曲的要求,使用相同的cookie:

$ch = curl_init($url); 

$postdata = http_build_query($postdata); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
$ip_address = $_SERVER['REMOTE_ADDR']; 
$cookie_file = 'cookies/' . sha1($ip_address . $agent . $url) . '.txt'; 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $referer); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($ch); 

的cookie文件名應該是相同的,第二次左右,因爲用戶的IP,代理和目標原始網址沒有改變。我已經驗證過它不會爲請求創建第二個cookie,也不會覆蓋第一個。

但是,當我提交表單時,即使我沒有更改隱藏表單字段,並且它匹配cookie中的一次性標記,並且我將HTTP referer設置爲目標url 。爲什麼CSRF檢查會失敗還有其他原因嗎?是否由於某種原因在第二次請求中使用了Cookie?任何幫助表示讚賞,謝謝:)

回答

0

您可能需要重新定義您的變量。您的$agent$url不會在幾秒鐘內發出cURL請求。

+0

他們在那裏,因爲我通過他們在那裏。如果他們不是,它會生成一個新的cookie,因爲名稱會有所不同。 – Joey 2012-03-17 20:09:26