Path: blob/master/modules/nops/sparc/random.rb
19851 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45###6#7# SingleByte8# ----------9#10# This class implements NOP generator for the SPARC platform11#12###13class MetasploitModule < Msf::Nop1415# Nop types16InsSethi = 017InsArithmetic = 118InsBranch = 21920# Generator table21SPARC_Table = [22[ InsSethi, [ ], ], # sethi23[ InsArithmetic, [ 0, 0 ], ], # add24[ InsArithmetic, [ 0, 1 ], ], # and25[ InsArithmetic, [ 0, 2 ], ], # or26[ InsArithmetic, [ 0, 3 ], ], # xor27[ InsArithmetic, [ 0, 4 ], ], # sub28[ InsArithmetic, [ 0, 5 ], ], # andn29[ InsArithmetic, [ 0, 6 ], ], # orn30[ InsArithmetic, [ 0, 7 ], ], # xnor31[ InsArithmetic, [ 0, 8 ], ], # addx32[ InsArithmetic, [ 0, 12 ], ], # subx33[ InsArithmetic, [ 0, 16 ], ], # addcc34[ InsArithmetic, [ 0, 17 ], ], # andcc35[ InsArithmetic, [ 0, 18 ], ], # orcc36[ InsArithmetic, [ 0, 19 ], ], # xorcc37[ InsArithmetic, [ 0, 20 ], ], # subcc38[ InsArithmetic, [ 0, 21 ], ], # andncc39[ InsArithmetic, [ 0, 22 ], ], # orncc40[ InsArithmetic, [ 0, 23 ], ], # xnorcc41[ InsArithmetic, [ 0, 24 ], ], # addxcc42[ InsArithmetic, [ 0, 28 ], ], # subxcc43[ InsArithmetic, [ 0, 32 ], ], # taddcc44[ InsArithmetic, [ 0, 33 ], ], # tsubcc45[ InsArithmetic, [ 0, 36 ], ], # mulscc46[ InsArithmetic, [ 2, 37 ], ], # sll47[ InsArithmetic, [ 2, 38 ], ], # srl48[ InsArithmetic, [ 2, 39 ], ], # sra49[ InsArithmetic, [ 4, 40 ], ], # rdy50[ InsArithmetic, [ 3, 48 ], ], # wry51[ InsBranch, [ 0 ] ], # bn[,a]52[ InsBranch, [ 1 ] ], # be[,a]53[ InsBranch, [ 2 ] ], # ble[,a]54[ InsBranch, [ 3 ] ], # bl[,a]55[ InsBranch, [ 4 ] ], # bleu[,a]56[ InsBranch, [ 5 ] ], # bcs[,a]57[ InsBranch, [ 6 ] ], # bneg[,a]58[ InsBranch, [ 7 ] ], # bvs[,a]59[ InsBranch, [ 8 ] ], # ba[,a]60[ InsBranch, [ 9 ] ], # bne[,a]61[ InsBranch, [ 10 ] ], # bg[,a]62[ InsBranch, [ 11 ] ], # bge[,a]63[ InsBranch, [ 12 ] ], # bgu[,a]64[ InsBranch, [ 13 ] ], # bcc[,a]65[ InsBranch, [ 14 ] ], # bpos[,a]66[ InsBranch, [ 15 ] ], # bvc[,a]67]6869def initialize70super(71'Name' => 'SPARC NOP Generator',72'Alias' => 'sparc_simple',73'Description' => 'SPARC NOP generator',74'Author' => 'vlad902',75'License' => MSF_LICENSE,76'Arch' => ARCH_SPARC)7778register_advanced_options(79[80OptBool.new('RandomNops', [ false, 'Generate a random NOP sled', true ])81]82)83end8485# Nops are always random...86def generate_sled(length, opts)87badchars = opts['BadChars'] || ''88blen = length8990buff = ''91count = 092while (buff.length < blen)93r = SPARC_Table[rand(SPARC_Table.length)]94t = ''9596case r[0]97when InsSethi98t = ins_sethi(r[1], blen - buff.length)99when InsArithmetic100t = ins_arithmetic(r[1], blen - buff.length)101when InsBranch102t = ins_branch(r[1], blen - buff.length)103else104print_status('Invalid opcode type')105raise RuntimeError106end107108failed = false109110t.each_byte do |c|111failed = true if badchars.include?(c.chr)112end113114if !failed115buff << t116count = -100117end118119if (count > length + 1000)120if !buff.empty?121return buff.slice(0, 4) * (blen / 4)122end123124print_status('The SPARC nop generator could not create a usable sled')125raise RuntimeError126end127128count += 1129end130131return buff132end133134def get_dst_reg135reg = rand(30).to_i136reg += 1 if (reg >= 14) # %sp137reg += 1 if (reg >= 30) # %fp138return reg139end140141def get_src_reg142return rand(32).to_i143end144145def ins_sethi(_ref, _len = 0)146[(get_dst_reg << 25) | (4 << 22) | rand(1 << 22)].pack('N')147end148149def ins_arithmetic(ref, _len = 0)150dst = get_dst_reg151ver = ref[0]152153# WRY fixups154if (ver == 3)155dst = 0156ver = 1157end158159# 0, ~1, !2, ~3, !4160# Use one src reg with a signed 13-bit immediate (non-0)161if (ver == 0 && rand(2)) || ver == 1162return [163(2 << 30) |164(dst << 25) |165(ref[1] << 19) |166(get_src_reg << 14) |167(1 << 13) |168(rand((1 << 13) - 1) + 1)169].pack('N')170end171172# ref[1] could be replaced with a static value since this only encodes for one function but it's done this way for173# conistancy/clarity.174if (ver == 4)175return [(2 << 30) | (dst << 25) | (ref[1] << 19)].pack('N')176end177178# Use two src regs179return [180(2 << 30) |181(dst << 25) |182(ref[1] << 19) |183(get_src_reg << 14) |184get_src_reg185].pack('N')186end187188def ins_branch(ref, len)189# We jump to 1 instruction before the payload so in cases where the delay slot is another branch instruction that is190# not taken with the anull bit set the first bit of the payload is not anulled.191len = (len / 4) - 1192193return '' if len == 0194195len = 0x3fffff if (len >= 0x400000)196197a = rand(2).floor198b = ref[0]199c = rand(len - 1).floor200201return [202(a << 29) |203(b << 25) |204(2 << 22) |205c + 1206].pack('N')207end208end209210211