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
|
//|_ _ _. _ ._ |_ _. _ |
//| | (/_ (_| \/ (/_ | | | | (_| (_ |<
#pragma once
#include <Windows.h>
#include <TlHelp32.h>
#include "ntutil.h"
#include "winintern.h"
#include "typedef.h"
#include "fnv.h"
#include "conout.h"
struct MODULE_ENTRY {
U64 base;
U64 size;
STR<64> name;
FNV1A hash;
};
class PROCESS32 {
private:
HANDLE m_base{};
U64 m_id{};
char m_name[256]{};
private:
public:
PROCESS32( const char* name ) {
memset( m_name, 0, 256 );
memcpy( m_name, name, strlen( name ) );
};
HANDLE get_base() { return m_base; }
I8 open() {
m_id = 0;
const U32 PINFO_ALLOC_SIZE = 0x400000;
_SYSTEM_PROCESS_INFORMATION64* pinfo;
ULONG received_bytes;
pinfo = (_SYSTEM_PROCESS_INFORMATION64*)VirtualAlloc(
0,
PINFO_ALLOC_SIZE,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE
);
NTSTATUS64 status = nt_query_system_information64(
SystemProcessInformation,
pinfo,
PINFO_ALLOC_SIZE,
&received_bytes
);
if( status != STATUS_SUCCESS )
return 0;
wchar_t name_buffer[128];
do {
if( pinfo->ImageName.Buffer ) {
nt_read_vm64( (HANDLE)-1, pinfo->ImageName.Buffer, name_buffer, 256 );
STR<128> pname = u_widebyte_to_ansi( name_buffer );
if( !strcmp( pname, m_name ) ) {
m_id = pinfo->UniqueProcessId;
break;
}
}
pinfo = (decltype( pinfo ))( (U32)pinfo + pinfo->NextEntryOffset );
} while( !!pinfo->NextEntryOffset );
VirtualFree( pinfo, PINFO_ALLOC_SIZE, MEM_FREE );
_OBJECT_ATTRIBUTES64 obj_attributes{};
_CLIENT_ID_T<U64> cid;
cid.UniqueProcess = (U64)( UlongToHandle( m_id ) );
cid.UniqueThread = 0;
obj_attributes.Length = sizeof( obj_attributes );
status = nt_open_process64(
&m_base,
PROCESS_ALL_ACCESS,
&obj_attributes,
&cid
);
return status == STATUS_SUCCESS;
}
U8 valid() {
PROCESS_BASIC_INFORMATION64 info;
// 4 = ObjectHandleFlagInformation
NTSTATUS64 status = nt_query_information_process64(
m_base, ProcessBasicInformation,
&info,
sizeof(info),
0
);
if( status != STATUS_SUCCESS )
return 0;
return info.ExitStatus != 0;
}
U32 get_module_size32( U64 module_base ) {
IMAGE_NT_HEADERS nt_headers;
IMAGE_DOS_HEADER dos_header;
read( module_base, &dos_header, sizeof( dos_header ) );
read( module_base + dos_header.e_lfanew, &nt_headers, sizeof( nt_headers ) );
return nt_headers.OptionalHeader.SizeOfImage;
}
U64 get_module_size64( U64 module_base ) {
IMAGE_NT_HEADERS64 nt_headers;
IMAGE_DOS_HEADER dos_header;
read( module_base, &dos_header, sizeof( dos_header ) );
read( module_base + dos_header.e_lfanew, &nt_headers, sizeof( nt_headers ) );
return nt_headers.OptionalHeader.SizeOfImage;
}
std::vector< MODULE_ENTRY > dump_modules64() {
std::vector< MODULE_ENTRY > ret;
PROCESS_BASIC_INFORMATION64 pbi;
ULONG pbi_len;
PEB64 peb;
NTSTATUS64 status;
status = nt_query_information_process64(
m_base,
ProcessBasicInformation,
&pbi,
sizeof( PROCESS_BASIC_INFORMATION64 ),
&pbi_len
);
read( pbi.PebBaseAddress, &peb, sizeof( PEB64 ) );
PEB_LDR_DATA64 ldr;
read( peb.Ldr, &ldr, sizeof( ldr ) );
U64 root = ldr.InMemoryOrderModuleList.Flink;
for( U64 entry = read<U64>( root ); entry != root && !!entry; entry = read<U64>( entry ) ) {
LDR_DATA_TABLE_ENTRY64 ldr_entry;
read( entry, &ldr_entry, sizeof( ldr_entry ) );
if( !ldr_entry.FullDllName.Buffer )
continue;
wchar_t module_buffer[256]{};
read(
ldr_entry.FullDllName.Buffer,
module_buffer, 256 * sizeof( wchar_t )
);
STR<256> module_name = u_widebyte_to_ansi<256>( module_buffer );
FNV1A module_hash = fnv1a( module_name );
U64 module_base = ldr_entry.DllBase;
U64 module_size = ldr_entry.SizeOfImage;
ret.push_back( {
module_base,
module_size,
module_name.data,
module_hash
} );
}
return ret;
}
U64 get_module64( FNV1A name, U32* out_size = 0 ) {
std::vector< MODULE_ENTRY > modules = dump_modules64();
for( auto& it : modules ) {
if( it.hash == name ) {
if( out_size )
*out_size = (U32)it.size;
return it.base;
}
}
return 0;
}
std::vector< MODULE_ENTRY > dump_modules32() {
std::vector< MODULE_ENTRY > ret;
U64 peb32_addr;
NTSTATUS64 status;
if( !m_id )
return ret;
ULONG out_ret = 0;
status = nt_query_information_process64(
m_base,
ProcessWow64Information,
&peb32_addr,
sizeof( U64 ),
&out_ret
);
if( status != STATUS_SUCCESS )
return ret;
PEB* peb = (PEB*)VirtualAlloc(
0,
sizeof( PEB ),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE
);
read( peb32_addr, peb, sizeof( PEB ) );
PEB_LDR_DATA ldr;
read( (U32)peb->Ldr, &ldr, sizeof( ldr ) );
VirtualFree( peb, sizeof( PEB64 ), MEM_FREE );
U64 root = (U32)ldr.InMemoryOrderModuleList.Flink;
for( U32 entry = read<U32>( root ); entry != root; entry = read<U32>( entry ) ) {
LDR_DATA_TABLE_ENTRY data_table{};
read( entry, &data_table, sizeof( data_table ) );
if( !data_table.FullDllName.Buffer )
continue;
wchar_t module_buffer[256]{};
read(
(U64)data_table.FullDllName.Buffer,
module_buffer, 256 * sizeof( wchar_t )
);
STR<256> module_name = u_widebyte_to_ansi<256>( module_buffer );
FNV1A module_hash = fnv1a( module_name );
U64 module_base = (U32)data_table.Reserved2[0];
U64 module_size = *(U32*)((U32)&data_table + 0x20);
ret.push_back( {
module_base,
module_size,
module_name.data,
module_hash
} );
}
return ret;
}
U32 get_module32( FNV1A name, U32* out_size = 0 ) {
std::vector< MODULE_ENTRY > modules = dump_modules32();
for( auto& it : modules ) {
if( it.hash == name ) {
if( out_size )
*out_size = (U32)it.size;
return (U32)it.base;
}
}
return 0;
}
U32 code_match( U32 module_base, const char* sig, U32 start = 0 ) {
U32 sig_length;
U8* sig_bytes = u_parse_signature( sig, &sig_length );
if( !sig_bytes || sig_length <= 2 )
return 0;
U32 ret = code_match( module_base, sig_bytes, sig_length, start );
free( sig_bytes );
return ret;
}
U32 code_match( U32 module_base, U8* bytes, U32 length, U32 start = 0 ) {
MEMORY_BASIC_INFORMATION64 mbi{0};
U32 module_size = get_module_size32( module_base );
if( start < module_base )
start = module_base;
U8* module_copy = (U8*)malloc( module_size );
read( module_base, module_copy, module_size );
bool first = true;
for( U64 off = start - module_base; off < module_size; off += mbi.RegionSize ) {
nt_query_vm64( m_base, module_base + off, MemoryRegionInfo, &mbi, sizeof( mbi ) );
if( mbi.State == MEM_FREE )
continue;
U32 mbi_address = (U32)mbi.BaseAddress - module_base;
U32 region_start = first? start - (U32)mbi.BaseAddress : 0;
for( U32 i = region_start; i < mbi.RegionSize - length; ++i ) {
if( u_binary_match( module_copy + mbi_address + i, bytes, length ) ) {
free( module_copy );
return (U32)mbi.BaseAddress + i;
}
first = false;
}
}
free( module_copy );
return 0;
}
U64 get_id() { return m_id; }
template < typename t > void write( U64 address, const t& value ) {
nt_write_vm64( m_base, address, (void*)&value, sizeof( t ) );
}
void write( U64 address, const void* buffer, U32 size ) {
nt_write_vm64( m_base, address, (void*)buffer, size );
}
template < typename t > t read( U64 address ) {
t buffer{};
read( address, &buffer, sizeof( t ) );
return buffer;
}
void read( U64 address, void* out, U32 size ) {
nt_read_vm64( m_base, address, out, size );
}
bool protect( U64 address, U32 size, ULONG protect ) {
}
U64 allocate(
U64 size,
ULONG protect = PAGE_EXECUTE_READWRITE,
ULONG alloc_type = MEM_COMMIT | MEM_RESERVE
) {
U64 out{};
NTSTATUS64 st = nt_allocate_vm64( m_base, &out, 0, &size, alloc_type, protect );
if( st != STATUS_SUCCESS ) {
return 0;
}
return out;
}
};
|