Created
March 11, 2014 05:13
-
-
Save starrify/9479862 to your computer and use it in GitHub Desktop.
solver for http://bctf.cn/problems/4
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
#!/usr/bin/env python2 | |
#-*- coding:utf-8 -*- | |
# This is solver for problem passcode in BCTF2014 | |
# See http://bctf.cn/problems/4 | |
# By Pengyu CHEN (cpy.prefers.you[at]gmail.com) | |
# COPY LEFT, ALL WRONGS RESERVED. | |
import base64 | |
import binascii | |
import zlib | |
import os | |
import random | |
import itertools | |
try: | |
import passcode | |
except ImportError: | |
raise ImportError('"passcode.py" required. Obtain it from http://bctf.cn/problems/4') | |
def get_func_combination(): | |
from passcode import f as f | |
for (f1, f2, f3, f4) in itertools.product(passcode.f, repeat=4): | |
try: | |
answer_hash = f['fun6'](f['fun2'](f[f1](f[f2](f[f3](f[f4](passcode.answer)))))) | |
assert(len(answer_hash) != 0) | |
print('Possible function combination: %s' %((f1, f2, f3, f4), )) | |
except (TypeError, ValueError, zlib.error, AssertionError): | |
pass | |
return | |
def get_answers(): | |
# obtained via invoking get_func_combination() | |
(f1,f2,f3,f4) = ('fun3', 'fun5', 'fun1', 'fun4') | |
from passcode import f as f | |
f_hash = lambda x: f['fun6'](f['fun2'](f[f1](f[f2](f[f3](f[f4](x)))))) | |
f_hash_stage2 = lambda x: f[f2](f[f3](f[f4](x))) | |
frev_hash_stage2 = lambda x: passcode.hex2dec(passcode.reverse(binascii.hexlify(x))) | |
frev_hash_stage1 = lambda x, z_level: zlib.compress(x, z_level) | |
frev_hash_stage0 = lambda x: base64.b64encode(x.encode('gb2312')) | |
answers = [] | |
answer_hash = f_hash(passcode.answer) | |
for z_level in range(10): # default is 6 | |
# ya for yet another | |
ya_hash = frev_hash_stage2(frev_hash_stage1(frev_hash_stage0(answer_hash), z_level)) | |
assert(f_hash(ya_hash) == answer_hash) | |
answers += [ya_hash] | |
return list(set(answers)) | |
if __name__ == '__main__': | |
get_func_combination() | |
answers = get_answers() | |
print('Here comes answers:') | |
for answer in answers: | |
print(answer) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment