Created
August 5, 2022 01:51
-
-
Save itsfolf/4b16565adfb00d7054dd7de0c537fcf3 to your computer and use it in GitHub Desktop.
StudSec CTF Token Challenge Cracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging # Have to use logging to print inside signals | |
import time | |
import fcntl | |
import os | |
import signal | |
from randcrack import RandCrack | |
import sys | |
dirname = "/tmp/" | |
filename = "py-log1376543.txt" | |
rc = RandCrack() | |
is_ready = False | |
queue = [] | |
def handler(): | |
last_line = read_file()[-1] | |
logging.info(f"New passwd change: {last_line}") | |
if is_ready: | |
logging.info(f'Next code prediction: {"".join(rc.predict_choice("0123456789abcdef") for _ in range(10)).upper()}') | |
else: | |
queue.append(last_line) | |
def main(): | |
global is_ready | |
start_file_listener(filename, handler) | |
lines = read_file() | |
randints = lines[1:701] | |
codes = lines[704:] | |
start_timer("Seeding with 624 initial ints...") | |
for _ in range(624): | |
rc.submit(int(randints.pop(0))); | |
start_timer(f"Feeding the last {len(randints)} ints...") | |
while len(randints) > 0: | |
real = int(randints.pop(0)) | |
pred = rc.predict_randint(0, 4294967294) | |
if not real == pred: | |
sys.exit(f"Something went wrong while feeding ints. Real = {real}, Pred = {pred}") | |
total_len = len(codes) | |
logging.info(f"Feeding all the {total_len} previous codes.") | |
start_timer(f"This will take a while, go grab some coffee... (Approx {2500 * total_len / 1e+6} sec)") | |
while len(codes) > 0: | |
prg = (total_len - len(codes)) / total_len * 100 | |
if (prg > 0 and prg % 5 == 0): | |
logging.info(str(round(prg)) + '%...') | |
for _ in range(10): | |
#rc.predict_choice('0123456789abcdef') | |
rc.predict_randbelow(16) | |
codes.pop(0) | |
handle_queue() | |
reset_timer() | |
logging.info(f'Next code prediction: {"".join(rc.predict_choice("0123456789abcdef") for _ in range(10)).upper()}') | |
is_ready = True | |
def handle_queue(): | |
while len(queue) > 0: | |
for _ in range(10): | |
rc.predict_choice('0123456789abcdef') | |
queue.pop(0) | |
if len(queue) > 0: | |
handle_queue() | |
#for pos in range(0, 700): | |
# starting = randints[int(pos)] | |
# rc = RandCrack() | |
# for pos2 in range(int(pos), min(int(pos)+624, 700)): | |
# rc.submit(int(randints[pos2])); | |
# if rc.predict_randint(0, 4294967294) == randints[min(int(pos)+625, 699)]: | |
# print("Pog?") | |
# else: | |
# print("F") | |
def read_file(): | |
with open(dirname + filename, "r") as f: | |
return f.readlines() | |
prevLastModifiedTime = 0 | |
def start_file_listener(filename, handler): | |
def check_changes(*_): | |
global prevLastModifiedTime | |
lastModifiedTime = os.stat(filename).st_mtime | |
if lastModifiedTime != prevLastModifiedTime: | |
prevLastModifiedTime = lastModifiedTime | |
handler() | |
signal.signal(signal.SIGIO, check_changes) | |
fd = os.open(dirname, os.O_RDONLY) | |
fcntl.fcntl(fd, fcntl.F_SETSIG, 0) | |
fcntl.fcntl(fd, fcntl.F_NOTIFY, fcntl.DN_MODIFY | fcntl.DN_CREATE | fcntl.DN_MULTISHOT) | |
start_time = 0 | |
def start_timer(line): | |
global start_time | |
if (start_time > 0): | |
reset_timer() | |
start_time = time.time() | |
if line: | |
logging.info(line) | |
def reset_timer(): | |
global start_time | |
logging.info("Took {}ms.".format(round((time.time() - start_time) * 1000))) | |
start_time = 0 | |
logging.basicConfig(level=logging.NOTSET, format='%(message)s') | |
main() | |
while True: | |
time.sleep(10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment