-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstrumentationCallbacks.cpp
183 lines (133 loc) · 5.19 KB
/
instrumentationCallbacks.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
#define PHNT_VERSION PHNT_WIN10_22H2
#include <phnt_windows.h>
#include <phnt.h>
#include <ntexapi.h>
#include <ntpsapi.h>
#include <minidumpapiset.h>
#include <asmjit/core/jitruntime.h>
#include <asmjit/x86/x86assembler.h>
#include <stdio.h>
#include <cstdint>
#include "instrumentationCallbacks.h"
#include "arxan.h"
#include "systemhooks.h"
DWORD tls_index;
extern "C" void InstrumentationCallbackThunk(void);
extern "C" void callbackFunc(PCONTEXT Context);
#define InstrumentationCallbackPreviousPc 0x2d8
#define InstrumentationCallbackPreviousSp 0x2e0
uint64_t ntdllStartLocation = 0;
uint64_t ntdllEndLocation = 0;
uint64_t win32uStartLocation = 0;
uint64_t win32uEndLocation = 0;
uint64_t arxanStubStartLocation = 0;
uint64_t arxanStubEndLocation = 0;
uint64_t baseAddrStart = 0;
uint64_t baseAddrEnd = 0;
bool* get_thread_data_pointer() {
void* thread_data = nullptr;
bool* data_pointer = nullptr;
thread_data = TlsGetValue(tls_index);
if (thread_data == nullptr) {
thread_data = reinterpret_cast<void*>(LocalAlloc(LPTR, 256));
if (thread_data == nullptr) {
return nullptr;
}
RtlZeroMemory(thread_data, 256);
if (!TlsSetValue(tls_index, thread_data)) {
return nullptr;
}
}
}
bool set_thread_handling_syscall(bool value) {
if (auto data_pointer = get_thread_data_pointer()) {
*data_pointer = value;
return true;
}
return false;
}
bool is_thread_handling_syscall() {
if (auto data_pointer = get_thread_data_pointer()) {
if ((uint64_t)data_pointer != 0x1)
return *data_pointer;
bool test = data_pointer;
return test;
}
return false;
}
void callbackFunc(CONTEXT* ctx) {
auto teb = reinterpret_cast<uint64_t>(NtCurrentTeb());
ctx->Rip = *reinterpret_cast<uint64_t*>(teb + 0x02d8);
ctx->Rsp = *reinterpret_cast<uint64_t*>(teb + 0x02e0);
ctx->Rcx = ctx->R10;
if (is_thread_handling_syscall()) {
RtlRestoreContext(ctx, nullptr);
}
if (!set_thread_handling_syscall(true)) {
RtlRestoreContext(ctx, nullptr);
}
auto return_address = reinterpret_cast<void*>(ctx->Rip);
auto return_value = reinterpret_cast<void*>(ctx->Rax);
uint64_t offset_into_function = 0;
bool base_b = (ctx->Rip > baseAddrStart && ctx->Rip < baseAddrEnd);
bool arxanStub_b = (ctx->Rip > arxanStubStartLocation && ctx->Rip < arxanStubEndLocation);
bool ntdll_b = (ctx->Rip > ntdllStartLocation && ctx->Rip < ntdllEndLocation);
bool win32u_b = (ctx->Rip > win32uStartLocation && ctx->Rip < win32uEndLocation);
if (!base_b && !arxanStub_b && !ntdll_b && !win32u_b)
printf("syscall called from random location rip %llx\n", ctx->Rip);
if (base_b)
printf("syscall called from base addr rip %llx\n", ctx->Rip);
set_thread_handling_syscall(false);
RtlRestoreContext(ctx, nullptr);
}
// if this ends up not helping us at all then i dont know what the issue is with the debugging
// shouldnt just ignore it and use cheat engines veh debugger for now and find the best time to install our checksum hooks
// fix the slow startup from virtualallocing, maybe clean up some source code and publish
uint64_t getEndOfSection(uint64_t baseAddrStart, const char* str)
{
IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(str);
IMAGE_NT_HEADERS* pNTHeaders = (IMAGE_NT_HEADERS*)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew);
DWORD sizeOfImage = pNTHeaders->OptionalHeader.SizeOfImage;
return baseAddrStart + sizeOfImage;
}
void initInstrumentation()
{
printf("init instrumentation\n");
arxanStubStartLocation = (uint64_t)ntdllAsmStubLocation;
arxanStubEndLocation = arxanStubStartLocation + 0x1000;
ntdllStartLocation = (uint64_t)GetModuleHandle("ntdll.dll");
ntdllEndLocation = getEndOfSection(ntdllStartLocation, "ntdll.dll");
win32uStartLocation = (uint64_t)GetModuleHandle("win32u.dll");
win32uEndLocation = getEndOfSection(win32uStartLocation, "win32u.dll");
baseAddrStart = (uint64_t)GetModuleHandle(nullptr);
baseAddrEnd = getEndOfSection(baseAddrStart, nullptr);
using asmjitFunc = void (*)();
static asmjit::JitRuntime runtime;
asmjit::CodeHolder code;
code.init(runtime.environment());
using namespace asmjit::x86;
Assembler a(&code);
// https://github.com/jackullrich/syscall-detect/blob/master/Thunk.asm
Mem PreviousSp(0x2e0);
PreviousSp.setSegment(asmjit::x86::gs);
a.mov(PreviousSp, rsp);
Mem PreviousPc(0x2d8);
PreviousPc.setSegment(asmjit::x86::gs);
a.mov(PreviousPc, r10);
a.mov(r10, rcx);
a.sub(rsp, 0x4d0);
a.and_(rsp, -0x10);
a.mov(rcx, rsp);
a.call(RtlCaptureContext);
a.sub(rsp, 0x20);
a.call(callbackFunc);
a.int3();
asmjitFunc InstrumentationCallbackThunk;
asmjit::Error err = runtime.add(&InstrumentationCallbackThunk, &code);
PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION Callback = { 0 };
Callback.Version = 0;
Callback.Reserved = 0;
Callback.Callback = (PVOID)(ULONG_PTR)InstrumentationCallbackThunk;
NTSTATUS result = NtSetInformationProcess(GetCurrentProcess(), (PROCESSINFOCLASS)ProcessInstrumentationCallback, &Callback, sizeof(Callback));
printf("done\n");
}