-
Notifications
You must be signed in to change notification settings - Fork 13
/
kernel32_windows.go
176 lines (157 loc) · 4.51 KB
/
kernel32_windows.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
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
// Package customWin32 provides a small subset of win32
// bindings. For most win32 purposes, this project uses
// https://github.com/0xrawsec/golang-win32, however some
// wrappers are undesirable without exposing the lower
// level calls, thus these are implemented here.
package customWin32
import (
"fmt"
"syscall"
"unsafe"
"github.com/0xrawsec/golang-win32/win32"
k32 "github.com/0xrawsec/golang-win32/win32/kernel32"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
readProcessMemory = kernel32.NewProc("ReadProcessMemory")
globalMemoryStatusEx = kernel32.NewProc("GlobalMemoryStatusEx")
process32NextW = kernel32.NewProc("Process32NextW")
thread32First = kernel32.NewProc("Thread32First")
thread32Next = kernel32.NewProc("Thread32Next")
createRemoteThread = kernel32.NewProc("CreateRemoteThread")
)
const ERROR_NOT_ENOUGH_MEMORY = 0x8
func ReadProcessMemory(hProcess win32.HANDLE, lpBaseAddress win32.LPCVOID, buffer []byte) (int, error) {
numberOfBytesRead := win32.SIZE_T(0)
r1, _, lastErr := readProcessMemory.Call(
uintptr(hProcess),
uintptr(lpBaseAddress),
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(len(buffer)),
uintptr(unsafe.Pointer(&numberOfBytesRead)))
if r1 == 0 {
return int(numberOfBytesRead), lastErr
} else {
return int(numberOfBytesRead), nil
}
}
func GlobalMemoryStatusEx() (*MemoryStatusEx, error) {
memStat := new(MemoryStatusEx)
memStat.Length = win32.DWORD(unsafe.Sizeof(*memStat))
r1, _, err := globalMemoryStatusEx.Call(uintptr(unsafe.Pointer(memStat)))
if r1 == 0 {
return nil, err
}
return memStat, nil
}
func Process32NextW(hSnapshot win32.HANDLE, lpte k32.LPPROCESSENTRY32W) error {
_, _, lastErr := process32NextW.Call(
uintptr(hSnapshot),
uintptr(unsafe.Pointer(lpte)))
if lastErr.(syscall.Errno) == 0 {
return nil
}
return lastErr
}
func Thread32First(hSnapshot win32.HANDLE, lpte k32.LPTHREADENTRY32) error {
_, _, lastErr := thread32First.Call(
uintptr(hSnapshot),
uintptr(unsafe.Pointer(lpte)))
if lastErr.(syscall.Errno) == 0 {
return nil
}
return lastErr
}
// Thread32Next Win32 API wrapper
func Thread32Next(hSnapshot win32.HANDLE, lpte k32.LPTHREADENTRY32) error {
_, _, lastErr := thread32Next.Call(
uintptr(hSnapshot),
uintptr(unsafe.Pointer(lpte)))
if lastErr.(syscall.Errno) == 0 {
return nil
}
return lastErr
}
func ListThreads(pid int) ([]int, error) {
snap, err := k32.CreateToolhelp32Snapshot(k32.TH32CS_SNAPTHREAD, 0)
if err != nil {
return nil, err
}
threadIDs := make([]int, 0)
threadEntry := k32.NewThreadEntry32()
err = Thread32First(snap, &threadEntry)
if err != nil {
if err.(syscall.Errno) != win32.ERROR_NO_MORE_FILES {
return nil, err
} else {
return nil, nil
}
}
if int(threadEntry.Th32OwnerProcessID) == pid {
threadIDs = append(threadIDs, int(threadEntry.Th32ThreadID))
}
for {
err = Thread32Next(snap, &threadEntry)
if err != nil {
break
}
if int(threadEntry.Th32OwnerProcessID) == pid {
threadIDs = append(threadIDs, int(threadEntry.Th32ThreadID))
}
}
if err.(syscall.Errno) != win32.ERROR_NO_MORE_FILES {
return nil, err
}
return threadIDs, nil
}
func SuspendProcess(pid int) error {
threads, err := ListThreads(pid)
if err != nil {
return fmt.Errorf("could not list process threads, reason: %w", err)
}
for _, tid := range threads {
hThread, err := k32.OpenThread(k32.THREAD_SUSPEND_RESUME, win32.FALSE, win32.DWORD(tid))
if err != nil {
return fmt.Errorf("could not open thread %d, reason: %w", tid, err)
}
_, err = k32.SuspendThread(hThread)
k32.CloseHandle(hThread)
if err != nil {
return fmt.Errorf("could not open suspend thread %d, reason: %w", tid, err)
}
}
return nil
}
func ResumeProcess(pid int) error {
threads, err := ListThreads(pid)
if err != nil {
return fmt.Errorf("could not list process threads, reason: %w", err)
}
for _, tid := range threads {
hThread, err := k32.OpenThread(k32.THREAD_SUSPEND_RESUME, win32.FALSE, win32.DWORD(tid))
if err != nil {
return fmt.Errorf("could not open thread %d, reason: %w", tid, err)
}
_, err = k32.ResumeThread(hThread)
k32.CloseHandle(hThread)
if err != nil {
return fmt.Errorf("could not open resume thread %d, reason: %w", tid, err)
}
}
return nil
}
func CreateRemoteThreadMinimal(hProcess win32.HANDLE, startAddress uintptr) error {
_, _, lastErr := createRemoteThread.Call(
uintptr(hProcess),
uintptr(0),
uintptr(0),
startAddress,
uintptr(0),
uintptr(0),
uintptr(0),
)
if lastErr.(syscall.Errno) == 0 {
return nil
}
return lastErr
}