This repository was archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.go
More file actions
189 lines (159 loc) · 3.88 KB
/
Copy pathprocesses.go
File metadata and controls
189 lines (159 loc) · 3.88 KB
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
package sysutils
/*
#include "winapiutils.h"
*/
import "C"
import (
"fmt"
"path/filepath"
"reflect"
"unsafe"
)
type OSProcess struct {
PID int
PPID int
IsRemote bool
ExecName string
CommandLine string
UProfile UserProfile
}
func intToBool(i int) bool {
return i != 0
}
func GetOSProcesses() ([]OSProcess, error) {
var (
procs *C.OSProcess
n C.DWORD
eTag C.DWORD
leCode C.DWORD
)
procs = C.GetOSProcesses((*C.DWORD)(unsafe.Pointer(&n)), (*C.DWORD)(unsafe.Pointer(&eTag)), (*C.DWORD)(unsafe.Pointer(&leCode)))
if et := uint32(eTag); et != 0 {
return nil, fmt.Errorf("C.GetOSProcesses: exit tag %d; last error code %d", et, int32(leCode))
}
defer C.FreeOSProcesses((*C.OSProcess)(unsafe.Pointer(procs)), n)
var procEntries []C.OSProcess
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&procEntries)))
sliceHeader.Cap = int(n)
sliceHeader.Len = sliceHeader.Cap
sliceHeader.Data = uintptr(unsafe.Pointer(procs))
var rprocs []OSProcess
for _, pe := range procEntries {
up := UserProfile{
WCHARPtrToGoString((*C.WCHAR)(unsafe.Pointer(pe.UProfile.Name))),
WCHARPtrToGoString((*C.WCHAR)(unsafe.Pointer(pe.UProfile.Domain))),
WCHARPtrToGoString((*C.WCHAR)(unsafe.Pointer(pe.UProfile.SID))),
intToBool(int(pe.UProfile.Elevated)),
}
osp := OSProcess{
int(pe.PID),
int(pe.PPID),
intToBool(int(pe.IsRemote)),
WCHARPtrToGoString((*C.WCHAR)(unsafe.Pointer(pe.ExecName))),
WCHARPtrToGoString((*C.WCHAR)(unsafe.Pointer(pe.CommandLine))),
up,
}
rprocs = append(rprocs, osp)
}
return rprocs, nil
}
func GetCurrentProcessID() int {
return int(C.GetCurrentProcessId())
}
func IsProcessByNameAlone(name string) (bool, error) {
procs, err := GetOSProcesses()
if err != nil {
return false, err
}
pc := 0
for _, p := range procs {
if name == p.ExecName {
pc++
}
}
return pc == 1, nil
}
func ProcessByNameExists(name string) (bool, error) {
procs, err := GetOSProcesses()
if err != nil {
return false, err
}
pc := 0
for _, p := range procs {
if name == p.ExecName {
pc++
}
}
return pc != 0, nil
}
func ProcessByNameAndPIDExists(name string, pid int) (bool, error) {
procs, err := GetOSProcesses()
if err != nil {
return false, err
}
for _, p := range procs {
if name == p.ExecName && pid == p.PID {
return true, nil
}
}
return false, nil
}
func GetThisExecutableDirAndName() (string, string, error) {
var eTag, leCode C.DWORD
var fn *C.WCHAR = C.GetCurrentExecutableFullName((*C.DWORD)(unsafe.Pointer(&eTag)), (*C.DWORD)(unsafe.Pointer(&leCode)))
defer C.free(unsafe.Pointer(fn))
if et := int32(eTag); et != 0 {
return "", "", fmt.Errorf("C.GetCurrentExecutableFullName: exit tag %d; last error code %d", et, int32(leCode))
}
d, n := filepath.Split(WCHARPtrToGoString(fn))
return d, n, nil
}
type UserProfile struct {
Name string
Domain string
SID string
IsElevated bool
}
func GetCurrentProcessUserProfile() (*UserProfile, error) {
var eTag C.DWORD
up, err := C.GetCurrentProcessUserProfile((*C.DWORD)(unsafe.Pointer(&eTag)))
if err != nil {
return nil, err
}
if et := uint32(eTag); et != 0 {
return nil, fmt.Errorf("C.GetCurrentUserProfile: exit tag %d", et)
}
defer C.FreeUserProfile(up)
gup := new(UserProfile)
gup.Name = WCHARPtrToGoString(up.Name)
gup.Domain = WCHARPtrToGoString(up.Domain)
gup.SID = WCHARPtrToGoString(up.SID)
gup.IsElevated = intToBool(int(up.Elevated))
return gup, nil
}
func HasCurrentProcessAdminPrivileges() (bool, error) {
wup, err := GetCurrentProcessUserProfile()
if err != nil {
return false, err
}
return wup.IsElevated, nil
}
func HideConsoleWindow() (err error) {
_, err = C.FreeConsole()
if err != nil {
return
}
_, err = C.AllocConsole()
if err != nil {
return
}
hwnd, err := C.GetConsoleWindow()
if err != nil {
return
}
_, err = C.ShowWindow(hwnd, C.SW_HIDE)
if err != nil {
return
}
return
}