Пример #1
0
        private ADObject GetParent(string distinguishedname)
        {
            try
            {
                // Find the domain the object belongs to
                KeyValuePair <string, ADObject> domain = forest.Where(d => distinguishedname.EndsWith(d.Key)).OrderByDescending(d => d.Key.Length).First();

                if (domain.Key != null)
                {
                    string[] oupath = Regex.Split(distinguishedname.Replace("," + domain.Key, ""), @"(?<!\\),");
                    ADObject parent = domain.Value;
                    if (oupath.Length > 1)
                    {
                        for (int i = oupath.Length - 1; i > 0; i--)
                        {
                            bool parentFound = false;
                            foreach (KeyValuePair <string, ADObject> container in parent.GetOUChildren())
                            {
                                if (oupath[i].Equals(container.Key))
                                {
                                    parent      = container.Value;
                                    parentFound = true;
                                    break;
                                }
                            }

                            // Containers are missing in BloodHound so they have to be created manually
                            if (!parentFound)
                            {
                                string containerDistinguishedname = oupath[i] + "," + parent.Distinguishedname;
                                string objectId = "container-" + containerDistinguishedname;
                                string cn       = oupath[i].Replace("CN=", "");
                                string name     = (cn + "@" + domain.Value.Name).ToUpper();
                                string tier     = DefaultTieringConstants.DefaultTierNumber.ToString();

                                // Create as OU in application data
                                ADObject adContainer = new ADObject(objectId, ADObjectType.OU, cn, name, containerDistinguishedname, tier, this);
                                idLookupTable.Add((string)objectId, adContainer);
                                parent.Children.Add(oupath[i], adContainer);
                                parent = adContainer;

                                // Create as OU in DB
                                CreateADObjectInDB(objectId, ADObjectType.OU, name, containerDistinguishedname, domain.Value.Name, tier);
                            }
                        }
                    }

                    return(parent);
                }
            }
            catch
            {
                throw;
            }

            throw new Exception("Error: Could not find ADObjects OU/Domain parent");
        }