Skip to content

Instantly share code, notes, and snippets.

@yashodhank
Created June 5, 2024 16:53
Show Gist options
  • Save yashodhank/8565a9fadf41721bd862dd127edea256 to your computer and use it in GitHub Desktop.
Save yashodhank/8565a9fadf41721bd862dd127edea256 to your computer and use it in GitHub Desktop.

DISCLAIMER:

This script is provided as a Proof of Concept (POC) and has never been tested in a real-world scenario. It comes with no warranty or guarantee that it will function as expected. Use it at your own risk. If you encounter any issues, good luck in fixing them and making it work for your purposes. The authors bear no responsibility for any consequences arising from the use of this script.

Changes Made:

  1. Replaced hardcoded script path with __DIR__ to make it relative to the script's current directory.

  2. Adjusted log file and JSON file paths to use the relative path.

  3. Domain Fetching: Fetches a domain from https://api.mail.tm/domains.

  4. Account Creation: Creates a new email account using the domain from Mail.tm.

  5. Token Retrieval: Retrieves the token required for authentication from Mail.tm API.

  6. Message Checking: Modified the part of the script that checks for messages using the new Mail.tm API endpoints.

Instructions:

  1. Set the Anti-Captcha Key:

    • Update the $anticaptcha_key variable with your Anti-Captcha API key.
  2. Upload the Script:

    • Upload auto_plesk_trial.php to any desired path.
  3. Create a Scheduled Task:

    • In the Plesk root account, create a scheduled task to run the PHP script at the specified path.
  4. Monitor Logs:

    • Check the log file generated by the script for status updates and error messages.
<?php
/*
Make sure to enter $anticaptcha_key before upload to server.
Upload "auto_plesk_trial.php" file to any desired path.
Then create a Scheduled Task in the Plesk Root account.
Task Type: Run a PHP script
Script path: [path to auto_plesk_trial.php]
Use PHP version: 7.x.x / 8.x.x
Run: Cron style * * * * *
Notify: Errors only
*/
$update_frequency = 60 * 60 * 24 * 14; // Update Every 14 Days
$anticaptcha_key = ""; // Anti-Recaptcha Key
set_time_limit(600);
$filename = __DIR__ . "/" . pathinfo(__FILE__, PATHINFO_FILENAME) . ".json";
function update_log($text) {
$fh = fopen(__DIR__ . "/" . pathinfo(__FILE__, PATHINFO_FILENAME) . ".log", "a");
fwrite($fh, date("[Y-m-d H:i:s] ") . $text . "\n");
fclose($fh);
echo date("[Y-m-d H:i:s] ") . $text . "\n";
}
$info = json_decode(@file_get_contents($filename), true);
if (!is_array($info)) $info = array();
if (!isset($info["last_updated"])) $info["last_updated"] = 0;
if ($info["last_updated"] + $update_frequency > time()) {
exit();
}
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "AutoPleskTrial/2.0",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSLVERSION => defined("CURL_SSLVERSION_TLSv1_3") ? CURL_SSLVERSION_TLSv1_3 : CURL_SSLVERSION_TLSv1_2
);
$ch = curl_init();
if (!isset($info["email_address"])) {
// Step 1: Get domain
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_URL => "https://api.mail.tm/domains"
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$domains = json_decode($response, true);
if (isset($domains["hydra:member"][0]["domain"])) {
$domain = $domains["hydra:member"][0]["domain"];
} else {
update_log("Error: unable to fetch domain");
exit();
}
} else {
update_log("Error: unable to fetch domain");
exit();
}
// Step 2: Create account
$email = "user" . rand(1000, 9999) . "@$domain";
$password = "password" . rand(1000, 9999);
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_URL => "https://api.mail.tm/accounts",
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"address" => $email,
"password" => $password
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 201) {
$account = json_decode($response, true);
$info["email_address"] = $account["address"];
$info["email_password"] = $password;
file_put_contents($filename, json_encode($info));
update_log("Update: email_address = " . $info["email_address"]);
} else {
update_log("Error: unable to create account");
exit();
}
// Step 3: Get token
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_URL => "https://api.mail.tm/token",
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"address" => $info["email_address"],
"password" => $info["email_password"]
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$token_response = json_decode($response, true);
$info["email_token"] = $token_response["token"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_token = " . $info["email_token"]);
} else {
update_log("Error: unable to get token");
exit();
}
}
// The rest of the script continues as usual with the new email and token...
if (!isset($info["task_id"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://api.anti-captcha.com/createTask",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"clientKey" => $anticaptcha_key,
"task" => array(
"type" => "RecaptchaV2TaskProxyless",
"websiteURL" => "https://www.plesk.com/plesk-free-download/",
"websiteKey" => "6LfC_lQaAAAAAES5qQ5CZbVfE1_RoA_azMlBZQkN"
)
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && $result = json_decode($response, true)) {
if (isset($result["errorId"]) && $result["errorId"] == 0 && isset($result["taskId"])) {
$info["task_id"] = $result["taskId"];
file_put_contents($filename, json_encode($info));
update_log("Update: task_id = " . $info["task_id"]);
exit();
} else {
update_log("Error: task_id = null");
exit();
}
} else {
update_log("Error: task_id = null");
exit();
}
}
if (!isset($info["g_recaptcha_response"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://api.anti-captcha.com/getTaskResult",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"clientKey" => $anticaptcha_key,
"taskId" => $info["task_id"]
))
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && $result = json_decode($response, true)) {
if (isset($result["errorId"]) && $result["errorId"] == 0 && isset($result["status"])) {
if ($result["status"] == "ready" && isset($result["solution"]["gRecaptchaResponse"])) {
$info["g_recaptcha_response"] = $result["solution"]["gRecaptchaResponse"];
file_put_contents($filename, json_encode($info));
update_log("Update: g_recaptcha_response = " . $info["g_recaptcha_response"]);
} elseif ($result["status"] !== "processing") {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: g_recaptcha_response = null");
exit();
} else {
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: g_recaptcha_response = null");
exit();
}
} else {
update_log("Error: task_id = null");
exit();
}
}
if (!isset($info["confirm_requested"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_URL => "https://www.plesk.com/plesk-free-download/",
CURLOPT_POSTFIELDS => http_build_query(array(
"input_1.3" => "John",
"input_1.6" => "Scott",
"input_2" => $info["email_address"],
"input_15" => "Developer",
"input_16" => "Junior",
"input_17" => "Canada",
"input_21.1" => "true",
"g-recaptcha-response" => $info["g_recaptcha_response"],
"is_submit_55" => "1",
"gform_submit" => "55",
"gf_zero_spam_key" => "eN4*@MnEel3KQ!o!zyCZK5j*Om\$wQ82kI10BI$*
AW7\$IKutxZerPTGUoGTRKxor@"
)),
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 302 && preg_match("/[Ll]ocation: https:\/\/www\.plesk\.com\/free-trial-for-web-professionals-thank-you-page\//", $response)) {
$info["confirm_requested"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_requested = true");
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_requested = false");
exit();
}
}
if (!isset($info["trial_requested"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://www.plesk.com/free-trial-for-web-professionals-thank-you-page/",
CURLOPT_HTTPHEADER => array(
"Cookie: Plesk_TrialFormData=".urlencode(base64_encode(json_encode(array(
"name" => "John",
"lastname" => "Scott",
"email" => $info["email_address"]
))))
),
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200 && preg_match("/[Ss]et-[Cc]ookie: Plesk_TrialForm_Status=1;/", $response)) {
$info["trial_requested"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: trial_requested = true");
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: trial_requested = false");
exit();
}
}
if (!isset($info["email_id"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://api.mail.tm/messages",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$data = json_decode($response, true);
if (isset($data["hydra:member"]) && is_array($data["hydra:member"])) {
foreach ($data["hydra:member"] as $mail) {
if ($mail["subject"] === "Please confirm your email address") {
$info["email_id"] = $mail["id"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_id = " . $info["email_id"]);
break;
}
}
if (!isset($info["email_id"])) {
exit();
}
} else {
exit();
}
} else {
update_log("Error: email_id = null");
exit();
}
}
if (!isset($info["confirm_link"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://api.mail.tm/messages/" . $info["email_id"],
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && $data = json_decode($response, true)) {
if (isset($data["html"]) && preg_match("/please confirm your email address by clicking on the link below:(?:.*?)<a(?:.*?)href=\"(.*?)\"(?:.*?)>(?:.*?)CONFIRM MY EMAIL(?:.*?)<\/a>/s", $data["html"], $matches)) {
$info["confirm_link"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link = " . $info["confirm_link"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_link = null");
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: confirm_link = null");
exit();
}
}
if (!isset($info["confirm_link2"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link"]
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && preg_match("/window\.location\.replace\(\"(.*?)\"\);/", $response, $matches)) {
$info["confirm_link2"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link2 = " . $info["confirm_link2"]);
} else {
update_log("Error: confirm_link2 = null");
exit();
}
}
if (!isset($info["confirm_link3"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link2"],
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 307 && preg_match("/[Ll]ocation: (https:\/\/api(.*)\.hubapi\.com\/email\/v1\/optin\/confirm\/doi\?(?:[^\r\n]*))/", $response, $matches)) {
$info["confirm_link3"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: confirm_link3 = " . $info["confirm_link3"]);
} else {
update_log("Error: confirm_link3 = null");
exit();
}
}
if (!isset($info["email_confirmed"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => $info["confirm_link3"],
CURLOPT_HEADER => true
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 307 && preg_match("/[Ll]ocation: https:\/\/page\.plesk\.com\/confirmsubscribe/", $response)) {
$info["email_confirmed"] = true;
file_put_contents($filename, json_encode($info));
update_log("Update: email_confirmed = true");
} else {
update_log("Error: email_confirmed = false");
exit();
}
}
if (!isset($info["email_id2"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://api.mail.tm/messages",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$data = json_decode($response, true);
if (isset($data["hydra:member"]) && is_array($data["hydra:member"])) {
foreach ($data["hydra:member"] as $mail) {
if ($mail["subject"] === "Your Plesk Trial Activation Code") {
$info["email_id2"] = $mail["id"];
file_put_contents($filename, json_encode($info));
update_log("Update: email_id2 = " . $info["email_id2"]);
break;
}
}
if (!isset($info["email_id2"])) {
exit();
}
} else {
exit();
}
} else {
update_log("Error: email_id2 = null");
exit();
}
}
if (!isset($info["activation_code"])) {
curl_reset($ch);
curl_setopt_array($ch, $curl_options + array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_URL => "https://api.mail.tm/messages/" . $info["email_id2"],
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $info["email_token"]
)
));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200 && $data = json_decode($response, true)) {
if (isset($data["html"]) && preg_match("/Now here's your unique activation code:(?:.*?)>([A-Z0
-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6}-[A-Z0-9]{6})</s", $data["html"], $matches)) {
$info["activation_code"] = $matches[1];
file_put_contents($filename, json_encode($info));
update_log("Update: activation_code = " . $info["activation_code"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: activation_code = null");
exit();
}
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: activation_code = null");
exit();
}
}
$status = trim(shell_exec("/usr/sbin/plesk bin license --install " . $info["activation_code"]));
if ($status === "The license key was successfully installed.") {
$info = array(
"last_updated" => time()
);
file_put_contents($filename, json_encode($info));
update_log("Update: status = " . $status);
update_log("Update: last_updated = " . $info["last_updated"]);
} else {
$info = array(
"last_updated" => $info["last_updated"]
);
file_put_contents($filename, json_encode($info));
update_log("Error: status = " . $status);
}
?>
@denizci2000
Copy link

Does it work now with new plesk trial system. now trial licenses limit only up to 3 domains.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment