|
| 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 | + |
| 8 | +class MetasploitModule < Msf::Post |
| 9 | + |
| 10 | + include Msf::Post::File |
| 11 | + include Msf::Post::Windows::Priv |
| 12 | + |
| 13 | + def initialize(info={}) |
| 14 | + super(update_info(info, |
| 15 | + 'Name' => 'Multi Manage File Compressor', |
| 16 | + 'Description' => %q{ |
| 17 | + This module zips a file or a directory. On Linux, it uses the zip command. |
| 18 | + On Windows, it will try to use remote target's 7Zip if found. If not, it falls |
| 19 | + back to its own VBScript. |
| 20 | + }, |
| 21 | + 'License' => MSF_LICENSE, |
| 22 | + 'Author' => [ 'sinn3r' ], |
| 23 | + 'Platform' => [ 'win', 'linux' ], |
| 24 | + 'SessionTypes' => [ 'meterpreter', 'shell' ] |
| 25 | + )) |
| 26 | + |
| 27 | + register_options( |
| 28 | + [ |
| 29 | + OptString.new('DESTINATION', [true, 'The destination path']), |
| 30 | + OptString.new('SOURCE', [true, 'The directory or file to compress']) |
| 31 | + ], self.class) |
| 32 | + end |
| 33 | + |
| 34 | + def get_program_file_path |
| 35 | + get_env('ProgramFiles') |
| 36 | + end |
| 37 | + |
| 38 | + def has_7zip? |
| 39 | + file?("#{get_program_file_path}\\7-Zip\\7z.exe") |
| 40 | + end |
| 41 | + |
| 42 | + def vbs(dest, src) |
| 43 | + vbs_file = File.read(File.join(Msf::Config.data_directory, "post", "zip", "zip.vbs")) |
| 44 | + vbs_file << "WindowsZip \"#{src}\",\"#{dest}\"" |
| 45 | + vbs_file |
| 46 | + end |
| 47 | + |
| 48 | + def find_pid_by_user(username) |
| 49 | + computer_name = get_env('COMPUTERNAME') |
| 50 | + print_status("Searching for PID for #{computer_name}\\\\#{username}") |
| 51 | + session.sys.process.processes.each do |p| |
| 52 | + if p['user'] == "#{computer_name}\\#{username}" |
| 53 | + return p['pid'] |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + nil |
| 58 | + end |
| 59 | + |
| 60 | + def steal_token |
| 61 | + current_user = get_env('USERNAME') |
| 62 | + pid = find_pid_by_user(current_user) |
| 63 | + |
| 64 | + unless pid |
| 65 | + fail_with(Failure::Unknown, "Unable to find a PID for #{current_user} to execute .vbs") |
| 66 | + end |
| 67 | + |
| 68 | + print_status("Stealing token from PID #{pid} for #{current_user}") |
| 69 | + begin |
| 70 | + session.sys.config.steal_token(pid) |
| 71 | + rescue Rex::Post::Meterpreter::RequestError => e |
| 72 | + # It could raise an exception even when the token is successfully stolen, |
| 73 | + # so we will just log the exception and move on. |
| 74 | + elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}") |
| 75 | + end |
| 76 | + |
| 77 | + @token_stolen = true |
| 78 | + end |
| 79 | + |
| 80 | + def upload_exec_vbs_zip |
| 81 | + if is_system? |
| 82 | + unless session |
| 83 | + print_error('Unable to decompress with VBS technique without Meterpreter') |
| 84 | + return |
| 85 | + end |
| 86 | + |
| 87 | + steal_token |
| 88 | + end |
| 89 | + |
| 90 | + script = vbs(datastore['DESTINATION'], datastore['SOURCE']) |
| 91 | + tmp_path = "#{get_env('TEMP')}\\zip.vbs" |
| 92 | + print_status("VBS file uploaded to #{tmp_path}") |
| 93 | + write_file(tmp_path, script) |
| 94 | + cmd_exec("wscript.exe #{tmp_path}") |
| 95 | + end |
| 96 | + |
| 97 | + def do_7zip |
| 98 | + program_file_path = get_program_file_path |
| 99 | + output = cmd_exec("#{program_file_path}\\7-Zip\\7z.exe a -tzip \"#{datastore['DESTINATION']}\" \"#{datastore['SOURCE']}\"") |
| 100 | + vprint_line(output) |
| 101 | + end |
| 102 | + |
| 103 | + def do_zip |
| 104 | + output = cmd_exec("zip -D -q -r #{datastore['DESTINATION']} #{datastore['SOURCE']}") |
| 105 | + vprint_line(output) |
| 106 | + end |
| 107 | + |
| 108 | + def windows_zip |
| 109 | + if has_7zip? |
| 110 | + print_status("Compressing #{datastore['DESTINATION']} via 7zip") |
| 111 | + do_7zip |
| 112 | + else |
| 113 | + print_status("Compressing #{datastore['DESTINATION']} via VBS") |
| 114 | + upload_exec_vbs_zip |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def linux_zip |
| 119 | + print_status("Compressing #{datastore['DESTINATION']} via zip") |
| 120 | + do_zip |
| 121 | + end |
| 122 | + |
| 123 | + def cleanup |
| 124 | + if @token_stolen && session |
| 125 | + session.sys.config.revert_to_self |
| 126 | + print_status('Token restored.') |
| 127 | + end |
| 128 | + |
| 129 | + super |
| 130 | + end |
| 131 | + |
| 132 | + def run |
| 133 | + @token_stolen = false |
| 134 | + |
| 135 | + os = get_target_os |
| 136 | + case os |
| 137 | + when Msf::Module::Platform::Windows.realname.downcase |
| 138 | + windows_zip |
| 139 | + else |
| 140 | + linux_zip |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | +end |
| 145 | + |
0 commit comments