使用PHP进行安全cPanel登录的脚本

use*_*862 1 php curl cpanel

从最近以来,cPanel已经改变了它登录的方式.

登录前,网址为:https:// accessurl:2083 /

登录后:https:// accessurl:2083/cpsessXXXX/frontend/x3/index.html?post_login = 89711792346495

您将注意到网址中嵌入的cpsessXXXX.

访问AWSTATS的页面是:https:// accessurl:2083/cpsessXXXX/awstats.pl?config = domain_name&ssl =&lang = en

我尝试了以下PHP代码

$username = 'xxx';
$password = 'xxx';
$loginUrl = 'https://<accessurl>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT,2083);
$store = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

当我单步执行代码时,$ store的值为FALSE,这意味着登录过程失败.

我在网上发现的类似问题的唯一参考是在3月28日的http://blog.mcfang.com/.

我希望cookies.txt会有cpsessXXXX信息,但是没有创建文件.

谢谢你的帮助

小智 5

您需要将安全性令牌引用回cPanel,以便接受您的脚本.根据安全令牌的cPanel文档中的文档

请看以下示例:

function createSession() { // Example details
$ip = "127.0.0.1";
$cp_user = "username";
$cp_pwd = "password";
$url = "http://$ip:2082/login";
$cookies = "/path/to/storage/for/cookies.txt";

// Create new curl handle
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);

// Execute the curl handle and fetch info then close streams.
$f = curl_exec($ch);
$h = curl_getinfo($ch);
curl_close($ch);

// If we had no issues then try to fetch the cpsess
if ($f == true and strpos($h['url'],"cpsess"))
{
    // Get the cpsess part of the url
    $pattern="/.*?(\/cpsess.*?)\/.*?/is";
    $preg_res=preg_match($pattern,$h['url'],$cpsess);
}

// If we have a session then return it otherwise return empty string
return (isset($cpsess[1])) ? $cpsess[1] : "";
} 
Run Code Online (Sandbox Code Playgroud)

cpsess用于将URL附加到cPanel期望的正确令牌.