2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-06-09 04:24:45 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Arch
|
|
|
|
|
2005-06-09 17:35:39 +00:00
|
|
|
#
|
|
|
|
# everything here is mostly stole from vlad's perl x86 stuff
|
|
|
|
#
|
|
|
|
|
2005-06-09 04:24:45 +00:00
|
|
|
module X86
|
|
|
|
|
|
|
|
#
|
|
|
|
# Register number constants
|
|
|
|
#
|
|
|
|
EAX = AL = AX = ES = 0
|
|
|
|
ECX = CL = CX = CS = 1
|
|
|
|
EDX = DL = DX = SS = 2
|
|
|
|
EBX = BL = BX = DS = 3
|
|
|
|
ESP = AH = SP = FS = 4
|
|
|
|
EBP = CH = BP = GS = 5
|
|
|
|
ESI = DH = SI = 6
|
|
|
|
EDI = BH = DI = 7
|
|
|
|
|
2010-04-27 22:55:17 +00:00
|
|
|
REG_NAMES32 = [ 'eax', 'ecx', 'edx', 'ebx',
|
2005-11-02 23:03:02 +00:00
|
|
|
'esp', 'ebp', 'esi', 'edi' ] # :nodoc:
|
2005-09-07 17:23:30 +00:00
|
|
|
|
2006-01-27 21:14:24 +00:00
|
|
|
# Jump tp a specific register
|
|
|
|
def self.jmp_reg(str)
|
|
|
|
reg = reg_number(str)
|
|
|
|
_check_reg(reg)
|
|
|
|
"\xFF" + [224 + reg].pack('C')
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method returns the opcodes that compose a jump instruction to the
|
|
|
|
# supplied relative offset.
|
|
|
|
def self.jmp(addr)
|
|
|
|
"\xe9" + pack_dword(rel_number(addr))
|
|
|
|
end
|
|
|
|
|
2005-11-25 20:02:21 +00:00
|
|
|
#
|
|
|
|
# This method adds/subs a packed long integer
|
|
|
|
#
|
|
|
|
def self.dword_adjust(dword, amount=0)
|
2009-07-13 22:26:27 +00:00
|
|
|
pack_dword(dword.unpack('V')[0] + amount)
|
2005-11-25 20:02:21 +00:00
|
|
|
end
|
2010-04-27 22:55:17 +00:00
|
|
|
|
2005-11-25 20:02:21 +00:00
|
|
|
#
|
|
|
|
# This method returns the opcodes that compose a tag-based search routine
|
|
|
|
#
|
|
|
|
def self.searcher(tag)
|
|
|
|
"\xbe" + dword_adjust(tag,-1)+ # mov esi, Tag - 1
|
|
|
|
"\x46" + # inc esi
|
|
|
|
"\x47" + # inc edi (end_search:)
|
|
|
|
"\x39\x37" + # cmp [edi],esi
|
|
|
|
"\x75\xfb" + # jnz 0xa (end_search)
|
|
|
|
"\x46" + # inc esi
|
|
|
|
"\x4f" + # dec edi (start_search:)
|
|
|
|
"\x39\x77\xfc" + # cmp [edi-0x4],esi
|
|
|
|
"\x75\xfa" + # jnz 0x10 (start_search)
|
2010-04-27 22:55:17 +00:00
|
|
|
jmp_reg('edi') # jmp edi
|
2005-11-25 20:02:21 +00:00
|
|
|
end
|
2007-04-02 05:51:22 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Generates a buffer that will copy memory immediately following the stub
|
|
|
|
# that is generated to be copied to the stack
|
|
|
|
#
|
|
|
|
def self.copy_to_stack(len)
|
|
|
|
# four byte align
|
|
|
|
len = (len + 3) & ~0x3
|
|
|
|
|
|
|
|
stub =
|
|
|
|
"\xeb\x0f"+ # jmp _end
|
2009-07-13 22:26:27 +00:00
|
|
|
push_dword(len)+ # push n
|
2007-04-02 05:51:22 +00:00
|
|
|
"\x59"+ # pop ecx
|
|
|
|
"\x5e"+ # pop esi
|
|
|
|
"\x29\xcc"+ # sub esp, ecx
|
|
|
|
"\x89\xe7"+ # mov edi, esp
|
|
|
|
"\xf3\xa4"+ # rep movsb
|
|
|
|
"\xff\xe4"+ # jmp esp
|
|
|
|
"\xe8\xec\xff\xff\xff" # call _start
|
|
|
|
|
|
|
|
stub
|
|
|
|
end
|
2010-04-27 22:55:17 +00:00
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns the opcodes that compose a short jump instruction to
|
|
|
|
# the supplied relative offset.
|
|
|
|
#
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.jmp_short(addr)
|
2005-07-18 03:58:29 +00:00
|
|
|
"\xeb" + pack_lsb(rel_number(addr, -2))
|
2005-07-17 06:01:11 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns the opcodes that compose a relative call instruction
|
|
|
|
# to the address specified.
|
|
|
|
#
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.call(addr)
|
|
|
|
"\xe8" + pack_dword(rel_number(addr, -5))
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns a number offset to the supplied string.
|
|
|
|
#
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.rel_number(num, delta = 0)
|
|
|
|
s = num.to_s
|
|
|
|
|
|
|
|
case s[0, 2]
|
|
|
|
when '$+'
|
|
|
|
num = s[2 .. -1].to_i
|
|
|
|
when '$-'
|
|
|
|
num = -1 * s[2 .. -1].to_i
|
|
|
|
when '0x'
|
|
|
|
num = s.hex
|
|
|
|
else
|
|
|
|
delta = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return num + delta
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns the number associated with a named register.
|
|
|
|
#
|
2005-06-09 04:24:45 +00:00
|
|
|
def self.reg_number(str)
|
|
|
|
return self.const_get(str.upcase)
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns the register named associated with a given register
|
|
|
|
# number.
|
|
|
|
#
|
2005-09-07 17:23:30 +00:00
|
|
|
def self.reg_name32(num)
|
|
|
|
_check_reg(num)
|
|
|
|
return REG_NAMES32[num].dup
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates the encoded effective value for a register.
|
|
|
|
#
|
2005-07-10 20:49:13 +00:00
|
|
|
def self.encode_effective(shift, dst)
|
|
|
|
return (0xc0 | (shift << 3) | dst)
|
|
|
|
end
|
2005-06-09 17:35:39 +00:00
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-12-30 20:26:41 +00:00
|
|
|
# This method generates the mod r/m character for a source and destination
|
2005-11-02 23:03:02 +00:00
|
|
|
# register.
|
|
|
|
#
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.encode_modrm(dst, src)
|
|
|
|
_check_reg(dst, src)
|
|
|
|
return (0xc0 | src | dst << 3).chr
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates a push byte instruction.
|
|
|
|
#
|
2005-06-09 04:24:45 +00:00
|
|
|
def self.push_byte(byte)
|
|
|
|
# push byte will sign extend...
|
|
|
|
if byte < 128 && byte >= -128
|
|
|
|
return "\x6a" + (byte & 0xff).chr
|
|
|
|
end
|
2005-10-01 05:55:15 +00:00
|
|
|
raise ::ArgumentError, "Can only take signed byte values!", caller()
|
2005-06-09 17:35:39 +00:00
|
|
|
end
|
2005-11-02 23:03:02 +00:00
|
|
|
|
2008-11-16 22:54:03 +00:00
|
|
|
#
|
|
|
|
# This method generates a push word instruction.
|
|
|
|
#
|
|
|
|
def self.push_word(val)
|
2008-12-25 02:00:46 +00:00
|
|
|
return "\x66\x68" + pack_word(val)
|
2008-11-16 22:54:03 +00:00
|
|
|
end
|
|
|
|
|
2006-01-14 17:45:42 +00:00
|
|
|
#
|
|
|
|
# This method generates a push dword instruction.
|
|
|
|
#
|
|
|
|
def self.push_dword(val)
|
2008-12-25 02:00:46 +00:00
|
|
|
return "\x68" + pack_dword(val)
|
2006-01-14 17:45:42 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates a pop dword instruction into a register.
|
|
|
|
#
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.pop_dword(dst)
|
|
|
|
_check_reg(dst)
|
|
|
|
return (0x58 | dst).chr
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates an instruction that clears the supplied register in
|
|
|
|
# a manner that attempts to avoid bad characters, if supplied.
|
|
|
|
#
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.clear(reg, badchars = '')
|
|
|
|
_check_reg(reg)
|
2006-01-09 02:07:56 +00:00
|
|
|
return set(reg, 0, badchars)
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates the opcodes that set the low byte of a given
|
|
|
|
# register to the supplied value.
|
|
|
|
#
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.mov_byte(reg, val)
|
|
|
|
_check_reg(reg)
|
|
|
|
# chr will raise RangeError if val not between 0 .. 255
|
|
|
|
return (0xb0 | reg).chr + val.chr
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates the opcodes that set the low word of a given
|
|
|
|
# register to the supplied value.
|
|
|
|
#
|
2005-06-09 17:44:43 +00:00
|
|
|
def self.mov_word(reg, val)
|
|
|
|
_check_reg(reg)
|
|
|
|
if val < 0 || val > 0xffff
|
|
|
|
raise RangeError, "Can only take unsigned word values!", caller()
|
|
|
|
end
|
2009-07-13 22:26:27 +00:00
|
|
|
return "\x66" + (0xb8 | reg).chr + pack_word(val)
|
2005-06-09 17:44:43 +00:00
|
|
|
end
|
|
|
|
|
2005-12-30 20:26:41 +00:00
|
|
|
#
|
|
|
|
# This method generates the opcodes that set the a register to the
|
|
|
|
# supplied value.
|
|
|
|
#
|
|
|
|
def self.mov_dword(reg, val)
|
|
|
|
_check_reg(reg)
|
2009-07-13 22:26:27 +00:00
|
|
|
return (0xb8 | reg).chr + pack_dword(val)
|
2005-12-30 20:26:41 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method is a general way of setting a register to a value. Depending
|
|
|
|
# on the value supplied, different sets of instructions may be used.
|
|
|
|
#
|
2006-01-14 17:45:42 +00:00
|
|
|
# TODO: Make this moderatly intelligent so it chain instructions by itself
|
2006-01-27 05:33:08 +00:00
|
|
|
# (ie. xor eax, eax + mov al, 4 + xchg ah, al)
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.set(dst, val, badchars = '')
|
|
|
|
_check_reg(dst)
|
|
|
|
|
2006-01-09 02:07:56 +00:00
|
|
|
# If the value is 0 try xor/sub dst, dst (2 bytes)
|
|
|
|
if(val == 0)
|
|
|
|
opcodes = Rex::Text.remove_badchars("\x29\x2b\x31\x33", badchars)
|
|
|
|
if !opcodes.empty?
|
|
|
|
return opcodes[rand(opcodes.length)].chr + encode_modrm(dst, dst)
|
|
|
|
end
|
2006-01-14 17:45:42 +00:00
|
|
|
# TODO: SHL/SHR
|
2010-04-27 22:55:17 +00:00
|
|
|
# TODO: AND
|
2006-01-09 02:07:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# try push BYTE val; pop dst (3 bytes)
|
2005-06-09 17:35:39 +00:00
|
|
|
begin
|
|
|
|
return _check_badchars(push_byte(val) + pop_dword(dst), badchars)
|
2006-04-15 20:26:41 +00:00
|
|
|
rescue ::ArgumentError, ::RuntimeError, ::RangeError
|
2005-06-09 17:35:39 +00:00
|
|
|
end
|
|
|
|
|
2006-01-09 02:07:56 +00:00
|
|
|
# try clear dst, mov BYTE dst (4 bytes)
|
2005-06-09 17:35:39 +00:00
|
|
|
begin
|
2006-04-15 20:26:41 +00:00
|
|
|
# break if val == 0
|
2005-06-09 17:35:39 +00:00
|
|
|
return _check_badchars(clear(dst, badchars) + mov_byte(dst, val), badchars)
|
2006-04-15 20:26:41 +00:00
|
|
|
rescue ::ArgumentError, ::RuntimeError, ::RangeError
|
2005-06-09 17:35:39 +00:00
|
|
|
end
|
|
|
|
|
2006-01-14 17:45:42 +00:00
|
|
|
# try mov DWORD dst (5 bytes)
|
2005-06-09 17:44:43 +00:00
|
|
|
begin
|
2006-01-14 17:45:42 +00:00
|
|
|
return _check_badchars(mov_dword(dst, val), badchars)
|
2006-04-15 20:26:41 +00:00
|
|
|
rescue ::ArgumentError, ::RuntimeError, ::RangeError
|
2005-06-09 17:44:43 +00:00
|
|
|
end
|
|
|
|
|
2006-01-14 17:45:42 +00:00
|
|
|
# try push DWORD, pop dst (6 bytes)
|
2005-12-30 20:26:41 +00:00
|
|
|
begin
|
2006-01-14 17:45:42 +00:00
|
|
|
return _check_badchars(push_dword(val) + pop_dword(dst), badchars)
|
2006-04-15 20:26:41 +00:00
|
|
|
rescue ::ArgumentError, ::RuntimeError, ::RangeError
|
2006-01-14 17:45:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# try clear dst, mov WORD dst (6 bytes)
|
|
|
|
begin
|
2006-04-15 20:26:41 +00:00
|
|
|
# break if val == 0
|
2006-01-14 17:45:42 +00:00
|
|
|
return _check_badchars(clear(dst, badchars) + mov_word(dst, val), badchars)
|
2006-04-15 20:26:41 +00:00
|
|
|
rescue ::ArgumentError, ::RuntimeError, ::RangeError
|
2005-12-30 20:26:41 +00:00
|
|
|
end
|
|
|
|
|
2005-06-09 17:35:39 +00:00
|
|
|
raise RuntimeError, "No valid set instruction could be created!", caller()
|
|
|
|
end
|
|
|
|
|
2005-07-10 20:49:13 +00:00
|
|
|
#
|
|
|
|
# Builds a subtraction instruction using the supplied operand
|
|
|
|
# and register.
|
|
|
|
#
|
2005-11-11 01:22:03 +00:00
|
|
|
def self.sub(val, reg, badchars = '', add = false, adjust = false, bits = 0)
|
2005-07-10 20:49:13 +00:00
|
|
|
opcodes = []
|
2005-10-01 05:55:15 +00:00
|
|
|
shift = (add == true) ? 0 : 5
|
2005-07-10 20:49:13 +00:00
|
|
|
|
2005-11-11 01:22:03 +00:00
|
|
|
if (bits <= 8 and val >= -0x7f and val <= 0x7f)
|
2010-04-27 22:55:17 +00:00
|
|
|
opcodes <<
|
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
|
|
|
"\x83" +
|
2005-10-01 05:55:15 +00:00
|
|
|
[ encode_effective(shift, reg) ].pack('C') +
|
2005-07-10 20:49:13 +00:00
|
|
|
[ val.to_i ].pack('C')
|
|
|
|
end
|
|
|
|
|
2005-11-11 01:22:03 +00:00
|
|
|
if (bits <= 16 and val >= -0xffff and val <= 0)
|
2010-04-27 22:55:17 +00:00
|
|
|
opcodes <<
|
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
|
|
|
"\x66\x81" +
|
2005-10-01 05:55:15 +00:00
|
|
|
[ encode_effective(shift, reg) ].pack('C') +
|
2005-07-10 20:49:13 +00:00
|
|
|
[ val.to_i ].pack('v')
|
|
|
|
end
|
2010-04-27 22:55:17 +00:00
|
|
|
|
|
|
|
opcodes <<
|
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
|
|
|
"\x81" +
|
2005-10-01 05:55:15 +00:00
|
|
|
[ encode_effective(shift, reg) ].pack('C') +
|
2005-07-10 20:49:13 +00:00
|
|
|
[ val.to_i ].pack('V')
|
|
|
|
|
|
|
|
# Search for a compatible opcode
|
|
|
|
opcodes.each { |op|
|
2010-04-27 22:55:17 +00:00
|
|
|
begin
|
2005-07-10 20:49:13 +00:00
|
|
|
_check_badchars(op, badchars)
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
|
|
|
if opcodes.empty?
|
|
|
|
raise RuntimeError, "Could not find a usable opcode", caller()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method generates the opcodes equivalent to subtracting with a
|
|
|
|
# negative value from a given register.
|
|
|
|
#
|
2005-11-11 01:22:03 +00:00
|
|
|
def self.add(val, reg, badchars = '', adjust = false, bits = 0)
|
|
|
|
sub(val, reg, badchars, true, adjust, bits)
|
2005-10-01 05:55:15 +00:00
|
|
|
end
|
|
|
|
|
2008-12-25 01:55:05 +00:00
|
|
|
#
|
|
|
|
# This method wrappers packing a short integer as a little-endian buffer.
|
|
|
|
#
|
|
|
|
def self.pack_word(num)
|
|
|
|
[num].pack('v')
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method wrappers packing an integer as a little-endian buffer.
|
|
|
|
#
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.pack_dword(num)
|
|
|
|
[num].pack('V')
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method returns the least significant byte of a packed dword.
|
|
|
|
#
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.pack_lsb(num)
|
2005-07-18 01:47:03 +00:00
|
|
|
pack_dword(num)[0,1]
|
2005-07-17 06:01:11 +00:00
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
|
|
|
# This method adjusts the value of the ESP register by a given amount.
|
|
|
|
#
|
2005-11-11 01:22:03 +00:00
|
|
|
def self.adjust_reg(reg, adjustment)
|
2005-10-01 05:55:15 +00:00
|
|
|
if (adjustment > 0)
|
2005-11-11 01:22:03 +00:00
|
|
|
sub(adjustment, reg, '', false, false, 32)
|
2005-10-01 05:55:15 +00:00
|
|
|
else
|
2005-11-11 01:22:03 +00:00
|
|
|
add(adjustment, reg, '', true, 32)
|
2005-10-01 05:55:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
def self._check_reg(*regs) # :nodoc:
|
2005-06-09 17:35:39 +00:00
|
|
|
regs.each { |reg|
|
|
|
|
if reg > 7 || reg < 0
|
|
|
|
raise ArgumentError, "Invalid register #{reg}", caller()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
def self._check_badchars(data, badchars) # :nodoc:
|
2005-11-09 04:18:08 +00:00
|
|
|
idx = Rex::Text.badchar_index(data, badchars)
|
2005-06-09 17:35:39 +00:00
|
|
|
if idx
|
|
|
|
raise RuntimeError, "Bad character at #{idx}", caller()
|
|
|
|
end
|
2005-06-09 04:24:45 +00:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2006-09-10 05:10:48 +00:00
|
|
|
#
|
|
|
|
# This method returns an array of 'safe' FPU instructions
|
|
|
|
#
|
|
|
|
def self.fpu_instructions
|
|
|
|
fpus = []
|
|
|
|
|
|
|
|
0xe8.upto(0xee) { |x| fpus << "\xd9" + x.chr }
|
|
|
|
0xc0.upto(0xcf) { |x| fpus << "\xd9" + x.chr }
|
|
|
|
0xc0.upto(0xdf) { |x| fpus << "\xda" + x.chr }
|
|
|
|
0xc0.upto(0xdf) { |x| fpus << "\xdb" + x.chr }
|
|
|
|
0xc0.upto(0xc7) { |x| fpus << "\xdd" + x.chr }
|
|
|
|
|
|
|
|
fpus << "\xd9\xd0"
|
|
|
|
fpus << "\xd9\xe1"
|
|
|
|
fpus << "\xd9\xf6"
|
|
|
|
fpus << "\xd9\xf7"
|
|
|
|
fpus << "\xd9\xe5"
|
|
|
|
|
|
|
|
# This FPU instruction seems to fail consistently on Linux
|
|
|
|
#fpus << "\xdb\xe1"
|
|
|
|
|
|
|
|
fpus
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method returns an array containing a geteip stub, a register, and an offset
|
|
|
|
# This method will return nil if the getip generation fails
|
|
|
|
#
|
|
|
|
def self.geteip_fpu(badchars)
|
2010-04-27 22:55:17 +00:00
|
|
|
|
2006-09-10 05:10:48 +00:00
|
|
|
#
|
|
|
|
# Default badchars to an empty string
|
|
|
|
#
|
|
|
|
badchars ||= ''
|
2010-04-27 22:55:17 +00:00
|
|
|
|
2006-09-10 05:10:48 +00:00
|
|
|
#
|
|
|
|
# Bail out early if D9 is restricted
|
|
|
|
#
|
|
|
|
return nil if badchars.index("\xd9")
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a list of FPU instructions
|
|
|
|
#
|
|
|
|
fpus = *self.fpu_instructions
|
|
|
|
bads = []
|
|
|
|
badchars.each_byte do |c|
|
|
|
|
fpus.each do |str|
|
|
|
|
bads << str if (str.index(c.chr))
|
|
|
|
end
|
2010-04-27 22:55:17 +00:00
|
|
|
end
|
2006-09-10 05:10:48 +00:00
|
|
|
bads.each { |str| fpus.delete(str) }
|
|
|
|
return nil if fpus.length == 0
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a list of registers to use for fnstenv
|
|
|
|
#
|
|
|
|
dsts = []
|
|
|
|
0.upto(7) do |c|
|
|
|
|
dsts << c if (not badchars.index( (0x70+c).chr ))
|
|
|
|
end
|
|
|
|
|
|
|
|
if (dsts.include?(ESP) and badchars.index("\x24"))
|
|
|
|
dsts.delete(ESP)
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil if dsts.length == 0
|
|
|
|
|
|
|
|
#
|
|
|
|
# Grab a random FPU instruction
|
|
|
|
#
|
|
|
|
fpu = fpus[ rand(fpus.length) ]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Grab a random register from dst
|
|
|
|
#
|
|
|
|
while(dsts.length > 0)
|
|
|
|
buf = ''
|
|
|
|
dst = dsts[ rand(dsts.length) ]
|
|
|
|
dsts.delete(dst)
|
|
|
|
|
|
|
|
# If the register is not ESP, copy ESP
|
|
|
|
if (dst != ESP)
|
|
|
|
next if badchars.index( (0x70 + dst).chr )
|
|
|
|
|
2009-10-25 17:18:23 +00:00
|
|
|
if !(badchars.index("\x89") or badchars.index( (0xE0+dst).chr ))
|
2006-09-10 05:10:48 +00:00
|
|
|
buf << "\x89" + (0xE0 + dst).chr
|
|
|
|
else
|
|
|
|
next if badchars.index("\x54")
|
|
|
|
next if badchars.index( (0x58+dst).chr )
|
|
|
|
buf << "\x54" + (0x58 + dst).chr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pad = 0
|
2009-06-03 03:02:42 +00:00
|
|
|
while (pad < (128-12) and badchars.index( (256-12-pad).chr))
|
2006-09-10 05:10:48 +00:00
|
|
|
pad += 4
|
|
|
|
end
|
|
|
|
|
|
|
|
# Give up on finding a value to use here
|
|
|
|
if (pad == (128-12))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
out = buf + fpu + "\xd9" + (0x70 + dst).chr
|
|
|
|
out << "\x24" if dst == ESP
|
|
|
|
out << (256-12-pad).chr
|
|
|
|
|
|
|
|
regs = [*(0..7)]
|
|
|
|
while (regs.length > 0)
|
|
|
|
reg = regs[ rand(regs.length) ]
|
|
|
|
regs.delete(reg)
|
|
|
|
next if reg == ESP
|
|
|
|
next if badchars.index( (0x58 + reg).chr )
|
|
|
|
|
|
|
|
# Pop the value back out
|
|
|
|
0.upto(pad / 4) { |c| out << (0x58 + reg).chr }
|
|
|
|
|
|
|
|
# Fix the value to point to self
|
|
|
|
gap = out.length - buf.length
|
|
|
|
|
|
|
|
return [out, REG_NAMES32[reg].upcase, gap]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2008-11-16 22:54:03 +00:00
|
|
|
end end
|
2010-04-27 22:55:17 +00:00
|
|
|
|