-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathumium.hpp
277 lines (240 loc) · 8.33 KB
/
umium.hpp
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
#ifndef UMIUM_HPP
#define UMIUM_HPP
#pragma region UMIUM_INCLUDES
#include <Windows.h>
#include <functional>
#include <intrin.h>
#include <strsafe.h>
#include <msclr/marshal_cppstd.h>
#pragma endregion UMIUM_INCLUDES
#pragma region UMIUM_WARNINGS
#pragma warning(disable : 4838)
#pragma warning(disable : 4309)
#pragma warning(disable : 4312)
#pragma warning(disable : 4311)
#pragma warning(disable : 4302)
#pragma warning(disable : 4715)
#pragma endregion UMIUM_WARNINGS
#ifndef UMIUM_NO_FORCEINLINE
#if defined(_MSC_VER)
#define UMIUM_FORCEINLINE __forceinline
#elif defined(__GNUC__) && __GNUC__ > 3
#define UMIUM_FORCEINLINE inline __attribute__((__always_inline__))
#else
#define UMIUM_FORCEINLINE inline
#endif
#else
#define UMIUM_FORCEINLINE inline
#endif
namespace umium
{
namespace typedefs
{
using NtQuerySystemInformationTypedef = NTSTATUS(*)(ULONG, PVOID, ULONG, PULONG);
typedef struct _SYSTEM_CODEINTEGRITY_INFORMATION
{
ULONG Length;
ULONG CodeIntegrityOptions;
} SYSTEM_CODEINTEGRITY_INFORMATION, *PSYSTEM_CODEINTEGRITY_INFORMATION;
typedef enum _SYSTEM_INFORMATION_CLASS
{
SystemBasicInformation = 0,
SystemPerformanceInformation = 2,
SystemTimeOfDayInformation = 3,
SystemProcessInformation = 5,
SystemProcessorPerformanceInformation = 8,
SystemInterruptInformation = 23,
SystemExceptionInformation = 33,
SystemRegistryQuotaInformation = 37,
SystemLookasideInformation = 45,
SystemCodeIntegrityInformation = 103,
SystemPolicyInformation = 134,
} SYSTEM_INFORMATION_CLASS;
}
namespace utils
{
UMIUM_FORCEINLINE const PEB* get_peb() noexcept
{
#if defined(_M_X64) || defined(__amd64__)
return reinterpret_cast<const PEB*>(__readgsqword(0x60));
#elif defined(_M_IX86) || defined(__i386__)
return reinterpret_cast<const PEB*>(__readfsdword(0x30));
#elif defined(_M_ARM) || defined(__arm__)
return *reinterpret_cast<const PEB**>(_MoveFromCoprocessor(15, 0, 13, 0, 2) + 0x30);
#elif defined(_M_ARM64) || defined(__aarch64__)
return *reinterpret_cast<const PEB**>(__getReg(18) + 0x60);
#elif defined(_M_IA64) || defined(__ia64__)
return *reinterpret_cast<const PEB**>(static_cast<char*>(_rdteb()) + 0x60);
#else
#error Unsupported platform.
#endif
}
}
namespace security
{
std::function<void(void)> ProtectionThread = []()
{
while (true)
{
BYTE* overflow = reinterpret_cast<BYTE*>("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
PostQuitMessage(0);
TerminateProcess(GetCurrentProcess(), 0);
PostQuitMessage(0);
WriteProcessMemory(GetCurrentProcess(), main, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), FindWindowA, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), memcpy, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), OpenProcess, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), GetProcAddress, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), WriteProcessMemory, overflow, 1024, nullptr);
WriteProcessMemory(GetCurrentProcess(), GetAsyncKeyState, overflow, 1024, nullptr);
}
};
std::function<void(void)> find_window = []()
{
if (FindWindowA(nullptr, "IDA v7.0.170914") || FindWindowA(nullptr, "x64dbg") || FindWindowA(nullptr, "Scylla x64 v0.9.8") || FindWindowA(nullptr, "IAT Autosearch"))
{
ProtectionThread();
}
};
std::function<void(void)> is_debugger_present = []()
{
auto is_dbg_present = FALSE;
if (sDebuggerPresent())
{
ProtectionThread();
}
if (CheckRemoteDebuggerPresent(GetCurrentProcess(), &is_dbg_present))
{
if (is_dbg_present)
{
ProtectionThread();
}
};
std::function<void(void)> anti_attach = []()
{
HMODULE h_ntdll = GetModuleHandleA("ntdll.dll");
if (!h_ntdll)
return;
FARPROC p_dbg_break_point = GetProcAddress(h_ntdll, "DbgBreakPoint");
if (!p_dbg_break_point)
return;
DWORD dw_old_protect;
if (!VirtualProtect(p_dbg_break_point, 1, PAGE_EXECUTE_READWRITE, &dw_old_protect))
return;
*reinterpret_cast<PBYTE>(p_dbg_break_point) = static_cast<BYTE>(0xC3); // 0xC3 == RET
};
std::function<void(void)> is_memory_traversed = []()
{
const auto m = VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
PSAPI_WORKING_SET_EX_INFORMATION set;
set.VirtualAddress = m;
if (K32QueryWorkingSetEx(GetCurrentProcess(), &set, sizeof(set)) && (set.VirtualAttributes.Valid & 0x1))
{
ProtectionThread();
}
};
std::function<int(void)> check_remote_session = []()
{
const auto session_metrics = GetSystemMetrics(SM_REMOTESESSION);
return session_metrics != 0;
};
std::function<int(void)> check_window_name = []()
{
if (FindWindowA("Qt5QWindowIcon", nullptr) || FindWindowA("x64dbg", nullptr) || FindWindowA("SunAwtFrame", nullptr) || FindWindowA("ID", nullptr))
{
return 1;
}
return 0;
};
std::function<int(void)> check_sandboxie = []()
{
if (GetModuleHandleA("SbieDll.dll"))
{
return 1;
}
return 0;
};
std::function<int(void)> check_kernel_drivers = []()
{
LPVOID drivers[1024];
DWORD cb_needed;
if (K32EnumDeviceDrivers(drivers, sizeof(drivers), &cb_needed) && cb_needed < sizeof(drivers))
{
wchar_t szDriver[1024];
const wchar_t* bl_driver_list[] = { L"kprocesshacker.sys", L"SbieSvc.sys", L"HttpDebuggerSdk.sys", L"dbk64.sys",
L"dbk32.sys", L"SharpOD_Drv.sys" }; /* unicode anyways */
const int c_drivers = cb_needed / sizeof(drivers[0]);
for (auto i = 0; i < c_drivers; i++)
{
if (K32GetDeviceDriverBaseNameW(drivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))
{
for (const auto* driver_name : bl_driver_list)
{
if (wcscmp(szDriver, driver_name) == 0)
{
std::wstring ws(driver_name);
std::string str(ws.begin(), ws.end());
std::string output = "Detected blacklisted driver loaded (" + str + "). Please unload it from memory.";
MessageBoxA(nullptr, output.c_str(), "umium", MB_ICONERROR | MB_OK);
return 1;
}
}
}
}
}
return 0;
};
std::function<int(void)> check_titan_hide = []()
{
const auto module = GetModuleHandleA("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 1;
return 0;
};
std::function<int(void)> check_dbg_print = []()
{
__try
{
RaiseException(DBG_PRINTEXCEPTION_C, 0, 0, nullptr);
}
__except (_exception_code() == DBG_PRINTEXCEPTION_C)
{
return 1;
}
return 0;
};
std::function<int(void)> check_guard_hook = []()
{
MEMORY_BASIC_INFORMATION memory_info;
PEB* peb = utils::get_peb();
LIST_ENTRY head = peb->Ldr->InMemoryOrderModuleList;
LIST_ENTRY curr = head;
for (auto curr = head; curr.Flink != &peb->Ldr->InMemoryOrderModuleList; curr = *curr.Flink)
{
LDR_DATA_TABLE_ENTRY* mod = (LDR_DATA_TABLE_ENTRY*)CONTAINING_RECORD(curr.Flink, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
if (mod->FullDllName.Buffer)
{
auto* headers = reinterpret_cast<PIMAGE_NT_HEADERS>(static_cast<char*>(mod->DllBase) + static_cast<PIMAGE_DOS_HEADER>(mod->DllBase)->e_lfanew);
auto* sections = IMAGE_FIRST_SECTION(headers);
for (auto i = 0; i <= headers->FileHeader.NumberOfSections; i++)
{
auto* section = §ions[i];
auto virtualAddress = static_cast<PBYTE>(mod->DllBase) + section->VirtualAddress;
if (VirtualQuery(virtualAddress, &memory_info, sizeof(MEMORY_BASIC_INFORMATION)))
{
if (memory_info.State == MEM_COMMIT && (memory_info.Protect & PAGE_GUARD))
return 1;
}
}
}
}
return 0;
};
}
}
#endif /* include guard */