forked from ldebug/BetaShield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionChecksumScan.cpp
More file actions
406 lines (343 loc) · 11.9 KB
/
Copy pathSectionChecksumScan.cpp
File metadata and controls
406 lines (343 loc) · 11.9 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "ProjectMain.h"
#include "Main.h"
#include "Scan.h"
#include "DynamicWinapi.h"
#include "Functions.h"
#include "Access.h"
#include "DirFuncs.h"
#include "XOR.h"
#include "Utils.h"
#include "CLog.h"
#include "Data.h"
#include "Threads.h"
bool bDataSectionInitialized = false;
DWORD dwDataSectionChecksumFirst = 0;
bool bTextSectionInitialized = false;
DWORD dwTextSectionChecksumFirst = 0;
#pragma region SectionChecksumUtils
DWORD CalculateChecksum(DWORD dwSectionStart, DWORD dwSectionLen)
{
DWORD dwChecksumResult = 0;
__try {
dwChecksumResult = BetaFunctionTable->RtlComputeCrc32(0, (const BYTE*)dwSectionStart, dwSectionLen);
}
__except (1) {
}
if (dwChecksumResult)
goto skip;
auto pSectionBuf = (BYTE*)malloc(sizeof(BYTE)*dwSectionLen);
auto pSectionAddress = (DWORD*)dwSectionStart;
BetaFunctionTable->Toolhelp32ReadProcessMemory(BetaFunctionTable->GetCurrentProcessId(), pSectionAddress, pSectionBuf, dwSectionLen, NULL);
for (DWORD i = 0; i < dwSectionLen; i++)
dwChecksumResult += pSectionBuf[i++];
if (dwChecksumResult != 0)
dwChecksumResult = (dwChecksumResult & 0x0000FFFF);
free(pSectionBuf);
skip:
return dwChecksumResult;
}
bool FindSection(PBYTE pbImageBase, const char* c_szSectionName, LPDWORD dwOffset, LPDWORD dwLen)
{
PIMAGE_DOS_HEADER pImageDosHeader = (PIMAGE_DOS_HEADER)pbImageBase;
PIMAGE_NT_HEADERS pImageNtHeaders = (PIMAGE_NT_HEADERS)(pbImageBase + pImageDosHeader->e_lfanew);
PIMAGE_SECTION_HEADER pImageSectionHeader = IMAGE_FIRST_SECTION(pImageNtHeaders);
for (int i = 0; i < pImageNtHeaders->FileHeader.NumberOfSections; ++i)
{
//LPLog->AddLog(0, "%s %s | %d", (char*)pImageSectionHeader[i].Name, c_szSectionName, !strcmp(c_szSectionName, (char*)pImageSectionHeader[i].Name));
if (!strcmp(c_szSectionName, (char*)pImageSectionHeader[i].Name))
{
*dwOffset = DWORD(pbImageBase + pImageSectionHeader[i].VirtualAddress);
*dwLen = pImageSectionHeader[i].SizeOfRawData;
return true;
}
}
return false;
}
#pragma endregion SectionChecksumUtils
#pragma region DataSectionCheck
DWORD GetDataSectionHash()
{
DWORD dwDataBase = 0;
DWORD dwDataSize = 0;
bool bFindDataSection = FindSection((BYTE*)BetaModuleTable->hBaseModule, XOR(".data"), &dwDataBase, &dwDataSize);
if (!bFindDataSection) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section can not found!");
#endif
return 1;
}
if (dwDataBase == 0) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section base address is null! Size: %p", dwDataSize);
#endif
return 2;
}
if (dwDataSize == 0) {
dwDataSize = 1024 * 100;
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section size can not detected! Size created as manual: %p IsPacked result: %d Base: %p",
dwDataSize, LPData->IsPackedProcess() ? 1 : 0, dwDataBase);
#endif
}
#if 0
if (LPData->IsPackedProcess())
{
#ifdef _DEBUG
LPLog->AddLog(0, "Process is packed! Data section protecting with PAGE_READONLY flag! Base: %p Size: %p",
dwDataBase, dwDataSize);
#endif
DWORD dwOldProtect = 0;
if (BetaFunctionTable->VirtualProtect((LPVOID)dwDataBase, dwDataSize, PAGE_READONLY, &dwOldProtect) == FALSE) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section can not protected with PAGE_READONLY!");
#endif
return 3;
}
#ifdef _DEBUG
LPLog->AddLog(0, "Data section succesfully protected with PAGE_READONLY!");
#endif
return 0;
}
else
#endif
{
if (IsBadReadPtr((LPCVOID)dwDataBase, dwDataSize)) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section is not readable area! Base: %p Size: %p", dwDataBase, dwDataSize);
#endif
return 4;
}
auto dwChecksum = CalculateChecksum(dwDataBase, dwDataSize);
if (!dwChecksum) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section checksum can not calculated!");
#endif
return 5;
}
#ifdef _DEBUG
LPLog->AddLog(0, "Data section checksum calculated: %p - %p -> (%p)", dwDataBase, dwDataSize, dwChecksum);
#endif
return dwChecksum;
}
return 0;
}
void InitCheckDataSectionHash()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Data section check init started!");
#endif
KARMA_MACRO_1
re_search:
if (dwDataSectionChecksumFirst == 0)
dwDataSectionChecksumFirst = GetDataSectionHash();
BetaFunctionTable->Sleep(100);
if (dwDataSectionChecksumFirst == 0) {
#ifdef _DEBUG
LPLog->AddLog(0, "Data section can not detected yet! result: %p", dwDataSectionChecksumFirst);
#endif
goto re_search;
}
bDataSectionInitialized = true;
#ifdef _DEBUG
LPLog->AddLog(0, "Data section check initialized... first value = %p", dwDataSectionChecksumFirst);
#endif
}
void CheckDataSectionHash()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Data section check started!");
#endif
KARMA_MACRO_1
if (bDataSectionInitialized == false) {
#ifdef _DEBUG
LPLog->AddLog(0, "Data section can not initialized yet!");
#endif
return;
}
DWORD dwDataSectionChecksumSecond = GetDataSectionHash();
#ifdef _DEBUG
LPLog->AddLog(0, "Data section Checking.. second value = %p", dwDataSectionChecksumSecond);
#endif
KARMA_MACRO_2
if (LPData->IsPackedProcess() == false && (!dwDataSectionChecksumFirst || !dwDataSectionChecksumSecond)){
#ifdef _DEBUG
LPLog->AddLog(0, "Data section Check failed! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwDataSectionChecksumFirst, dwDataSectionChecksumSecond);
#endif
LPFunctions->CloseProcess("Anticheat integrity check failed! Error code: 1", false, "");
}
if (dwDataSectionChecksumFirst != dwDataSectionChecksumSecond)
{
#ifdef _DEBUG
LPLog->ErrorLog(0, "Data section is corrupted! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwDataSectionChecksumFirst, dwDataSectionChecksumSecond);
#endif
CHAR __warn[] = { 'G', 'a', 'm', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 'd', 'a', 't', 'a', ' ', 'i', 'n', 't', 'e', 'g', 'r', 'i', 't', 'y', ' ', 'f', 'a', 'i', 'l', 'e', 'd', '.', ' ', 'P', 'l', 'e', 'a', 's', 'e', ' ', 'd', 'i', 's', 'a', 'b', 'l', 'e', ' ', 'a', 'n', 't', 'i', 'v', 'i', 'r', 'u', 's', ' ', 'o', 'r', ' ', 'f', 'i', 'r', 'e', 'w', 'a', 'l', 'l', ' ', 's', 'o', 'f', 't', 'w', 'a', 'r', 'e', 's', 0x0 }; // Game process data integrity failed. Please disable antivirus or firewall softwares
LPFunctions->CloseProcess(__warn, false, "");
}
#ifdef _DEBUG
else {
LPLog->AddLog(0, "Data section Check is OK! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwDataSectionChecksumFirst, dwDataSectionChecksumSecond);
}
#endif
}
#pragma endregion DataSectionCheck
#pragma region TextSectionCheck
DWORD GetTextSectionHash()
{
DWORD dwTextBase = 0;
DWORD dwTextSize = 0;
bool bFindTextSection = FindSection((BYTE*)BetaModuleTable->hBaseModule, XOR(".text"), &dwTextBase, &dwTextSize);
if (!bFindTextSection) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section can not found!");
#endif
return 1;
}
if (dwTextBase == 0) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section base address is null! Base wrapped as module base: %p IsPacked result: %d",
BetaModuleTable->hBaseModule, LPData->IsPackedProcess() ? 1 : 0);
#endif
dwTextBase = (DWORD)BetaModuleTable->hBaseModule;
}
if (dwTextSize == 0) {
dwTextSize = 1024 * 1024;
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section size can not detected! Size created as manual: %p IsPacked result: %d Base: %p",
dwTextSize, LPData->IsPackedProcess() ? 1 : 0, dwTextBase);
#endif
}
#if 0
if (LPData->IsPackedProcess())
{
#ifdef _DEBUG
LPLog->AddLog(0, "Process is packed! Text section protecting with PAGE_READONLY flag! Base: %p Size: %p",
dwTextBase, dwTextSize);
#endif
DWORD dwOldProtect = 0;
if (BetaFunctionTable->VirtualProtect((LPVOID)dwTextBase, dwTextSize, PAGE_READONLY, &dwOldProtect) == FALSE) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section can not protected with PAGE_READONLY!");
#endif
return 3;
}
#ifdef _DEBUG
LPLog->AddLog(0, "Text section succesfully protected with PAGE_READONLY!");
#endif
return 0;
}
else
#endif
{
if (IsBadReadPtr((LPCVOID)dwTextBase, dwTextSize)) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section is not readable area! Base: %p Size: %p", dwTextBase, dwTextSize);
#endif
return 4;
}
auto dwChecksum = CalculateChecksum(dwTextBase, dwTextSize);
if (!dwChecksum) {
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section checksum can not calculated!");
#endif
return 5;
}
#ifdef _DEBUG
LPLog->AddLog(0, "Text section checksum calculated: %p - %p -> (%p)", dwTextBase, dwTextSize, dwChecksum);
#endif
return dwChecksum;
}
return 0;
}
void InitCheckTextSectionHash()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Text section check init started!");
#endif
KARMA_MACRO_1
re_search:
if (dwTextSectionChecksumFirst == 0)
dwTextSectionChecksumFirst = GetTextSectionHash();
BetaFunctionTable->Sleep(100);
if (dwTextSectionChecksumFirst == 0 && LPData->IsPackedProcess() == false) {
#ifdef _DEBUG
LPLog->AddLog(0, "Text section can not detected yet! result: %p", dwTextSectionChecksumFirst);
#endif
goto re_search;
}
else if (dwTextSectionChecksumFirst == 0 && LPData->IsPackedProcess() == true) {
bTextSectionInitialized = false;
return;
}
bTextSectionInitialized = true;
#ifdef _DEBUG
LPLog->AddLog(0, "Text section check initialized... first value = %p", dwTextSectionChecksumFirst);
#endif
}
void CheckTextSectionHash()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Text section check started!");
#endif
KARMA_MACRO_1
if (bTextSectionInitialized == false) {
#ifdef _DEBUG
LPLog->AddLog(0, "Text section can not initialized yet!");
#endif
return;
}
DWORD dwTextSectionChecksumSecond = GetTextSectionHash();
#ifdef _DEBUG
LPLog->AddLog(0, "Text section Checking.. second value = %p", dwTextSectionChecksumSecond);
#endif
KARMA_MACRO_2
if (LPData->IsPackedProcess() == false && (!dwTextSectionChecksumFirst || !dwTextSectionChecksumSecond)){
#ifdef _DEBUG
LPLog->AddLog(0, "Text section Check failed! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwTextSectionChecksumFirst, dwTextSectionChecksumSecond);
#endif
LPFunctions->CloseProcess("Anticheat integrity check failed! Error code: 2", false, "");
}
if (dwTextSectionChecksumFirst != dwTextSectionChecksumSecond)
{
#ifdef _DEBUG
LPLog->ErrorLog(0, "Text section is corrupted! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwTextSectionChecksumFirst, dwTextSectionChecksumSecond);
#endif
CHAR __warn[] = { 'G', 'a', 'm', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 'c', 'o', 'd', 'e', ' ', 'i', 'n', 't', 'e', 'g', 'r', 'i', 't', 'y', ' ', 'f', 'a', 'i', 'l', 'e', 'd', '.', ' ', 'P', 'l', 'e', 'a', 's', 'e', ' ', 'd', 'i', 's', 'a', 'b', 'l', 'e', ' ', 'a', 'n', 't', 'i', 'v', 'i', 'r', 'u', 's', ' ', 'o', 'r', ' ', 'f', 'i', 'r', 'e', 'w', 'a', 'l', 'l', ' ', 's', 'o', 'f', 't', 'w', 'a', 'r', 'e', 's', 0x0 }; // Game process code integrity failed. Please disable antivirus or firewall softwares
LPFunctions->CloseProcess(__warn, false, "");
}
#ifdef _DEBUG
else {
LPLog->AddLog(0, "Text section Check is OK! Ispacked: %d First: %p Second: %p", LPData->IsPackedProcess() ? 1 : 0, dwTextSectionChecksumFirst, dwTextSectionChecksumSecond);
}
#endif
}
#pragma endregion TextSectionCheck
DWORD WINAPI CheckSectionsHash(LPVOID)
{
BetaFunctionTable->Sleep(3000);
//InitCheckDataSectionHash();
InitCheckTextSectionHash();
while (1) {
//CheckDataSectionHash();
CheckTextSectionHash();
LPThreads->IncreaseThreadTick(9);
BetaFunctionTable->Sleep(6000);
}
return 0;
}
HANDLE CScan::InitChecksumScan()
{
#ifdef _DEBUG
LPLog->AddLog(0, "Checksum control thread creation has been started!");
#endif
KARMA_MACRO_1
HANDLE hThread = LPThreads->_CreateThread((LPTHREAD_START_ROUTINE)CheckSectionsHash, 0, 9);
if (!hThread) {
CHAR __warn[] = { 'E', 'R', 'R', 'O', 'R', '!', ' ', 'T', 'h', 'r', 'e', 'a', 'd', ' ', 'c', 'r', 'e', 'a', 't', 'i', 'o', 'n', ' ', 'f', 'a', 'i', 'l', 'e', 'd', ',', ' ', 'T', 'h', 'r', 'e', 'a', 'd', ' ', 'C', 'o', 'd', 'e', ' ', '0', 'x', '9', '!', 0x0 }; /* ERROR! Thread creation failed, Thread Code 0x9! */
LPFunctions->CloseProcess(__warn, false, "");
}
KARMA_MACRO_2
#ifdef _DEBUG
LPLog->AddLog(0, "Checksum control thread creation completed!");
#endif
return hThread;
}