Skip to content

Instantly share code, notes, and snippets.

@nbulischeck
Created March 11, 2019 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbulischeck/8c698caee08b931a67b32677e6d86e75 to your computer and use it in GitHub Desktop.
Save nbulischeck/8c698caee08b931a67b32677e6d86e75 to your computer and use it in GitHub Desktop.
import re
import os, shutil, pathlib
from zipfile import ZipFile, BadZipFile
from string import ascii_uppercase, ascii_lowercase
whitespace = ["\r", "\n", "\t", "\f", "\v", " "]
rep_var = ["{i}", "{j}", "{k}"]
def solve_challenge():
with open("hint.txt", "r") as f:
regex = f.read()[1:-1]
print(regex)
count = 0
while regex.find("[a-z]") != -1:
regex = regex.replace("[a-z]", rep_var[count], 1)
count += 1
if count == 1:
i_iter = ascii_lowercase
elif count == 2:
j_iter = ascii_lowercase
elif count == 3:
k_iter = ascii_lowercase
while regex.find("[A-Z]") != -1:
regex = regex.replace("[A-Z]", rep_var[count], 1)
count += 1
if count == 1:
i_iter = ascii_uppercase
elif count == 2:
j_iter = ascii_uppercase
elif count == 3:
k_iter = ascii_uppercase
while regex.find("\d") != -1:
regex = regex.replace("\d", rep_var[count], 1)
count += 1
if count == 1:
i_iter = range(0, 10)
elif count == 2:
j_iter = range(0, 10)
elif count == 3:
k_iter = range(0, 10)
while regex.find("\s") != -1:
regex = regex.replace("\s", rep_var[count], 1)
count += 1
if count == 1:
i_iter = whitespace
elif count == 2:
j_iter = whitespace
elif count == 3:
k_iter = whitespace
print(regex)
print(i_iter, j_iter, k_iter)
for i in i_iter:
for j in j_iter:
for k in k_iter:
with ZipFile('archive.zip') as zf:
try:
password = regex.format(i=i, j=j, k=k).encode()
zf.extractall("output", pwd=password)
return password
except:
pass
return None
for i in range(0, 10000):
# Crack the zip file
password = solve_challenge()
if not password:
quit()
# Remove the cracking artifacts
shutil.rmtree("output")
# Extract the contents to ./solved
with ZipFile('archive.zip') as zf:
zf.extractall("solved", pwd=password)
# Move the files for the next level up and replace current level
shutil.move("solved/archive.zip", "archive.zip")
shutil.move("solved/hint.txt", "hint.txt")
# If there are any remnants, we most likely hit the flag
for dirpath, dirnames, files in os.walk('solved'):
if files:
quit()
# If we didn't hit the flag, rm -rf ./solved
shutil.rmtree("solved")
@t94j0
Copy link

t94j0 commented May 24, 2019

Wow! Great project. Keep up the good work

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