Skip to content

Commit

Permalink
[process][windows] Fix #466 add SeDebugPrivilege to current process
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomanic committed Dec 22, 2018
1 parent a5ace91 commit 4104adf
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const (
var (
modpsapi = windows.NewLazySystemDLL("psapi.dll")
procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo")

advapi32 = windows.NewLazySystemDLL("advapi32.dll")
procLookupPrivilegeValue = advapi32.NewProc("LookupPrivilegeValueW")
procAdjustTokenPrivileges = advapi32.NewProc("AdjustTokenPrivileges")
)

type SystemProcessInformation struct {
Expand Down Expand Up @@ -90,8 +94,61 @@ type Win32_Process struct {
WorkingSetSize uint64
}

type winLUID struct {
LowPart winDWord
HighPart winLong
}

// LUID_AND_ATTRIBUTES
type winLUIDAndAttributes struct {
Luid winLUID
Attributes winDWord
}

// TOKEN_PRIVILEGES
type winTokenPriviledges struct {
PrivilegeCount winDWord
Privileges [1]winLUIDAndAttributes
}

type winLong int32
type winDWord uint32

func init() {
wmi.DefaultClient.AllowMissingFields = true

// enable SeDebugPrivilege https://github.com/midstar/proci/blob/6ec79f57b90ba3d9efa2a7b16ef9c9369d4be875/proci_windows.go#L80-L119
handle, err := syscall.GetCurrentProcess()
if err != nil {
return
}

var token syscall.Token
err = syscall.OpenProcessToken(handle, 0x0028, &token)
if err != nil {
return
}
defer token.Close()

tokenPriviledges := winTokenPriviledges{PrivilegeCount: 1}
lpName := syscall.StringToUTF16("SeDebugPrivilege")
ret, _, _ := procLookupPrivilegeValue.Call(
0,
uintptr(unsafe.Pointer(&lpName[0])),
uintptr(unsafe.Pointer(&tokenPriviledges.Privileges[0].Luid)))
if ret == 0 {
return
}

tokenPriviledges.Privileges[0].Attributes = 0x00000002 // SE_PRIVILEGE_ENABLED

procAdjustTokenPrivileges.Call(
uintptr(token),
0,
uintptr(unsafe.Pointer(&tokenPriviledges)),
uintptr(unsafe.Sizeof(tokenPriviledges)),
0,
0)
}

func Pids() ([]int32, error) {
Expand Down

0 comments on commit 4104adf

Please sign in to comment.