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/nops/sparc/random.rb
Views: 11623
##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])82end83848586# Nops are always random...87def generate_sled(length, opts)8889badchars = opts['BadChars'] || ''90random = opts['Random'] || datastore['RandomNops']91blen = length9293buff = ''94count = 095while (buff.length < blen)96r = SPARC_Table[ rand(SPARC_Table.length) ]97t = ''9899case r[0]100when InsSethi101t = ins_sethi(r[1], blen - buff.length)102when InsArithmetic103t = ins_arithmetic(r[1], blen - buff.length)104when InsBranch105t = ins_branch(r[1], blen - buff.length)106else107print_status("Invalid opcode type")108raise RuntimeError109end110111failed = false112113t.each_byte do |c|114failed = true if badchars.include?(c.chr)115end116117if (not failed)118buff << t119count = -100120end121122if (count > length + 1000)123if(buff.length != 0)124return buff.slice(0, 4) * (blen / 4)125end126print_status("The SPARC nop generator could not create a usable sled")127raise RuntimeError128end129130count += 1131end132133return buff134end135136def get_dst_reg137reg = rand(30).to_i138reg += 1 if (reg >= 14) # %sp139reg += 1 if (reg >= 30) # %fp140return reg141end142143def get_src_reg144return rand(32).to_i145end146147def ins_sethi(ref, len=0)148[(get_dst_reg() << 25) | (4 << 22) | rand(1 << 22)].pack('N')149end150151def ins_arithmetic(ref, len=0)152dst = get_dst_reg()153ver = ref[0]154155# WRY fixups156if (ver == 3)157dst = 0158ver = 1159end160161# 0, ~1, !2, ~3, !4162# Use one src reg with a signed 13-bit immediate (non-0)163if((ver == 0 && rand(2)) || ver == 1)164return [165(2 << 30) |166(dst << 25) |167(ref[1] << 19) |168(get_src_reg() << 14) |169(1 << 13) |170(rand((1 << 13) - 1) + 1)171].pack('N')172end173174# ref[1] could be replaced with a static value since this only encodes for one function but it's done this way for175# conistancy/clarity.176if (ver == 4)177return [(2 << 30) | (dst << 25) | (ref[1] << 19)].pack('N')178end179180# Use two src regs181return [182(2 << 30) |183(dst << 25) |184(ref[1] << 19) |185(get_src_reg() << 14) |186get_src_reg()187].pack('N')188end189190def ins_branch(ref, len)191# We jump to 1 instruction before the payload so in cases where the delay slot is another branch instruction that is192# not taken with the anull bit set the first bit of the payload is not anulled.193len = (len / 4) - 1194195return '' if len == 0196len = 0x3fffff if (len >= 0x400000)197198a = rand(2).floor199b = ref[0]200c = rand(len - 1).floor201202return [203(a << 29) |204(b << 25) |205(2 << 22) |206c + 1207].pack('N')208end209end210211212