-
Notifications
You must be signed in to change notification settings - Fork 13
/
gettoken.go
136 lines (114 loc) · 4.4 KB
/
gettoken.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package wintoken
import (
"fmt"
"unsafe"
"golang.org/x/sys/windows"
)
const (
WTS_CURRENT_SERVER_HANDLE windows.Handle = 0
)
//OpenProcessToken opens a process token using PID, pass 0 as PID for self token
func OpenProcessToken(pid int, tokenType tokenType) (*Token, error) {
var (
t windows.Token
duplicatedToken windows.Token
procHandle windows.Handle
err error
)
if pid == 0 {
procHandle = windows.CurrentProcess()
} else {
procHandle, err = windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, uint32(pid))
}
if err != nil {
return nil, err
}
if err = windows.OpenProcessToken(procHandle, windows.TOKEN_ALL_ACCESS, &t); err != nil {
return nil, err
}
defer windows.CloseHandle(windows.Handle(t))
switch tokenType {
case TokenPrimary:
if err := windows.DuplicateTokenEx(t, windows.MAXIMUM_ALLOWED, nil, windows.SecurityDelegation, windows.TokenPrimary, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
case TokenImpersonation:
if err := windows.DuplicateTokenEx(t, windows.MAXIMUM_ALLOWED, nil, windows.SecurityImpersonation, windows.TokenImpersonation, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
case TokenLinked:
if err := windows.DuplicateTokenEx(t, windows.MAXIMUM_ALLOWED, nil, windows.SecurityDelegation, windows.TokenPrimary, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
dt, err := duplicatedToken.GetLinkedToken()
windows.CloseHandle(windows.Handle(duplicatedToken))
if err != nil {
return nil, fmt.Errorf("error while getting LinkedToken: %w", err)
}
duplicatedToken = dt
}
return &Token{token: duplicatedToken, typ: tokenType}, nil
}
//GetInteractiveToken gets the interactive token associated with current logged in user
//It uses windows API WTSEnumerateSessions, WTSQueryUserToken and DuplicateTokenEx to return a valid wintoken
func GetInteractiveToken(tokenType tokenType) (*Token, error) {
switch tokenType {
case TokenPrimary, TokenImpersonation, TokenLinked:
default:
return nil, ErrOnlyPrimaryImpersonationTokenAllowed
}
var (
sessionPointer uintptr
sessionCount uint32
interactiveToken windows.Token
duplicatedToken windows.Token
sessionID uint32
)
err := windows.WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, (**windows.WTS_SESSION_INFO)(unsafe.Pointer(&sessionPointer)), &sessionCount)
if err != nil {
return nil, fmt.Errorf("error while enumerating sessions: %v", err)
}
defer windows.WTSFreeMemory(sessionPointer)
sessions := make([]*windows.WTS_SESSION_INFO, sessionCount)
size := unsafe.Sizeof(windows.WTS_SESSION_INFO{})
for i := range sessions {
sessions[i] = (*windows.WTS_SESSION_INFO)(unsafe.Pointer(sessionPointer + (size * uintptr(i))))
}
for i := range sessions {
if sessions[i].State == windows.WTSActive {
sessionID = sessions[i].SessionID
break
}
}
if sessionID == 0 {
return nil, ErrNoActiveSession
}
if err := windows.WTSQueryUserToken(sessionID, &interactiveToken); err != nil {
return nil, fmt.Errorf("error while WTSQueryUserToken: %w", err)
}
defer windows.CloseHandle(windows.Handle(interactiveToken))
switch tokenType {
case TokenPrimary:
if err := windows.DuplicateTokenEx(interactiveToken, windows.MAXIMUM_ALLOWED, nil, windows.SecurityDelegation, windows.TokenPrimary, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
case TokenImpersonation:
if err := windows.DuplicateTokenEx(interactiveToken, windows.MAXIMUM_ALLOWED, nil, windows.SecurityImpersonation, windows.TokenImpersonation, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
case TokenLinked:
if err := windows.DuplicateTokenEx(interactiveToken, windows.MAXIMUM_ALLOWED, nil, windows.SecurityDelegation, windows.TokenPrimary, &duplicatedToken); err != nil {
return nil, fmt.Errorf("error while DuplicateTokenEx: %w", err)
}
dt, err := duplicatedToken.GetLinkedToken()
windows.CloseHandle(windows.Handle(duplicatedToken))
if err != nil {
return nil, fmt.Errorf("error while getting LinkedToken: %w", err)
}
duplicatedToken = dt
}
if windows.Handle(duplicatedToken) == windows.InvalidHandle {
return nil, ErrInvalidDuplicatedToken
}
return &Token{typ: tokenType, token: duplicatedToken}, nil
}