Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Last active August 29, 2015 14:15
Show Gist options
  • Save DataKinds/ceb9c68a22fe6da00b9c to your computer and use it in GitHub Desktop.
Save DataKinds/ceb9c68a22fe6da00b9c to your computer and use it in GitHub Desktop.
corrupter
#!/usr/bin/env ruby
if ARGV.length != 1
puts "Please call this as \"ruby corrupt.rb <input file>\""
exit
end
def get_char
state = `stty -g`
`stty raw -echo -icanon isig`
return $stdin.getc.chr
ensure
`stty #{state}`
end
$startByte = 10000
$corruptionType = 0
$corruptionRate = 1000
$corruptionChance = 1000
$byteOffset = 1
def corrupt()
inputFile = File.open("#{ARGV[0]}", "rb")
outputFile = File.open("Corrupted_#{ARGV[0]}", "wb")
currentByte = 0
inputFile.each_byte do |c|
willCorrupt = false
if $corruptionType == 0
willCorrupt = (currentByte % $corruptionRate == 0)
elsif $corruptionType == 1
willCorrupt = (rand < (1.0/$corruptionChance))
end
if willCorrupt
tempByte = c + $byteOffset*(rand(2) == 1 ? 1 : -1)
if tempByte > 255
tempByte = tempByte % 255
end
if tempByte < 0
tempByte = -tempByte % 255
end
outputFile.write(tempByte.chr)
else
outputFile.write(c.chr)
end
currentByte += 1
end
outputFile.close
inputFile.close
end
loop do
system("clear")
puts "Current file: #{ARGV[0]}\n\n"
puts "Starting byte (s): #{$startByte}\n\n"
puts "Corruption type (t): #{%w(every-n chance)[$corruptionType]}\n\n"
puts ("Corruption " + ["rate (r): #{$corruptionRate}", "chance (c): #{$corruptionChance}"][$corruptionType] + "\n\n")
puts "Byte offset (b): #{$byteOffset}\n\n"
puts "Begin corruption with o"
case get_char
when ?s
system("clear")
puts "New starting byte (old was #{$startByte}): "
$startByte = $stdin.gets.chomp.to_i
when ?t
system("clear")
puts "Corruption types (old was #{$corruptionType}):\n0 - every-n\n1 - chance"
$corruptionType = $stdin.gets.chomp.to_i
when ?r
system("clear")
puts "New corruption rate (old was #{$corruptionRate}): "
$corruptionRate = $stdin.gets.chomp.to_i
when ?c
system("clear")
puts "New corruption chance (old was 1/#{$corruptionChance}): "
print "1/"
$corruptionChance = $stdin.gets.chomp.to_i
when ?b
system("clear")
puts "New byte offset (old was #{$byteOffset}): "
$byteOffset = $stdin.gets.chomp.to_i
when ?o
system("clear")
puts "Beginning corruption, outputting to Corrupted_#{ARGV[0]}"
corrupt
puts "Corruption complete, press any key to return"
get_char
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment