我想为asp密码保护的网页开发自动剪贴簿。我有此页面的登录名/密码。
首先,通过firefox在授权期间查看Firebug日志。我发现了:
http://mysite
http://mysite/Account/Login
进行POST查询,同时使用步骤1中保存的cookie http://mysite/Account/Index
(我要废弃的页面)我的代码
//1. Get __RequestVerificationToken cookie
$urlLogin = "http://mysite";
$cookieFile = "cookie.txt";
$regs=array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlLogin);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_STDERR,$f = fopen("answer.txt", "w+"));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0' );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
$data=curl_exec($ch);
//2. Parse token value for the post request
$hash=file_get_contents("answer.txt");
preg_match_all('/=(.*); p/i',$hash, $regs);
//3. Make a post request
$postData = '__RequestVerificationToken='.$regs[1][0].'&UserName=someLogin'.'&Password=somePassword';
$urlSecuredPage = "http://mysite/Account/Login";
curl_setopt($ch, CURLOPT_URL, $urlSecuredPage);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
$data = curl_exec($ch);
curl_close($ch);
在步骤3中,我在步骤1中保存的cookie将使用__RequestVerificationToken的新值进行重写。我不明白为什么会这样。因此,我无法通过错误的__RequestVerificationToken进行授权并获得HTTP 500错误。
我哪里错了?
答案 0 :(得分:2)
__RequestVerificationToken应该有两件事。其中一个是隐藏的输入值,第二个是cookie。隐藏输入值的值在每个请求中发送。对于每个请求,它都有一个新值。这取决于cookie值。
因此您需要保存输入值和cookie,并将它们一起发送回来。如果您不从隐藏输入发送值,那么Asp.Net MVC认为这是一次攻击,并生成新的cookie。仅当验证失败或cookie本身不存在时,才会生成新cookie。如果你得到那个cookie,并且总是发送带有POST请求的__RequestVerificationToken输入值,那么它不应该生成新的cookie。
如果它仍然生成,那么您从隐藏的输入值发送错误的__RequestVerificationToken。尝试从Fiddler \ Charles那里做同样的事情,检查是否会返回成功结果。
它们用于防止CSRF攻击。
答案 1 :(得分:0)
正确的代码
$urlLogin = "http://mysite";
$cookieFile = "/Volumes/Media/WebServer/aszh/cookie.txt";
$regs=array();
$ch = curl_init();
//Make GET request and get __RequestVerificationToken cookie
curl_setopt($ch, CURLOPT_URL, $urlLogin);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_STDERR,$f = fopen("/Volumes/Media/WebServer/aszh/answer.txt", "w+"));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0' );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
$data=curl_exec($ch);
//Parse answer and get __RequestVerificationToken hidden input value
preg_match_all('/type="hidden" value="(.*)" /i', $data, $regs);
$token = $regs[1][0];
$postData = array('__RequestVerificationToken'=>$token,
'UserName'=>'userName',
'Password'=>'password');
//Make POST request and get .ASPXAUTH cookie
$urlSecuredPage = "http://mysite/Account/Login";
curl_setopt($ch, CURLOPT_URL, $urlSecuredPage);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
$data = curl_exec($ch);
curl_close($ch);