Skip to content

Instantly share code, notes, and snippets.

@dadatuputi
Created May 25, 2020 17:54
Show Gist options
  • Save dadatuputi/5ceb2d258664d8fa2961aa43647d4fa5 to your computer and use it in GitHub Desktop.
Save dadatuputi/5ceb2d258664d8fa2961aa43647d4fa5 to your computer and use it in GitHub Desktop.
CTF: Derbycon 2016 Hilary E-Mail Buster
## ## ##
### ### # # # ##### # # #####
# # # # # # # # # #
# # ## # # ### ## # ## ## # #### ## ### ## # # # ## ## ## ### ## ## #
##### # # # # # ## # # ### # # # # # # # #### # # # # # # ##
# # # # # ### # # # # # # # ### # # # # # # # # #### #
# # # # # # # # # # # # # # # # # # # # # # # # # #
### ### ### ### ### #### ### # ##### ### ## ## #### ### ### ##### #### ## ## ### ###
# #
##
# iterate through a file by chunk_size-chunks
def read_in_chunks(infile, chunk_size):
chunk = infile.read(chunk_size)
while chunk:
yield chunk
chunk = infile.read(chunk_size)
# get the location of a chunk in another file
def get_chunk_location(infile, search_string):
location = infile.tell()
chunky = infile.read(len(search_string))
while chunky:
if (search_string == chunky):
infile.seek(0, 0)
return location
location = infile.tell()
chunky = infile.read(len(search_string))
infile.seek(0, 0)
return -1
# open ye olde files
femail = open("email.enc", 'rb')
fclasst = open("class.txt", 'rb')
fclasse = open("class.enc", 'rb')
fout = open("email.txt", 'wb')
foutunk = open("unk.enc", 'r+b')
finunk = open("unk.txt", 'rb')
paddedspace = b'\x20\x00\x00\x00\x20\x00\x00\x00'
paddednull = b'\x00\x00\x00\x00\x00\x00\x00\x00'
# magic stuff
for chunk in read_in_chunks(femail, 8):
# Search for each chunk in encrypted file
location = get_chunk_location(fclasse, chunk)
# if location isn't -1 (not found), find the chunk in the plaintext
if location != -1:
fclasst.seek(location)
plaintext = fclasst.read(8)
fout.write(plaintext)
else:
# is it in unknown file?
location = get_chunk_location(foutunk, chunk)
if (location == -1):
foutunk.seek(0, 2)
foutunk.write(chunk)
fout.write(paddedspace)
else:
finunk.seek(location)
plaintext = finunk.read(8)
if (plaintext == paddednull):
# write spaces if we don't have a value yet
# fout.write(chunk)
fout.write(paddedspace)
else:
fout.write(plaintext)
femail.close()
fclasst.close()
fclasse.close()
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment