-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
83 lines (67 loc) · 1.81 KB
/
main.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
package main
import (
"fmt"
"os/exec"
"syscall"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
"golang.org/x/sys/windows"
)
const (
seDebugPrivilege = "SeDebugPrivilege"
tiServiceName = "TrustedInstaller"
tiExecutableName = "trustedinstaller.exe"
)
func RunAsTrustedInstaller(path string, args []string) error {
if !checkIfAdmin() {
if err := elevate(); err != nil {
return fmt.Errorf("cannot elevate Privs: %v", err)
}
}
if err := enableSeDebugPrivilege(); err != nil {
return fmt.Errorf("cannot enable %v: %v", seDebugPrivilege, err)
}
svcMgr, err := mgr.Connect()
if err != nil {
return fmt.Errorf("cannot connect to svc manager: %v", err)
}
s, err := openService(svcMgr.Handle, tiServiceName)
if err != nil {
return fmt.Errorf("cannot open ti service: %v", err)
}
status, err := s.Query()
if err != nil {
return fmt.Errorf("cannot query ti service: %v", err)
}
if status.State != svc.Running {
if err := s.Start(); err != nil {
return fmt.Errorf("cannot start ti service: %v", err)
} else {
defer s.Control(svc.Stop)
}
}
tiPid, err := getTrustedInstallerPid()
if err != nil {
return err
}
hand, err := windows.OpenProcess(windows.PROCESS_CREATE_PROCESS|windows.PROCESS_DUP_HANDLE|windows.PROCESS_SET_INFORMATION, true, tiPid)
if err != nil {
return fmt.Errorf("cannot open ti process: %v", err)
}
cmd := exec.Command(path, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: windows.CREATE_NEW_CONSOLE,
ParentProcess: syscall.Handle(hand),
}
err = cmd.Start()
if err != nil {
return fmt.Errorf("cannot start new process: %v", err)
}
fmt.Println("Started process with PID", cmd.Process.Pid)
return nil
}
func main() {
if err := RunAsTrustedInstaller("cmd.exe", []string{"/c", "start", "cmd.exe"}); err != nil {
panic(err)
}
}