Debian Codegraph @

pac/kerb_validation_info.go

raw symbols SCIP
1 // Package pac implements Microsoft Privilege Attribute Certificate (PAC) processing.
2 package pac
3
4 import (
5 "errors"
6 "fmt"
7
8 "gopkg.in/jcmturner/gokrb5.v5/mstypes"
9 "gopkg.in/jcmturner/rpc.v0/ndr"
10 )
11
12 // KERB_VALIDATION_INFO flags.
13 const (
14 USERFLAG_GUEST = 31 // Authentication was done via the GUEST account; no password was used.
15 USERFLAG_NO_ENCRYPTION_AVAILABLE = 30 // No encryption is available.
16 USERFLAG_LAN_MANAGER_KEY = 28 // LAN Manager key was used for authentication.
17 USERFLAG_SUB_AUTH = 25 // Sub-authentication used; session key came from the sub-authentication package.
18 USERFLAG_EXTRA_SIDS = 26 // Indicates that the ExtraSids field is populated and contains additional SIDs.
19 USERFLAG_MACHINE_ACCOUNT = 24 // Indicates that the account is a machine account.
20 USERFLAG_DC_NTLM2 = 23 // Indicates that the domain controller understands NTLMv2.
21 USERFLAG_RESOURCE_GROUPIDS = 22 // Indicates that the ResourceGroupIds field is populated.
22 USERFLAG_PROFILEPATH = 21 // Indicates that ProfilePath is populated.
23 USERFLAG_NTLM2_NTCHALLENGERESP = 20 // The NTLMv2 response from the NtChallengeResponseFields ([MS-NLMP] section 2.2.1.3) was used for authentication and session key generation.
24 USERFLAG_LM2_LMCHALLENGERESP = 19 // The LMv2 response from the LmChallengeResponseFields ([MS-NLMP] section 2.2.1.3) was used for authentication and session key generation.
25 USERFLAG_AUTH_LMCHALLENGERESP_KEY_NTCHALLENGERESP = 18 // The LMv2 response from the LmChallengeResponseFields ([MS-NLMP] section 2.2.1.3) was used for authentication and the NTLMv2 response from the NtChallengeResponseFields ([MS-NLMP] section 2.2.1.3) was used session key generation.
26 )
27
28 // KerbValidationInfo implement https://msdn.microsoft.com/en-us/library/cc237948.aspx
29 type KerbValidationInfo struct {
30 LogOnTime mstypes.FileTime
31 LogOffTime mstypes.FileTime
32 KickOffTime mstypes.FileTime
33 PasswordLastSet mstypes.FileTime
34 PasswordCanChange mstypes.FileTime
35 PasswordMustChange mstypes.FileTime
36 EffectiveName mstypes.RPCUnicodeString
37 FullName mstypes.RPCUnicodeString
38 LogonScript mstypes.RPCUnicodeString
39 ProfilePath mstypes.RPCUnicodeString
40 HomeDirectory mstypes.RPCUnicodeString
41 HomeDirectoryDrive mstypes.RPCUnicodeString
42 LogonCount uint16
43 BadPasswordCount uint16
44 UserID uint32
45 PrimaryGroupID uint32
46 GroupCount uint32
47 pGroupIDs uint32
48 GroupIDs []mstypes.GroupMembership
49 UserFlags uint32
50 UserSessionKey mstypes.UserSessionKey
51 LogonServer mstypes.RPCUnicodeString
52 LogonDomainName mstypes.RPCUnicodeString
53 pLogonDomainID uint32
54 LogonDomainID mstypes.RPCSID
55 Reserved1 []uint32 // Has 2 elements
56 UserAccountControl uint32
57 SubAuthStatus uint32
58 LastSuccessfulILogon mstypes.FileTime
59 LastFailedILogon mstypes.FileTime
60 FailedILogonCount uint32
61 Reserved3 uint32
62 SIDCount uint32
63 pExtraSIDs uint32
64 ExtraSIDs []mstypes.KerbSidAndAttributes
65 pResourceGroupDomainSID uint32
66 ResourceGroupDomainSID mstypes.RPCSID
67 ResourceGroupCount uint32
68 pResourceGroupIDs uint32
69 ResourceGroupIDs []mstypes.GroupMembership
70 }
71
72 // Unmarshal bytes into the DeviceInfo struct
73 func (k *KerbValidationInfo) Unmarshal(b []byte) (err error) {
74 ch, _, p, err := ndr.ReadHeaders(&b)
75 if err != nil {
76 return fmt.Errorf("error parsing byte stream headers: %v", err)
77 }
78 e := &ch.Endianness
79
80 //The next 4 bytes are an RPC unique pointer referent. We just skip these
81 p += 4
82
83 k.LogOnTime = mstypes.ReadFileTime(&b, &p, e)
84 k.LogOffTime = mstypes.ReadFileTime(&b, &p, e)
85 k.KickOffTime = mstypes.ReadFileTime(&b, &p, e)
86 k.PasswordLastSet = mstypes.ReadFileTime(&b, &p, e)
87 k.PasswordCanChange = mstypes.ReadFileTime(&b, &p, e)
88 k.PasswordMustChange = mstypes.ReadFileTime(&b, &p, e)
89
90 if k.EffectiveName, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
91 return
92 }
93 if k.FullName, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
94 return
95 }
96 if k.LogonScript, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
97 return
98 }
99 if k.ProfilePath, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
100 return
101 }
102 if k.HomeDirectory, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
103 return
104 }
105 if k.HomeDirectoryDrive, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
106 return
107 }
108
109 k.LogonCount = ndr.ReadUint16(&b, &p, e)
110 k.BadPasswordCount = ndr.ReadUint16(&b, &p, e)
111 k.UserID = ndr.ReadUint32(&b, &p, e)
112 k.PrimaryGroupID = ndr.ReadUint32(&b, &p, e)
113 k.GroupCount = ndr.ReadUint32(&b, &p, e)
114 k.pGroupIDs = ndr.ReadUint32(&b, &p, e)
115
116 k.UserFlags = ndr.ReadUint32(&b, &p, e)
117 k.UserSessionKey = mstypes.ReadUserSessionKey(&b, &p, e)
118
119 if k.LogonServer, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
120 return
121 }
122 if k.LogonDomainName, err = mstypes.ReadRPCUnicodeString(&b, &p, e); err != nil {
123 return
124 }
125
126 k.pLogonDomainID = ndr.ReadUint32(&b, &p, e)
127
128 k.Reserved1 = []uint32{
129 ndr.ReadUint32(&b, &p, e),
130 ndr.ReadUint32(&b, &p, e),
131 }
132
133 k.UserAccountControl = ndr.ReadUint32(&b, &p, e)
134 k.SubAuthStatus = ndr.ReadUint32(&b, &p, e)
135 k.LastSuccessfulILogon = mstypes.ReadFileTime(&b, &p, e)
136 k.LastFailedILogon = mstypes.ReadFileTime(&b, &p, e)
137 k.FailedILogonCount = ndr.ReadUint32(&b, &p, e)
138 k.Reserved3 = ndr.ReadUint32(&b, &p, e)
139
140 k.SIDCount = ndr.ReadUint32(&b, &p, e)
141 k.pExtraSIDs = ndr.ReadUint32(&b, &p, e)
142
143 k.pResourceGroupDomainSID = ndr.ReadUint32(&b, &p, e)
144 k.ResourceGroupCount = ndr.ReadUint32(&b, &p, e)
145 k.pResourceGroupIDs = ndr.ReadUint32(&b, &p, e)
146
147 // Populate pointers
148 if err = k.EffectiveName.UnmarshalString(&b, &p, e); err != nil {
149 return
150 }
151 if err = k.FullName.UnmarshalString(&b, &p, e); err != nil {
152 return
153 }
154 if err = k.LogonScript.UnmarshalString(&b, &p, e); err != nil {
155 return
156 }
157 if err = k.ProfilePath.UnmarshalString(&b, &p, e); err != nil {
158 return
159 }
160 if err = k.HomeDirectory.UnmarshalString(&b, &p, e); err != nil {
161 return
162 }
163 if err = k.HomeDirectoryDrive.UnmarshalString(&b, &p, e); err != nil {
164 return
165 }
166 var ah ndr.ConformantArrayHeader
167 if k.GroupCount > 0 {
168 ah, err = ndr.ReadUniDimensionalConformantArrayHeader(&b, &p, e)
169 if err != nil {
170 return
171 }
172 if ah.MaxCount != int(k.GroupCount) {
173 err = errors.New("error with size of group list")
174 return
175 }
176 g := make([]mstypes.GroupMembership, k.GroupCount, k.GroupCount)
177 for i := range g {
178 g[i] = mstypes.ReadGroupMembership(&b, &p, e)
179 }
180 k.GroupIDs = g
181 }
182
183 if err = k.LogonServer.UnmarshalString(&b, &p, e); err != nil {
184 return
185 }
186 if err = k.LogonDomainName.UnmarshalString(&b, &p, e); err != nil {
187 return
188 }
189
190 if k.pLogonDomainID != 0 {
191 k.LogonDomainID, err = mstypes.ReadRPCSID(&b, &p, e)
192 if err != nil {
193 return fmt.Errorf("error reading LogonDomainID: %v", err)
194 }
195 }
196
197 if k.SIDCount > 0 {
198 ah, err = ndr.ReadUniDimensionalConformantArrayHeader(&b, &p, e)
199 if err != nil {
200 return
201 }
202 if ah.MaxCount != int(k.SIDCount) {
203 return fmt.Errorf("error with size of ExtraSIDs list. Expected: %d, Actual: %d", k.SIDCount, ah.MaxCount)
204 }
205 es := make([]mstypes.KerbSidAndAttributes, k.SIDCount, k.SIDCount)
206 attr := make([]uint32, k.SIDCount, k.SIDCount)
207 ptr := make([]uint32, k.SIDCount, k.SIDCount)
208 for i := range attr {
209 ptr[i] = ndr.ReadUint32(&b, &p, e)
210 attr[i] = ndr.ReadUint32(&b, &p, e)
211 }
212 for i := range es {
213 if ptr[i] != 0 {
214 s, err := mstypes.ReadRPCSID(&b, &p, e)
215 es[i] = mstypes.KerbSidAndAttributes{SID: s, Attributes: attr[i]}
216 if err != nil {
217 return ndr.Malformed{EText: fmt.Sprintf("could not read ExtraSIDs: %v", err)}
218 }
219 }
220 }
221 k.ExtraSIDs = es
222 }
223
224 if k.pResourceGroupDomainSID != 0 {
225 k.ResourceGroupDomainSID, err = mstypes.ReadRPCSID(&b, &p, e)
226 if err != nil {
227 return err
228 }
229 }
230
231 if k.ResourceGroupCount > 0 {
232 ah, err = ndr.ReadUniDimensionalConformantArrayHeader(&b, &p, e)
233 if err != nil {
234 return
235 }
236 if ah.MaxCount != int(k.ResourceGroupCount) {
237 return fmt.Errorf("error with size of ResourceGroup list. Expected: %d, Actual: %d", k.ResourceGroupCount, ah.MaxCount)
238 }
239 g := make([]mstypes.GroupMembership, k.ResourceGroupCount, k.ResourceGroupCount)
240 for i := range g {
241 g[i] = mstypes.ReadGroupMembership(&b, &p, e)
242 }
243 k.ResourceGroupIDs = g
244 }
245
246 //Check that there is only zero padding left
247 if len(b) >= p {
248 for _, v := range b[p:] {
249 if v != 0 {
250 return ndr.Malformed{EText: "non-zero padding left over at end of data stream"}
251 }
252 }
253 }
254
255 return nil
256 }
257
258 // GetGroupMembershipSIDs returns a slice of strings containing the group membership SIDs found in the PAC.
259 func (k *KerbValidationInfo) GetGroupMembershipSIDs() []string {
260 var g []string
261 lSID := k.LogonDomainID.ToString()
262 for i := range k.GroupIDs {
263 g = append(g, fmt.Sprintf("%s-%d", lSID, k.GroupIDs[i].RelativeID))
264 }
265 for _, s := range k.ExtraSIDs {
266 var exists = false
267 for _, es := range g {
268 if es == s.SID.ToString() {
269 exists = true
270 break
271 }
272 }
273 if !exists {
274 g = append(g, s.SID.ToString())
275 }
276 }
277 for _, r := range k.ResourceGroupIDs {
278 var exists = false
279 s := fmt.Sprintf("%s-%d", k.ResourceGroupDomainSID.ToString(), r.RelativeID)
280 for _, es := range g {
281 if es == s {
282 exists = true
283 break
284 }
285 }
286 if !exists {
287 g = append(g, s)
288 }
289 }
290 return g
291 }