MCPcopy Create free account
hub / github.com/Synzack/ldapper / GetUserSPNs

Function GetUserSPNs

Queries/GetUserSPNs.go:11–60  ·  view source on GitHub ↗
(baseDN string, conn *ldap.Conn)

Source from the content-addressed store, hash-verified

9)
10
11func GetUserSPNs(baseDN string, conn *ldap.Conn) (queryResult string) {
12 query := "(&(servicePrincipalName=*)(UserAccountControl:1.2.840.113556.1.4.803:=512)(!(UserAccountControl:1.2.840.113556.1.4.803:=2))(!(objectCategory=computer)))"
13 searchReq := Globals.LdapSearch(baseDN, query)
14 result, err := conn.SearchWithPaging(searchReq, 10)
15
16 if err != nil {
17 fmt.Printf("Query error, %s", err)
18 }
19
20 // Build output header Row
21 queryResult = ("SPN\tUsername\tPasswordLastSet\tLastLogon\tDelegation\n")
22
23 // check if LDAPSearch returned any entries
24 if len(result.Entries) > 0 {
25 for ldapResult := range result.Entries {
26
27 username := result.Entries[ldapResult].GetAttributeValues("sAMAccountName")[0]
28
29 // Get Delegation Information
30 userAccountControl, _ := strconv.Atoi(result.Entries[ldapResult].GetAttributeValue("userAccountControl"))
31 delegationInfo := ""
32 if userAccountControl&0x00080000 > 0 {
33 delegationInfo = "unconstrained"
34 } else if userAccountControl&0x01000000 > 0 {
35 delegationInfo = "constrained"
36 }
37
38 //convert LDAP time for pwdLastSet
39 pwdLastSet, _ := strconv.Atoi(result.Entries[ldapResult].GetAttributeValue("pwdLastSet"))
40 pwdLastSetString := Globals.ConvertLDAPTime(pwdLastSet).String()
41
42 //Assume the account has never logged in
43 lastLogonString := "<never>"
44 // If the account has logged in convert the LDAP Time
45 lastLogon, _ := strconv.Atoi(result.Entries[ldapResult].GetAttributeValue("lastLogon"))
46 if lastLogon != 0 {
47 lastLogonString = Globals.ConvertLDAPTime(lastLogon).String()
48 }
49
50 // Get each SPN for the account
51 for spnResult := range result.Entries[ldapResult].GetAttributeValues("servicePrincipalName") {
52 spn := result.Entries[ldapResult].GetAttributeValues("servicePrincipalName")[spnResult]
53 queryResult += fmt.Sprintf("%s\t%s\t%s\t%s\t%s\n", spn, username, pwdLastSetString, lastLogonString, delegationInfo)
54 }
55
56 }
57 }
58
59 return queryResult
60}

Callers 1

mainFunction · 0.92

Calls 2

LdapSearchFunction · 0.92
ConvertLDAPTimeFunction · 0.92

Tested by

no test coverage detected