-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathprocess_windows.go
382 lines (333 loc) · 10.5 KB
/
process_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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package windows
import (
"os"
"path/filepath"
"strings"
"syscall"
"time"
"unsafe"
"github.com/pkg/errors"
syswin "golang.org/x/sys/windows"
windows "github.com/elastic/go-windows"
"github.com/elastic/go-sysinfo/types"
)
var (
selfPID = os.Getpid()
devMapper = newDeviceMapper()
)
func (s windowsSystem) Processes() (procs []types.Process, err error) {
pids, err := windows.EnumProcesses()
if err != nil {
return nil, errors.Wrap(err, "EnumProcesses")
}
procs = make([]types.Process, 0, len(pids))
var proc types.Process
for _, pid := range pids {
if pid == 0 || pid == 4 {
// The Idle and System processes (PIDs 0 and 4) can never be
// opened by user-level code (see documentation for OpenProcess).
continue
}
if proc, err = s.Process(int(pid)); err == nil {
procs = append(procs, proc)
}
}
if len(procs) == 0 {
return nil, err
}
return procs, nil
}
func (s windowsSystem) Process(pid int) (types.Process, error) {
return newProcess(pid)
}
func (s windowsSystem) Self() (types.Process, error) {
return newProcess(selfPID)
}
type process struct {
pid int
info types.ProcessInfo
}
func (p *process) PID() int {
return p.pid
}
func (p *process) Parent() (types.Process, error) {
info, err := p.Info()
if err != nil {
return nil, err
}
return newProcess(info.PPID)
}
func newProcess(pid int) (*process, error) {
p := &process{pid: pid}
if err := p.init(); err != nil {
return nil, err
}
return p, nil
}
func (p *process) init() error {
handle, err := p.open()
if err != nil {
return err
}
defer syscall.CloseHandle(handle)
var path string
if imgf, err := windows.GetProcessImageFileName(handle); err == nil {
path, err = devMapper.DevicePathToDrivePath(imgf)
if err != nil {
path = imgf
}
}
var creationTime, exitTime, kernelTime, userTime syscall.Filetime
if err := syscall.GetProcessTimes(handle, &creationTime, &exitTime, &kernelTime, &userTime); err != nil {
return err
}
// Try to read the RTL_USER_PROCESS_PARAMETERS struct from the target process
// memory. This can fail due to missing access rights or when we are running
// as a 32bit process in a 64bit system (WOW64).
// Don't make this a fatal error: If it fails, `args` and `cwd` fields will
// be missing.
var args []string
var cwd string
var ppid int
pbi, err := getProcessBasicInformation(handle)
if err == nil {
ppid = int(pbi.InheritedFromUniqueProcessID)
userProcParams, err := getUserProcessParams(handle, pbi)
if err == nil {
if argsW, err := readProcessUnicodeString(handle, &userProcParams.CommandLine); err == nil {
args, err = splitCommandline(argsW)
if err != nil {
args = nil
}
}
if cwdW, err := readProcessUnicodeString(handle, &userProcParams.CurrentDirectoryPath); err == nil {
cwd, _, err = windows.UTF16BytesToString(cwdW)
if err != nil {
cwd = ""
}
// Remove trailing separator
cwd = strings.TrimRight(cwd, "\\")
}
}
}
p.info = types.ProcessInfo{
Name: filepath.Base(path),
PID: p.pid,
PPID: ppid,
Exe: path,
Args: args,
CWD: cwd,
StartTime: time.Unix(0, creationTime.Nanoseconds()),
}
return nil
}
func getProcessBasicInformation(handle syscall.Handle) (pbi windows.ProcessBasicInformationStruct, err error) {
actualSize, err := windows.NtQueryInformationProcess(handle, windows.ProcessBasicInformation, unsafe.Pointer(&pbi), uint32(windows.SizeOfProcessBasicInformationStruct))
if actualSize < uint32(windows.SizeOfProcessBasicInformationStruct) {
return pbi, errors.New("bad size for PROCESS_BASIC_INFORMATION")
}
return pbi, err
}
func getUserProcessParams(handle syscall.Handle, pbi windows.ProcessBasicInformationStruct) (params windows.RtlUserProcessParameters, err error) {
const is32bitProc = unsafe.Sizeof(uintptr(0)) == 4
// Offset of params field within PEB structure.
// This structure is different in 32 and 64 bit.
paramsOffset := 0x20
if is32bitProc {
paramsOffset = 0x10
}
// Read the PEB from the target process memory
pebSize := paramsOffset + 8
peb := make([]byte, pebSize)
nRead, err := windows.ReadProcessMemory(handle, pbi.PebBaseAddress, peb)
if err != nil {
return params, err
}
if nRead != uintptr(pebSize) {
return params, errors.Errorf("PEB: short read (%d/%d)", nRead, pebSize)
}
// Get the RTL_USER_PROCESS_PARAMETERS struct pointer from the PEB
paramsAddr := *(*uintptr)(unsafe.Pointer(&peb[paramsOffset]))
// Read the RTL_USER_PROCESS_PARAMETERS from the target process memory
paramsBuf := make([]byte, windows.SizeOfRtlUserProcessParameters)
nRead, err = windows.ReadProcessMemory(handle, paramsAddr, paramsBuf)
if err != nil {
return params, err
}
if nRead != uintptr(windows.SizeOfRtlUserProcessParameters) {
return params, errors.Errorf("RTL_USER_PROCESS_PARAMETERS: short read (%d/%d)", nRead, windows.SizeOfRtlUserProcessParameters)
}
params = *(*windows.RtlUserProcessParameters)(unsafe.Pointer(¶msBuf[0]))
return params, nil
}
// read an UTF-16 string from another process memory. Result is an []byte
// with the UTF-16 data.
func readProcessUnicodeString(handle syscall.Handle, s *windows.UnicodeString) ([]byte, error) {
// Allocate an extra UTF-16 null character at the end in case the read string
// is not terminated.
extra := 2
if s.Size&1 != 0 {
extra = 3 // If size is odd, need 3 nulls to terminate.
}
buf := make([]byte, int(s.Size)+extra)
nRead, err := windows.ReadProcessMemory(handle, s.Buffer, buf[:s.Size])
if err != nil {
return nil, err
}
if nRead != uintptr(s.Size) {
return nil, errors.Errorf("unicode string: short read: (%d/%d)", nRead, s.Size)
}
return buf, nil
}
// Use Windows' CommandLineToArgv API to split an UTF-16 command line string
// into a list of parameters.
func splitCommandline(utf16 []byte) ([]string, error) {
n := len(utf16)
// Discard odd byte
if n&1 != 0 {
n--
utf16 = utf16[:n]
}
if n == 0 {
return nil, nil
}
terminated := false
for i := 0; i < n && !terminated; i += 2 {
terminated = utf16[i] == 0 && utf16[i+1] == 0
}
if !terminated {
// Append a null uint16 at the end if terminator is missing
utf16 = append(utf16, 0, 0)
}
var numArgs int32
argsWide, err := syscall.CommandLineToArgv((*uint16)(unsafe.Pointer(&utf16[0])), &numArgs)
if err != nil {
return nil, err
}
// Free memory allocated for CommandLineToArgvW arguments.
defer syscall.LocalFree((syscall.Handle)(unsafe.Pointer(argsWide)))
args := make([]string, numArgs)
for idx := range args {
args[idx] = syscall.UTF16ToString(argsWide[idx][:])
}
return args, nil
}
func (p *process) open() (handle syscall.Handle, err error) {
if p.pid == selfPID {
return syscall.GetCurrentProcess()
}
// Try different access rights, from broader to more limited.
// PROCESS_VM_READ is needed to get command-line and working directory
// PROCESS_QUERY_LIMITED_INFORMATION is only available in Vista+
for _, permissions := range [4]uint32{
syscall.PROCESS_QUERY_INFORMATION | windows.PROCESS_VM_READ,
windows.PROCESS_QUERY_LIMITED_INFORMATION | windows.PROCESS_VM_READ,
syscall.PROCESS_QUERY_INFORMATION,
windows.PROCESS_QUERY_LIMITED_INFORMATION,
} {
if handle, err = syscall.OpenProcess(permissions, false, uint32(p.pid)); err == nil {
break
}
}
return handle, err
}
func (p *process) Info() (types.ProcessInfo, error) {
return p.info, nil
}
func (p *process) User() (types.UserInfo, error) {
handle, err := p.open()
if err != nil {
return types.UserInfo{}, errors.Wrap(err, "OpenProcess failed")
}
defer syscall.CloseHandle(handle)
var accessToken syswin.Token
err = syswin.OpenProcessToken(syswin.Handle(handle), syscall.TOKEN_QUERY, &accessToken)
if err != nil {
return types.UserInfo{}, errors.Wrap(err, "OpenProcessToken failed")
}
defer accessToken.Close()
tokenUser, err := accessToken.GetTokenUser()
if err != nil {
return types.UserInfo{}, errors.Wrap(err, "GetTokenUser failed")
}
sid, err := sidToString(tokenUser.User.Sid)
if sid == "" || err != nil {
const errStr = "failed to look up user SID"
if err != nil {
return types.UserInfo{}, errors.Wrap(err, errStr)
}
return types.UserInfo{}, errors.New(errStr)
}
tokenGroup, err := accessToken.GetTokenPrimaryGroup()
if err != nil {
return types.UserInfo{}, errors.Wrap(err, "GetTokenPrimaryGroup failed")
}
gsid, err := sidToString(tokenGroup.PrimaryGroup)
if gsid == "" || err != nil {
const errStr = "failed to look up primary group SID"
if err != nil {
return types.UserInfo{}, errors.Wrap(err, errStr)
}
return types.UserInfo{}, errors.New(errStr)
}
return types.UserInfo{
UID: sid,
GID: gsid,
}, nil
}
func (p *process) Memory() (types.MemoryInfo, error) {
handle, err := p.open()
if err != nil {
return types.MemoryInfo{}, err
}
defer syscall.CloseHandle(handle)
counters, err := windows.GetProcessMemoryInfo(handle)
if err != nil {
return types.MemoryInfo{}, err
}
return types.MemoryInfo{
Resident: uint64(counters.WorkingSetSize),
Virtual: uint64(counters.PrivateUsage),
}, nil
}
func (p *process) CPUTime() (types.CPUTimes, error) {
handle, err := p.open()
if err != nil {
return types.CPUTimes{}, err
}
defer syscall.CloseHandle(handle)
var creationTime, exitTime, kernelTime, userTime syscall.Filetime
if err := syscall.GetProcessTimes(handle, &creationTime, &exitTime, &kernelTime, &userTime); err != nil {
return types.CPUTimes{}, err
}
return types.CPUTimes{
User: windows.FiletimeToDuration(&userTime),
System: windows.FiletimeToDuration(&kernelTime),
}, nil
}
// OpenHandles returns the number of open handles of the process.
func (p *process) OpenHandleCount() (int, error) {
handle, err := p.open()
if err != nil {
return 0, err
}
defer syscall.CloseHandle(handle)
count, err := windows.GetProcessHandleCount(handle)
return int(count), err
}