...
1
2
3
4
5
6
7 package svc
8
9 import (
10 "strings"
11 "unsafe"
12
13 "golang.org/x/sys/windows"
14 )
15
16 func allocSid(subAuth0 uint32) (*windows.SID, error) {
17 var sid *windows.SID
18 err := windows.AllocateAndInitializeSid(&windows.SECURITY_NT_AUTHORITY,
19 1, subAuth0, 0, 0, 0, 0, 0, 0, 0, &sid)
20 if err != nil {
21 return nil, err
22 }
23 return sid, nil
24 }
25
26
27
28
29
30
31 func IsAnInteractiveSession() (bool, error) {
32 interSid, err := allocSid(windows.SECURITY_INTERACTIVE_RID)
33 if err != nil {
34 return false, err
35 }
36 defer windows.FreeSid(interSid)
37
38 serviceSid, err := allocSid(windows.SECURITY_SERVICE_RID)
39 if err != nil {
40 return false, err
41 }
42 defer windows.FreeSid(serviceSid)
43
44 t, err := windows.OpenCurrentProcessToken()
45 if err != nil {
46 return false, err
47 }
48 defer t.Close()
49
50 gs, err := t.GetTokenGroups()
51 if err != nil {
52 return false, err
53 }
54
55 for _, g := range gs.AllGroups() {
56 if windows.EqualSid(g.Sid, interSid) {
57 return true, nil
58 }
59 if windows.EqualSid(g.Sid, serviceSid) {
60 return false, nil
61 }
62 }
63 return false, nil
64 }
65
66
67
68 func IsWindowsService() (bool, error) {
69
70
71
72
73
74
75 var currentProcess windows.PROCESS_BASIC_INFORMATION
76 infoSize := uint32(unsafe.Sizeof(currentProcess))
77 err := windows.NtQueryInformationProcess(windows.CurrentProcess(), windows.ProcessBasicInformation, unsafe.Pointer(¤tProcess), infoSize, &infoSize)
78 if err != nil {
79 return false, err
80 }
81 var parentProcess *windows.SYSTEM_PROCESS_INFORMATION
82 for infoSize = uint32((unsafe.Sizeof(*parentProcess) + unsafe.Sizeof(uintptr(0))) * 1024); ; {
83 parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(&make([]byte, infoSize)[0]))
84 err = windows.NtQuerySystemInformation(windows.SystemProcessInformation, unsafe.Pointer(parentProcess), infoSize, &infoSize)
85 if err == nil {
86 break
87 } else if err != windows.STATUS_INFO_LENGTH_MISMATCH {
88 return false, err
89 }
90 }
91 for ; ; parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(uintptr(unsafe.Pointer(parentProcess)) + uintptr(parentProcess.NextEntryOffset))) {
92 if parentProcess.UniqueProcessID == currentProcess.InheritedFromUniqueProcessId {
93 return parentProcess.SessionID == 0 && strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil
94 }
95 if parentProcess.NextEntryOffset == 0 {
96 break
97 }
98 }
99 return false, nil
100 }
101
View as plain text