-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
204 lines (179 loc) · 5.45 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"unicode/utf16"
"unsafe"
)
const (
PS_ALL = iota
PS_ANY
)
func eInfo(v ...interface{}) {
m := fmt.Sprint(v...) // does not insert space in between items.
el.Info(1, m)
}
func eError(v ...interface{}) {
m := fmt.Sprint(v...) // does not insert space in between items.
el.Error(1, m)
}
// readLines reads a whole file into memory and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// Get the full name (with path) of the executing module.
func getModuleFileName() (string, error) {
var sysproc = syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleFileNameW")
b := make([]uint16, syscall.MAX_PATH)
r, _, err := sysproc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
n := uint32(r)
if n == 0 {
return "", err
}
return string(utf16.Decode(b[0:n])), nil
}
// Up to 15 args only.
func execute(args []string) ([]byte, error) {
var (
con []byte
err error
)
switch len(args) {
case 1:
cmd := exec.Command(args[0])
con, err = cmd.Output()
case 2:
cmd := exec.Command(args[0], args[1])
con, err = cmd.Output()
case 3:
cmd := exec.Command(args[0], args[1], args[2])
con, err = cmd.Output()
case 4:
cmd := exec.Command(args[0], args[1], args[2], args[3])
con, err = cmd.Output()
case 5:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4])
con, err = cmd.Output()
case 6:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5])
con, err = cmd.Output()
case 7:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6])
con, err = cmd.Output()
case 8:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7])
con, err = cmd.Output()
case 9:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8])
con, err = cmd.Output()
case 10:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9])
con, err = cmd.Output()
case 11:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10])
con, err = cmd.Output()
case 12:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11])
con, err = cmd.Output()
case 13:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12])
con, err = cmd.Output()
case 14:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13])
con, err = cmd.Output()
case 15:
cmd := exec.Command(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14])
con, err = cmd.Output()
}
return con, err
}
// Run process as SYSTEM in the same session as winlogon.exe, not session 0.
func runInteractive(cmd string, args string, wait bool, waitms int) (uint32, error) {
path, _ := getModuleFileName()
lib := filepath.Dir(path) + `\libcore.dll`
if _, err := os.Stat(lib); os.IsNotExist(err) {
return uint32(syscall.ENOENT), fmt.Errorf("Cannot find libcore.dll.")
}
var (
exitCode uint32
runUser = syscall.MustLoadDLL(lib).MustFindProc("StartSystemUserProcess")
)
shouldWait := 1
if !wait {
shouldWait = 0
}
_, _, err := runUser.Call(
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(cmd))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(args))),
0,
uintptr(unsafe.Pointer(&exitCode)),
uintptr(shouldWait),
uintptr(waitms))
return exitCode, err
}
// The 'and' argument specifies the type of check for the list names; true is all names should
// be running, false if its only one (or any) of the names list.
func isProcessActive(check int, names ...string) bool {
if len(names) == 0 {
return false
}
res := true
cmd := exec.Command("c:/windows/system32/tasklist.exe", "/fo", "csv", "/nh")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.Output()
if err != nil {
return false
}
switch check {
case PS_ALL:
s := strings.ToLower(fmt.Sprintf("%s", out))
for _, name := range names {
if !strings.Contains(s, strings.ToLower(name)) {
return false
}
}
case PS_ANY:
s := strings.ToLower(fmt.Sprintf("%s", out))
found := 0
for _, name := range names {
if strings.Contains(s, strings.ToLower(name)) {
found++
}
}
if found > 1 {
return true
} else {
res = false
}
default:
return false
}
return res
}
// Note that user has no option to cancel since this is from session 0. Default to 10 seconds.
func rebootSystem() error {
cmd := exec.Command("shutdown", "/r", "/t", "10")
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// The way to detect this is if git.exe and/or msbuild.exe is/are running.
func isRunnerActive() bool {
return isProcessActive(PS_ANY, "git.exe", "msbuild.exe")
}