Last active
April 2, 2018 12:42
-
-
Save pich4ya/39e9199b822e615929a84143735a919a to your computer and use it in GitHub Desktop.
SwampCTF 2018 - Locked Dungeon
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 python | |
# SwampCTF 2018 - Locked Dungeon | |
# https://play.swampctf.com/ | |
import argparse | |
from hashlib import sha256 | |
from Crypto.Cipher import AES | |
import os | |
import sys | |
BLOCK_SIZE = 16 | |
PAD_LIMIT = 48 | |
KEY = os.urandom(16) | |
pad_len = lambda inp: (BLOCK_SIZE - len(inp) % BLOCK_SIZE) | |
pad = lambda inp: inp + chr(pad_len(inp))*pad_len(inp) | |
class AESCipher: | |
def __init__(self, key): | |
self.key = sha256(key).digest() | |
def encrypt(self, raw): | |
cipher = AES.new(self.key, AES.MODE_ECB) | |
return "".join("{:02x}".format(ord(c)) for c in cipher.encrypt(raw)) | |
def mod_pad(self, inp, flag_size): | |
input_len = len(inp) | |
if input_len > PAD_LIMIT: | |
excess_len = input_len - PAD_LIMIT | |
if excess_len > flag_size: | |
padded_inp = inp[flag_size:flag_size + PAD_LIMIT] | |
else: | |
padded_inp = inp[:flag_size - excess_len] + inp[flag_size:] | |
return padded_inp | |
else: | |
padded_inp= pad(inp) | |
return padded_inp | |
def mod_encrypt(self, raw, flag_size): | |
raw = self.mod_pad(raw, flag_size) | |
encrypted_data = self.encrypt(raw) | |
return encrypted_data | |
if __name__ == "__main__": | |
with open("flag.txt") as fp: | |
flag = fp.read() | |
flag_size = len(flag) | |
if flag_size > PAD_LIMIT: | |
print("Flag is too big") | |
exit(1) | |
aescipher = AESCipher(key=KEY) | |
req_count = 0 | |
while req_count < 0x1900: | |
req_count += 1 | |
user_input = raw_input() | |
if len(user_input) > 0x64: | |
continue | |
sys.stdout.write(aescipher.mod_encrypt(flag + user_input, flag_size)) | |
sys.stdout.write('\n') | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment