import requests | |
import json | |
import re | |
import time | |
import random | |
machine = "machine (like Irked)" | |
server = "server (like eu-free-1)" | |
api_token = "access token for HTB RESTful API (like MpkzFFRaZyP3xd0BKCavc8a5m23y72OvcwXONjNdgYOvXijLfOBev6ajKq6z)" | |
request_headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246" } | |
get_shoutbox_url = "https://www.hackthebox.eu/api/shouts/get/initial/html/18?api_token={}".format(api_token) | |
post_shout_url = "https://www.hackthebox.eu/api/shouts/new/?api_token={}".format(api_token) | |
reset_pattern = re.compile("\[(.+?)\]\s(.+?)\srequested a reset on {}\s\[{}\]\s\[Type (\/cancel \d+?) within two minutes to cancel the reset\]".format(machine, server)) | |
def write_log(message, success=True): | |
log_template = "[{}] {}" | |
flag = "+" if success else "-" | |
print log_template.format(flag, message) | |
def get_shoutbox(old_shouts): | |
r = requests.post(get_shoutbox_url, headers=request_headers) | |
data = json.loads(r.text) | |
success = int(data['success']) | |
if not success: | |
raise Exception(r.text) | |
shouts = data['html'] | |
html_pattern = re.compile('<.*?>') | |
new_shouts = [re.sub(html_pattern, '', item) for item in shouts if re.sub(html_pattern, '', item) not in old_shouts] | |
return new_shouts | |
def cancel_reset(cancel_message): | |
r = requests.post(post_shout_url, headers=request_headers, data = {"text": cancel_message}) | |
data = json.loads(r.text) | |
success = int(data['success']) | |
return success | |
def random_sleep(): | |
sleep_seconds = random.randint(10, 30) | |
write_log("Waiting random delay - {} seconds".format(sleep_seconds), success=True) | |
time.sleep(sleep_seconds) | |
old_shoutbox = [] | |
while True: | |
shoutbox = get_shoutbox(old_shoutbox) | |
if len(shoutbox): | |
for e in shoutbox: | |
matches = reset_pattern.match(e) | |
if matches: | |
date = matches.group(1) | |
name = matches.group(2) | |
cancel_string = matches.group(3) | |
write_log("Detected reset from '{}'".format(name), success=True) | |
random_sleep() | |
if cancel_reset(cancel_string): | |
write_log("Reset canceled!") | |
else: | |
write_log("Could not cancel reset of '{}' - did somebody cancel it before us?".format(name), success=False) | |
old_shoutbox = old_shoutbox + shoutbox | |
random_sleep() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment