security.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "strings"
  9. "unsafe"
  10. "golang.org/x/sys/windows"
  11. )
  12. func allocSid(subAuth0 uint32) (*windows.SID, error) {
  13. var sid *windows.SID
  14. err := windows.AllocateAndInitializeSid(&windows.SECURITY_NT_AUTHORITY,
  15. 1, subAuth0, 0, 0, 0, 0, 0, 0, 0, &sid)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return sid, nil
  20. }
  21. // IsAnInteractiveSession determines if calling process is running interactively.
  22. // It queries the process token for membership in the Interactive group.
  23. // http://stackoverflow.com/questions/2668851/how-do-i-detect-that-my-application-is-running-as-service-or-in-an-interactive-s
  24. //
  25. // Deprecated: Use IsWindowsService instead.
  26. func IsAnInteractiveSession() (bool, error) {
  27. interSid, err := allocSid(windows.SECURITY_INTERACTIVE_RID)
  28. if err != nil {
  29. return false, err
  30. }
  31. defer windows.FreeSid(interSid)
  32. serviceSid, err := allocSid(windows.SECURITY_SERVICE_RID)
  33. if err != nil {
  34. return false, err
  35. }
  36. defer windows.FreeSid(serviceSid)
  37. t, err := windows.OpenCurrentProcessToken()
  38. if err != nil {
  39. return false, err
  40. }
  41. defer t.Close()
  42. gs, err := t.GetTokenGroups()
  43. if err != nil {
  44. return false, err
  45. }
  46. for _, g := range gs.AllGroups() {
  47. if windows.EqualSid(g.Sid, interSid) {
  48. return true, nil
  49. }
  50. if windows.EqualSid(g.Sid, serviceSid) {
  51. return false, nil
  52. }
  53. }
  54. return false, nil
  55. }
  56. // IsWindowsService reports whether the process is currently executing
  57. // as a Windows service.
  58. func IsWindowsService() (bool, error) {
  59. // The below technique looks a bit hairy, but it's actually
  60. // exactly what the .NET framework does for the similarly named function:
  61. // https://github.com/dotnet/extensions/blob/f4066026ca06984b07e90e61a6390ac38152ba93/src/Hosting/WindowsServices/src/WindowsServiceHelpers.cs#L26-L31
  62. // Specifically, it looks up whether the parent process has session ID zero
  63. // and is called "services".
  64. var currentProcess windows.PROCESS_BASIC_INFORMATION
  65. infoSize := uint32(unsafe.Sizeof(currentProcess))
  66. err := windows.NtQueryInformationProcess(windows.CurrentProcess(), windows.ProcessBasicInformation, unsafe.Pointer(&currentProcess), infoSize, &infoSize)
  67. if err != nil {
  68. return false, err
  69. }
  70. var parentProcess *windows.SYSTEM_PROCESS_INFORMATION
  71. for infoSize = uint32((unsafe.Sizeof(*parentProcess) + unsafe.Sizeof(uintptr(0))) * 1024); ; {
  72. parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(&make([]byte, infoSize)[0]))
  73. err = windows.NtQuerySystemInformation(windows.SystemProcessInformation, unsafe.Pointer(parentProcess), infoSize, &infoSize)
  74. if err == nil {
  75. break
  76. } else if err != windows.STATUS_INFO_LENGTH_MISMATCH {
  77. return false, err
  78. }
  79. }
  80. for ; ; parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(uintptr(unsafe.Pointer(parentProcess)) + uintptr(parentProcess.NextEntryOffset))) {
  81. if parentProcess.UniqueProcessID == currentProcess.InheritedFromUniqueProcessId {
  82. return parentProcess.SessionID == 0 && strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil
  83. }
  84. if parentProcess.NextEntryOffset == 0 {
  85. break
  86. }
  87. }
  88. return false, nil
  89. }