-
Notifications
You must be signed in to change notification settings - Fork 31
/
NtUtils.Sections.pas
412 lines (339 loc) · 10.7 KB
/
NtUtils.Sections.pas
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
unit NtUtils.Sections;
{
The module provides a set of operations with sections including support for
mapping files and known DLLs.
}
interface
uses
Ntapi.WinNt, Ntapi.ntmmapi, Ntapi.ntseapi, Ntapi.ntioapi, Ntapi.Versions,
NtUtils, NtUtils.Objects, NtUtils.Files, DelphiUtils.AutoObjects;
const
// Forward commonly used constants
PAGE_READONLY = Ntapi.ntmmapi.PAGE_READONLY;
PAGE_READWRITE = Ntapi.ntmmapi.PAGE_READWRITE;
SEC_COMMIT = Ntapi.ntmmapi.SEC_COMMIT;
SEC_IMAGE = Ntapi.ntmmapi.SEC_IMAGE;
// Get SEC_IMAGE_NO_EXECUTE when supported or SEC_IMAGE otherwise
function RtlxSecImageNoExecute: TAllocationAttributes;
// Create a section object backed by a paging or a regular file
function NtxCreateSection(
out hxSection: IHandle;
[opt] const MaximumSize: UInt64;
PageProtection: TMemoryProtection = PAGE_READWRITE;
AllocationAttributes: TAllocationAttributes = SEC_COMMIT;
[opt] const ObjectAttributes: IObjectAttributes = nil;
[opt, Access(FILE_MAP_SECTION)] hFile: THandle = 0
): TNtxStatus;
// Create a section from a file
function NtxCreateFileSection(
out hxSection: IHandle;
[Access(FILE_MAP_SECTION)] hFile: THandle;
PageProtection: TMemoryProtection;
AllocationAttributes: TAllocationAttributes;
[opt] const ObjectAttributes: IObjectAttributes = nil;
[opt] const MaximumSize: UInt64 = 0
): TNtxStatus;
// Open a section object by name
function NtxOpenSection(
out hxSection: IHandle;
DesiredAccess: TSectionAccessMask;
const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Map a view section into a process's address space
function NtxMapViewOfSection(
out MappedMemory: IMemory;
[Access(SECTION_MAP_ANY)] hSection: THandle;
PageProtection: TMemoryProtection;
[opt, Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle = nil;
[in, opt] Address: Pointer = nil;
[opt] SectionOffset: UInt64 = 0;
[opt] ViewSize: NativeUInt = 0;
[opt] ZeroBits: NativeUInt = 0;
AllocationType: TAllocationType = 0;
[opt] CommitSize: NativeUInt = 0;
InheritDisposition: TSectionInherit = ViewShare
) : TNtxStatus;
// Unmap a view of section
function NtxUnmapViewOfSection(
[in] Address: Pointer;
[opt, Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle = nil
): TNtxStatus;
// Determine a name of a backing file for a section
function NtxQueryFileNameSection(
[Access(SECTION_MAP_READ)] hSection: THandle;
out FileName: String
): TNtxStatus;
type
NtxSection = class abstract
// Query fixed-size information about a section
class function Query<T>(
[Access(SECTION_QUERY)] hSection: THandle;
InfoClass: TSectionInformationClass;
out Buffer: T
): TNtxStatus; static;
end;
// Query the entrypoint RVA of an image section
[MinOSVersion(OsWin10RS2)]
function RtlxQueryEntrypointRvaSection(
[Access(SECTION_QUERY)] hSection: THandle;
out EntryPointRva: Cardinal
): TNtxStatus;
{ Helper functions }
// Create a section from a file
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpForBypassingChecks)]
function RtlxCreateFileSection(
out hxSection: IHandle;
const FileParameters: IFileParameters;
PageProtection: TMemoryProtection;
AllocationAttributes: TAllocationAttributes;
const SectionObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Map it into into the memory
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpForBypassingChecks)]
function RtlxMapFile(
out MappedMemory: IMemory;
[Access(FILE_MAP_SECTION)] hFile: THandle;
PageProtection: TMemoryProtection;
AllocationAttributes: TAllocationAttributes = SEC_COMMIT;
[opt, Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle = nil
): TNtxStatus;
// Open a file and map it into into the memory
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpForBypassingChecks)]
function RtlxMapFileByName(
out MappedMemory: IMemory;
const FileParameters: IFileParameters;
PageProtection: TMemoryProtection;
AllocationAttributes: TAllocationAttributes = SEC_COMMIT;
[opt, Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle = nil
): TNtxStatus;
// Map a known DLL
function RtlxMapKnownDll(
out MappedMemory: IMemory;
DllName: String;
WoW64: Boolean
): TNtxStatus;
// Create a pagefile-backed copy of a section
function RtlxDuplicateDataSection(
hSectionIn: THandle;
out hxSectionOut: IHandle;
MakeExecutable: Boolean = False
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntexapi, NtUtils.Processes, NtUtils.Memory,
NtUtils.Files.Open;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TMappedAutoSection = class(TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
[opt] FProcess: IHandle;
procedure Release; override;
constructor Create(
[opt] const hxProcess: IHandle;
Address: Pointer;
Size: NativeUInt
);
end;
constructor TMappedAutoSection.Create;
begin
inherited Capture(Address, Size);
FProcess := hxProcess;
end;
procedure TMappedAutoSection.Release;
begin
if Assigned(FData) then
NtxUnmapViewOfSection(FData, FProcess);
FProcess := nil;
FData := nil;
inherited;
end;
function RtlxSecImageNoExecute;
begin
if RtlOsVersionAtLeast(OsWin8) then
Result := SEC_IMAGE_NO_EXECUTE
else
Result := SEC_IMAGE;
end;
function NtxCreateSection;
var
ObjAttr: PObjectAttributes;
hSection: THandle;
pSize: PUInt64;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
if MaximumSize <> 0 then
pSize := @MaximumSize
else
pSize := nil;
Result.Location := 'NtCreateSection';
Result.LastCall.Expects(ExpectedSectionFileAccess(PageProtection));
Result.Status := NtCreateSection(
hSection,
AccessMaskOverride(SECTION_ALL_ACCESS, ObjectAttributes),
ObjAttr,
pSize,
PageProtection,
AllocationAttributes,
hFile
);
if Result.IsSuccess then
hxSection := Auto.CaptureHandle(hSection);
end;
function NtxCreateFileSection;
begin
Result := NtxCreateSection(hxSection, MaximumSize, PageProtection,
AllocationAttributes, ObjectAttributes, hFile);
end;
function NtxOpenSection;
var
ObjAttr: PObjectAttributes;
hSection: THandle;
begin
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtOpenSection';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtOpenSection(hSection, DesiredAccess, ObjAttr^);
if Result.IsSuccess then
hxSection := Auto.CaptureHandle(hSection);
end;
function NtxMapViewOfSection;
begin
Result.Location := 'NtMapViewOfSection';
Result.LastCall.Expects(ExpectedSectionMapAccess(PageProtection));
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
Result.Status := NtMapViewOfSection(hSection, HandleOrDefault(hxProcess,
NtCurrentProcess), Address, ZeroBits, CommitSize, @SectionOffset, ViewSize,
InheritDisposition, AllocationType, PageProtection);
if Result.IsSuccess then
MappedMemory := TMappedAutoSection.Create(hxProcess, Address, ViewSize);
end;
function NtxUnmapViewOfSection;
begin
Result.Location := 'NtUnmapViewOfSection';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
Result.Status := NtUnmapViewOfSection(HandleOrDefault(hxProcess,
NtCurrentProcess), Address);
end;
function NtxQueryFileNameSection;
var
MappedMemory: IMemory;
begin
Result := NtxMapViewOfSection(MappedMemory, hSection, PAGE_NOACCESS);
if Result.IsSuccess then
Result := NtxQueryFileNameMemory(NtCurrentProcess, MappedMemory.Data,
FileName);
end;
class function NtxSection.Query<T>;
begin
Result.Location := 'NtQuerySection';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects<TSectionAccessMask>(SECTION_QUERY);
Result.Status := NtQuerySection(hSection, InfoClass, @Buffer, SizeOf(Buffer),
nil);
end;
function RtlxQueryEntrypointRvaSection;
var
OriginalBase, RelocationDelta: UIntPtr;
ImageInfo: TSectionImageInformation;
begin
// Determine the image base before dynamic relocation
Result := NtxSection.Query(hSection, SectionOriginalBaseInformation,
OriginalBase);
if not Result.IsSuccess then
Exit;
// Determine delta for dynamic relocation
Result := NtxSection.Query(hSection, SectionRelocationInformation,
RelocationDelta);
if not Result.IsSuccess then
Exit;
// Determine the entrypoint address after dynamic relocation
Result := NtxSection.Query(hSection, SectionImageInformation, ImageInfo);
if not Result.IsSuccess then
Exit;
{$R-}{$Q-}
// Compute the RVA
EntryPointRva := Cardinal(UIntPtr(ImageInfo.TransferAddress) - OriginalBase -
RelocationDelta);
{$IFDEF Q+}{$Q+}{$ENDIF}{$IFDEF R+}{$R+}{$ENDIF}
end;
{ Helper functions }
function RtlxCreateFileSection;
var
hxFile: IHandle;
begin
Result := NtxOpenFile(hxFile, FileParameters
.UseOptions(FileParameters.Options or FILE_NON_DIRECTORY_FILE)
.UseAccess(FileParameters.Access or
ExpectedSectionFileAccess(PageProtection))
.UseSyncMode(fsAsynchronous)
);
if Result.IsSuccess then
Result := NtxCreateFileSection(hxSection, hxFile.Handle, PageProtection,
AllocationAttributes, SectionObjectAttributes);
end;
function RtlxMapFile;
var
hxSection: IHandle;
begin
Result := NtxCreateFileSection(hxSection, hFile, PageProtection,
AllocationAttributes);
if Result.IsSuccess then
Result := NtxMapViewOfSection(MappedMemory, hxSection.Handle,
PageProtection, hxProcess);
end;
function RtlxMapFileByName;
var
hxSection: IHandle;
begin
Result := RtlxCreateFileSection(hxSection, FileParameters, PageProtection,
AllocationAttributes);
if Result.IsSuccess then
Result := NtxMapViewOfSection(MappedMemory, hxSection.Handle,
PageProtection, hxProcess);
end;
function RtlxMapKnownDll;
var
hxSection: IHandle;
begin
if Wow64 then
DllName := '\KnownDlls32\' + DllName
else
DllName := '\KnownDlls\' + DllName;
// Open a known-dll section
Result := NtxOpenSection(hxSection, SECTION_MAP_READ, DllName);
if not Result.IsSuccess then
Exit;
// Map it
Result := NtxMapViewOfSection(MappedMemory, hxSection.Handle, PAGE_READONLY);
end;
function RtlxDuplicateDataSection;
var
InView, OutView: IMemory;
Protection: TMemoryProtection;
begin
// Map the input
Result := NtxMapViewOfSection(InView, hSectionIn, PAGE_READONLY);
if not Result.IsSuccess then
Exit;
if MakeExecutable then
Protection := PAGE_EXECUTE_READWRITE
else
Protection := PAGE_READWRITE;
// Create an output section of required size
Result := NtxCreateSection(hxSectionOut, InView.Size, Protection);
if not Result.IsSuccess then
Exit;
// Map the output
Result := NtxMapViewOfSection(OutView, hxSectionOut.Handle, PAGE_READWRITE);
if not Result.IsSuccess then
Exit;
// Copy data
Move(InView.Data^, OutView.Data^, InView.Size);
end;
end.