-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathenumerate-remote-process-modules-via-peb.pas
More file actions
195 lines (156 loc) · 5.51 KB
/
enumerate-remote-process-modules-via-peb.pas
File metadata and controls
195 lines (156 loc) · 5.51 KB
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
uses
System.SysUtils, Winapi.Windows, Generics.Collections;
// ...
type
TProcessInformationClass = (
// ...
ProcessBasicInformation = 0
// ...
);
// ...
function NtQueryInformationProcess(
ProcessHandle : THandle;
ProcessInformationClass : TProcessInformationClass;
ProcessInformation : Pointer;
ProcessInformationLength : ULONG;
var ReturnLength : ULONG
) : NTSTATUS; stdcall; external 'NTDLL.DLL';
// ...
type
PPEB = Pointer;
_PROCESS_BASIC_INFORMATION = record
Reserved1 : PVOID;
PebBaseAddress : PPEB;
Reserved2 : array[0..1] of PVOID;
UniqueProcessId : ULONG_PTR;
Reserved3 : PVOID;
end;
TProcessBasicInformation = _PROCESS_BASIC_INFORMATION;
PProcessBasicInformation = ^TProcessBasicInformation;
_LDR_DATA_TABLE_ENTRY = record
InLoadOrderLinks : TListEntry;
InMemoryOrderLinks : TListEntry;
InInitializationOrderLinkes : TListEntry;
DllBase : PVOID;
EntryPoint : PVOID;
SizeOfImage : ULONG;
FullDllName : TUnicodeString;
BaseDllName : TUnicodeString;
end;
TLdrDataTableEntry = _LDR_DATA_TABLE_ENTRY;
PLdrDataTableEntry = ^TLdrDataTableEntry;
// ...
(* TModuleInformation *)
type
TModuleInformation = class
private
FImagePath : String;
FBaseAddress : NativeUInt;
FEntryPoint : NativeUInt;
FSizeOfImage : Cardinal;
public
{@C}
constructor Create(const AImagePath : String; const ABaseAddress, AEntryPoint: NativeUInt; const ASizeOfImage : Cardinal);
{@G}
property ImagePath : String read FImagePath;
property BaseAddress : NativeUInt read FBaseAddress;
property EntryPoint : NativeUInt read FEntryPoint;
property SizeOfImage : Cardinal read FSizeOfImage;
end;
// ...
constructor TModuleInformation.Create(const AImagePath : String; const ABaseAddress, AEntryPoint: NativeUInt; const ASizeOfImage : Cardinal);
begin
inherited Create();
///
FImagePath := AImagePath;
FBaseAddress := ABaseAddress;
FEntryPoint := AEntryPoint;
FSizeOfImage := ASizeOfImage;
end;
(* Local *)
function EnumerateProcessModules(const ATargetProcessId : Cardinal; var AList : TObjectList<TModuleInformation>) : Cardinal;
begin
result := 0;
///
if not Assigned(AList) then
AList := TObjectList<TModuleInformation>.Create(True)
else begin
AList.Clear();
///
if not AList.OwnsObjects then
AList.OwnsObjects := True;
end;
///
var hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ATargetProcessId);
if hProcess = 0 then
raise EWindowsException.Create('OpenProcess');
try
var AProcessBasicInformation : TProcessBasicInformation;
var AReturnLength : Cardinal;
var ARet := NtQueryInformationProcess(
hProcess,
ProcessBasicInformation,
@AProcessBasicInformation,
SizeOf(TProcessBasicInformation),
AReturnLength
);
if ARet < 0 then
raise Exception.Create('Could not retrieve target process PEB address.');
///
var ppPEBLdrData := Pointer(NativeUInt(AProcessBasicInformation.PebBaseAddress) + {$IFDEF WIN64}$00000018{$ELSE}$0000000c{$ENDIF});
var pPEBLdrData : Pointer;
var ABytesRead : SIZE_T;
if not ReadProcessMemory(hProcess, ppPEBLdrData, @pPEBLdrData, SizeOf(Pointer), ABytesRead) then
raise EWindowsException.Create('ReadProcessMemory(1)');
var AEntry : TListEntry;
var pFirstEntryOffset := Pointer(NativeUInt(pPEBLdrData) + {$IFDEF WIN64}$00000010{$ELSE}$0000000c{$ENDIF});
if not ReadProcessMemory(hProcess, pFirstEntryOffset, @AEntry, SizeOf(TListEntry), ABytesRead) then
raise EWindowsException.Create('ReadProcessMemory(2)');
var pEntryOffset := AEntry.Flink;
var ADataTableEntry : TLdrDataTableEntry;
repeat
if not ReadProcessMemory(hProcess, pEntryOffset, @ADataTableEntry, SizeOf(TLdrDataTableEntry), ABytesRead) then
raise EWindowsException.Create(Format('ReadProcessMemory(3), Index: %d', [AList.Count]));
var pImageFileName : PWideChar;
var pImageFileNameSize := ADataTableEntry.FullDllName.Length * SizeOf(WideChar);
GetMem(pImageFileName, pImageFileNameSize);
try
if not ReadProcessMemory(hProcess, ADataTableEntry.FullDllName.Buffer, pImageFileName, pImageFileNameSize, ABytesRead) then
raise EWindowsException.Create(Format('ReadProcessMemory(4), Index: %d', [AList.Count]));
///
AList.Add(TModuleInformation.Create(
string(pImageFileName),
NativeUInt(ADataTableEntry.DllBase),
NativeUInt(ADataTableEntry.EntryPoint),
ADataTableEntry.SizeOfImage
));
finally
FreeMem(pImageFileName, pImageFileNameSize);
end;
///
pEntryOffset := ADataTableEntry.InLoadOrderLinks.Flink;
until pFirstEntryOffset = ADataTableEntry.InLoadOrderLinks.Flink;
finally
CloseHandle(hProcess);
end;
///
result := AList.Count;
end;
// ...
begin
try
var AList := TObjectList<TModuleInformation>.Create(True);
try
EnumerateProcessModules(<target_process_id>, AList);
///
for var AModuleEntry in AList do
WriteLn(Format('Base:[0x%p], EP:[0x%p], Size:[0x%p] -> %s', [
Pointer(AModuleEntry.BaseAddress),
Pointer(AModuleEntry.EntryPoint),
Pointer(AModuleEntry.SizeOfImage),
AModuleEntry.ImagePath
]));
finally
FreeAndNil(AList);
end;
// ...