This repository has been archived by the owner on Apr 14, 2019. It is now read-only.
/
ISectionChecksumScanner.cpp
284 lines (230 loc) · 8.05 KB
/
ISectionChecksumScanner.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
#include "NM_Index.h"
#include "NM_Main.h"
#include "NM_App.h"
#include "ScannerInterface.h"
#include "Defines.h"
#include "PEHelper.h"
#include "ProcessFunctions.h"
#include "ScannerInterface.h"
inline bool GetSectionHash(DWORD dwModuleBase, const std::string & szSectionName, LPDWORD pdwSectionHash)
{
DEBUG_LOG(LL_SYS, "Section hash calculate function has been started! Target module base: %p Target session: %s", dwModuleBase, szSectionName.c_str());
auto dwSectionBase = 0UL, dwSectionSize = 0UL;
bool bFindTextSection = CPEFunctions::GetSectionInformation(szSectionName, dwModuleBase, &dwSectionBase, &dwSectionSize);
if (!bFindTextSection)
{
DEBUG_LOG(LL_ERR, "Section: %s can not found for: %p", szSectionName.c_str(), dwModuleBase);
return false;
}
auto szModuleOwner = g_nmApp->FunctionsInstance()->GetModuleOwnerName(reinterpret_cast<LPVOID>(dwModuleBase));
if (szModuleOwner.empty())
{
DEBUG_LOG(LL_ERR, "Module: %p informations not found! Last error: %u", dwModuleBase, g_winapiApiTable->GetLastError());
return false;
}
if (!dwSectionBase || !dwSectionSize)
{
DEBUG_LOG(LL_ERR, "Section base address or size is null! Target Module base: %p Section: %s Module: %s IsPacked result: %d",
dwModuleBase, szSectionName.c_str(), szModuleOwner.c_str(), g_nmApp->DirFunctionsInstance()->IsPackedExecutable(szModuleOwner));
return false;
}
if (IsBadReadPtr((LPCVOID)dwSectionBase, dwSectionSize))
{
DEBUG_LOG(LL_ERR, "Section is not at readable area! Base: %p Size: %p", dwSectionBase, dwSectionSize);
return false;
}
MEMORY_BASIC_INFORMATION mbi;
if (!g_winapiApiTable->VirtualQuery((LPCVOID)dwSectionBase, &mbi, sizeof(mbi)))
{
DEBUG_LOG(LL_ERR, "VirtualQuery fail! Last error: %u", g_winapiApiTable->GetLastError());
return false;
}
if (!(mbi.State & MEM_COMMIT) || (mbi.State & MEM_RELEASE) || (mbi.Protect == PAGE_NOACCESS) || (mbi.Protect & PAGE_GUARD))
{
DEBUG_LOG(LL_ERR, "Section is not a accesible memory!");
return false;
}
auto dwOldProtect = 0UL;
if (!g_winapiApiTable->VirtualProtect((LPVOID)dwSectionBase, dwSectionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect))
{
DEBUG_LOG(LL_ERR, "VirtualProtect(pre) fail! Last error: %u", g_winapiApiTable->GetLastError());
return false;
}
auto dwChecksum = CPEFunctions::CalculateChecksum(dwSectionBase, dwSectionSize);
if (!dwChecksum)
{
DEBUG_LOG(LL_ERR, "Section checksum can not calculated! Last error: %u", g_winapiApiTable->GetLastError());
return false;
}
if (!g_winapiApiTable->VirtualProtect((LPVOID)dwSectionBase, dwSectionSize, dwOldProtect, &dwOldProtect))
{
DEBUG_LOG(LL_ERR, "VirtualProtect(post) fail! Last error: %u", g_winapiApiTable->GetLastError());
return false;
}
DEBUG_LOG(LL_SYS, "Section: %s checksum calculated: %p - %p -> (%p)", szSectionName.c_str(), dwSectionBase, dwSectionSize, dwChecksum);
if (pdwSectionHash) *pdwSectionHash = dwChecksum;
return true;
}
bool IScanner::InitSectionHash(DWORD dwBase)
{
DEBUG_LOG(LL_SYS, "Section checksum create init started! Target: %p", dwBase);
auto bCompleted = false;
auto iAttemptCount = 0;
auto dwTextHash = 0UL;
auto dwRDataHash = 0UL;
do
{
// TODO: PE Header, .edata, .rsrc, .reloc
if (!GetSectionHash(dwBase, xorstr(".text").crypt_get(), &dwTextHash))
{
DEBUG_LOG(LL_ERR, "GetSectionHash(.text) fail!");
break;
}
if (!GetSectionHash(dwBase, xorstr(".rdata").crypt_get(), &dwRDataHash))
{
DEBUG_LOG(LL_ERR, "GetSectionHash(.rdata) fail!");
break;
}
if (dwTextHash && dwRDataHash)
{
DEBUG_LOG(LL_SYS, "Section checksum calculate completed! result: %p|%p", dwTextHash, dwRDataHash);
bCompleted = true;
break;
}
DEBUG_LOG(LL_ERR, "Section checksum can not calculated! result: %p|%p", dwTextHash, dwRDataHash);
auto szModuleOwner = g_nmApp->FunctionsInstance()->GetModuleOwnerName(reinterpret_cast<LPVOID>(dwBase));
if (g_nmApp->DirFunctionsInstance()->IsPackedExecutable(szModuleOwner))
{
DEBUG_LOG(LL_ERR, "Section checksum fail! Executable: %s is probably packed!", szModuleOwner.c_str());
return false;
}
else
{
Sleep(2000);
iAttemptCount++;
}
} while (iAttemptCount < 3);
auto pSectionChecksumInfos = std::make_shared<SSectionChecksumInfos>();
if (IS_VALID_SMART_PTR(pSectionChecksumInfos))
{
pSectionChecksumInfos->dwBase = dwBase;
pSectionChecksumInfos->dwTextHash = dwTextHash;
pSectionChecksumInfos->dwRDataHash = dwRDataHash;
pSectionChecksumInfos->bStatus = bCompleted;
m_mSectionChecksumInfoMap[dwBase] = pSectionChecksumInfos;
}
return bCompleted;
}
bool IScanner::InitSectionHashes()
{
DEBUG_LOG(LL_SYS, "Section checksum check init started!");
// Clear old hash list
m_mSectionChecksumInfoMap.clear();
/*
auto pEnumerator = std::make_unique<CEnumerator>(g_winapiApiTable->GetCurrentProcessId());
if (!pEnumerator || !pEnumerator.get())
return;
auto vModuleList = pEnumerator->EnumerateModules();
if (vModuleList.empty())
return;
for (const auto & hModule : vModuleList)
{
if (!InitSectionHash((DWORD)hModule, true))
{
DEBUG_LOG(LL_ERR, "Section hash checker can not created! Target module base: %p Name: %s", hModule, GetModuleOwnerName(hModule).c_str());
// close process
}
}
*/
InitSectionHash((DWORD)g_winapiModuleTable->hBaseModule);
return true;
}
bool IScanner::CheckSectionHashes()
{
DEBUG_LOG(LL_SYS, "Section checksum check started!");
/*
auto vModuleList=createmodulelist();
for (const auto & hModule : vModuleList)
{
DEBUG_LOG(LL_SYS, "Module: %p section checksum check started!", hModule);
// get old informations
auto pOldSectionInformations = m_sectionChecksumInfoMap[hModule];
if (!pOldSectionInformations)
{
}
// clear old informations
m_sectionChecksumInfoMap.erase(pOldSectionInformations);
// create current hashes
auto bHashResult = InitSectionHash(hModule);
if (!bHashResult)
{
}
// get new informations
auto pCurrentSectionInformations = m_sectionChecksumInfoMap[hModule];
if (!pCurrentSectionInformations)
{
}
// check
if (pOldSectionInformations != pCurrentSectionInformations)
{
}
}
*/
return true;
}
DWORD WINAPI CheckSectionsHashRoutine(LPVOID)
{
DEBUG_LOG(LL_SYS, "Section hash check thread event has been started");
auto pSectionCheckThreadTimer = CTimer<std::chrono::milliseconds>();
auto currentThread = std::shared_ptr<CThread>();
do
{
if (pSectionCheckThreadTimer.diff() > 5000)
g_nmApp->OnCloseRequest(EXIT_ERR_SECTION_HASH_THREAD_TIMEOUT, g_winapiApiTable->GetLastError());
g_winapiApiTable->Sleep(10);
currentThread = g_nmApp->ThreadManagerInstance()->GetThreadFromThreadCode(SELF_THREAD_SECTIONCHECKSUM);
} while (!IS_VALID_SMART_PTR(currentThread));
// ---
g_nmApp->ScannerInstance()->InitSectionHashes();
while (1)
{
g_nmApp->ScannerInstance()->CheckSectionHashes();
// Update ticks
g_nmApp->SelfThreadMgrInstance()->IncreaseThreadTick(SELF_THREAD_SECTIONCHECKSUM);
g_nmApp->SelfThreadMgrInstance()->SetLastCheckTime(SELF_THREAD_SECTIONCHECKSUM, GetCurrentTime());
g_winapiApiTable->Sleep(6000);
}
g_nmApp->ThreadManagerInstance()->DestroyThread(currentThread);
return 0;
}
#ifndef _M_X64
__declspec(naked) void DummyFunc8()
{
__asm
{
_emit 0xAA
_emit 0xAA
_emit 0xAA
_emit 0xAA
}
}
#endif
bool IScanner::InitChecksumScan()
{
DEBUG_LOG(LL_SYS, "Thread creation has been started!");
auto ulFuncSize = g_nmApp->ThreadManagerInstance()->GetThreadFuncSize((LPBYTE)CheckSectionsHashRoutine);
if (!ulFuncSize || ulFuncSize > 1024)
{
DEBUG_LOG(LL_ERR, "Not valid thread func size: %u", ulFuncSize);
return false;
}
auto thread = g_nmApp->ThreadManagerInstance()->CreateCustomThread(SELF_THREAD_SECTIONCHECKSUM, CheckSectionsHashRoutine, ulFuncSize, nullptr, 6000 + 1000, false);
if (!IS_VALID_SMART_PTR(thread) || thread->IsCorrect() == false)
{
DEBUG_LOG(LL_ERR, "Thread can NOT created! Error: %u", g_winapiApiTable->GetLastError());
return false;
}
DEBUG_LOG(LL_SYS, "Info - %u[%p->%p][%d-%s] - Completed! Shared:%p Custom:%p",
thread->GetId(), thread->GetHandle(), thread->GetStartAddress(), thread->GetCustomCode(), thread->GetThreadCustomName().c_str(), thread, thread.get());
return true;
}