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

Function AddComputerAccount

Commands/AddComputer.go:13–54  ·  view source on GitHub ↗
(username string, baseDN string, conn *ldap.Conn)

Source from the content-addressed store, hash-verified

11)
12
13func AddComputerAccount(username string, baseDN string, conn *ldap.Conn) (queryResult string) {
14 password, err := random.String(15)
15 if err != nil {
16 fmt.Println(err)
17 }
18
19 utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
20 pwdEncoded, err := utf16.NewEncoder().String(fmt.Sprintf("%q", password))
21 if err != nil {
22 log.Fatal(err)
23 }
24
25 WORKSTATION_TRUST_ACCOUNT := fmt.Sprintf("%d", 0x1000)
26
27 //set vars for request
28 usernameMinusDollar := username[:len(username)-1]
29
30 dn := fmt.Sprintf("CN=%s,CN=Computers,%s", username, baseDN)
31 spn1 := fmt.Sprintf("HOST/%s", usernameMinusDollar)
32 spn2 := fmt.Sprintf("RestrictedKrbHost/%s", usernameMinusDollar)
33 domain := baseDN
34 domain = strings.Replace(domain, "DC=", "", -1)
35 domain = strings.Replace(domain, ",", ".", -1)
36 domain = strings.ToLower(domain)
37 dnsHostName := fmt.Sprintf("%s.%s", usernameMinusDollar, domain)
38
39 //create add computer request
40 addReq := ldap.NewAddRequest(dn, []ldap.Control{})
41 addReq.Attribute("objectClass", []string{"top", "organizationalPerson", "user", "computer"})
42 addReq.Attribute("sAMAccountName", []string{username})
43 addReq.Attribute("userAccountControl", []string{WORKSTATION_TRUST_ACCOUNT})
44 addReq.Attribute("unicodePwd", []string{pwdEncoded})
45 addReq.Attribute("servicePrincipalName", []string{spn1, spn2})
46 addReq.Attribute("dnsHostName", []string{dnsHostName})
47
48 //send request
49 if err := conn.Add(addReq); err != nil {
50 return fmt.Sprintf("Error Adding Service: %s", err)
51 }
52
53 return fmt.Sprintf("Successfully created computer account \"%s\" with password \"%s\"\n", username, password)
54}

Callers 1

mainFunction · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected