Path: blob/master/modules/nops/loongarch64/simple.rb
27885 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45# This class implements a simple NOP generator for LoongArch 64-bit (Little Endian)6class MetasploitModule < Msf::Nop78def initialize9super(10'Name' => 'Simple',11'Alias' => 'loongarch64_simple',12'Description' => 'Simple NOP generator',13'License' => MSF_LICENSE,14'Author' => ['bcoles'],15'Arch' => ARCH_LOONGARCH64)16register_advanced_options([17OptBool.new('RandomNops', [false, 'Generate a random NOP sled', true]),18])19end2021def generate_sled(length, opts)22badchars = opts['BadChars'] || ''23random = opts['Random'] || datastore['RandomNops']2425# Safe NULL-free nops using general purpose registers ($r12 - $r31)26# All instructions are NULL-free from the base ISA,27# excluding optional extensions (FPU, LSX, LASX, ...)28nops = [29[0x03c0018c].pack('V'), # xori $t0, $t0, 030[0x03c001ad].pack('V'), # xori $t1, $t1, 031[0x03c001ce].pack('V'), # xori $t2, $t2, 032[0x03c001ef].pack('V'), # xori $t3, $t3, 033[0x03c00210].pack('V'), # xori $t4, $t4, 034[0x03c00231].pack('V'), # xori $t5, $t5, 035[0x03c00252].pack('V'), # xori $t6, $t6, 036[0x03c00273].pack('V'), # xori $t7, $t7, 037[0x03c00294].pack('V'), # xori $t8, $t8, 038[0x03c002b5].pack('V'), # xori $r21, $r21, 039[0x03c002d6].pack('V'), # xori $fp, $fp, 040[0x03c002f7].pack('V'), # xori $s0, $s0, 041[0x03c00318].pack('V'), # xori $s1, $s1, 042[0x03c00339].pack('V'), # xori $s2, $s2, 043[0x03c0035a].pack('V'), # xori $s3, $s3, 044[0x03c0037b].pack('V'), # xori $s4, $s4, 045[0x03c0039c].pack('V'), # xori $s5, $s5, 046[0x03c003bd].pack('V'), # xori $s6, $s6, 047[0x03c003de].pack('V'), # xori $s7, $s7, 048[0x03c003ff].pack('V'), # xori $s8, $s8, 04950[0x0380018c].pack('V'), # ori $t0, $t0, 051[0x038001ad].pack('V'), # ori $t1, $t1, 052[0x038001ce].pack('V'), # ori $t2, $t2, 053[0x038001ef].pack('V'), # ori $t3, $t3, 054[0x03800210].pack('V'), # ori $t4, $t4, 055[0x03800231].pack('V'), # ori $t5, $t5, 056[0x03800252].pack('V'), # ori $t6, $t6, 057[0x03800273].pack('V'), # ori $t7, $t7, 058[0x03800294].pack('V'), # ori $t8, $t8, 059[0x038002b5].pack('V'), # ori $r21, $r21, 060[0x038002d6].pack('V'), # ori $fp, $fp, 061[0x038002f7].pack('V'), # ori $s0, $s0, 062[0x03800318].pack('V'), # ori $s1, $s1, 063[0x03800339].pack('V'), # ori $s2, $s2, 064[0x0380035a].pack('V'), # ori $s3, $s3, 065[0x0380037b].pack('V'), # ori $s4, $s4, 066[0x0380039c].pack('V'), # ori $s5, $s5, 067[0x038003bd].pack('V'), # ori $s6, $s6, 068[0x038003de].pack('V'), # ori $s7, $s7, 069[0x038003ff].pack('V'), # ori $s8, $s8, 07071[0x02c0018c].pack('V'), # addi.d $t0, $t0, 072[0x02c001ad].pack('V'), # addi.d $t1, $t1, 073[0x02c001ce].pack('V'), # addi.d $t2, $t2, 074[0x02c001ef].pack('V'), # addi.d $t3, $t3, 075[0x02c00210].pack('V'), # addi.d $t4, $t4, 076[0x02c00231].pack('V'), # addi.d $t5, $t5, 077[0x02c00252].pack('V'), # addi.d $t6, $t6, 078[0x02c00273].pack('V'), # addi.d $t7, $t7, 079[0x02c00294].pack('V'), # addi.d $t8, $t8, 080[0x02c002b5].pack('V'), # addi.d $r21, $r21, 081[0x02c002d6].pack('V'), # addi.d $fp, $fp, 082[0x02c002f7].pack('V'), # addi.d $s0, $s0, 083[0x02c00318].pack('V'), # addi.d $s1, $s1, 084[0x02c00339].pack('V'), # addi.d $s2, $s2, 085[0x02c0035a].pack('V'), # addi.d $s3, $s3, 086[0x02c0037b].pack('V'), # addi.d $s4, $s4, 087[0x02c0039c].pack('V'), # addi.d $s5, $s5, 088[0x02c003bd].pack('V'), # addi.d $s6, $s6, 089[0x02c003de].pack('V'), # addi.d $s7, $s7, 090[0x02c003ff].pack('V'), # addi.d $s8, $s8, 09192[0x0280018c].pack('V'), # addi.w $t0, $t0, 093[0x028001ad].pack('V'), # addi.w $t1, $t1, 094[0x028001ce].pack('V'), # addi.w $t2, $t2, 095[0x028001ef].pack('V'), # addi.w $t3, $t3, 096[0x02800210].pack('V'), # addi.w $t4, $t4, 097[0x02800231].pack('V'), # addi.w $t5, $t5, 098[0x02800252].pack('V'), # addi.w $t6, $t6, 099[0x02800273].pack('V'), # addi.w $t7, $t7, 0100[0x02800294].pack('V'), # addi.w $t8, $t8, 0101[0x028002b5].pack('V'), # addi.w $r21, $r21, 0102[0x028002d6].pack('V'), # addi.w $fp, $fp, 0103[0x028002f7].pack('V'), # addi.w $s0, $s0, 0104[0x02800318].pack('V'), # addi.w $s1, $s1, 0105[0x02800339].pack('V'), # addi.w $s2, $s2, 0106[0x0280035a].pack('V'), # addi.w $s3, $s3, 0107[0x0280037b].pack('V'), # addi.w $s4, $s4, 0108[0x0280039c].pack('V'), # addi.w $s5, $s5, 0109[0x028003bd].pack('V'), # addi.w $s6, $s6, 0110[0x028003de].pack('V'), # addi.w $s7, $s7, 0111[0x028003ff].pack('V'), # addi.w $s8, $s8, 0112]113114# Remove nops containing BadChars115nops.delete_if do |nop|116nop.bytes.any? { |byte| badchars.force_encoding('BINARY').include?(byte.chr) }117end118119# Give up if no safe nops are available120return if nops.empty?121122# Use random instructions for all NOPs123if random124sled = ''125(length / 4).times do126sled << nops.sample127end128return sled129end130131# Use a single instruction for all NOPs132return (nops.sample * (length / 4))133end134end135136137