security.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build windows
  5. // +build windows
  6. package svc
  7. import (
  8. "path/filepath"
  9. "strings"
  10. "unsafe"
  11. "golang.org/x/sys/windows"
  12. )
  13. func allocSid(subAuth0 uint32) (*windows.SID, error) {
  14. var sid *windows.SID
  15. err := windows.AllocateAndInitializeSid(&windows.SECURITY_NT_AUTHORITY,
  16. 1, subAuth0, 0, 0, 0, 0, 0, 0, 0, &sid)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return sid, nil
  21. }
  22. // IsAnInteractiveSession determines if calling process is running interactively.
  23. // It queries the process token for membership in the Interactive group.
  24. // http://stackoverflow.com/questions/2668851/how-do-i-detect-that-my-application-is-running-as-service-or-in-an-interactive-s
  25. //
  26. // Deprecated: Use IsWindowsService instead.
  27. func IsAnInteractiveSession() (bool, error) {
  28. interSid, err := allocSid(windows.SECURITY_INTERACTIVE_RID)
  29. if err != nil {
  30. return false, err
  31. }
  32. defer windows.FreeSid(interSid)
  33. serviceSid, err := allocSid(windows.SECURITY_SERVICE_RID)
  34. if err != nil {
  35. return false, err
  36. }
  37. defer windows.FreeSid(serviceSid)
  38. t, err := windows.OpenCurrentProcessToken()
  39. if err != nil {
  40. return false, err
  41. }
  42. defer t.Close()
  43. gs, err := t.GetTokenGroups()
  44. if err != nil {
  45. return false, err
  46. }
  47. for _, g := range gs.AllGroups() {
  48. if windows.EqualSid(g.Sid, interSid) {
  49. return true, nil
  50. }
  51. if windows.EqualSid(g.Sid, serviceSid) {
  52. return false, nil
  53. }
  54. }
  55. return false, nil
  56. }
  57. // IsWindowsService reports whether the process is currently executing
  58. // as a Windows service.
  59. func IsWindowsService() (bool, error) {
  60. // The below technique looks a bit hairy, but it's actually
  61. // exactly what the .NET framework does for the similarly named function:
  62. // https://github.com/dotnet/extensions/blob/f4066026ca06984b07e90e61a6390ac38152ba93/src/Hosting/WindowsServices/src/WindowsServiceHelpers.cs#L26-L31
  63. // Specifically, it looks up whether the parent process has session ID zero
  64. // and is called "services".
  65. var pbi windows.PROCESS_BASIC_INFORMATION
  66. pbiLen := uint32(unsafe.Sizeof(pbi))
  67. err := windows.NtQueryInformationProcess(windows.CurrentProcess(), windows.ProcessBasicInformation, unsafe.Pointer(&pbi), pbiLen, &pbiLen)
  68. if err != nil {
  69. return false, err
  70. }
  71. var psid uint32
  72. err = windows.ProcessIdToSessionId(uint32(pbi.InheritedFromUniqueProcessId), &psid)
  73. if err != nil || psid != 0 {
  74. return false, nil
  75. }
  76. pproc, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pbi.InheritedFromUniqueProcessId))
  77. if err != nil {
  78. return false, err
  79. }
  80. defer windows.CloseHandle(pproc)
  81. var exeNameBuf [261]uint16
  82. exeNameLen := uint32(len(exeNameBuf) - 1)
  83. err = windows.QueryFullProcessImageName(pproc, 0, &exeNameBuf[0], &exeNameLen)
  84. if err != nil {
  85. return false, err
  86. }
  87. exeName := windows.UTF16ToString(exeNameBuf[:exeNameLen])
  88. if !strings.EqualFold(filepath.Base(exeName), "services.exe") {
  89. return false, nil
  90. }
  91. system32, err := windows.GetSystemDirectory()
  92. if err != nil {
  93. return false, err
  94. }
  95. targetExeName := filepath.Join(system32, "services.exe")
  96. return strings.EqualFold(exeName, targetExeName), nil
  97. }