forked from outflanknl/FindObjects-BOF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindModule.c
266 lines (210 loc) · 7.53 KB
/
FindModule.c
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
#include <windows.h>
#include <stdio.h>
#include "NativeAPI.h"
#include "Syscalls.h"
#include "beacon.h"
BOOL IsElevated() {
BOOL fRet = FALSE;
HANDLE hToken = NULL;
NTSTATUS status = ZwOpenProcessToken(NtCurrentProcess(), TOKEN_QUERY, &hToken);
if (status == STATUS_SUCCESS) {
TOKEN_ELEVATION Elevation = { 0 };
ULONG ReturnLength;
status = ZwQueryInformationToken(hToken, TokenElevation, &Elevation, sizeof(Elevation), &ReturnLength);
if (status == STATUS_SUCCESS) {
fRet = Elevation.TokenIsElevated;
}
}
if (hToken != NULL) {
ZwClose(hToken);
}
return fRet;
}
BOOL SetDebugPrivilege() {
HANDLE hToken = NULL;
TOKEN_PRIVILEGES TokenPrivileges = { 0 };
NTSTATUS status = ZwOpenProcessToken(NtCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
if (status != STATUS_SUCCESS) {
return FALSE;
}
TokenPrivileges.PrivilegeCount = 1;
TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
LPCWSTR lpwPriv = L"SeDebugPrivilege";
if (!ADVAPI32$LookupPrivilegeValueW(NULL, lpwPriv, &TokenPrivileges.Privileges[0].Luid)) {
ZwClose(hToken);
return FALSE;
}
status = ZwAdjustPrivilegesToken(hToken, FALSE, &TokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
if (status != STATUS_SUCCESS) {
ZwClose(hToken);
return FALSE;
}
ZwClose(hToken);
return TRUE;
}
ULONG GetCurrentPid() {
PROCESS_BASIC_INFORMATION pbi = { 0 };
NTSTATUS status = ZwQueryInformationProcess(NtCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
if (status != STATUS_SUCCESS) {
return 0;
}
return (ULONG)pbi.UniqueProcessId;
}
LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath) {
LPCWSTR lastSlash = lpszPath;
while (lpszPath && *lpszPath)
{
if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
lastSlash = lpszPath + 1;
lpszPath++;
}
return (LPWSTR)lastSlash;
}
BOOL EnumerateProcessModules(HANDLE hProcess, LPCWSTR lpwModuleName, PUNICODE_STRING uProcName, ULONG ulPid) {
PROCESS_BASIC_INFORMATION BasicInformation;
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
PPEB_LDR_DATA pLoaderData;
LDR_DATA_TABLE_ENTRY LoaderModule;
PLIST_ENTRY ListHead, Current;
UNICODE_STRING uImagePathName;
WCHAR wcImagePathName[MAX_PATH * 2];
WCHAR wcFullDllName[MAX_PATH * 2];
LPWSTR lpwDllName = NULL;
NTSTATUS status = ZwQueryInformationProcess(hProcess, ProcessBasicInformation, &BasicInformation, sizeof(BasicInformation), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
// Get the address of the Process Parameters struct and read ImagePathName data.
status = ZwReadVirtualMemory(hProcess, &(BasicInformation.PebBaseAddress->ProcessParameters), &ProcessParameters, sizeof(ProcessParameters), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
status = ZwReadVirtualMemory(hProcess, &(ProcessParameters->ImagePathName), &uImagePathName, sizeof(uImagePathName), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
MSVCRT$memset(wcImagePathName, 0, sizeof(wcImagePathName));
status = ZwReadVirtualMemory(hProcess, uImagePathName.Buffer, &wcImagePathName, uImagePathName.MaximumLength, NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
// Get the address of the PE Loader data.
status = ZwReadVirtualMemory(hProcess, &(BasicInformation.PebBaseAddress->Ldr), &pLoaderData, sizeof(pLoaderData), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
// Head of the module list: the last element in the list will point to this.
ListHead = &pLoaderData->InLoadOrderModuleList;
// Get the address of the first element in the list.
status = ZwReadVirtualMemory(hProcess, &(pLoaderData->InLoadOrderModuleList.Flink), &Current, sizeof(Current), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
while (Current != ListHead)
{
// Read the current module.
status = ZwReadVirtualMemory(hProcess, CONTAINING_RECORD(Current, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks), &LoaderModule, sizeof(LoaderModule), NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
MSVCRT$memset(wcFullDllName, 0, sizeof(wcFullDllName));
status = ZwReadVirtualMemory(hProcess, (LPVOID)LoaderModule.FullDllName.Buffer, &wcFullDllName, LoaderModule.FullDllName.MaximumLength, NULL);
if (status != STATUS_SUCCESS) {
return FALSE;
}
lpwDllName = PathFindFileNameW(wcFullDllName);
if (MSVCRT$_wcsicmp(lpwDllName, lpwModuleName) == 0) {
BeaconPrintf(CALLBACK_OUTPUT,
" ProcessName: %wZ\n"
" ProcessID: %lu\n"
" ImagePath: %ls\n"
" ModuleName: %ls\n", uProcName, ulPid, wcImagePathName, wcFullDllName);
}
// Address of the next module in the list.
Current = LoaderModule.InLoadOrderLinks.Flink;
}
return TRUE;
}
VOID go(IN PCHAR Args, IN ULONG Length) {
NTSTATUS status;
PSYSTEM_PROCESSES pProcInfo = NULL;
ULONG ulCurPid = 0;
UNICODE_STRING uLsass;
UNICODE_STRING uWinlogon;
LPVOID pProcInfoBuffer = NULL;
SIZE_T procInfoSize = 0x10000;
ULONG uReturnLength = 0;
LPCWSTR lpwModuleName = NULL;
// Parse Arguments
datap parser;
BeaconDataParse(&parser, Args, Length);
lpwModuleName = (LPWSTR)BeaconDataExtract(&parser, NULL);
if (lpwModuleName == NULL) {
BeaconPrintf(CALLBACK_ERROR, "Invalid argument...\n");
return;
}
_RtlInitUnicodeString RtlInitUnicodeString = (_RtlInitUnicodeString)
GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlInitUnicodeString");
if (RtlInitUnicodeString == NULL) {
return;
}
_RtlEqualUnicodeString RtlEqualUnicodeString = (_RtlEqualUnicodeString)
GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlEqualUnicodeString");
if (RtlEqualUnicodeString == NULL) {
return;
}
if (IsElevated()) {
SetDebugPrivilege();
}
do {
pProcInfoBuffer = NULL;
status = ZwAllocateVirtualMemory(NtCurrentProcess(), &pProcInfoBuffer, 0, &procInfoSize, MEM_COMMIT, PAGE_READWRITE);
if (status != STATUS_SUCCESS) {
return;
}
status = ZwQuerySystemInformation(SystemProcessInformation, pProcInfoBuffer, (ULONG)procInfoSize, &uReturnLength);
if (status == STATUS_INFO_LENGTH_MISMATCH) {
ZwFreeVirtualMemory(NtCurrentProcess(), &pProcInfoBuffer, &procInfoSize, MEM_RELEASE);
procInfoSize += uReturnLength;
}
} while (status != STATUS_SUCCESS);
ulCurPid = GetCurrentPid();
RtlInitUnicodeString(&uLsass, L"lsass.exe");
RtlInitUnicodeString(&uWinlogon, L"winlogon.exe");
pProcInfo = (PSYSTEM_PROCESSES)pProcInfoBuffer;
do {
pProcInfo = (PSYSTEM_PROCESSES)(((LPBYTE)pProcInfo) + pProcInfo->NextEntryDelta);
if ((ULONG)(ULONG_PTR)pProcInfo->ProcessId == 4) {
continue;
}
if ((ULONG)(ULONG_PTR)pProcInfo->ProcessId == ulCurPid) {
continue;
}
// Don't trigger sysmon by touching lsass or winlogon
if (RtlEqualUnicodeString(&pProcInfo->ProcessName, &uLsass, TRUE)) {
continue;
}
if (RtlEqualUnicodeString(&pProcInfo->ProcessName, &uWinlogon, TRUE)) {
continue;
}
HANDLE hProcess = NULL;
OBJECT_ATTRIBUTES ObjectAttributes;
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
CLIENT_ID uPid = { 0 };
uPid.UniqueProcess = pProcInfo->ProcessId;
uPid.UniqueThread = (HANDLE)0;
NTSTATUS status = ZwOpenProcess(&hProcess, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, &ObjectAttributes, &uPid);
if (hProcess != NULL) {
EnumerateProcessModules(hProcess, lpwModuleName, &pProcInfo->ProcessName, (ULONG)(ULONG_PTR)pProcInfo->ProcessId);
ZwClose(hProcess);
}
if (pProcInfo->NextEntryDelta == 0) {
break;
}
} while (pProcInfo && pProcInfo->NextEntryDelta);
if (pProcInfoBuffer) {
status = ZwFreeVirtualMemory(NtCurrentProcess(), &pProcInfoBuffer, &procInfoSize, MEM_RELEASE);
}
return;
}