-
Notifications
You must be signed in to change notification settings - Fork 8
/
util_windows.go
48 lines (45 loc) · 1.06 KB
/
util_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
package escalate
import (
"bytes"
"errors"
"os"
"os/exec"
"strconv"
"strings"
)
// GetVer gets the major version of the current installed
// Windows
func GetVer() (int, error) {
cmd := exec.Command("cmd", "ver")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return 0, err
}
osStr := strings.Replace(out.String(), "\n", "", -1)
osStr = strings.Replace(osStr, "\r\n", "", -1)
tmp1 := strings.Index(osStr, "[Version")
tmp2 := strings.Index(osStr, "]")
if tmp1 == -1 || tmp2 == -1 {
return 0, errors.New("Version string has wrong format")
}
longVer := osStr[tmp1+9 : tmp2]
majorVerStr := strings.SplitN(longVer, ".", 2)[0]
majorVerInt, err := strconv.Atoi(majorVerStr)
if err != nil {
return 0, errors.New("Version could not be converted to int")
}
return majorVerInt, nil
}
// CheckElevate checks whether the current process has administrator
// privileges
func CheckElevate() bool {
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
if err != nil {
return false
}
return true
}