Created
September 21, 2012 14:33
-
-
Save Darfk/3761804 to your computer and use it in GitHub Desktop.
Easily corrupt any file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, random | |
if not len ( sys.argv ) > 2 : | |
print 'Usage: python ' + sys.argv[0] + ' seed,amount[,variant] infile [corrupted]' | |
exit ( 1 ) | |
original = sys.argv[2] | |
# Parse the parameters | |
params = sys.argv[1].split ( ',' ) | |
if len ( params ) == 2: | |
( seed, amount ) = params | |
variant = 0 | |
elif len ( params ) == 3: | |
( seed, amount, variant ) = params | |
try: | |
seed = int ( seed ) | |
amount = int ( amount ) | |
variant = int ( variant ) | |
except ValueError: | |
print 'seed, amount and variant must be integers' | |
exit ( 1 ) | |
# Seed the RNG | |
random.seed ( seed ) | |
# Open the file and read it into memory as a mutable byte array | |
with open ( original, 'rb' ) as f : | |
b = bytearray ( f.read() ) | |
# Make sure that we don't select more bytes than we have | |
amount = min ( len ( b ), amount ) | |
# Select the bytes to be corrupted | |
victims = random.sample ( xrange ( len ( b ) ), amount ) | |
# Corrupt 'em | |
for v in victims : | |
b[v] = chr ( ( random.randint ( 0, 255 ) + variant ) % 0x100 ) | |
# Generate the filename or use the one provided | |
outfile = sys.argv[3] if len ( sys.argv ) >= 4 else sys.argv[2] + '.' + str ( seed ) + '-' + str ( amount ) + '-' + str( variant ) + '.corrupted' | |
# Write out the corrupted file | |
with open ( outfile, 'wb' ) as output : | |
output.write ( b ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment