-
Notifications
You must be signed in to change notification settings - Fork 76
/
Utils.cpp
254 lines (211 loc) · 6.67 KB
/
Utils.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
#include "utils.h"
NTSTATUS getKernelModuleByName(const char* moduleName, PVOID* moduleStart, size_t* moduleSize)
{
if (!moduleStart)
return STATUS_INVALID_PARAMETER;
size_t size{};
ZwQuerySystemInformation(SystemModuleInformation, nullptr, size, reinterpret_cast<PULONG>(&size));
const auto listHeader = ExAllocatePool(NonPagedPool, size);
if (!listHeader)
return STATUS_MEMORY_NOT_ALLOCATED;
if (const auto status = ZwQuerySystemInformation(SystemModuleInformation, listHeader, size, reinterpret_cast<PULONG>(&size)))
return status;
auto currentModule = reinterpret_cast<PSYSTEM_MODULE_INFORMATION>(listHeader)->Module;
for (size_t i = 0; i < reinterpret_cast<PSYSTEM_MODULE_INFORMATION>(listHeader)->Count; ++i, ++currentModule)
{
const auto currentModuleName = reinterpret_cast<const char*>(currentModule->FullPathName + currentModule->OffsetToFileName);
if (!strcmp(moduleName, currentModuleName))
{
*moduleStart = currentModule->ImageBase;
if (moduleSize)
*moduleSize = currentModule->ImageSize;
return STATUS_SUCCESS;
}
}
return STATUS_NOT_FOUND;
}
#define SizeAlign(Size) ((Size + 0xFFF) & 0xFFFFFFFFFFFFF000)
#define IMAGE_FIRST_SECTION(NtHeader) (PIMAGE_SECTION_HEADER)(NtHeader + 1)
#define NT_HEADER(ModBase) (PIMAGE_NT_HEADERS)((ULONG64)(ModBase) + ((PIMAGE_DOS_HEADER)(ModBase))->e_lfanew)
BOOLEAN StrICmp(const char* Str, const char* InStr, BOOLEAN Two)
{
#define ToLower(Char) ((Char >= 'A' && Char <= 'Z') ? (Char + 32) : Char)
if (!Str || !InStr)
return FALSE;
WCHAR c1, c2; do {
c1 = *Str++; c2 = *InStr++;
c1 = ToLower(c1); c2 = ToLower(c2);
if (!c1 && (Two ? !c2 : 1))
return TRUE;
} while (c1 == c2);
return FALSE;
}
PVOID FindSection(PVOID ModBase, const char* Name, PULONG SectSize)
{
//get & enum sections
PIMAGE_NT_HEADERS NT_Header = NT_HEADER(ModBase);
PIMAGE_SECTION_HEADER Sect = IMAGE_FIRST_SECTION(NT_Header);
for (PIMAGE_SECTION_HEADER pSect = Sect; pSect < Sect + NT_Header->FileHeader.NumberOfSections; pSect++)
{
//copy section name
char SectName[9]; SectName[8] = 0;
*(ULONG64*)&SectName[0] = *(ULONG64*)&pSect->Name[0];
//check name
if (StrICmp(Name, SectName, true))
{
//save size
if (SectSize) {
ULONG SSize = SizeAlign(max(pSect->Misc.VirtualSize, pSect->SizeOfRawData));
*SectSize = SSize;
}
//ret full sect ptr
return (PVOID)((ULONG64)ModBase + pSect->VirtualAddress);
}
}
//no section
return nullptr;
}
bool readByte(PVOID addr, UCHAR* ret)
{
*ret = *(volatile char*)addr;
return true;
}
PUCHAR FindPatternSect(PVOID ModBase, const char* SectName, const char* Pattern)
{
//find pattern utils
#define InRange(x, a, b) (x >= a && x <= b)
#define GetBits(x) (InRange(x, '0', '9') ? (x - '0') : ((x - 'A') + 0xA))
#define GetByte(x) ((UCHAR)(GetBits(x[0]) << 4 | GetBits(x[1])))
//get sect range
ULONG SectSize;
PUCHAR ModuleStart = (PUCHAR)FindSection(ModBase, SectName, &SectSize);
PUCHAR ModuleEnd = ModuleStart + SectSize;
if (!ModuleStart) return nullptr;
//scan pattern main
PUCHAR FirstMatch = nullptr;
const char* CurPatt = Pattern;
if (*Pattern == '\0')
CurPatt++;
for (; ModuleStart < ModuleEnd; ++ModuleStart)
{
bool SkipByte = (*CurPatt == '\?');
//hp(ModuleStart);
UCHAR byte1;
if (!readByte(ModuleStart, &byte1)) {
auto addr2 = (u64)ModuleStart;
addr2 &= 0xFFFFFFFFFFFFF000;
addr2 += 0xFFF;
ModuleStart = (PUCHAR)addr2;
//sp("123");
goto Skip;
}
if (SkipByte || byte1 == GetByte(CurPatt)) {
if (!FirstMatch) FirstMatch = ModuleStart;
if (SkipByte)
CurPatt += 2;
else
CurPatt += 3;
if (CurPatt[-1] == 0) return FirstMatch;
}
else if (FirstMatch) {
ModuleStart = FirstMatch;
Skip:
FirstMatch = nullptr;
CurPatt = Pattern;
}
}
//failed
return nullptr;
}
PUCHAR FindPatternRange(PVOID Start, u32 size, const char* Pattern)
{
//find pattern utils
#define InRange(x, a, b) (x >= a && x <= b)
#define GetBits(x) (InRange(x, '0', '9') ? (x - '0') : ((x - 'A') + 0xA))
#define GetByte(x) ((UCHAR)(GetBits(x[0]) << 4 | GetBits(x[1])))
//get sect range
ULONG SectSize;
PUCHAR ModuleStart = (PUCHAR)Start;
PUCHAR ModuleEnd = ModuleStart + size;
//scan pattern main
PUCHAR FirstMatch = nullptr;
const char* CurPatt = Pattern;
if (*Pattern == '\0')
CurPatt++;
for (; ModuleStart < ModuleEnd; ++ModuleStart)
{
bool SkipByte = (*CurPatt == '\?');
//hp(ModuleStart);
UCHAR byte1;
if (!readByte(ModuleStart, &byte1)) {
auto addr2 = (u64)ModuleStart;
addr2 &= 0xFFFFFFFFFFFFF000;
addr2 += 0xFFF;
ModuleStart = (PUCHAR)addr2;
//sp("123");
goto Skip;
}
if (SkipByte || byte1 == GetByte(CurPatt)) {
if (!FirstMatch) FirstMatch = ModuleStart;
SkipByte ? CurPatt += 2 : CurPatt += 3;
if (CurPatt[-1] == 0) return FirstMatch;
}
else if (FirstMatch) {
ModuleStart = FirstMatch;
Skip:
FirstMatch = nullptr;
CurPatt = Pattern;
}
}
//failed
return nullptr;
}
NTSTATUS CopyPhysics(void* Dst, const void* PhySics, size_t _MaxCount)
{
MM_COPY_ADDRESS copyaddr;
copyaddr.PhysicalAddress.QuadPart = (LONGLONG)PhySics;
SIZE_T copyed = 0;
return MmCopyMemory(Dst, copyaddr, _MaxCount, MM_COPY_MEMORY_PHYSICAL, ©ed);
}
typedef struct _SYSTEM_KERNEL_DEBUGGER_INFORMATION
{
BOOLEAN DebuggerEnabled;
BOOLEAN DebuggerNotPresent;
} SYSTEM_KERNEL_DEBUGGER_INFORMATION, * PSYSTEM_KERNEL_DEBUGGER_INFORMATION;
BOOLEAN IsKernelDebuggerPresent()
{
SYSTEM_KERNEL_DEBUGGER_INFORMATION DebuggerInfo;
ULONG RetLen = 0;
ZwQuerySystemInformation(SystemKernelDebuggerInformation, &DebuggerInfo, 8, &RetLen);
return !DebuggerInfo.DebuggerNotPresent;
}
PVOID GetProcAddress(PVOID ModBase, const char* Name)
{
if (!ModBase) return 0;
//parse headers
PIMAGE_NT_HEADERS NT_Head = NT_HEADER(ModBase);
PIMAGE_EXPORT_DIRECTORY ExportDir = (PIMAGE_EXPORT_DIRECTORY)((ULONG64)ModBase + NT_Head->OptionalHeader.DataDirectory[0].VirtualAddress);
//process records
for (ULONG i = 0; i < ExportDir->NumberOfNames; i++)
{
//get ordinal & name
USHORT Ordinal = ((USHORT*)((ULONG64)ModBase + ExportDir->AddressOfNameOrdinals))[i];
const char* ExpName = (const char*)ModBase + ((ULONG*)((ULONG64)ModBase + ExportDir->AddressOfNames))[i];
//check export name
if (StrICmp(Name, ExpName, true))
return (PVOID)((ULONG64)ModBase + ((ULONG*)((ULONG64)ModBase + ExportDir->AddressOfFunctions))[Ordinal]);
}
//no export
return nullptr;
}
//MmMapIoSpace, not allow write to page table
NTSTATUS WritePhysicalSafe2(DWORD64 PhysicalAddress, pv Buffer, u64 Length)
{
PHYSICAL_ADDRESS phy; phy.QuadPart = PhysicalAddress;
PVOID MapedVirt = MmMapIoSpace(phy, Length, MmNonCached);
if (!MapedVirt)
return STATUS_UNSUCCESSFUL;
memcpy(MapedVirt, Buffer, Length);
MmUnmapIoSpace(MapedVirt, Length);
return STATUS_SUCCESS;
}