Exemplo n.º 1
0
        private Task StartCompListRunner(BlockingCollection <Wrapper <string> > input,
                                         BlockingCollection <Wrapper <OutputBase> > output)
        {
            return(Task.Factory.StartNew(() =>
            {
                foreach (var wrapper in input.GetConsumingEnumerable())
                {
                    var item = wrapper.Item;

                    var resolved = _utils.ResolveHost(item);

                    if (!_utils.PingHost(resolved))
                    {
                        Interlocked.Increment(ref _currentCount);
                        Interlocked.Increment(ref _noPing);
                        continue;
                    }

                    var netbios = Utils.GetComputerNetbiosName(resolved);

                    var fullItem = new ResolvedEntry
                    {
                        BloodHoundDisplay = resolved,
                        ComputerSamAccountName = netbios
                    };

                    var c = _options.CollectMethod;

                    if (c.Equals(CollectionMethod.Session) ||
                        c.Equals(CollectionMethod.SessionLoop) ||
                        c.Equals(CollectionMethod.ComputerOnly))
                    {
                        try
                        {
                            var sessions = SessionHelpers.GetNetSessions(fullItem, _currentDomain);
                            foreach (var session in sessions)
                            {
                                output.Add(new Wrapper <OutputBase> {
                                    Item = session
                                });
                            }
                        }
                        catch (TimeoutException)
                        {
                            Interlocked.Increment(ref _timeouts);
                        }
                    }

                    if (c.Equals(CollectionMethod.LocalGroup) || c.Equals(CollectionMethod.ComputerOnly))
                    {
                        try
                        {
                            var admins = LocalAdminHelpers.GetSamAdmins(fullItem);
                            foreach (var admin in admins)
                            {
                                output.Add(new Wrapper <OutputBase> {
                                    Item = admin
                                });
                            }
                        }
                        catch (TimeoutException)
                        {
                            Interlocked.Increment(ref _timeouts);
                        }
                    }

                    if (c.Equals(CollectionMethod.LoggedOn))
                    {
                        var sessions = SessionHelpers.GetNetLoggedOn(fullItem, _currentDomain);
                        sessions = sessions.Concat(SessionHelpers.GetRegistryLoggedOn(fullItem));

                        foreach (var session in sessions)
                        {
                            output.Add(new Wrapper <OutputBase> {
                                Item = session
                            });
                        }
                    }
                    Interlocked.Increment(ref _currentCount);
                    wrapper.Item = null;
                }
            }));
        }
Exemplo n.º 2
0
        private Task StartRunner(BlockingCollection <Wrapper <SearchResultEntry> > processQueue, BlockingCollection <Wrapper <OutputBase> > writeQueue)
        {
            return(Task.Factory.StartNew(() =>
            {
                foreach (var wrapper in processQueue.GetConsumingEnumerable())
                {
                    var entry = wrapper.Item;

                    var resolved = entry.ResolveAdEntry();

                    if (resolved == null)
                    {
                        Interlocked.Increment(ref _currentCount);
                        wrapper.Item = null;
                        continue;
                    }

                    switch (_options.CollectMethod)
                    {
                    case CollectionMethod.ObjectProps:
                        {
                            OutputBase props;
                            if (resolved.ObjectType.Equals("computer"))
                            {
                                props = ObjectPropertyHelpers.GetComputerProps(entry, resolved);
                            }
                            else
                            {
                                props = ObjectPropertyHelpers.GetUserProps(entry, resolved);
                            }

                            if (props != null)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = props
                                });
                            }
                        }
                        break;

                    case CollectionMethod.Group:
                        {
                            var groups = GroupHelpers.ProcessAdObject(entry, resolved, _currentDomainSid);
                            foreach (var g in groups)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = g
                                });
                            }
                        }
                        break;

                    case CollectionMethod.ComputerOnly:
                        {
                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }

                            try
                            {
                                var admins = LocalAdminHelpers.GetSamAdmins(resolved);

                                foreach (var admin in admins)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = admin
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }

                            if (_options.ExcludeDC && entry.DistinguishedName.Contains("OU=Domain Controllers"))
                            {
                                break;
                            }

                            try
                            {
                                var sessions = SessionHelpers.GetNetSessions(resolved, _currentDomain);

                                foreach (var session in sessions)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = session
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }
                        }
                        break;

                    case CollectionMethod.LocalGroup:
                        {
                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }

                            try
                            {
                                var admins = LocalAdminHelpers.GetSamAdmins(resolved);

                                foreach (var admin in admins)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = admin
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }
                        }
                        break;

                    case CollectionMethod.GPOLocalGroup:
                        foreach (var admin in LocalAdminHelpers.GetGpoAdmins(entry, _currentDomain))
                        {
                            writeQueue.Add(new Wrapper <OutputBase> {
                                Item = admin
                            });
                        }
                        break;

                    case CollectionMethod.Session:
                        {
                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }

                            if (_options.ExcludeDC && entry.DistinguishedName.Contains("OU=Domain Controllers"))
                            {
                                break;
                            }

                            try
                            {
                                var sessions = SessionHelpers.GetNetSessions(resolved, _currentDomain);

                                foreach (var session in sessions)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = session
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }
                        }
                        break;

                    case CollectionMethod.LoggedOn:
                        {
                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }
                            var sessions =
                                SessionHelpers.GetNetLoggedOn(resolved,
                                                              _currentDomain);

                            foreach (var s in sessions)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = s
                                });
                            }
                            sessions = SessionHelpers.GetRegistryLoggedOn(resolved);
                            foreach (var s in sessions)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = s
                                });
                            }
                        }
                        break;

                    case CollectionMethod.Trusts:
                        break;

                    case CollectionMethod.ACL:
                        {
                            var acls = AclHelpers.ProcessAdObject(entry, _currentDomain);
                            foreach (var a in acls)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = a
                                });
                            }
                        }
                        break;

                    case CollectionMethod.SessionLoop:
                        {
                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }

                            if (_options.ExcludeDC && entry.DistinguishedName.Contains("OU=Domain Controllers"))
                            {
                                break;
                            }

                            try
                            {
                                var sessions = SessionHelpers.GetNetSessions(resolved, _currentDomain);

                                foreach (var session in sessions)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = session
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }
                        }
                        break;

                    case CollectionMethod.Default:
                        {
                            var groups = GroupHelpers.ProcessAdObject(entry, resolved, _currentDomainSid);
                            foreach (var g in groups)
                            {
                                writeQueue.Add(new Wrapper <OutputBase> {
                                    Item = g
                                });
                            }

                            if (!resolved.ObjectType.Equals("computer"))
                            {
                                break;
                            }

                            if (_options.Ou != null && !entry.DistinguishedName.Contains(_options.Ou))
                            {
                                break;
                            }

                            if (!_utils.PingHost(resolved.BloodHoundDisplay))
                            {
                                Interlocked.Increment(ref _noPing);
                                break;
                            }

                            try
                            {
                                var admins = LocalAdminHelpers.GetSamAdmins(resolved);

                                foreach (var admin in admins)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = admin
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }

                            if (_options.ExcludeDC && entry.DistinguishedName.Contains("OU=Domain Controllers"))
                            {
                                break;
                            }

                            try
                            {
                                var sessions = SessionHelpers.GetNetSessions(resolved, _currentDomain);

                                foreach (var session in sessions)
                                {
                                    writeQueue.Add(new Wrapper <OutputBase> {
                                        Item = session
                                    });
                                }
                            }
                            catch (TimeoutException)
                            {
                                Interlocked.Increment(ref _timeouts);
                            }
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    Interlocked.Increment(ref _currentCount);
                    wrapper.Item = null;
                }
            }, TaskCreationOptions.LongRunning));
        }