Path: blob/master/modules/encoders/x86/alpha_mixed.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/encoder/alpha2/alpha_mixed'67class MetasploitModule < Msf::Encoder::Alphanum8Rank = LowRanking910def initialize11super(12'Name' => 'Alpha2 Alphanumeric Mixedcase Encoder',13'Description' => %q{14Encodes payloads as alphanumeric mixedcase text. This encoder uses15SkyLined's Alpha2 encoding suite.16A pure alpha encoder is impossible without having a register that points at or near the shellcode.17In a default configuration the first few bytes at the beginning are an fnstenv getpc stub (the same as used in shikata_ga_nai) and thus are not alphanumeric.18You can set BufferRegister for full alpha (see Encoder options for details).19},20'Author' => [ 'pusscat', 'skylined' ],21'Arch' => ARCH_X86,22'License' => BSD_LICENSE,23'EncoderType' => Msf::Encoder::Type::AlphanumMixed,24'Decoder' => {25'BlockSize' => 126})27end2829#30# Returns the decoder stub that is adjusted for the size of the buffer31# being encoded.32#33def decoder_stub(state)34modified_registers = []35reg = datastore['BufferRegister']36off = (datastore['BufferOffset'] || 0).to_i37buf = ''3839# We need to create a GetEIP stub for the exploit40if !reg41if datastore['AllowWin32SEH'] && datastore['AllowWin32SEH'].to_s =~ (/^(t|y|1)/i)42buf = 'VTX630VXH49HHHPhYAAQhZYYYYAAQQDDDd36FFFFTXVj0PPTUPPa301089'43reg = 'ECX'44off = 045modified_registers.concat(46[47Rex::Arch::X86::ESP,48Rex::Arch::X86::EDI,49Rex::Arch::X86::ESI,50Rex::Arch::X86::EBP,51Rex::Arch::X86::EBX,52Rex::Arch::X86::EDX,53Rex::Arch::X86::ECX,54Rex::Arch::X86::EAX55]56)57else58res = Rex::Arch::X86.geteip_fpu(state.badchars, modified_registers)59if !res60raise EncodingError, 'Unable to generate geteip code'61end6263buf, reg, off = res64end65else66reg.upcase!67end6869stub = buf + Rex::Encoder::Alpha2::AlphaMixed.gen_decoder(reg, off, modified_registers)7071# Sanity check that saved_registers doesn't overlap with modified_registers72modified_registers.uniq!73if !(modified_registers & saved_registers).empty?74raise BadGenerateError75end7677stub78end7980#81# Encodes a one byte block with the current index of the length of the82# payload.83#84def encode_block(state, block)85Rex::Encoder::Alpha2::AlphaMixed.encode_byte(block.unpack('C')[0], state.badchars)86end8788#89# Tack on our terminator90#91def encode_end(state)92state.encoded += Rex::Encoder::Alpha2::AlphaMixed.add_terminator93end9495# Indicate that this module can preserve some registers96def can_preserve_registers?97true98end99100# Convert the SaveRegisters to an array of x86 register constants101def saved_registers102Rex::Arch::X86.register_names_to_ids(datastore['SaveRegisters'])103end104end105106107