Skip to content

Instantly share code, notes, and snippets.

@darvell
Created December 13, 2011 00:29
Show Gist options
  • Save darvell/1469818 to your computer and use it in GitHub Desktop.
Save darvell/1469818 to your computer and use it in GitHub Desktop.
Huffman find and ruin
def CorruptHuffman(filename,chance,maxlen):
with open(filename,"r+b") as f:
filesize = os.path.getsize(filename)
huffmanStart = 0
huffmanEnd = 0
found = 0
for i in range(0,filesize):
if found == 2:
break
f.seek(i)
if f.read(1) == '\xFF':
f.seek(i + 1)
if f.read(1) == '\xC4':
# Ok, we found a single huffman table, that's good!
# Let's go hunt for the end of it.
huffmanStart = i + 2
found += 1
found = False
for i in range(huffmanStart + 2,filesize):
if found == True:
break
f.seek(i)
if f.read(1) == '\xFF':
f.seek(i + 1)
if f.read(1) != '\x00':
huffmanEnd = i - 2
found = True
f.seek(random.randint(huffmanStart,huffmanEnd))
f.write(hex(random.randint(1,250)))
print 'Filesize: ' + str(filesize)
print 'Huffman Start: ' + str(huffmanStart)
print 'Huffman End: ' + str(huffmanEnd)
@darvell
Copy link
Author

darvell commented Dec 14, 2011

uh i'm not sure why i did f.read(1), wasn't thinking straight i guess

whatever this is just a dodgy implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment