Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Last active December 5, 2017 14:54
Show Gist options
  • Save AhnMo/3ae17406ba17f169182f027e5e90b469 to your computer and use it in GitHub Desktop.
Save AhnMo/3ae17406ba17f169182f027e5e90b469 to your computer and use it in GitHub Desktop.
2017 WITHCON Prequals - 다산관 제육볶음 - Q10. catdb
from Crypto.Cipher import Blowfish
from struct import unpack
def once(d):
iv = '\x01\x02\x03\x04\x05\x06\x07\x08'
key, d = d[:2], d[2:]
name, d = d[:8], d[8:]
iv, name = name, Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(name)
print 'fileName:', name
comm, d = d[:8], d[8:]
iv, comm = comm, Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(comm)
print 'comment:', comm
length, d = unpack('<H', d[:2])[0], d[2:]
data = d
data = data + '\x00' * (8 - len(data) % 8)
resu = ''
for i in range(0, len(data), 8):
cur = data[i: i + 8]
iv, cur = cur, Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(cur)
resu += cur
if '\xFF\xD9\x00' in resu[-16:]:
break
offs = len(resu)
resu, d = resu[:offs], d[offs:]
with open(name.strip('\0'), 'wb+') as f:
f.write(resu)
return d
def main():
with open('cat.db', 'rb') as f:
assert 'MEOW' == f.read(4)
data = f.read()
while len(data) > 0:
print '----------------------------------------'
data = once(data)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment