|
| 1 | +## |
| 2 | +# This module requires Metasploit: http://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | +require 'rex' |
| 8 | + |
| 9 | +class Metasploit3 < Msf::Post |
| 10 | + |
| 11 | + include Msf::Post::File |
| 12 | + |
| 13 | + def initialize(info={}) |
| 14 | + super( update_info( info, |
| 15 | + 'Name' => 'FileDropper code test case', |
| 16 | + 'Description' => %q{ Test case for issue #4667 }, |
| 17 | + 'License' => MSF_LICENSE, |
| 18 | + 'Author' => [ 'juan vazquez' ], |
| 19 | + 'Platform' => %w{ linux osx unix win java php python }, |
| 20 | + 'SessionTypes' => [ 'shell', 'meterpreter' ] |
| 21 | + )) |
| 22 | + end |
| 23 | + |
| 24 | + def check_file(file, win_file) |
| 25 | + if session.platform =~ /win/ |
| 26 | + res = file_exist?(win_file) |
| 27 | + else |
| 28 | + res = file_exist?(file) |
| 29 | + end |
| 30 | + |
| 31 | + res |
| 32 | + end |
| 33 | + |
| 34 | + def file_deleted?(file, win_file, exists_before) |
| 35 | + if exists_before |
| 36 | + if check_file(file, win_file) |
| 37 | + print_error("Unable to delete #{file}") |
| 38 | + false |
| 39 | + else |
| 40 | + print_good("Deleted #{file}") |
| 41 | + true |
| 42 | + end |
| 43 | + else |
| 44 | + print_warning("Tried to delete #{file}, unknown result") |
| 45 | + true |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def run |
| 50 | + @dropped_files = [ |
| 51 | + '/tmp/test1.txt', |
| 52 | + '/tmp/test2.txt', |
| 53 | + '/tmp/test3.txt' |
| 54 | + ] |
| 55 | + |
| 56 | + @dropped_files.delete_if do |file| |
| 57 | + print_status("Trying to delete #{file}... ") |
| 58 | + win_file = file.gsub("/", "\\\\") |
| 59 | + exists_before = check_file(file, win_file) |
| 60 | + |
| 61 | + if session.type == "meterpreter" |
| 62 | + begin |
| 63 | + # Meterpreter should do this automatically as part of |
| 64 | + # fs.file.rm(). Until that has been implemented, remove the |
| 65 | + # read-only flag with a command. |
| 66 | + if session.platform =~ /win/ |
| 67 | + session.shell_command_token(%Q|attrib.exe -r #{win_file}|) |
| 68 | + end |
| 69 | + session.fs.file.rm(file) |
| 70 | + rescue ::Rex::Post::Meterpreter::RequestError |
| 71 | + return false |
| 72 | + end |
| 73 | + file_deleted?(file, win_file, exists_before) |
| 74 | + else |
| 75 | + win_cmds = [ |
| 76 | + %Q|attrib.exe -r "#{win_file}"|, |
| 77 | + %Q|del.exe /f /q "#{win_file}"| |
| 78 | + ] |
| 79 | + # We need to be platform-independent here. Since we can't be |
| 80 | + # certain that {#target} is accurate because exploits with |
| 81 | + # automatic targets frequently change it, we just go ahead and |
| 82 | + # run both a windows and a unix command in the same line. One |
| 83 | + # of them will definitely fail and the other will probably |
| 84 | + # succeed. Doing it this way saves us an extra round-trip. |
| 85 | + # Trick shared by @mihi42 |
| 86 | + session.shell_command_token("rm -f \"#{file}\" >/dev/null ; echo ' & #{win_cmds.join(" & ")} & echo \" ' >/dev/null") |
| 87 | + file_deleted?(file, win_file, exists_before) |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | +end |
0 commit comments