forked from tukewin/artemis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArtemis.cpp
308 lines (251 loc) · 8.29 KB
/
Artemis.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
#pragma once
#include "Artemis.h"
/*
Whenever a memory is traversed, it gets added into the working set
Put this in a standalone thread for best results
*/
bool IsMemoryTraversed()
{
auto m = VirtualAlloc(NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
PSAPI_WORKING_SET_EX_INFORMATION _set;
_set.VirtualAddress = m;
while (true)
if (QueryWorkingSetEx(GetCurrentProcess(), &_set, sizeof(_set)) && ( _set.VirtualAttributes.Valid & 0x1 ))
{
for (long long int i = 0; ++i; ( &i )[ i ] = i);
*( ( char * ) NULL ) = 0;
}
}
CArtemis::CArtemis()
{
}
CArtemis::~CArtemis()
{
}
void CArtemis::Start()
{
std::thread traverse(&IsMemoryTraversed);
traverse.detach();
while (true) {
/*
List of functions to search for breakpoints
*/
fnList.push_back(&MessageBoxA);
fnList.push_back(&GetProcAddress);
fnList.push_back(&VirtualProtect);
this->AntiAttach();
if (this->IsDebuggerPresentPatched())
{
for (long long int i = 0; ++i; ( &i )[ i ] = i); // essentially just a way to crash the program
*( ( char * ) NULL ) = 0;
}
this->PatchRemoteBreakin();
this->Watchover();
if (this->TitanHide()) {
MessageBoxA(0, "The program can not be run with test-signing on. Please disable it and try again.", 0, 0);
for (long long int i = 0; ++i; ( &i )[ i ] = i);
}
if (this->NtQuerySystemInfoCheck())
{
/* If this is hooked, we can suppose that its an Anti-Anti-Debug measure,
yet we cannot rely just on this, an ideal solution would be to unhook
critical DLLs such as `kernel32.dll`, `ntdll.dll`, `user32.dll` or similar */
}
}
}
/*
kernel32!IsDebuggerPresent()
A popular way to detect a debugger is to call kernel32!IsDebuggerPresent()
Instead of examining the process memory for breakpoints, we can verify if kernel32!IsDebuggerPresent() was modified.
Even with enabled ASLR, Windows libraries are loaded to the same base addresses in all the processes.
*/
bool CArtemis::IsDebuggerPresentPatched()
{
HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
if (!hKernel32)
return false;
FARPROC pIsDebuggerPresent = GetProcAddress(hKernel32, "IsDebuggerPresent");
if (!pIsDebuggerPresent)
return false;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
return false;
PROCESSENTRY32W ProcessEntry;
ProcessEntry.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnapshot, &ProcessEntry))
return false;
bool bDebuggerPresent = false;
HANDLE hProcess = NULL;
DWORD dwFuncBytes = 0;
const DWORD dwCurrentPID = GetCurrentProcessId();
do
{
__try
{
if (dwCurrentPID == ProcessEntry.th32ProcessID)
continue;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessEntry.th32ProcessID);
if (NULL == hProcess)
continue;
if (!ReadProcessMemory(hProcess, pIsDebuggerPresent, &dwFuncBytes, sizeof(DWORD), NULL))
continue;
if (dwFuncBytes != *( PDWORD ) pIsDebuggerPresent)
{
bDebuggerPresent = true;
break;
}
}
__finally
{
if (hProcess)
CloseHandle(hProcess);
else
{
}
}
} while (Process32NextW(hSnapshot, &ProcessEntry));
if (hSnapshot)
CloseHandle(hSnapshot);
return bDebuggerPresent;
}
/*
ntdll!DbgBreakPoint()
- is called when a debugger attaches to a running process.
- allows the debugger to gain control because an exception is raised which it can intercept
- erasing the breakpoint will result in the debugger not breaking in and exiting the thread
*/
void CArtemis::AntiAttach()
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
if (!hNtdll)
return;
FARPROC pDbgBreakPoint = GetProcAddress(hNtdll, "DbgBreakPoint");
if (!pDbgBreakPoint)
return;
DWORD dwOldProtect;
if (!VirtualProtect(pDbgBreakPoint, 1, PAGE_EXECUTE_READWRITE, &dwOldProtect))
return;
*( PBYTE ) pDbgBreakPoint = ( BYTE ) 0xC3; // 0xC3 == RET
}
/*
ntdll!DbgUiRemoteBreakin()
When a debugger calls the kernel32!DebugActiveProcess(), a debugger calls ntdll!DbgUiRemoteBreakin() correspondingly.
To prevent the debugger from attaching to the process, we can patch ntdll!DbgUiRemoteBreakin() code to invoke the kernel32!TerminateProcess().
*/
void CArtemis::PatchRemoteBreakin()
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
if (!hNtdll)
return;
FARPROC pDbgUiRemoteBreakin = GetProcAddress(hNtdll, "DbgUiRemoteBreakin");
if (!pDbgUiRemoteBreakin)
return;
HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
if (!hKernel32)
return;
FARPROC pTerminateProcess = GetProcAddress(hKernel32, "TerminateProcess");
if (!pTerminateProcess)
return;
DbgUiRemoteBreakinPatch patch = { 0 };
patch.push_0 = '\x6A\x00';
patch.push = '\x68';
patch.CurrentPorcessHandle = 0xFFFFFFFF;
patch.mov_eax = '\xB8';
patch.TerminateProcess = ( DWORD ) pTerminateProcess;
patch.call_eax = '\xFF\xD0';
DWORD dwOldProtect;
if (!VirtualProtect(pDbgUiRemoteBreakin, sizeof(DbgUiRemoteBreakinPatch), PAGE_READWRITE, &dwOldProtect))
return;
::memcpy_s(pDbgUiRemoteBreakin, sizeof(DbgUiRemoteBreakinPatch),
&patch, sizeof(DbgUiRemoteBreakinPatch));
VirtualProtect(pDbgUiRemoteBreakin, sizeof(DbgUiRemoteBreakinPatch), dwOldProtect, &dwOldProtect);
}
/*
Debug registers DR0, DR1, DR2 and DR3 can be retrieved from the thread context.
If they contain non-zero values, it may mean that a hardware breakpoint was set.
*/
bool CArtemis::CheckHardwareBP()
{
CONTEXT ctx;
ZeroMemory(&ctx, sizeof(CONTEXT));
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if (!GetThreadContext(GetCurrentThread(), &ctx))
return false;
return ctx.Dr0 || ctx.Dr1 || ctx.Dr2 || ctx.Dr3;
}
/*
Search the first MemorySize of bytes for an INT3 instruction
*/
bool CArtemis::FoundBP(BYTE Byte, std::vector<PVOID> Memory, SIZE_T MemorySize = 0)
{
for (PVOID function : this->fnList) {
PBYTE pBytes = ( PBYTE ) function;
for (SIZE_T i = 0; i <= SIZE_T(function); i++)
{
if (( ( MemorySize > 0 ) && ( i >= MemorySize ) ) ||
( ( MemorySize == 0 ) && ( pBytes[ i ] == 0xC3 ) ))
break;
if (pBytes[ i ] == Byte)
return true;
}
return false;
}
}
/*
Traverses the function in the CArtemis!fnList for the 0xCC (int3) instructions
*/
bool CArtemis::Watchover()
{
if (this->FoundBP(0xCC, fnList))
{
for (long long int i = 0; ++i; ( &i )[ i ] = i);
*( ( char * ) NULL ) = 0;
}
return false;
}
/*
Plugins like ScyllaHide, SharpOD do tend to hook NtQuerySystemInformation
in order to circumvent the detection of a debugger by placing a hook on
the first instruction. This can be revealed by checking whether the 1st
opcode is equal to an 'invalid' (for ex. jmp) instruction.
For extra validation, I'd recommend restoring the original bytes on critical
functions like this.
[!] This might trigger a false positive since apart from ScyllaHide, certain
anti-virus programs tend to hook this (ex. Kaspersky).
*/
bool CArtemis::NtQuerySystemInfoCheck()
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
if (!hNtdll) // If this fails, somebody tried to prevent ntdll from loading &or has their Windows seriously corrupted.
return true;
FARPROC _instr = GetProcAddress(hNtdll, "NtQuerySystemInformation");
if (*( BYTE * ) _instr == 0xE9 || *( BYTE * ) _instr == 0x90 || *( BYTE * ) _instr == 0xC3)
{
return true;
}
return false;
}
/*
TitanHide is a driver intended to hide debuggers from certain processes.
The driver hooks various Nt* kernel functions (using SSDT table hooks) and modifies the return values of the original functions.
https://github.com/mrexodia/TitanHide
While the driver does a very good job of hiding a debugger, it requires test signing to be enabled to work.
While sure, Patchguard CAN be disabled, most people will not bother and will just enable test-signing.
[!] This might trigger a false positive as some people have their test-signing enabled on accident, a simple warning
should be enough.
*/
bool CArtemis::TitanHide()
{
const auto module = GetModuleHandleW(L"ntdll.dll");
const auto information = reinterpret_cast< typedefs::NtQuerySystemInformationTypedef >( GetProcAddress(
module, "NtQuerySystemInformation") );
typedefs::SYSTEM_CODEINTEGRITY_INFORMATION sci;
sci.Length = sizeof sci;
information(typedefs::SystemCodeIntegrityInformation, &sci, sizeof sci, nullptr);
const auto ret = sci.CodeIntegrityOptions & CODEINTEGRITY_OPTION_TESTSIGN || sci.CodeIntegrityOptions &
CODEINTEGRITY_OPTION_DEBUGMODE_ENABLED;
if (ret != 0)
return true;
else
return false;
}