new stuffs

git-svn-id: file:///home/svn/incoming/trunk@2608 4d416f70-5f16-0410-b530-b9f4589650da
unstable
Spoon M 2005-06-09 17:35:39 +00:00
parent 60f9c96b3f
commit bd8971130d
1 changed files with 58 additions and 13 deletions

View File

@ -3,6 +3,10 @@
module Rex
module Arch
#
# everything here is mostly stole from vlad's perl x86 stuff
#
module X86
#
@ -21,30 +25,71 @@ module X86
return self.const_get(str.upcase)
end
def self.encode_modrm(dst, src)
_check_reg(dst, src)
return (0xc0 | src | dst << 3).chr
end
def self.push_byte(byte)
# push byte will sign extend...
if byte < 128 && byte >= -128
return "\x6a" + (byte & 0xff).chr
end
raise ::ArgumentError, "Can only take signed byte values!", caller()
raise ::RangeError, "Can only take signed byte values!", caller()
end
def self.check_reg(reg)
if reg > 7 || reg < 0
raise ArgumentError, "Invalid register #{reg}", caller()
end
end
def self.pop_dword(dst)
check_reg(dst)
_check_reg(dst)
return (0x58 | dst).chr
end
def self.set(dst, val, badchars)
# I'm a lazy bum fix this!"
data = push_byte(val) + pop_dword(dst)
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()
end
return opcodes[rand(opcodes.length)].chr + encode_modrm(reg, reg)
end
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
end
def self.set(dst, val, badchars = '')
_check_reg(dst)
begin
return _check_badchars(push_byte(val) + pop_dword(dst), badchars)
rescue RuntimeError, RangeError
end
begin
return _check_badchars(clear(dst, badchars) + mov_byte(dst, val), badchars)
rescue RuntimeError, RangeError
end
raise RuntimeError, "No valid set instruction could be created!", caller()
end
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
return data
# !!! check bad chars!
end
end