The CSAW CTF 2013 online qualifiers were held last weekend (09/19/2013 through 09/22/2013). The top 10 undergraduate teams will participate in the CSAW CTF finals in November; however the qualifiers were open to everyone and a small team from Digital Operatives participated. Below is a writeup of one of the Crypto challenges.

The Crypto 300 challenge was contained entirely in a tarball that contains a custom encryption Python script and nine encrypted files. The encryption algorithm reuses a single 256-byte key to XOR each subsequent block of the input file. Simple XOR encryption of uncompressed files often leads to the key sticking out of the ciphertext when the input file contains many zeroes. This is also the case with some rudimentary binary packers that XOR data inside themselves. In the case of Crypto 300, thousands of blocks were given to us in the ciphertext files, providing many opportunities to find (0 XOR key[i]) instances scattered throughout the files. For instance, if at byte offsets 0+blocksize * x (where x is a non-negative integer) in the ciphertexts frequently contain 0x40, it is likely that byte 0 of the key is 0x40.

We created a simple Python script to count the number of times each byte value occurs at each block offset.

import os
import sys



for x in range(0,9):
  fxname = prefix + str(x) + suffix
    print "Opening " + fxname
    fx = open(fxname,'rb')
    print "Failed to open " + fxname
  moretoread = True
  while moretoread:
    block =
    if(len(block) < blocksize):
      moretoread = False
      print "Last block was " + str(len(block)) + " bytes."

print "Extracted " + str(len(blocks)) + " blocks."

#Calculate the number of times each byte value occurs at each position in a block
histogram = [[0 for i in range(blocksize)] for j in range(blocksize)]
for block in blocks:
  for b in range(0, len(block)):
    val = ord(block[b])
    histogram[b][val] = histogram[b][val] + 1

#Get the most used byte value for each position in the block
maxvals=[0 for i in range(blocksize)]
for hidx in range(0, len(histogram)):
  bytearr = histogram[hidx]
  cur_max_pos = 0
  cur_max_count = 0
  for idx in range(0,len(bytearr)):
    count = bytearr[idx]
    if count > cur_max_count:
      cur_max_count = count
      cur_max_pos = idx
  maxvals[hidx] = cur_max_pos

f = open("newsecretkey.dat","wb")

print "Done"

With the key in ournewsecretkey.dat, we are able to decrypt all of the files from the challenge output folder with some simple Python borrowed from

import os
import sys
import argparse


parser = argparse.ArgumentParser(description="Decryption")
parser.add_argument('--infile', metavar='i', nargs='?',
  type=argparse.FileType('r'), help='input file, defaults to standard in',
parser.add_argument('--outfile', metavar='o', nargs='?',
  type=argparse.FileType('wb'), help='output file, defaults to standard out',
parser.add_argument('--secretkey', metavar='s', nargs='?',
  type=argparse.FileType('a+'), help='output file, defaults to secretkey.dat',

args = parser.parse_args()

keydata =
print "Using secret key: "
print keydata

while 1:
  byte =
  if not byte:
  args.outfile.write(chr(ord(keydata[counter % len(keydata)]) ^ ord(byte)))

sys.stderr.write('n' +
  ('Secret keyfile: %sn' % ( +
  ('Input file:     %sn' % ( +
  ('Output file:    %sn' % ( +
  ('Total bytes:    %dn' % (counter)))

Use the following commands with the above:

./ --infile=output/file4.enc --outfile=file4.enc.gz --secretkey=newsecretkey.dat
gzip -d file4.enc.gz

After decryption we have nine plaintext files. The fifth file, file4.enc, is a gzip-compressed ASCII file that contains a message and the key: BuildYourOwnCryptoSoOthersHaveJobSecurity

For Hackers nostalgia, play the MIDI file0!