Entropy Challenge - ATENEA
This time they came up with a challenge regarding file entropy.
They drop some clues and a compressed file containing a bunch of images. So we take Python script created by Kenneth Hartman and apply some modifications. We remove info we don’t need and add a loop so we can cycle through all the image files in the current directory. We then store the file name with the highest entropy and that’s all.
Like so:
#
# graph_file_entropy.py
#
# Shannon Entropy of a file
# = minimum average number of bits per character
# required for encoding (compressing) the file
#
# So the theoretical limit (in bytes) for data compression:
# Shannon Entropy of the file * file size (in bytes) / 8
# (Assuming the file is a string of byte-size (UTF-8?) characters
# because if not then the Shannon Entropy value would be different.)
# FB - 201011291
import sys
import math
from os import listdir
from os.path import isfile, join
if len(sys.argv) != 2:
print "Usage: entropy.py extension"
sys.exit()
onlyfiles = [f for f in listdir("./") if isfile(join("./", f))]
highest=0
winner=''
for eachfile in onlyfiles:
if sys.argv[1] in eachfile:
# read the whole file into a byte array
f = open(eachfile, "rb")
byteArr = map(ord, f.read())
f.close()
fileSize = len(byteArr)
# calculate the frequency of each byte value in the file
freqList = []
for b in range(256):
ctr = 0
for byte in byteArr:
if byte == b:
ctr += 1
freqList.append(float(ctr) / fileSize)
# print 'Frequencies of each byte-character:'
# print freqList
# print
# Shannon entropy
ent = 0.0
for freq in freqList:
if freq > 0:
ent = ent + freq * math.log(freq, 2)
ent = -ent
if ent > highest:
highest=ent
winner=eachfile
print 'Shannon entropy (min bits per byte-character) for ' + eachfile + ' : ' + str(ent)
print 'Winner is: ' + winner
Run it and apply the MD5 sum to it:
And just like that we got our flag.