2005-07-09 21:22:32 +00:00
|
|
|
require 'msf/core'
|
2005-05-18 04:42:56 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
module Nops
|
2005-07-13 18:54:41 +00:00
|
|
|
module X86
|
2005-05-18 04:42:56 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2005-07-13 18:54:41 +00:00
|
|
|
# This class implements single-byte NOP generation for X86. It takes from
|
2005-05-18 04:42:56 +00:00
|
|
|
# ADMmutate and from spoonfu.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class SingleByte < Msf::Nop
|
|
|
|
|
|
|
|
SINGLE_BYTE_SLED =
|
|
|
|
{
|
|
|
|
# opcode affected registers
|
|
|
|
# ------ ------------------
|
2005-07-18 02:01:36 +00:00
|
|
|
"\x90" => nil , # nop
|
|
|
|
"\x97" => [ 'eax', 'edi' ], # xchg eax,edi
|
|
|
|
"\x96" => [ 'eax', 'esi' ], # xchg eax,esi
|
|
|
|
"\x95" => [ 'eax', 'ebp' ], # xchg eax,ebp
|
|
|
|
"\x93" => [ 'eax', 'ebx' ], # xchg eax,ebx
|
|
|
|
"\x92" => [ 'eax', 'edx' ], # xchg eax,edx
|
|
|
|
"\x91" => [ 'eax', 'ecx' ], # xchg eax,ecx
|
|
|
|
"\x99" => [ 'edx' ], # cdq
|
|
|
|
"\x4d" => [ 'ebp' ], # dec ebp
|
|
|
|
"\x48" => [ 'eax' ], # dec eax
|
|
|
|
"\x47" => [ 'edi' ], # inc edi
|
|
|
|
"\x4f" => [ 'edi' ], # dec edi
|
|
|
|
"\x40" => [ 'eax' ], # inc eax
|
|
|
|
"\x41" => [ 'ecx' ], # inc ecx
|
|
|
|
"\x37" => [ 'eax' ], # aaa
|
|
|
|
"\x3f" => [ 'eax' ], # aas
|
|
|
|
"\x27" => [ 'eax' ], # daa
|
|
|
|
"\x2f" => [ 'eax' ], # das
|
|
|
|
"\x46" => [ 'esi' ], # inc esi
|
|
|
|
"\x4e" => [ 'esi' ], # dec esi
|
|
|
|
"\xfc" => nil , # cld
|
|
|
|
"\xfd" => nil , # std
|
|
|
|
"\xf8" => nil , # clc
|
|
|
|
"\xf9" => nil , # stc
|
|
|
|
"\xf5" => nil , # cmc
|
|
|
|
"\x98" => [ 'eax' ], # cwde
|
|
|
|
"\x9f" => [ 'eax' ], # lahf
|
|
|
|
"\x4a" => [ 'edx' ], # dec edx
|
|
|
|
"\x44" => [ 'esp', 'align' ], # inc esp
|
|
|
|
"\x42" => [ 'edx' ], # inc edx
|
|
|
|
"\x43" => [ 'ebx' ], # inc ebx
|
|
|
|
"\x49" => [ 'ecx' ], # dec ecx
|
|
|
|
"\x4b" => [ 'ebx' ], # dec ebx
|
|
|
|
"\x45" => [ 'ebp' ], # inc ebp
|
|
|
|
"\x4c" => [ 'esp', 'align' ], # dec esp
|
|
|
|
"\x9b" => nil , # wait
|
|
|
|
"\x60" => [ 'esp' ], # pusha
|
|
|
|
"\x0e" => [ 'esp', 'align' ], # push cs
|
|
|
|
"\x1e" => [ 'esp', 'align' ], # push ds
|
|
|
|
"\x50" => [ 'esp' ], # push eax
|
|
|
|
"\x55" => [ 'esp' ], # push ebp
|
|
|
|
"\x53" => [ 'esp' ], # push ebx
|
|
|
|
"\x51" => [ 'esp' ], # push ecx
|
|
|
|
"\x57" => [ 'esp' ], # push edi
|
|
|
|
"\x52" => [ 'esp' ], # push edx
|
|
|
|
"\x06" => [ 'esp', 'align' ], # push es
|
|
|
|
"\x56" => [ 'esp' ], # push esi
|
|
|
|
"\x54" => [ 'esp' ], # push esp
|
|
|
|
"\x16" => [ 'esp', 'align' ], # push ss
|
|
|
|
"\x58" => [ 'esp', 'eax' ], # pop eax
|
|
|
|
"\x5d" => [ 'esp', 'ebp' ], # pop ebp
|
|
|
|
"\x5b" => [ 'esp', 'ebx' ], # pop ebx
|
|
|
|
"\x59" => [ 'esp', 'ecx' ], # pop ecx
|
|
|
|
"\x5f" => [ 'esp', 'edi' ], # pop edi
|
|
|
|
"\x5a" => [ 'esp', 'edx' ], # pop edx
|
|
|
|
"\x5e" => [ 'esp', 'esi' ], # pop esi
|
|
|
|
"\xd6" => [ 'eax' ], # salc
|
2005-05-18 04:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'Single Byte',
|
2005-07-07 23:11:03 +00:00
|
|
|
'Alias' => 'ia32_singlebyte',
|
2005-05-18 04:42:56 +00:00
|
|
|
'Version' => '$Revision$',
|
|
|
|
'Description' => 'Single-byte NOP generator',
|
|
|
|
'Author' => 'spoonm',
|
2005-07-13 18:54:41 +00:00
|
|
|
'Arch' => ARCH_X86)
|
2005-07-11 02:03:48 +00:00
|
|
|
|
|
|
|
register_advanced_options(
|
|
|
|
[
|
|
|
|
OptBool.new('RandomNops', [ false, "Generate a random NOP sled", true ])
|
2005-07-13 18:54:41 +00:00
|
|
|
], Msf::Nops::X86::SingleByte)
|
2005-05-18 04:42:56 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 18:54:41 +00:00
|
|
|
# Generate a single-byte NOP sled for X86
|
2005-12-30 05:09:45 +00:00
|
|
|
def generate_sled(length, opts = {})
|
2005-05-18 04:42:56 +00:00
|
|
|
sled_hash = SINGLE_BYTE_SLED
|
|
|
|
sled_max_idx = sled_hash.length
|
|
|
|
sled_cur_idx = 0
|
|
|
|
out_sled = ''
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
random = opts['Random']
|
2005-07-14 06:34:58 +00:00
|
|
|
badchars = opts['BadChars'] || ''
|
2005-05-18 04:42:56 +00:00
|
|
|
badregs = opts['SaveRegisters'] || []
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
# Did someone specify random NOPs in the environment?
|
|
|
|
if (!random and datastore['RandomNops'])
|
|
|
|
random = (datastore['RandomNops'].match(/true|1|y/i) != nil)
|
|
|
|
end
|
|
|
|
|
2005-05-18 04:42:56 +00:00
|
|
|
# Generate the whole sled...
|
|
|
|
1.upto(length) { |current|
|
|
|
|
|
|
|
|
cur_char = nil
|
|
|
|
threshold = 0
|
|
|
|
|
|
|
|
# Keep snagging characters until we find one that satisfies both the
|
|
|
|
# bad character and bad register requirements
|
|
|
|
begin
|
2005-07-10 23:19:59 +00:00
|
|
|
sled_cur_idx = rand(sled_max_idx) if (random == true)
|
2005-05-18 04:42:56 +00:00
|
|
|
cur_char = sled_hash.keys[sled_cur_idx]
|
2005-07-10 23:19:59 +00:00
|
|
|
sled_cur_idx += 1 if (random == false)
|
|
|
|
sled_cur_idx = 0 if (sled_cur_idx >= sled_max_idx)
|
2005-05-18 04:42:56 +00:00
|
|
|
|
|
|
|
# Make sure that we haven't gone over the sled repeat threshold
|
|
|
|
if ((threshold += 1) > self.nop_repeat_threshold)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end while ((badchars.include?(cur_char)) or
|
|
|
|
((sled_hash[cur_char]) and
|
2005-07-18 02:01:36 +00:00
|
|
|
((sled_hash[cur_char] & badregs).length > 0)))
|
2005-05-18 04:42:56 +00:00
|
|
|
|
|
|
|
# Add the character to the sled now that it's passed our checks
|
|
|
|
out_sled += cur_char
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the sled fails to entirely generate itself, then that's bogus,
|
|
|
|
# man...
|
|
|
|
if (out_sled.length != length)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return out_sled
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end end end
|