Path: blob/master/modules/encoders/mipsle/longxor.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'metasm'67class MetasploitModule < Msf::Encoder::Xor89def initialize10super(11'Name' => 'XOR Encoder',12'Description' => %q{13Mips Web server exploit friendly xor encoder.14},15'Author' => [16'Julien Tinnes <julien[at]cr0.org>', # original shellcode17'Pedro Ribeiro <[email protected]>' # fix Linux >= 2.6.11 and toupper() compat18],19'Arch' => ARCH_MIPSLE,20'License' => MSF_LICENSE,21'Decoder' => {22'KeySize' => 4,23'BlockSize' => 4,24'KeyPack' => 'V'25}26)27end2829#30# Returns the decoder stub that is adjusted for the size of the buffer31# being encoded.32#33def decoder_stub(state)34# add one xor operation for the key (see comment below)35number_of_passes = state.buf.length / 4 + 136raise EncodingError, "The payload being encoded is too long (#{state.buf.length} bytes)" if number_of_passes > 1024037raise EncodingError, "The payload is not padded to 4-bytes (#{state.buf.length} bytes)" if state.buf.length % 4 != 03839# 16-bits not (again, see below)40reg_10 = (number_of_passes + 1) ^ 0xFFFF41reg_5 = state.buf.length ^ 0xFFFF42decoder = Metasm::Shellcode.assemble(Metasm::MIPS.new(:little), <<~EOS).encoded.data43;44; MIPS nul-free xor decoder45;46; (C) 2006 Julien TINNES47; <julien at cr0.org>48;49; The first four bytes in encoded shellcode must be the xor key50; This means that you have to put the xor key right after51; this xor decoder52; This key will be considered part of the encoded shellcode53; by this decoder and will be xored, thus becoming 4NULs, meaning nop54;55; This is Linux-only because I use the cacheflush system call56;57; You can use shellforge to assemble this, but be sure to discard all58; the nul bytes at the end (everything after x01\\x4a\\x54\\x0c)59;60; change 2 bytes in the first instruction's opcode with the number of passes61; the number of passes is the number of xor operations to apply, which should be62; 1 (for the key) + the number of 4-bytes words you have in your shellcode63; you must encode ~(number_of_passes + 1) (to ensure that you're nul-free)646566;.text67;.align 268;.globl main69;.ent main70;.type main,@function7172main:7374li macro reg, imm75; lui reg, ((imm) >> 16) & 0ffffh76; ori reg, reg, (imm) & 0ffffh77addiu reg, $0, imm ; sufficient if imm.abs <= 0x7fff78endm7980li( $10, #{reg_10}) ; load number of passes ^ 0xffff81nor $10, $10, $0 ; put number of passes in $108283li( $11,-89) ; addend to calculated PC is 7384;.set noreorder85next:86bltzal $8, next87;.set reorder88slti $8, $0, 0x828289nor $11, $11, $0 ; addend in $990addu $25, $31, $11 ; $25 points to encoded shellcode +491addu $16, $31, $11 ; $16 too (used to set up the cacheflush() arg down below)9293; lui $2, 0xDDDD ; first part of the xor (old method)94slti $23, $0, 0x8282 ; store 0 in $23 (our counter)95; ori $17, $2, 0xDDDD ; second part of the xor (old method)96lw $17, -4($25) ; load xor key in $17979899li( $9, -5)100nor $9, $9, $0 ; 4 in $9101102addi $15, $9, -3 ; 1 in $15103loop:104lw $8, -4($25)105106addu $23, $23, $15 ; increment counter107xor $3, $8, $17108sltu $30, $23, $10 ; enough loops?109sw $3, -4($25)110addi $6, $9, -1 ; 3 in $6 (for cacheflush)111bne $0, $30, loop112addu $25, $25, $9 ; next instruction to decode :)113114115addiu $4, $16, -4 ; cacheflush() addr parameter116li( $10,#{reg_5}) ; cacheflush() nbytes parameter117nor $5, $10, $0 ; same as above118; li $6,3 ; $6 is set above, 3rd arg for cacheflush()119120; .set noreorder121li( $2, 4147) ; cacheflush122; .ascii "\\x01JT\\x0c" ; nul-free syscall123syscall 0x52950124; .set reorder125126127; write last decoder opcode and decoded shellcode128; li $4,1 ; stdout129; addi $5, $16, -8130; li $6,40 ; how much to write131; .set noreorder132; li $2, 4004 ; write133; syscall134; .set reorder135136137nop ; encoded shellcoded must be here (xor key right here ;)138; $t9 (aka $25) points here139140EOS141# put the key at the end of the decoder142state.decoder_key_offset = decoder.length - 4143144return decoder145end146end147148149