2005-06-09 04:24:45 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2005-09-07 17:23:30 +00:00
|
|
|
REG_NAMES32 = [ 'eax', 'ecx', 'edx', 'ebx',
|
|
|
|
'esp', 'ebp', 'esi', 'edi' ]
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def self.call(addr)
|
|
|
|
"\xe8" + pack_dword(rel_number(addr, -5))
|
|
|
|
end
|
|
|
|
|
|
|
|
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-06-09 04:24:45 +00:00
|
|
|
def self.reg_number(str)
|
|
|
|
return self.const_get(str.upcase)
|
|
|
|
end
|
|
|
|
|
2005-09-07 17:23:30 +00:00
|
|
|
def self.reg_name32(num)
|
|
|
|
_check_reg(num)
|
|
|
|
return REG_NAMES32[num].dup
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
def self.encode_modrm(dst, src)
|
|
|
|
_check_reg(dst, src)
|
|
|
|
return (0xc0 | src | dst << 3).chr
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
def self.pop_dword(dst)
|
|
|
|
_check_reg(dst)
|
|
|
|
return (0x58 | dst).chr
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.clear(reg, badchars = '')
|
|
|
|
_check_reg(reg)
|
|
|
|
opcodes = Rex::StringUtils.remove_badchars("\x29\x2b\x31\x33", badchars)
|
|
|
|
if opcodes.empty?
|
|
|
|
raise RuntimeError, "Could not find a usable opcode", caller()
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
2005-06-09 17:35:39 +00:00
|
|
|
|
|
|
|
return opcodes[rand(opcodes.length)].chr + encode_modrm(reg, reg)
|
2005-06-09 04:24:45 +00:00
|
|
|
end
|
|
|
|
|
2005-06-09 17:44:43 +00:00
|
|
|
# B004 mov al,0x4
|
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-06-09 17:44:43 +00:00
|
|
|
# 66B80400 mov ax,0x4
|
|
|
|
def self.mov_word(reg, val)
|
|
|
|
_check_reg(reg)
|
|
|
|
if val < 0 || val > 0xffff
|
|
|
|
raise RangeError, "Can only take unsigned word values!", caller()
|
|
|
|
end
|
|
|
|
return "\x66" + (0xb8 | reg).chr + [ val ].pack('v')
|
|
|
|
end
|
|
|
|
|
2005-06-09 17:35:39 +00:00
|
|
|
def self.set(dst, val, badchars = '')
|
|
|
|
_check_reg(dst)
|
|
|
|
|
2005-06-09 17:44:43 +00:00
|
|
|
# try push BYTE val; pop dst
|
2005-06-09 17:35:39 +00:00
|
|
|
begin
|
|
|
|
return _check_badchars(push_byte(val) + pop_dword(dst), badchars)
|
2005-11-01 00:48:40 +00:00
|
|
|
rescue ::ArgumentError, RuntimeError, RangeError
|
2005-06-09 17:35:39 +00:00
|
|
|
end
|
|
|
|
|
2005-06-09 17:44:43 +00:00
|
|
|
# try clear dst, mov BYTE dst
|
2005-06-09 17:35:39 +00:00
|
|
|
begin
|
|
|
|
return _check_badchars(clear(dst, badchars) + mov_byte(dst, val), badchars)
|
2005-11-01 00:48:40 +00:00
|
|
|
rescue ::ArgumentError, RuntimeError, RangeError
|
2005-06-09 17:35:39 +00:00
|
|
|
end
|
|
|
|
|
2005-06-09 17:44:43 +00:00
|
|
|
# try clear dst, mov WORD dst
|
|
|
|
begin
|
|
|
|
return _check_badchars(clear(dst, badchars) + mov_word(dst, val), badchars)
|
2005-11-01 00:48:40 +00:00
|
|
|
rescue ::ArgumentError, RuntimeError, RangeError
|
2005-06-09 17:44:43 +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-10-01 05:55:15 +00:00
|
|
|
def self.sub(val, reg, badchars = '', add = false, adjust = false)
|
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
|
|
|
|
|
|
|
if (val >= -0x7f and val <= 0x7f)
|
|
|
|
opcodes <<
|
2005-10-01 05:55:15 +00:00
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
2005-07-10 20:49:13 +00:00
|
|
|
"\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
|
|
|
|
|
|
|
|
if (val >= -0xffff and val <= 0)
|
|
|
|
opcodes <<
|
2005-10-01 05:55:15 +00:00
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
2005-07-10 20:49:13 +00:00
|
|
|
"\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
|
|
|
|
|
|
|
|
opcodes <<
|
2005-10-01 05:55:15 +00:00
|
|
|
((adjust) ? '' : clear(reg, badchars)) +
|
2005-07-10 20:49:13 +00:00
|
|
|
"\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|
|
|
|
|
begin
|
|
|
|
_check_badchars(op, badchars)
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
|
|
|
if opcodes.empty?
|
|
|
|
raise RuntimeError, "Could not find a usable opcode", caller()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-01 05:55:15 +00:00
|
|
|
def self.add(val, reg, badchars = '', adjust = false)
|
|
|
|
sub(val, reg, badchars, true, adjust)
|
|
|
|
end
|
|
|
|
|
2005-07-17 06:01:11 +00:00
|
|
|
def self.pack_dword(num)
|
|
|
|
[num].pack('V')
|
|
|
|
end
|
|
|
|
|
|
|
|
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-10-01 05:55:15 +00:00
|
|
|
def self.adjust_reg(adjustment)
|
|
|
|
if (adjustment > 0)
|
|
|
|
sub(adjustment, ESP, '', false, false)
|
|
|
|
else
|
|
|
|
add(adjustment, ESP, '', true, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-06-09 17:35:39 +00:00
|
|
|
def self._check_reg(*regs)
|
|
|
|
regs.each { |reg|
|
|
|
|
if reg > 7 || reg < 0
|
|
|
|
raise ArgumentError, "Invalid register #{reg}", caller()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self._check_badchars(data, badchars)
|
|
|
|
idx = Rex::StringUtils.badchar_index(data, badchars)
|
|
|
|
if idx
|
|
|
|
raise RuntimeError, "Bad character at #{idx}", caller()
|
|
|
|
end
|
2005-06-09 04:24:45 +00:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end end
|