Created
April 21, 2012 04:58
-
-
Save phikshun/2434112 to your computer and use it in GitHub Desktop.
Shellcode Function Hash Generator
This file contains hidden or 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
#!/usr/bin/env ruby | |
def b32_to_i(bin_arr) | |
val = 0; bin_arr.each_with_index { |b,i| val += b*(2**(31-i)) }; val | |
end | |
def i_to_b32(num) | |
val = 31.downto(0).each.map { |i| num[i] } | |
end | |
def trunc_to_b32(num) | |
b32_to_i(i_to_b32(num)) | |
end | |
def ror32(reg, offset) | |
reg_bits = i_to_b32(reg) | |
bits = ((31-offset)-31).upto(31-offset).each.map { |i| reg_bits[i] } | |
b32_to_i(bits) | |
end | |
def add32(dst, src) | |
result = src + dst | |
trunc_to_b32(result) | |
end | |
def func_hash(str) | |
hash_val = 0 | |
str.each_byte do |b| | |
hash_val = ror32(hash_val, 13) | |
hash_val = add32(hash_val, b) | |
end | |
hash_val | |
end | |
def to_hex(val) | |
"0x" + ("%08x" % [val]).upcase | |
end | |
def unicodeify(val) | |
val.each_char.map {|b| b + "\x00" }.join('') | |
end | |
if ARGV[0] && ARGV[1] | |
mod = ARGV[0].upcase + "\x00" | |
func = ARGV[1] + "\x00" | |
puts to_hex( add32(func_hash(unicodeify(mod)), func_hash(func)) ) | |
else | |
puts "This utility generates function hashes for use with Metasploit's" | |
puts "block_api.asm, which locates DLL functions at runtime. block_api.asm" | |
puts "was originally written by Stephen Fewer @ Harmony Security." | |
puts "" | |
puts "Usage: hash.rb <module_name> <function_name>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment