Created
May 2, 2017 06:33
-
-
Save zTrix/c17acd49328d7f6f8090c7ee0b97bec6 to your computer and use it in GitHub Desktop.
One script solve all crackme2000 in defcon ctf quals 2017
This file contains hidden or 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
#!/usr/bin/env python2 | |
import os, sys | |
import string | |
import subprocess | |
prefix = 'enlightenment' # change this to solve other crackme2000 challenges | |
def bf(bp): | |
flag = '' | |
while True: | |
for i in string.printable: | |
p = subprocess.Popen(bp, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = p.communicate(flag + i) | |
exit_code = p.returncode | |
print repr(flag), i, exit_code | |
if exit_code != len(flag)+1 or stderr: | |
flag += i | |
print 'found', i | |
print 'flag', repr(flag) | |
if exit_code == 0: | |
return flag | |
break | |
else: | |
raise Exception('flag not found') | |
if flag.endswith('00000000000000') or len(flag) > 64: | |
raise Exception('error flag: %s' % bp) | |
return flag | |
# bf(prefix + '_dist/fe6582d661a0a5dcfd63d6fc8e3302b97b0341df166978dffd022a0bc9f24d86') | |
# sys.exit() | |
def handle(x): | |
print 'handling', x | |
i = x | |
if os.path.exists('ans/%s' % i): return | |
flag = bf(prefix + '_dist/%s' % i) | |
with open('ans/%s' % i, 'wb') as f: | |
f.write(flag) | |
def worker(): | |
while True: | |
item = q.get() | |
if not item: | |
return | |
thread = Thread(target=handle, args=(item,)) | |
thread.start() | |
thread.join(30) | |
# handle(item) | |
q.task_done() | |
from queue import Queue | |
from threading import Thread | |
q = Queue() | |
for i in range(4): | |
t = Thread(target=worker) | |
t.daemon = True | |
t.start() | |
targets = filter(lambda x:len(x) == 64, os.listdir(prefix + '_dist')) | |
for item in targets: | |
q.put(item) | |
q.join() # block until all tasks are done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment