-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
memory.go
438 lines (380 loc) · 9.56 KB
/
memory.go
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package ntdll
//go:generate -command mkcode go run mkcode.go --
//go:generate mkcode $GOFILE
/*
func:
NTSTATUS NtAllocateVirtualMemory(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_In_ ULONG_PTR ZeroBits,
_Inout_ PSIZE_T RegionSize,
_In_ ULONG AllocationType,
_In_ ULONG Protect
);
*/
/*
func:
NTSTATUS NtFreeVirtualMemory(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_Inout_ PSIZE_T RegionSize,
_In_ ULONG FreeType
);
*/
/*
func:
NTSTATUS NtReadVirtualMemory(
_In_ HANDLE ProcessHandle,
_In_opt_ PVOID BaseAddress,
_Out_ PVOID Buffer,
_In_ SIZE_T BufferSize,
_Out_opt_ PSIZE_T NumberOfBytesRead
);
*/
/*
func:
NTSTATUS NtWriteVirtualMemory(
_In_ HANDLE ProcessHandle,
_In_opt_ PVOID BaseAddress,
_In_ PVOID Buffer,
_In_ SIZE_T BufferSize,
_Out_opt_ PSIZE_T NumberOfBytesWritten
);
*/
/*
func:
NTSTATUS NtProtectVirtualMemory(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_Inout_ PSIZE_T RegionSize,
_In_ ULONG NewProtect,
_Out_ PULONG OldProtect
);
*/
/*
func:
NTSTATUS NtQueryVirtualMemory(
_In_ HANDLE ProcessHandle,
_In_opt_ PVOID BaseAddress,
_In_ MEMORY_INFORMATION_CLASS MemoryInformationClass,
_Out_ PVOID MemoryInformation,
_In_ SIZE_T MemoryInformationLength,
_Out_opt_ PSIZE_T ReturnLength
);
*/
/*
func:
NTSTATUS NtFlushVirtualMemory(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_Inout_ PSIZE_T RegionSize,
_Out_ PIO_STATUS_BLOCK IoStatus
);
*/
/*
enum:
typedef enum _MEMORY_INFORMATION_CLASS
{
MemoryBasicInformation,
MemoryWorkingSetInformation,
MemoryMappedFilenameInformation,
MemoryRegionInformation,
MemoryWorkingSetExInformation,
MemorySharedCommitInformation,
MemoryImageInformation,
MemoryRegionInformationEx,
MemoryPrivilegedBasicInformation,
MemoryEnclaveImageInformation,
MemoryBasicInformationCapped,
MemoryPhysicalContiguityInformation,
} MEMORY_INFORMATION_CLASS;
*/
/*
type:
typedef struct _MEMORY_BASIC_INFORMATION {
PVOID BaseAddress;
PVOID AllocationBase;
ULONG AllocationProtect;
USHORT PartitionId;
SIZE_T RegionSize;
ULONG State;
ULONG Protect;
ULONG Type;
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
*/
/*
type:
typedef struct _MEMORY_BASIC_INFORMATION32 {
DWORD BaseAddress;
DWORD AllocationBase;
DWORD AllocationProtect;
DWORD RegionSize;
DWORD State;
DWORD Protect;
DWORD Type;
} MEMORY_BASIC_INFORMATION32, *PMEMORY_BASIC_INFORMATION32;
*/
/*
type:
typedef struct _MEMORY_BASIC_INFORMATION64 {
ULONGLONG BaseAddress;
ULONGLONG AllocationBase;
DWORD AllocationProtect;
DWORD __alignment1;
ULONGLONG RegionSize;
DWORD State;
DWORD Protect;
DWORD Type;
DWORD __alignment2;
} MEMORY_BASIC_INFORMATION64, *PMEMORY_BASIC_INFORMATION64;
*/
/*
type:
typedef struct _MEMORY_WORKING_SET_INFORMATION
{
ULONG_PTR NumberOfEntries;
MEMORY_WORKING_SET_BLOCK WorkingSetInfo[1];
} MEMORY_WORKING_SET_INFORMATION, *PMEMORY_WORKING_SET_INFORMATION;
*/
/*
NOTE: type contains unions -- define below.
typedef struct _MEMORY_WORKING_SET_BLOCK
{
ULONG_PTR Protection : 5;
ULONG_PTR ShareCount : 3;
ULONG_PTR Shared : 1;
ULONG_PTR Node : 3;
#ifdef _WIN64
ULONG_PTR VirtualPage : 52;
#else
ULONG VirtualPage : 20;
#endif
} MEMORY_WORKING_SET_BLOCK, *PMEMORY_WORKING_SET_BLOCK;
*/
type MemoryWorkingSetBlock uintptr
func (b MemoryWorkingSetBlock) Protection() uintptr {
return uintptr(b) & 31
}
func (b MemoryWorkingSetBlock) ShareCount() uintptr {
return (uintptr(b) >> 5) & 7
}
func (b MemoryWorkingSetBlock) Shared() bool {
return (b>>8)&1 != 0
}
func (b MemoryWorkingSetBlock) Node() uintptr {
return (uintptr(b) >> 9) & 7
}
func (b MemoryWorkingSetBlock) VirtualPage() uintptr {
return uintptr(b) >> 12
}
/*
NOTE: type contains unions -- define below.
typedef struct _MEMORY_REGION_INFORMATION
{
PVOID AllocationBase;
ULONG AllocationProtect;
union
{
ULONG RegionType;
struct
{
ULONG Private : 1;
ULONG MappedDataFile : 1;
ULONG MappedImage : 1;
ULONG MappedPageFile : 1;
ULONG MappedPhysical : 1;
ULONG DirectMapped : 1;
ULONG SoftwareEnclave : 1; // REDSTONE3
ULONG PageSize64K : 1;
ULONG PlaceholderReservation : 1; // REDSTONE4
ULONG MappedAwe : 1; // 21H1
ULONG MappedWriteWatch : 1;
ULONG PageSizeLarge : 1;
ULONG PageSizeHuge : 1;
ULONG Reserved : 19;
};
};
SIZE_T RegionSize;
SIZE_T CommitSize;
ULONG_PTR PartitionId; // 19H1
ULONG_PTR NodePreference; // 20H1
} MEMORY_REGION_INFORMATION, *PMEMORY_REGION_INFORMATION;
*/
/*
type:
typedef struct _MEMORY_REGION_INFORMATION
{
PVOID AllocationBase;
ULONG AllocationProtect;
ULONG RegionType;
SIZE_T RegionSize;
SIZE_T CommitSize;
ULONG_PTR PartitionId;
ULONG_PTR NodePreference;
} MEMORY_REGION_INFORMATION, *PMEMORY_REGION_INFORMATION;
*/
func (i *MemoryRegionInformationT) Private() bool {
return i.RegionType&1 != 0
}
func (i *MemoryRegionInformationT) MappedDataFile() bool {
return (i.RegionType>>1)&1 != 0
}
func (i *MemoryRegionInformationT) MappedImage() bool {
return (i.RegionType>>2)&1 != 0
}
func (i *MemoryRegionInformationT) MappedPageFile() bool {
return (i.RegionType>>3)&1 != 0
}
func (i *MemoryRegionInformationT) MappedPhysical() bool {
return (i.RegionType>>4)&1 != 0
}
func (i *MemoryRegionInformationT) DirectMapped() bool {
return (i.RegionType>>5)&1 != 0
}
func (i *MemoryRegionInformationT) SoftwareEnclave() bool {
return (i.RegionType>>6)&1 != 0
}
func (i *MemoryRegionInformationT) PageSize64K() bool {
return (i.RegionType>>7)&1 != 0
}
func (i *MemoryRegionInformationT) PlaceholderReservation() bool {
return (i.RegionType>>8)&1 != 0
}
func (i *MemoryRegionInformationT) MappedAwe() bool {
return (i.RegionType>>9)&1 != 0
}
func (i *MemoryRegionInformationT) MappedWriteWatch() bool {
return (i.RegionType>>10)&1 != 0
}
func (i *MemoryRegionInformationT) PageSizeLarge() bool {
return (i.RegionType>>11)&1 != 0
}
func (i *MemoryRegionInformationT) PageSizeHuge() bool {
return (i.RegionType>>12)&1 != 0
}
/*
type:
typedef struct _MEMORY_WORKING_SET_EX_INFORMATION
{
PVOID VirtualAddress;
MEMORY_WORKING_SET_EX_BLOCK VirtualAttributes;
} MEMORY_WORKING_SET_EX_INFORMATION, *PMEMORY_WORKING_SET_EX_INFORMATION;
*/
/*
NOTE: type contains unions -- define below.
typedef struct _MEMORY_WORKING_SET_EX_BLOCK
{
union
{
struct
{
ULONG_PTR Valid : 1;
ULONG_PTR ShareCount : 3;
ULONG_PTR Win32Protection : 11;
ULONG_PTR Shared : 1;
ULONG_PTR Node : 6;
ULONG_PTR Locked : 1;
ULONG_PTR LargePage : 1;
ULONG_PTR Priority : 3;
ULONG_PTR Reserved : 3;
ULONG_PTR SharedOriginal : 1;
ULONG_PTR Bad : 1;
#ifdef _WIN64
ULONG_PTR ReservedUlong : 32;
#endif
};
struct
{
ULONG_PTR Valid : 1;
ULONG_PTR Reserved0 : 14;
ULONG_PTR Shared : 1;
ULONG_PTR Reserved1 : 5;
ULONG_PTR PageTable : 1;
ULONG_PTR Location : 2;
ULONG_PTR Priority : 3;
ULONG_PTR ModifiedList : 1;
ULONG_PTR Reserved2 : 2;
ULONG_PTR SharedOriginal : 1;
ULONG_PTR Bad : 1;
#ifdef _WIN64
ULONG_PTR ReservedUlong : 32;
#endif
} Invalid;
};
} MEMORY_WORKING_SET_EX_BLOCK, *PMEMORY_WORKING_SET_EX_BLOCK;
*/
type MemoryWorkingSetExBlock uintptr
func (b MemoryWorkingSetExBlock) Valid() bool {
return b&1 != 0
}
func (b MemoryWorkingSetExBlock) ShareCount() uintptr {
return (uintptr(b) >> 1) & ((1 << 3) - 1)
}
func (b MemoryWorkingSetExBlock) Win32Protection() uintptr {
return (uintptr(b) >> 4) & ((1 << 11) - 1)
}
func (b MemoryWorkingSetExBlock) Shared() bool {
return b&(1<<15) != 0
}
func (b MemoryWorkingSetExBlock) Node() uintptr {
return (uintptr(b) >> 16) & ((1 << 6) - 1)
}
func (b MemoryWorkingSetExBlock) Locked() bool {
return b&(1<<15) != 0
}
func (b MemoryWorkingSetExBlock) LargePage() bool {
return b&(1<<16) != 0
}
func (b MemoryWorkingSetExBlock) Priority() uintptr {
return (uintptr(b) >> 24) & ((1 << 3) - 1)
}
func (b MemoryWorkingSetExBlock) SharedOriginal() bool {
return b&(1<<30) != 0
}
func (b MemoryWorkingSetExBlock) Bad() bool {
return b&(1<<31) != 0
}
/*
type:
typedef struct _MEMORY_SHARED_COMMIT_INFORMATION
{
SIZE_T CommitSize;
} MEMORY_SHARED_COMMIT_INFORMATION, *PMEMORY_SHARED_COMMIT_INFORMATION;
*/
/*
NOTE: type contains unions -- define below.
typedef struct _MEMORY_IMAGE_INFORMATION
{
PVOID ImageBase;
SIZE_T SizeOfImage;
union
{
ULONG ImageFlags;
struct
{
ULONG ImagePartialMap : 1;
ULONG ImageNotExecutable : 1;
ULONG ImageSigningLevel : 4; // REDSTONE3
ULONG Reserved : 26;
};
};
} MEMORY_IMAGE_INFORMATION, *PMEMORY_IMAGE_INFORMATION;
*/
/*
type:
typedef struct _MEMORY_IMAGE_INFORMATION
{
PVOID ImageBase;
SIZE_T SizeOfImage;
ULONG ImageFlags;
} MEMORY_IMAGE_INFORMATION, *PMEMORY_IMAGE_INFORMATION;
*/
func (i MemoryImageInformationT) PartialMap() bool {
return i.ImageFlags&1 != 0
}
func (i MemoryImageInformationT) NotExecutable() bool {
return (i.ImageFlags>>1)&1 != 0
}
func (i MemoryImageInformationT) SigningLevel() uintptr {
return (uintptr(i.ImageFlags) >> 2) & 15
}