-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathScyllaHideIDAProPlugin.cpp
389 lines (326 loc) · 10.9 KB
/
ScyllaHideIDAProPlugin.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
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
#include <Windows.h>
#define USE_STANDARD_FILE_FUNCTIONS
//for 64bit - p64
#ifdef BUILD_IDA_64BIT
#define __EA64__
#pragma comment(lib, "x64_win_vc_64_pro/ida.lib")
#else
//for 32bit - plw
#pragma comment(lib, "x64_win_vc_32_pro/ida.lib")
#endif
#pragma warning(push, 0)
#include <ida.hpp>
#include <idp.hpp>
#include <dbg.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#pragma warning(pop)
#include <Scylla/Logger.h>
#include <Scylla/Settings.h>
#include <Scylla/Version.h>
#include <Scylla/Util.h>
#include <Scylla/OsInfo.h>
#include "..\PluginGeneric\Injector.h"
#include "..\PluginGeneric\OptionsDialog.h"
#include "IdaServerClient.h"
#include "resource.h"
typedef void(__cdecl * t_AttachProcess)(DWORD dwPID);
extern t_AttachProcess _AttachProcess;
const WCHAR g_scyllaHideDllFilename[] = L"HookLibraryx86.dll";
const WCHAR g_scyllaHidex64ServerFilename[] = L"ScyllaHideIDAServerx64.exe";
scl::Settings g_settings;
scl::Logger g_log;
std::wstring g_scyllaHideDllPath;
std::wstring g_scyllaHideIniPath;
std::wstring g_scyllaHidex64ServerPath;
HOOK_DLL_DATA g_hdd;
//globals
HINSTANCE hinst;
DWORD ProcessId = 0;
bool bHooked = false;
HMODULE hNtdllModule = 0;
PROCESS_INFORMATION ServerProcessInfo = { 0 };
STARTUPINFO ServerStartupInfo = { 0 };
bool isAttach = false;
static void LogCallback(const char *message)
{
msg("[%s] %s\n", SCYLLA_HIDE_NAME_A, message);
}
static void AttachProcess(DWORD dwPID)
{
int res = attach_process((pid_t)dwPID);
switch (res) {
case -1:
{
MessageBoxA(GetForegroundWindow(),
"Can't attach to that process !",
"ScyllaHide Plugin", MB_OK | MB_ICONERROR);
break;
}
case -2:
{
MessageBoxA(GetForegroundWindow(),
"Can't find that PID !",
"ScyllaHide Plugin", MB_OK | MB_ICONERROR);
break;
}
}
}
static bool SetDebugPrivileges()
{
TOKEN_PRIVILEGES Debug_Privileges;
bool retVal = false;
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &Debug_Privileges.Privileges[0].Luid))
{
HANDLE hToken = 0;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
Debug_Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
Debug_Privileges.PrivilegeCount = 1;
retVal = AdjustTokenPrivileges(hToken, FALSE, &Debug_Privileges, 0, NULL, NULL) != FALSE;
CloseHandle(hToken);
}
}
return retVal;
}
//callback for various debug events
static ssize_t idaapi debug_mainloop(void *user_data, int notif_code, va_list va)
{
switch (notif_code)
{
case dbg_process_attach:
{
isAttach = true;
break; //attaching not supported
}
case dbg_process_start:
{
isAttach = false;
const debug_event_t* dbgEvent = va_arg(va, const debug_event_t*);
ProcessId = dbgEvent->pid;
bHooked = false;
ZeroMemory(&g_hdd, sizeof(HOOK_DLL_DATA));
if (dbg != nullptr)
{
//char text[1000];
//wsprintfA(text, "dbg->id %d processor %s", dbg->id , dbg->processor);
//MessageBoxA(0, text, text,0);
// dbg->id DEBUGGER_ID_WINDBG -> 64bit and 32bit
// dbg->id DEBUGGER_ID_X86_IA32_WIN32_USER -> 32bit
if (dbg->is_remote())
{
qstring hoststring;
char host[MAX_PATH] = { 0 };
char port[6] = { 0 };
wcstombs(port, g_settings.opts().idaServerPort.c_str(), _countof(port));
get_process_options(NULL, NULL, NULL, &hoststring, NULL, NULL);
GetHost((char*)hoststring.c_str(), host);
//msg("Host-String: %s\n", hoststring.c_str());
//msg("Host: %s\n", host);
#ifdef BUILD_IDA_64BIT
//autostart server if necessary
if(g_settings.opts().idaAutoStartServer)
{
if (!scl::FileExistsW(g_scyllaHidex64ServerPath.c_str()))
{
g_log.LogError(L"Cannot find server executable %s\n", g_scyllaHidex64ServerPath.c_str());
}
DWORD dwRunningStatus = 0;
if (ServerProcessInfo.hProcess)
{
GetExitCodeProcess(ServerProcessInfo.hProcess, &dwRunningStatus);
}
if(dwRunningStatus != STILL_ACTIVE)
{
if (ServerProcessInfo.hProcess)
{
CloseHandle(ServerProcessInfo.hProcess);
CloseHandle(ServerProcessInfo.hThread);
}
ZeroMemory(&ServerStartupInfo, sizeof(ServerStartupInfo));
ZeroMemory(&ServerProcessInfo, sizeof(ServerProcessInfo));
WCHAR commandline[MAX_PATH*2] = {0};
wcscpy(commandline, g_scyllaHidex64ServerPath.c_str());
wcscat(commandline, L" ");
wcscat(commandline, g_settings.opts().idaServerPort.c_str());
ServerStartupInfo.cb = sizeof(ServerStartupInfo);
if (!CreateProcessW(0, commandline, NULL, NULL, FALSE, 0, NULL, NULL, &ServerStartupInfo, &ServerProcessInfo))
{
g_log.LogError(L"Cannot start server, error %d", GetLastError());
}
else
{
g_log.LogInfo(L"Started IDA Server successfully");
}
}
}
#endif
if (ConnectToServer(host, port))
{
if (!SendEventToServer(notif_code, ProcessId))
{
g_log.LogError(L"SendEventToServer failed");
}
}
else
{
g_log.LogError(L"Cannot connect to host %s", host);
}
}
else
{
#ifndef BUILD_IDA_64BIT
if (!scl::IsWindows64() && !bHooked) // Only apply on native x86 OS, see dbg_library_unload below
{
ReadNtApiInformation(&g_hdd);
bHooked = true;
startInjection(ProcessId, &g_hdd, g_scyllaHideDllPath.c_str(), true);
}
#else
g_log.LogError(L"Error IDA_64BIT please contact ScyllaHide developers!");
#endif
}
}
}
break;
case dbg_process_exit:
{
if (!isAttach && dbg->is_remote())
{
if (!SendEventToServer(notif_code, ProcessId))
{
g_log.LogError(L"SendEventToServer failed");
}
CloseServerSocket();
}
ProcessId = 0;
bHooked = false;
}
break;
case dbg_library_load:
{
if (!isAttach && dbg->is_remote())
{
if (!SendEventToServer(notif_code, ProcessId))
{
g_log.LogError(L"SendEventToServer failed");
}
}
else if (!isAttach)
{
#ifndef BUILD_IDA_64BIT
if (bHooked)
{
startInjection(ProcessId, &g_hdd, g_scyllaHideDllPath.c_str(), false);
}
#endif
}
}
break;
#ifndef BUILD_IDA_64BIT
case dbg_library_unload:
{
if (scl::IsWindows64() && !bHooked)
{
// Bogus unload event which is actually a load of a native x64 DLL (ntdll, wow64, wow64cpu, wow64win)
ReadNtApiInformation(&g_hdd);
bHooked = true;
startInjection(ProcessId, &g_hdd, g_scyllaHideDllPath.c_str(), true);
}
}
break;
#endif
case dbg_bpt:
{
thid_t tid = va_arg(va, thid_t);
ea_t breakpoint_ea = va_arg(va, ea_t);
va_arg(va, int*);
}
break;
case dbg_exception:
{
const debug_event_t* dbgEvent = va_arg(va, const debug_event_t*);
}
break;
}
return 0;
}
//cleanup on plugin unload
static void idaapi IDAP_term(void)
{
unhook_from_notification_point(HT_DBG, debug_mainloop, NULL);
}
//called when user clicks in plugin menu or presses hotkey
static bool idaapi IDAP_run(size_t arg)
{
DialogBoxW(hinst, MAKEINTRESOURCE(IDD_OPTIONS), GetForegroundWindow(), &OptionsDlgProc);
return true;
}
//init the plugin
static plugmod_t* idaapi IDAP_init(void)
{
//ensure target is PE executable
if (inf.filetype != f_PE) return PLUGIN_SKIP;
//install hook for debug mainloop
if (!hook_to_notification_point(HT_DBG, debug_mainloop, NULL))
{
g_log.LogError(L"Error hooking notification point");
return PLUGIN_SKIP;
}
msg("##################################################\n");
msg("# " SCYLLA_HIDE_NAME_A " v" SCYLLA_HIDE_VERSION_STRING_A " Copyright 2014-" COMPILE_YEAR_A " Aguila / cypher #\n");
msg("##################################################\n");
bHooked = false;
ProcessId = 0;
ZeroMemory(&ServerStartupInfo, sizeof(ServerStartupInfo));
ZeroMemory(&ServerProcessInfo, sizeof(ServerProcessInfo));
return PLUGIN_KEEP;
}
// There isn't much use for these yet, but I set them anyway.
static char IDAP_comment[] = SCYLLA_HIDE_NAME_A " usermode Anti-Anti-Debug Plugin";
static char IDAP_help[] = SCYLLA_HIDE_NAME_A;
// The name of the plug-in displayed in the Edit->Plugins menu
static char IDAP_name[] = SCYLLA_HIDE_NAME_A;
// The hot-key the user can use to run your plug-in.
static char IDAP_hotkey[] = "Alt-X";
// The all-important exported PLUGIN object
idaman ida_module_data plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
0,
IDAP_init,
IDAP_term,
IDAP_run,
IDAP_comment,
IDAP_help,
IDAP_name,
IDAP_hotkey
};
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
hinst = hInstDll;
_AttachProcess = AttachProcess;
hNtdllModule = GetModuleHandleW(L"ntdll.dll");
auto wstrPath = scl::GetModuleFileNameW(hInstDll);
wstrPath.resize(wstrPath.find_last_of(L'\\') + 1);
g_scyllaHideDllPath = wstrPath + g_scyllaHideDllFilename;
g_scyllaHideIniPath = wstrPath + scl::Settings::kFileName;
g_scyllaHidex64ServerPath = wstrPath + g_scyllaHidex64ServerFilename;
auto log_file = wstrPath + scl::Logger::kFileName;
g_log.SetLogFile(log_file.c_str());
g_log.SetLogCb(scl::Logger::Info, LogCallback);
g_log.SetLogCb(scl::Logger::Error, LogCallback);
g_settings.Load(g_scyllaHideIniPath.c_str());
if (!SetDebugPrivileges())
{
g_log.LogInfo(L"Failed to set debug privileges");
}
if (!StartWinsock())
{
MessageBoxW(0, L"Failed to start Winsock!", L"Error", MB_ICONERROR);
}
}
return TRUE;
}