SU-CTF 2014 - Cryptography 100 - Huge key

Brute-force the key of a weak AES encryption implementation and decrypt the message.

Introduction

This is a write-up about one of the Sharif University CTF cryptography challenge. The goal of this challenge was to recover the original content of an AES encrypted file.

Context

We were given two files, the PHP script below which was used to encipher the flag and ciphertext.bin which the IV concatenated with the encrypted flag.The PHP script encipher the flag with Rijndael which is the algorithm used in AES, used functions are part of the mcrypt library.

The flaw inside AES key generation

The flaw lies in the foreach loop, each iteration only the first two bytes of the key are XORed.This leave us with an AES key with mostly NULL bytes in it. All we have to do to decrypt the flag is to brute-force the first two bytes.