-
Notifications
You must be signed in to change notification settings - Fork 4
/
handle.go
92 lines (84 loc) · 2.62 KB
/
handle.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
//+build windows
package handle
import (
"fmt"
"io"
"runtime"
"time"
)
type Handle struct {
Process uint32
Handle uint3264
Name string
Type string
}
func QueryHandles(buf []byte, processFilter *uint32, handleTypes []string, queryTimeout time.Duration) (handles []Handle, err error) {
systemHandles, err := NtQuerySystemHandles(buf)
if err != nil {
return nil, err
}
var typeFilter map[string]struct{}
if len(handleTypes) > 0 {
typeFilter = make(map[string]struct{})
for _, handleType := range handleTypes {
typeFilter[handleType] = struct{}{}
}
}
log("type filter: %#v", typeFilter)
log("handle count: %d", len(systemHandles))
inspector := NewInspector(queryTimeout)
defer inspector.Close()
for _, handle := range systemHandles {
log("handle: %#v", handle)
if processFilter != nil && *processFilter != uint32(handle.UniqueProcessID) {
log("skipping handle of process %d due to process filter %d", handle.UniqueProcessID, processFilter)
continue
}
// some handles can cause a permanent wait within NtQueryObject.
// While we handle those freezes (by killing the thread after a known timeout),
// doing so causes memory to leak - this is apparently inherent to TerminateThread.
// Therefore, we try to avoid blocking handles in general by blacklisting access masks
// that might indicate this issue.
if (handle.GrantedAccess == 0x0012019f) ||
(handle.GrantedAccess == 0x001a019f) ||
(handle.GrantedAccess == 0x00120189) ||
(handle.GrantedAccess == 0x00100000) {
log("skipping handle due to granted access")
continue
}
handleType, err := inspector.LookupHandleType(handle)
if err != nil {
log("could not query handle type for handle %d in process %d with access mask %d, error: %v", handle.HandleValue, handle.UniqueProcessID, handle.GrantedAccess, err)
continue
}
if typeFilter != nil {
if _, isTargetType := typeFilter[handleType]; !isTargetType {
continue
}
}
name, err := inspector.LookupHandleName(handle)
if err != nil {
log("could not query handle name for handle %d in process %d with access mask %d, error: %v", handle.HandleValue, handle.UniqueProcessID, handle.GrantedAccess, err)
continue
}
handles = append(handles, Handle{
Process: uint32(handle.UniqueProcessID),
Handle: handle.HandleValue,
Name: name,
Type: handleType,
})
}
runtime.KeepAlive(buf)
return handles, nil
}
var writer io.Writer
// DebugWriter sets a debug writer for debug logging, e.g. os.Stdout
func DebugWriter(w io.Writer) {
writer = w
}
func log(format string, a ...interface{}) {
if writer == nil {
return
}
fmt.Fprintf(writer, format+"\n", a...)
}