|
| 1 | +package ldap |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + ber "github.com/go-asn1-ber/asn1-ber" |
| 7 | + "github.com/go-ldap/ldap/v3" |
| 8 | +) |
| 9 | + |
| 10 | +type ControlMicrosoftSDFlags struct { |
| 11 | + Criticality bool |
| 12 | + ControlValue int32 |
| 13 | +} |
| 14 | + |
| 15 | +func (c *ControlMicrosoftSDFlags) GetControlType() string { |
| 16 | + // Source: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/3888c2b7-35b9-45b7-afeb-b772aa932dd0 |
| 17 | + return "1.2.840.113556.1.4.801" |
| 18 | +} |
| 19 | + |
| 20 | +func (c *ControlMicrosoftSDFlags) Encode() *ber.Packet { |
| 21 | + packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Control") |
| 22 | + packet.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "1.2.840.113556.1.4.801", "Control Type")) |
| 23 | + if c.Criticality { |
| 24 | + packet.AppendChild(ber.NewBoolean(ber.ClassUniversal, ber.TypePrimitive, ber.TagBoolean, c.Criticality, "Criticality")) |
| 25 | + } |
| 26 | + p2 := ber.Encode(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, nil, "Control Value(SDFlags)") |
| 27 | + seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "SDFlags") |
| 28 | + seq.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, c.ControlValue, "Flags")) |
| 29 | + p2.AppendChild(seq) |
| 30 | + packet.AppendChild(p2) |
| 31 | + return packet |
| 32 | +} |
| 33 | + |
| 34 | +func (c *ControlMicrosoftSDFlags) String() string { |
| 35 | + return fmt.Sprintf( |
| 36 | + "Control Type: %s (%q)", |
| 37 | + "SD Flags - Microsoft", |
| 38 | + "1.2.840.113556.1.4.801") |
| 39 | +} |
| 40 | + |
| 41 | +// NewControlMicrosoftSDFlags returns a ControlMicrosoftSDFlags control |
| 42 | +func NewControlMicrosoftSDFlags() *ControlMicrosoftSDFlags { |
| 43 | + return &ControlMicrosoftSDFlags{} |
| 44 | +} |
| 45 | + |
| 46 | +// GetNtSecurityDescriptorOf retrieves the NT Security Descriptor of an LDAP entry. |
| 47 | +// |
| 48 | +// Parameters: |
| 49 | +// - dn: A string representing the distinguished name (DN) of the LDAP entry to retrieve the NT Security Descriptor from. |
| 50 | +// |
| 51 | +// Returns: |
| 52 | +// - A string representing the NT Security Descriptor of the LDAP entry. |
| 53 | +// - An error if the search operation fails. |
| 54 | +func (s *Session) GetNtSecurityDescriptorOf(distinguishedName string) (string, error) { |
| 55 | + // Source: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/3888c2b7-35b9-45b7-afeb-b772aa932dd0 |
| 56 | + |
| 57 | + // Security Information constants for NT Security Descriptor flags |
| 58 | + const ( |
| 59 | + OWNER_SECURITY_INFORMATION = 0x1 // Owner identifier of the object |
| 60 | + GROUP_SECURITY_INFORMATION = 0x2 // Primary group identifier |
| 61 | + DACL_SECURITY_INFORMATION = 0x4 // Discretionary access control list (DACL) of the object |
| 62 | + SACL_SECURITY_INFORMATION = 0x8 // System access control list (SACL) of the object |
| 63 | + ) |
| 64 | + |
| 65 | + control := &ControlMicrosoftSDFlags{ |
| 66 | + Criticality: false, |
| 67 | + ControlValue: int32(OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION), |
| 68 | + } |
| 69 | + |
| 70 | + searchRequest := ldap.NewSearchRequest( |
| 71 | + distinguishedName, |
| 72 | + ldap.ScopeBaseObject, |
| 73 | + ldap.NeverDerefAliases, |
| 74 | + 0, |
| 75 | + 0, |
| 76 | + false, |
| 77 | + fmt.Sprintf("(distinguishedName=%s)", distinguishedName), |
| 78 | + []string{"nTSecurityDescriptor"}, |
| 79 | + []ldap.Control{control}, |
| 80 | + ) |
| 81 | + |
| 82 | + searchResult, err := s.connection.SearchWithPaging(searchRequest, 1000) |
| 83 | + if err != nil { |
| 84 | + return "", fmt.Errorf("error searching for nTSecurityDescriptor: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("len(searchResult.Entries): %d\n", len(searchResult.Entries)) |
| 88 | + |
| 89 | + ntsd := searchResult.Entries[0].GetEqualFoldRawAttributeValue("nTSecurityDescriptor") |
| 90 | + |
| 91 | + fmt.Printf("GetAttributeValue: %s\n", searchResult.Entries[0].GetAttributeValue("nTSecurityDescriptor")) |
| 92 | + fmt.Printf("GetAttributeValues: %s\n", searchResult.Entries[0].GetAttributeValues("nTSecurityDescriptor")) |
| 93 | + fmt.Printf("GetRawAttributeValue: %s\n", searchResult.Entries[0].GetRawAttributeValue("nTSecurityDescriptor")) |
| 94 | + fmt.Printf("GetRawAttributeValues: %s\n", searchResult.Entries[0].GetRawAttributeValues("nTSecurityDescriptor")) |
| 95 | + fmt.Printf("GetEqualFoldRawAttributeValue: %s\n", searchResult.Entries[0].GetEqualFoldRawAttributeValue("nTSecurityDescriptor")) |
| 96 | + fmt.Printf("GetEqualFoldRawAttributeValues: %s\n", searchResult.Entries[0].GetEqualFoldRawAttributeValues("nTSecurityDescriptor")) |
| 97 | + |
| 98 | + return string(ntsd), nil |
| 99 | +} |
0 commit comments