Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Stopwatch steptimer     = new Stopwatch();
            Stopwatch totaltimer    = new Stopwatch();
            string    _appdir       = AppDomain.CurrentDomain.BaseDirectory;
            string    neoconfigfile = _appdir + @"\config\neoconfig.json";
            string    configfile    = _appdir + @"\config\cmconfig.json";
            bool      batchmode     = false;
            string    scanid        = ShortGuid.NewGuid().ToString();
            string    scannerid     = string.Empty;

            IDriver driver = null;

            totaltimer.Start();
            try
            {
                foreach (string arg in args)
                {
                    string[] param = arg.Split(new[] { ":" }, 2, StringSplitOptions.None);
                    switch (param[0].ToUpper())
                    {
                    case "/?":
                        ShowUsage();
                        Environment.Exit(0);
                        break;

                    case "/CONFIG":
                        configfile = param[1];
                        break;

                    case "/BATCH":
                        batchmode = true;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("There is a problem with arguments: " + string.Join(" ", args));
                Console.WriteLine("");
                ShowUsage();
                Environment.Exit(1);
            }

            //load the config
            try
            {
                using (Configuration config = Configuration.LoadConfiguration(configfile))
                {
                    scannerid = config.ScannerID;
                    if (string.IsNullOrEmpty(config.Username))
                    {
                        Connector.Instance.Connect(config.SiteServer);
                    }
                    else
                    {
                        Connector.Instance.Connect(config.Username, config.Password, config.Domain, config.SiteServer);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error connecting to the server with your configuration");
                Console.WriteLine(e.Message);
                if (batchmode == false)
                {
                    Console.ReadLine();
                }
                Environment.Exit(1);
            }

            try
            {
                using (NeoConfiguration config = NeoConfiguration.LoadConfigurationFile(neoconfigfile))
                {
                    driver = Neo4jConnector.ConnectToNeo(config);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error loading your neo4j configuration");
                Console.WriteLine(e.Message);
                if (batchmode == false)
                {
                    Console.ReadLine();
                }
                Environment.Exit(2);
            }

            NeoWriter.ScanID = scanid;

            List <IDataCollector> collectors = new List <IDataCollector>
            {
                CmCollections.GetInstance(),
                                  CmLimitingCollections.GetInstance(),
                                  CmApplications.GetInstance(),
                                  CmPackages.GetInstance(),
                                  CmPackagePrograms.GetInstance(),
                                  CmTaskSequences.GetInstance(),
                                  CmUsers.GetInstance(),
                                  CmDevices.GetInstance(),
                                  CmDeviceAdConnections.GetInstance(),
                                  CmUserAdConnections.GetInstance(),
                                  CmCollectionMemberships.GetInstance(),
                                  CmApplicationsInTaskSequences.GetInstance(),
                                  CmSoftwareUpdate.GetInstance(),
                                  CmSoftwareUpdateSupersedence.GetInstance(),
                                  CmSoftwareUpdateGroupMembers.GetInstance(),
                                  CmDeployments.GetInstance()
            };

            NeoWriter.WriteHeaders();

            foreach (IDataCollector collector in collectors)
            {
                NeoWriter.WriteIDataCollector(collector, driver, true, true);
            }

            //cleanup
            Cleanup.CleanupCmObjects(driver);

            if (batchmode == true)
            {
                Console.Write("Exiting.");
                for (int i = 0; i < 3; i++)
                {
                    System.Threading.Thread.Sleep(500);
                    Console.Write(".");
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Stopwatch totaltimer = new Stopwatch();

            string _appdir       = AppDomain.CurrentDomain.BaseDirectory;
            string neoconfigfile = _appdir + @"\config\neoconfig.json";
            string configfile    = _appdir + @"\config\adconfig.json";

            NeoWriter.ScanID = ShortGuid.NewGuid().ToString();

            IDriver          driver  = null;
            PrincipalContext context = null;

            totaltimer.Start();
            try
            {
                foreach (string arg in args)
                {
                    string[] param = arg.Split(new[] { ":" }, 2, StringSplitOptions.None);
                    switch (param[0].ToUpper())
                    {
                    case "/?":
                        ShowUsage();
                        Environment.Exit(0);
                        break;

                    case "/CONFIG":
                        configfile = param[1];
                        break;

                    case "/BATCH":
                        _batchmode = true;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("There is a problem with arguments: " + string.Join(" ", args));
                Console.WriteLine("");
                ShowUsage();
                ExitError(1);
            }


            //load the config
            try
            {
                using (Configuration config = Configuration.LoadConfiguration(configfile))
                {
                    if (string.IsNullOrEmpty(config.ID))
                    {
                        Console.WriteLine("Your configuration does not have a scanner ID. A random ID will be generated for you below:");
                        Console.WriteLine(ShortGuid.NewGuid().ToString());
                        Console.WriteLine();
                        ExitError(2);
                    }
                    NeoWriter.ScannerID = config.ID;
                    context             = Connector.CreatePrincipalContext(config);
                }
            }
            catch (Exception e)
            {
                ExitError(e, "There was an error loading your configuration", 1);
            }



            //load the neo4j config
            try
            {
                using (NeoConfiguration config = NeoConfiguration.LoadConfigurationFile(neoconfigfile))
                {
                    driver = Neo4jConnector.ConnectToNeo(config);
                }
            }
            catch (Exception e)
            {
                ExitError(e, "There was an error loading your neo4j configuration", 2);
            }


            Console.WriteLine($"Starting scan\nScanner ID: {NeoWriter.ScannerID}\nScan ID: {NeoWriter.ScanID}\n");


            NeoWriter.WriteHeaders();

            //write the foreign principals
            NeoWriter.WriteIDataCollector(new ForeignSecurityPrincipalCollector(context), driver, true, true);

            //process users
            NeoWriter.WriteIDataCollector(new UsersCollector(context), driver, true, true);

            //load the computers
            NeoWriter.WriteIDataCollector(new ComputersCollector(context), driver, true, true);

            //process groups
            GroupsCollector groupscollector = new GroupsCollector(context);

            NeoWriter.WriteIDataCollector(groupscollector, driver, true, true);
            NeoWriter.WriteIDataCollector(groupscollector.GetMembershipsCollector(), driver, true, true);

            //process foreign item connections
            NeoWriter.WriteIDataCollector(new ForeignSecurityPrincipalConnectionCollector(), driver, true, true);
            NeoWriter.WriteIDataCollector(new RemoteForeignSecurityPrincipalConnectionCollector(), driver, true, true);

            NeoQueryData nopropsdata = new NeoQueryData();

            nopropsdata.ScanID    = NeoWriter.ScanID;
            nopropsdata.ScannerID = NeoWriter.ScannerID;

            //create primary group mappings
            Console.Write("Setting primary groups");
            NeoWriter.RunQuery(StandAloneQueries.SetPrimaryGroupRelationships, nopropsdata, driver, true, true);

            Console.WriteLine();
            Console.WriteLine("*Cleaning up");

            //*cleanup deleted items
            //remove group memberships that have been deleted
            Console.Write("Deleted group memberships");
            NeoWriter.RunQuery(StandAloneQueries.DeletedGroupMemberships, nopropsdata, driver, true, true);

            Console.Write("Deleted foreign group memberships");
            NeoWriter.RunQuery(StandAloneQueries.DeletedForeignGroupMemberShips, nopropsdata, driver, true, true);

            //mark deleted objects
            Console.Write("Mark deleted users");
            NeoWriter.RunQuery(StandAloneQueries.GetMarkDeletedObjectsQuery(Types.User), nopropsdata, driver, true, true);

            Console.Write("Mark deleted computers");
            NeoWriter.RunQuery(StandAloneQueries.GetMarkDeletedObjectsQuery(Types.Computer), nopropsdata, driver, true, true);

            Console.Write("Mark deleted groups");
            NeoWriter.RunQuery(StandAloneQueries.GetMarkDeletedObjectsQuery(Types.Group), nopropsdata, driver, true, true);

            Console.WriteLine("*Finished cleaning up");
            Console.WriteLine();

            Console.Write("Setting group scopes");
            NeoWriter.RunQuery(StandAloneQueries.SetGroupScope, nopropsdata, driver, true, true);

            Console.Write("Updating member counts");
            NeoWriter.RunQuery(StandAloneQueries.UpdateMemberCounts, nopropsdata, driver, true, true);

            //cleanup
            driver.Dispose();
            context.Dispose();

            totaltimer.Stop();
            double totaltime = totaltimer.ElapsedMilliseconds / 1000;

            Console.WriteLine();
            Console.WriteLine("Finished in " + totaltime + "secs");
            if (_batchmode == true)
            {
                Console.Write("Exiting.");
                for (int i = 0; i < 3; i++)
                {
                    System.Threading.Thread.Sleep(500);
                    Console.Write(".");
                }
            }
            else
            {
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string  appdir        = AppDomain.CurrentDomain.BaseDirectory;
            string  neoconfigfile = appdir + @"\config\neoconfig.json";
            string  configfile    = appdir + @"\config\ciconfig.json";
            bool    batchmode     = false;
            IDriver driver        = null;
            string  scanid        = ShortGuid.NewGuid().ToString();

            try
            {
                foreach (string arg in args)
                {
                    string[] param = arg.Split(new[] { ":" }, 2, StringSplitOptions.None);
                    switch (param[0].ToUpper())
                    {
                    case "/?":
                        ShowUsage();
                        Environment.Exit(0);
                        break;

                    case "/CONFIG":
                        configfile = param[1];
                        break;

                    case "/BATCH":
                        batchmode = true;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("There is a problem with arguments: " + string.Join(" ", args));
                Console.WriteLine("");
                ShowUsage();
                Environment.Exit(1);
            }

            Configuration config = null;

            try
            {
                config = Configuration.LoadConfiguration(configfile);
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error loading config: " + configfile);
                Console.WriteLine(e.Message);
                Environment.Exit(2);
            }

            //load the neo4j config
            try
            {
                using (NeoConfiguration neoconfig = NeoConfiguration.LoadConfigurationFile(neoconfigfile))
                {
                    driver = Neo4jConnector.ConnectToNeo(neoconfig);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error loading your neo4j configuration");
                Console.WriteLine(e.Message);
                if (batchmode == false)
                {
                    Console.ReadLine();
                }
                Environment.Exit(2);
            }


            foreach (CustomItem item in config.Items)
            {
                using (ISession session = driver.Session())
                {
                    object prop;
                    if (item.Properties.TryGetValue(item.PrimaryProperty, out prop) == false)
                    {
                        Console.WriteLine("Primary property does not have a value");
                        Environment.Exit(10);
                    }
                    StringBuilder builder = new StringBuilder();
                    builder.AppendLine($"MERGE (n:{item.PrimaryType} {{{item.PrimaryProperty}:\"{prop}\"}})");

                    if (item.Types.Count > 1)
                    {
                        foreach (string type in item.Types)
                        {
                            if (type != item.PrimaryType)
                            {
                                builder.AppendLine($"SET n:{type}");
                            }
                        }
                    }

                    foreach (string key in item.Properties.Keys)
                    {
                        object o = item.Properties[key];
                        string s = o as string;
                        if (s == null)
                        {
                            builder.AppendLine($"SET n.{key}={o}");
                        }
                        else
                        {
                            builder.AppendLine($"SET n.{key}=\"{s}\"");
                        }
                    }
                    builder.AppendLine("RETURN n");

                    string       query = builder.ToString();
                    NeoQueryData data  = new NeoQueryData();
                    data.ScanID     = scanid;
                    data.ScannerID  = config.ScannerID;
                    data.Properties = config.Items;
                    NeoWriter.RunQuery(query, data, driver, true);
                }
            }



            Console.WriteLine();
            Console.WriteLine("Finished");
            if (batchmode == true)
            {
                Console.Write("Exiting.");
                for (int i = 0; i < 3; i++)
                {
                    System.Threading.Thread.Sleep(500);
                    Console.Write(".");
                }
            }
            else
            {
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
            }
        }
Ejemplo n.º 4
0
        public static void CleanupCmObjects(IDriver driver)
        {
            NeoQueryData collectionsdata = new NeoQueryData();

            collectionsdata.ScanID    = NeoWriter.ScanID;
            collectionsdata.ScannerID = NeoWriter.ScannerID;

            //nodes first
            List <string> cmnodetypes = new List <string> {
                Types.CMApplication,
                Types.CMPackage,
                Types.CMPackageProgram,
                Types.CMSoftwareUpdateGroup,
                Types.CMTaskSequence,
                Types.CMCollection,
                Types.CMDevice,
                Types.CMUser,
                Types.CMClientSettings
            };

            string query;

            foreach (string type in cmnodetypes)
            {
                query = "MATCH (n:" + type + ") " +
                        "WHERE n.scannerid=$ScannerID AND n.lastscan<>$ScanID " +
                        "DETACH DELETE n " +
                        "RETURN n";

                Console.Write("Cleaning up " + type);
                NeoWriter.RunQuery(query, collectionsdata, driver, true, true);
            }

            //any remaining edges
            List <string> cmreltypes = new List <string> {
                Types.CMLimitingCollection,
                Types.CMMemberOf,
                Types.CMReferences,
                Types.CMHasProgram,
                Types.CMHasObject,
                Types.CMHasDeployment
            };



            foreach (string type in cmreltypes)
            {
                query = "MATCH ()-[r:" + type + "]->() " +
                        "WHERE r.scannerid=$ScannerID AND r.lastscan<>$ScanID " +
                        "DELETE r " +
                        "RETURN r";

                Console.Write("Cleaning up " + type);
                NeoWriter.RunQuery(query, collectionsdata, driver, true, true);
            }


            //We don't delete software updates. MS might expire something which might remove it from SCCM.
            //We want to keep that info
            query = "MATCH (n:" + Types.CMSoftwareUpdate + ") " +
                    "WHERE n.scannerid=$ScannerID AND n.lastscan<>$ScanID " +
                    "SET n.IsExpired = true " +
                    "RETURN n";

            Console.Write("Finding expired updates");
            NeoWriter.RunQuery(query, collectionsdata, driver, true, true);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Stopwatch steptimer     = new Stopwatch();
            Stopwatch totaltimer    = new Stopwatch();
            string    _appdir       = AppDomain.CurrentDomain.BaseDirectory;
            string    neoconfigfile = _appdir + @"\config\neoconfig.json";
            string    configfile    = _appdir + @"\config\aadconfig.json";
            bool      batchmode     = false;
            string    scanid        = ShortGuid.NewGuid().ToString();
            string    scannerid     = string.Empty;

            IDriver driver = null;

            totaltimer.Start();
            try
            {
                foreach (string arg in args)
                {
                    string[] param = arg.Split(new[] { ":" }, 2, StringSplitOptions.None);
                    switch (param[0].ToUpper())
                    {
                    case "/?":
                        ShowUsage();
                        Environment.Exit(0);
                        break;

                    case "/CONFIG":
                        configfile = param[1];
                        break;

                    case "/BATCH":
                        batchmode = true;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("There is a problem with arguments: " + string.Join(" ", args));
                Console.WriteLine("");
                ShowUsage();
                Environment.Exit(1);
            }

            //load the config
            try
            {
                using (Configuration config = Configuration.LoadConfiguration(configfile))
                {
                    Console.WriteLine("Loading config for scanner: " + scannerid);

                    scannerid           = config.ScannerID;
                    NeoWriter.ScannerID = scannerid;
                    Connector.Instance.Init(config);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error connecting to the server with your configuration");
                Console.WriteLine(e.Message);
                if (batchmode == false)
                {
                    Console.ReadLine();
                }
                Environment.Exit(1);
            }



            try
            {
                using (NeoConfiguration config = NeoConfiguration.LoadConfigurationFile(neoconfigfile))
                {
                    driver = Neo4jConnector.ConnectToNeo(config);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error loading your neo4j configuration");
                Console.WriteLine(e.Message);
                if (batchmode == false)
                {
                    Console.ReadLine();
                }
                Environment.Exit(2);
            }

            NeoWriter.ScanID = scanid;

            var aadgroupmembers = new AadGroupMemberships();

            aadgroupmembers.GroupIDs = AadGroups.Instance.GroupIDs;

            List <IDataCollectorAsync> collectors = new List <IDataCollectorAsync>
            {
                AadUsers.Instance,
                new AadUserToAdUserConnections(),
                AadGroups.Instance,
                aadgroupmembers,
                AadSites.Instance,
                new AadDrives()
            };

            NeoWriter.WriteHeaders();

            foreach (IDataCollectorAsync collector in collectors)
            {
                try
                {
                    NeoWriter.WriteIDataCollector(collector, driver, true, true);
                }
                catch (Exception e)
                {
                    Console.WriteLine("There was an error collecting data for " + collector.GetType());
                    Console.WriteLine(e.Message);
                }
            }



            //cleanup

            if (batchmode == true)
            {
                Console.Write("Exiting.");
                for (int i = 0; i < 3; i++)
                {
                    System.Threading.Thread.Sleep(500);
                    Console.Write(".");
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
            }
        }