0

次のコードを使用してaspWebサイトにログインしていますが、機能しません。

<?php
/************************************************
* ASP.NET web site scraping script;
* Developed by MishaInTheCloud.com
* Copyright 2009 MishaInTheCloud.com. All rights reserved.
* The use of this script is governed by the CodeProject Open License
* See the following link for full details on use and restrictions.
*   http://www.codeproject.com/info/cpol10.aspx
*
* The above copyright notice must be included in any reproductions of
this script.
************************************************/
error_reporting(E_ALL);
/************************************************
* values used throughout the script
************************************************/
// urls to call - the login page and the secured page
$urlLogin = "http://www.website.com/Default.aspx";
$urlSecuredPage = "http://www.website.com/Default.aspx";

// POST names and values to support login
$nameUsername=rawurlencode('ctl00$txtLoginName');       // the name of the username textbox on the login form
$namePassword=rawurlencode('ctl00$txtPassword');       // the name of the password textbox on the login form
$nameLoginBtn=rawurlencode('ctl00$btnLogin');          // the name of the login button (submit) on the login form
$valUsername ='myusername';        // the value to submit for the username
$valPassword ='mypass';        // the value to submit for the password
$valLoginBtn ='Login';             // the text value of the login button itself

// the path to a file we can read/write; this will
// store cookies we need for accessing secured pages
$cookieFile = 'cookie.txt';

// regular expressions to parse out the special ASP.NET
// values for __VIEWSTATE and __EVENTVALIDATION
$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
$regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';


/************************************************
* utility function: regexExtract
*    use the given regular expression to extract
*    a value from the given text;  $regs will
*    be set to an array of all group values
*    (assuming a match) and the nthValue item
*    from the array is returned as a string
************************************************/
function regexExtract($text, $regex, $regs, $nthValue)
{

if (preg_match($regex, $text, $regs)) {
 $result = $regs[$nthValue];
}
else {
 $result = "";
}
return $result;
}



/************************************************
* initialize a curl handle; we'll use this
*   handle throughout the script
************************************************/
$ch = curl_init();


/************************************************
* first, issue a GET call to the ASP.NET login
*   page.  This is necessary to retrieve the
*   __VIEWSTATE and __EVENTVALIDATION values
*   that the server issues
************************************************/
curl_setopt($ch, CURLOPT_URL, $urlLogin);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data=curl_exec($ch);
echo curl_error($ch);
// from the returned html, parse out the __VIEWSTATE and
// __EVENTVALIDATION values
$viewstate = regexExtract($data,$regexViewstate,$regs,1);
$eventval = regexExtract($data,$regexEventVal,$regs,1);
/************************************************
* now issue a second call to the Login page;
*   this time, it will be a POST; we'll send back
*   as post data the __VIEWSTATE and __EVENTVALIDATION
*   values the server previously sent us, as well as the
*   username/password.  We'll also set up a cookie
*   jar to retrieve the authentication cookie that
*   the server will generate and send us upon login.
************************************************/
$postData = '__VIEWSTATE='.rawurlencode($viewstate)
          .'&__EVENTVALIDATION='.rawurlencode($eventval)
          .'&'.$nameUsername.'='.$valUsername
          .'&'.$namePassword.'='.$valPassword
          .'&'.$nameLoginBtn.'='.$valLoginBtn
          ;

curl_setOpt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_URL, $urlLogin);  
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);    

$data = curl_exec($ch);


/************************************************
* with the authentication cookie in the jar,
* we'll now issue a GET to the secured page;
* we set curl's COOKIEFILE option to the same
* file we used for the jar before to ensure the
* authentication cookie is sent back to the
* server
************************************************/
curl_setOpt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_URL, $urlSecuredPage);  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);    

$data = curl_exec($ch);

// at this point the secured page may be parsed for
// values, or additional POSTS made to submit parameters
// and retrieve data.  For this sample, we'll just
// echo the results.
echo $data;


/************************************************
* that's it! Close the curl handle
************************************************/
curl_close($ch);
?>

ウェブサイトのHTMLコード:

<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTIblablabla" />
</div>
<div>
<input type="hidden" name="__PREVIOUSPAGE" id="__PREVIOUSPAGE" value="W3sZnblblabla" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWjwEC7blblabla" />
</div>
<div id="ctl00_pnlLoginBox">
<span id="ctl00_Label1">Username:</span><br />
<input name="ctl00$txtLoginName" type="text" id="ctl00_txtLoginName" style="width:100px;" /><br />
<span id="ctl00_Label2">Password:</span><br />
<input name="ctl00$txtPassword" type="password" id="ctl00_txtPassword" style="width:100px;" /><br />
<input type="submit" name="ctl00$btnLogin" value="Login" id="ctl00_btnLogin" style="width:104px;" />
</div>
<div align="center" style="padding-top:2px">
<a href="/getpassword.aspx" id="ctl00_aForgotPassword">
<span>Forgot your Password</span>
</a>
</div>

次のエラーが発生します。

Notice: Undefined variable: viewstate in /home/me/test3.php on line 101
Notice: Undefined variable: eventval in /home/me/test3.php on line 102

手動でviewstateとeventvalを設定しようとしましたが、ログインしません。このWebサイトで見つけたさまざまなスクリプトを試しましたが、どれも機能しませんでした。誰かが私を少し助けてくれますか?

4

1 に答える 1

1

多くの理由が考えられます、私はいくつかをカバーします(実際のURLでこれをテストせずに言うのは難しいです)

アイデア1:配列ポインターが正しくない:関数がvarregexExtract()に値を割り当てていない ようです-これは、asを呼び出したためである可能性があります-これは、配列にデータが入力されたときに、配列の2番目の項目を抽出していることを意味します( php配列ポインタは0ベースであることを忘れないでください。)$viewstateregexExtract()$nthValue1$regspreg_match()

アイデア2:正規表現の論理的な問題 論理エラーのため、正規表現がどのコンテンツとも一致していません-繰り返しになりますが、コンテンツが返されるのを確認できない限り、これを支援することはできませんcurl_exec()-これらの正規表現は、asp.netを使用するサイト用に設計されていますネイティブ認証システム-asp.netサイトに確実にログインしていますか?

また、質問からasp-classicタグを削除してください。当てはまらないと思いますか?

編集:わかりました、私はいくつかのテストを行い、正規表現が正常に機能していることを証明するために$viewstate$eventval変数を取得しました(そして「1」が正しいポインタであることを確認します)。私のテストは、curlがビューステートを正しく抽出してログインページに投稿していることを証明しています。

次のphpファイル(echopost.php)を作成して、これをテストしました。

<?
foreach($_GET as $name => $value) {
    print "QueryString: $name : $value<br>";
}
foreach($_POST as $name => $value) {
    print "POST: $name : $value<br>";
}
$cookie=$_COOKIE;
foreach ($cookie as $name=>$value) {
    print "Cookie: $name : $value<br>";
}
?>

次に、この行99〜を変更して、ログイン情報をechopostに送信し、結果を出力しました(正規表現が正しく入力されたことを証明するため、$viewstateおよび$eventval

99行目〜

curl_setOpt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_URL, "http://www.heavencore.co.uk/so/echopost.php"); //#### Note this line that changes 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);     

$data = curl_exec($ch); 
echo "Response from echopost.php (login): <hr />" . $data; 

ご覧のとおり、エコーページは正規表現から正しい値をカールするために戻ってきています。これは、$viewstateと$eventvalが入力されて機能していることを意味します。

デモ:ダミーのログインページhttp://www.heavencore.co.uk/so/login.htm に基づくhttp://www.heavencore.co.uk/so/login.php

私はこの行を変更しました:

function regexExtract($text, $regex, $regs, $nthValue) 

これに:

function regexExtract($text, $regex, $nthValue)

&これらの行を変更しました:

$viewstate = regexExtract($data,$regexViewstate,$regs,1); 
$eventval = regexExtract($data,$regexEventVal,$regs,1); 

これに:

$viewstate = regexExtract($data,$regexViewstate,1); 
$eventval = regexExtract($data,$regexEventVal,1); 

編集2:

OK、これは正常に機能しています:

http://www.heavencore.co.uk/so/login.php

login.phpは「Welcometo/../、HeavenCore!」を出力することに注意してください。ページ-curlがOKでログインしたことを意味します。

私の情報源:

<?php 
/************************************************ 
* ASP.NET web site scraping script; 
* Developed by MishaInTheCloud.com 
* Copyright 2009 MishaInTheCloud.com. All rights reserved. 
* The use of this script is governed by the CodeProject Open License 
* See the following link for full details on use and restrictions. 
*   http://www.codeproject.com/info/cpol10.aspx 
* 
* The above copyright notice must be included in any reproductions of 
this script. 
************************************************/ 
error_reporting(E_ALL); 
/************************************************ 
* values used throughout the script 
************************************************/ 
// urls to call - the login page and the secured page 
$urlLogin = "http://www.website.com/Default.aspx"; 
$urlSecuredPage = "http://www.website.com/Default.aspx"; 

// POST names and values to support login 
$nameUsername=rawurlencode('ctl00$txtLoginName');       // the name of the username textbox on the login form 
$namePassword=rawurlencode('ctl00$txtPassword');       // the name of the password textbox on the login form 
$nameLoginBtn=rawurlencode('ctl00$btnLogin');          // the name of the login button (submit) on the login form 
$valUsername ='HeavenCore';        // the value to submit for the username 
$valPassword ='password';        // the value to submit for the password 
$valLoginBtn ='Login';             // the text value of the login button itself 

// the path to a file we can read/write; this will 
// store cookies we need for accessing secured pages 
$cookieFile = 'cookie.txt'; 

// regular expressions to parse out the special ASP.NET 
// values for __VIEWSTATE and __EVENTVALIDATION 
$regexViewstate = "/__VIEWSTATE\" value=\"(.*)\"/i"; 
$regexEventVal  = "/__EVENTVALIDATION\" value=\"(.*)\"/i"; 

/************************************************ 
* utility function: regexExtract 
*    use the given regular expression to extract 
*    a value from the given text;  $regs will 
*    be set to an array of all group values 
*    (assuming a match) and the nthValue item 
*    from the array is returned as a string 
************************************************/ 
function regexExtract($text, $regex, $nthValue) 
{ 

if (preg_match($regex, $text, $regs)) { 
 $result = $regs[$nthValue]; 
} 
else { 
 $result = ""; 
} 
return $result; 
} 


/************************************************ 
* initialize a curl handle; we'll use this 
*   handle throughout the script 
************************************************/ 
$ch = curl_init(); 


/************************************************ 
* first, issue a GET call to the ASP.NET login 
*   page.  This is necessary to retrieve the 
*   __VIEWSTATE and __EVENTVALIDATION values 
*   that the server issues 
************************************************/ 
curl_setopt($ch, CURLOPT_URL, $urlLogin); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0' ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$data=curl_exec($ch); 
echo curl_error($ch); 
// from the returned html, parse out the __VIEWSTATE and 
// __EVENTVALIDATION values 
$viewstate = regexExtract($data,$regexViewstate,1); 
$eventval = regexExtract($data,$regexEventVal,1); 
/************************************************ 
* now issue a second call to the Login page; 
*   this time, it will be a POST; we'll send back 
*   as post data the __VIEWSTATE and __EVENTVALIDATION 
*   values the server previously sent us, as well as the 
*   username/password.  We'll also set up a cookie 
*   jar to retrieve the authentication cookie that 
*   the server will generate and send us upon login. 
************************************************/ 
$postData = '__VIEWSTATE='.rawurlencode($viewstate) 
          .'&__EVENTVALIDATION='.rawurlencode($eventval) 
          .'&'.$nameUsername.'='.$valUsername 
          .'&'.$namePassword.'='.$valPassword 
          .'&'.$nameLoginBtn.'='.$valLoginBtn 
          ; 

curl_setOpt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_URL, $urlLogin);   
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);     

$data = curl_exec($ch); 
 echo "Response from echopost.php (login): <hr />" . $data; 

/************************************************ 
* with the authentication cookie in the jar, 
* we'll now issue a GET to the secured page; 
* we set curl's COOKIEFILE option to the same 
* file we used for the jar before to ensure the 
* authentication cookie is sent back to the 
* server 
************************************************/ 
curl_setOpt($ch, CURLOPT_POST, FALSE); 
curl_setopt($ch, CURLOPT_URL, $urlSecuredPage);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);     

$data = curl_exec($ch); 

// at this point the secured page may be parsed for 
// values, or additional POSTS made to submit parameters 
// and retrieve data.  For this sample, we'll just 
// echo the results. 



/************************************************ 
* that's it! Close the curl handle 
************************************************/ 
curl_close($ch); 
?>
于 2012-05-10T13:14:20.487 に答える