Home
NaN / BoxAlive
Keep hackthebox.eu boxes from being reset
Newer
Older
BoxAlive / BoxAlive.php
#!/usr/bin/php
<?php
//error_reporting(0);

/***
 * Your details, change below
 */

$username = ""; // your username here
$password = ""; // your password here


/***
 * Main program - Don't edit below
 */
echo " _____         _____ _ _   \n| __  |___ _ _|  _  | |_|_ _ ___ \n| __ -| . |_'_|     | | | | | -_|\n|_____|___|_,_|__|__|_|_|\_/|___|\n";

if(!$argc)
	die('<h1>This script should be called in CLI environment</h1>');

$scriptName = array_shift($argv);

if(!isset($argv[0]) || $argc <= 2){
	print "\nKeep machine on hackthebox.eu from being reset.\n";
	print "Run: $scriptName [box] [server] \n\n";
	print "e.g. $scriptName Jerry eu-free-1\n\n";
	die();
}

$box = $argv[0];
$server = $argv[1];
$url = "https://www.hackthebox.eu/api/shouts/get/initial/info/html/30";
$post = true;

echo "[+] Watching for resets of $box on $server\n";

$data = fetch($url, $post);
$data_fixed = json_decode($data);
//print_r($data_fixed);

$found = array();
$tofind = array($box, $server, "reset", "/cancel");
foreach($data_fixed->html as $line){
	if (contains($line, $tofind)) {
		$no = getStringBetween($line,'/cancel</span> <span class="text-success">',"</span>");
		echo "[!] found: $no \n";
		$found[] = $no;
	}
}

while(true){
	$url = "https://www.hackthebox.eu/api/shouts/get/initial/info/html/30";
	$post = true;
	$data = fetch($url, $post);
	$data_fixed = json_decode($data);
	foreach($data_fixed->html as $line){

		if (contains($line, $tofind)) {
			$no = getStringBetween($line,'/cancel</span> <span class="text-success">',"</span>");
			if(!in_array($no, $found)){
				echo "[!] found: $no\n";
				$found[] = $no;

				$initial = get_csrf_token();
				$XSRF_Cookie = getStringBetween($initial,'XSRF-TOKEN=',"expires=");
				$session_cookie = getStringBetween($initial,'hackthebox_session=',"expires=");
				$token = getStringBetween($initial,'_token" value="','"> <div');
				$maincookie = "XSRF-TOKEN=".trim($XSRF_Cookie)." hackthebox_session=".trim($session_cookie); 

				$result = login($maincookie, $username, $password, $token);
				$XSRF_Cookie = getStringBetween($result,'XSRF-TOKEN=',"expires=");
				$session_cookie = getStringBetween($result,'hackthebox_session=',"expires=");
				$maincookie = "XSRF-TOKEN=".trim($XSRF_Cookie)." hackthebox_session=".trim($session_cookie); 

				$result2 = get_api_token($maincookie);
				$api_token = getStringBetween($result2,'post?api_token=',"', success:");

				send_message($api_token, "/cancel $no");

			}
		}

	}
	sleep(5);
}


function contains($string, array $array) {
    $count = 0;
    foreach($array as $value) {
        if (false !== stripos($string,$value)) {
            ++$count;
        };
    }
    return $count == count($array);
}
function GetStringBetween ($string, $start, $finish) {
    $string = " ".$string;
    $position = strpos($string, $start);
    if ($position == 0) return "";
    $position += strlen($start);
    $length = strpos($string, $finish, $position) - $position;
    return substr($string, $position, $length);
}
function get_csrf_token(){
	echo "[!] Getting Cookies + CSRF token\n"; 
	$url = "https://www.hackthebox.eu/login";
	$post = false;
	$data = fetch($url, $post, false, "https://www.hackthebox.eu/login", TRUE);
	return $data;
}
function login($cookie, $username, $password, $token){
	echo "[!] Logging in\n"; 
	$url = "https://www.hackthebox.eu/login";
	//$post = "email=".$username."&password=".$password."&_token=".$token;
	$postdata = array('email'=>$username,
              		'password'=>$password,
                	'_token'=>$token);
	$post = http_build_query($postdata) . "\n";
	$data = fetch($url, $post, $cookie, "https://www.hackthebox.eu/login", TRUE);
	return $data;
}
function get_api_token($cookie){
	echo "[!] Getting API token\n"; 
	$url = "https://www.hackthebox.eu/home";
	$post = false;
	$data = fetch($url, $post, $cookie, false, TRUE);
	return $data;
}
function send_message($token, $message){
	echo "[+] Cancelling reset request\n"; 
	$url = "https://www.hackthebox.eu/api/shouts/new/?api_token=$token";
	$postdata = array('text'=>$message);
	$post = http_build_query($postdata) . "\n";
	$data = fetch($url, $post);
	return $data;
}
function fetch($url,$post=false,$cookie=false,$referer=false,$showHeader=false, $timeout=30) {
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   if ($cookie) {
      curl_setopt($ch, CURLOPT_COOKIE, $cookie);
      
   }
   if ($post) {    
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   }
   
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
   curl_setopt($ch, CURLOPT_REFERER, $referer);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

   //curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8080'); //debug - proxy through burp

   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
   curl_setopt($ch, CURLOPT_HEADER, $showHeader); //debug
   //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking DEBUG
   $output=curl_exec($ch);
  
	$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); // DEBUG HEADERS
	echo $headerSent; // DEBUG HEADERS
   	curl_close($ch);
   	return $output;
}
?>