Skip to content

Instantly share code, notes, and snippets.

@Darfk
Created August 3, 2013 08:55
Show Gist options
  • Save Darfk/6145774 to your computer and use it in GitHub Desktop.
Save Darfk/6145774 to your computer and use it in GitHub Desktop.
Easily corrupt a file. A much better version of my previous corrupter. Tested on Python3 in Windows and Debian Linux
import argparse, subprocess
parser = argparse.ArgumentParser(description='Corrupts files.')
parser.add_argument('infile', type=str)
parser.add_argument('outfile', type=str)
parser.add_argument('-n', '--nth', metavar='N', type=int, help='corrupt every Nth byte', default=1000)
parser.add_argument('-i', '--increment', type=int, metavar='N', help='increment corrupted bytes by N', default=1)
parser.add_argument('-s', '--start', type=int, metavar='offset', help='start at corrupting at offset', default=0)
parser.add_argument('-e', '--execute', type=str, metavar='executable', help='execute a file after corruption')
args = parser.parse_args()
with open ( args.infile, 'rb' ) as f :
b = bytearray ( f.read() )
victims = range(args.start, len(b), args.nth)
for v in victims :
b[v] = ( b[v] + args.increment ) % 0x100
# Write out the corrupted file
with open ( args.outfile, 'wb' ) as output :
output.write ( b )
if ( args.execute ):
subprocess.call ( [args.execute, args.outfile] )
#Example use in Windows
#python corrupt.py -e e:\tom\games\zsnes\zsnesw.exe -i 1 -n 10000 -s 9384 dkc.smc dkc-c.smc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment