Skip to content

Instantly share code, notes, and snippets.

@mindware
Last active August 29, 2015 14:24
Show Gist options
  • Save mindware/842dc789895a66cbb16d to your computer and use it in GitHub Desktop.
Save mindware/842dc789895a66cbb16d to your computer and use it in GitHub Desktop.
Brute force your way through piles of hex to search keys (strings, digits)
######################################################################
# A simple script written to find keywords and digits inside #
# blobs of hex data, used for some quick and dirty basic protocol #
# reverse-engineering. #
# #
# You can find a given string or digit inside a hex #
# in a blob of text. #
# Usage: ruby brute.rb <keys_to_find> <paste your hex> #
# #
# Multiple keys: #
# if you want to find multiple keys just use space seperated values #
# Usage: ruby brute.rb <key1> <key2> <keyn> <hex> #
# #
# Manual input: #
# If you want to type the keys but would like us to prompt you for #
# the hex data, simply write 'i' or 'input' as the last parameter. #
# Usage: ruby brute.rb <key1> <keyn> input #
# #
# #
# By: Andrés Colón (github.com/mindware) #
# Released under MIT License #
######################################################################
require 'shellwords'
require 'colorize'
class String
def convert_base(from, to)
self.to_i(from).to_s(to)
end
end
if(ARGV.length < 2)
puts "Usage: ruby brute.rb <key1..keyn> < ('i'| 'input')> | \"<blob of text>\""
exit
end
text = ARGV.pop
keys = ARGV
# detect if user requested to input the data manually
if(text == "input" or text == "i")
text = ""
while(text.length == 0)
print "Enter your blob of text: "
text = STDIN.gets.chomp
end
end
index = {}
keys.each do |key|
index[key] = []
puts "Searching for #{key}"
(2..32).each do |i|
break if i > text.length
found = false
slice = text.chars.each_slice(i).map(&:join)
puts "Breaking by #{i} chars, result is #{slice.length} slices."
slice.each do |chunk|
# explicit check
word = chunk
if(word.include? key)
word = Shellwords.escape(word)
puts "--"
puts "Found (base): '#{key}'"
puts "'#{word}' in '#{chunk}'"
puts "Index starts at: #{text.index(chunk)}"
puts "--"
index[key] << [text.index(chunk), (text.index(chunk) + word.length - 1), word]
found = true
end
# convert chunk of hex to ascii
#word = chunk.gsub(/../) { |pair| pair.hex.chr }
word = chunk.convert_base(16, 10)
if(word.include? key)
word = Shellwords.escape(word)
puts "--"
puts "Found (base): '#{key}'"
puts "'#{word}' in '#{chunk}'"
puts "Index starts at: #{text.index(chunk)}"
puts "--"
index[key] << [text.index(chunk), (text.index(chunk) + word.length - 1), word]
found = true
end
# unpack hex
word = [chunk].pack("H*")
if(word.include? key)
word = Shellwords.escape(word)
puts "--"
puts "Found (unpack): '#{key}'"
puts "'#{word}' in '#{chunk}'"
puts "Index starts at: #{text.index(chunk)}"
puts "--"
index[key] << [text.index(chunk), (text.index(chunk) + word.length - 1), word]
found = true
break
end
end
break if found
end
end
if index.keys.length > 0
index.each do |key, values|
next if(index[key].length == 0)
puts "Result: "
puts "The string: #{key.red}"
values.each do |value|
puts "--".yellow
first = value[0]
last = value[1]
word = value[2]
puts "Converted: #{word.green}"
puts "Start: #{value[0]} End: #{value[1]}"
puts "#{text.gsub( text[(first)..(last)], text[(first)..(last)].red)}"
puts "--".yellow
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment