public static void GroupPolicy() // This is useless, I guess. { Console.WriteLine("Executing GroupPolicy()..."); XDocument doc = new XDocument(); GPDomain domain = new GPDomain(); XElement xmlDomain = new XElement("domain"); int count = 0; GpoCollection gpos = domain.GetAllGpos(); doc.Add(xmlDomain); foreach (Gpo currentGpo in gpos) { xmlDomain.Add(new XElement("GPO", new XElement("Computer", currentGpo.Computer), new XElement("Description", currentGpo.Description), new XElement("DisplayName", currentGpo.DisplayName), new XElement("DomainName", currentGpo.DomainName), new XElement("GpoStatus", currentGpo.GpoStatus), new XElement("Id", currentGpo.Id), new XElement("ModificationTime", currentGpo.ModificationTime), new XElement("Owner", currentGpo.Owner), new XElement("Path", currentGpo.Path), new XElement("User", currentGpo.User), new XElement("WmiFilter", currentGpo.WmiFilter) )); count++; Console.WriteLine(count); //if (count >= 20) break; //for testing purposes } doc.Save("D:\\GroupPolicy.xml"); Console.WriteLine("GroupPolicy() done!"); }
private GPDomain GetGroupPolicyDomain(string domainController) { GPDomain gpDomain; if (string.IsNullOrWhiteSpace(domainController)) { gpDomain = new GPDomain(); } else { var domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName; _logger.Info($"Getting GP domain object from domain '{domainName}' and domain contoller '{domainController}'"); gpDomain = new GPDomain(domainName, domainController); } return(gpDomain); }
public static void Main(string[] args) { // Pobranie biezacej domeny i jej GPO GPDomain dom = new GPDomain(); GpoCollection gpos = dom.GetAllGpos(); foreach (Gpo gpo in gpos) { // iteracja po GPO i generacja raportow XML-owych dla GPO string xml = gpo.GenerateReport(ReportType.Xml); XmlDocument xd = new XmlDocument(); xd.LoadXml(xml); XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable); xnm.AddNamespace("q1", "http://www.microsoft.com/GroupPolicy/Settings/Security"); // Wyszuwanie tych polityk, ktore dotycza LmCompatibilityLevel XmlNodeList nl = xd.SelectNodes("//*[q1:KeyName[text()[contains(.,'LmCompatibilityLevel')]]]", xnm); if (nl.Count > 0) { // Wypisanie danych polityki i jej ustawien odpowiedzialnych za LmCompatibilityLevel Console.WriteLine("\n " + gpo.DisplayName); for (int i = 0; i < nl.Count; i++) { // Dla kazdego pasujacego elementu wypisanie info XmlNode node = nl.Item(i); String keyName = node.SelectSingleNode("//q1:KeyName", xnm).InnerText; String name = node.SelectSingleNode("//q1:Name", xnm).InnerText; String displayString = node.SelectSingleNode("//q1:DisplayString", xnm).InnerText; Console.WriteLine("Policy: " + name); Console.WriteLine("Policy Setting: " + displayString); } } else { // Wyswietlenie polityki, ktora nie zawierala ustawienia Console.WriteLine("\n #" + gpo.DisplayName + " - nie zawiera"); } } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
static void Main(string[] args) { try { var domain = new GPDomain("edetoc.lab"); var gpo = domain.GetGpo("xxxGPO"); var somcollection = domain.SearchSoms(gpo); foreach (var som in somcollection) { //Console.WriteLine(som.Name); //Console.WriteLine(som.Path); foreach (var link in som.GpoLinks) { if (Guid.Equals(gpo.Id, link.GpoId)) { Console.WriteLine("Need to suppress link to " + link.DisplayName // GPO name + " in " + link.Target); // path link.Delete(); Console.WriteLine("link suppressed"); } } } // We have suppressed all links to the GPO in the domain // we can now suppress the GPO object itself gpo.Delete(); Console.WriteLine("xxxGPO deleted with success."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public int ReportAllGpos(string reportFolder, bool removeReadTimestamp) { var returnValue = 0; if (!_gpmcWindowsFeature.IsInstalled()) { _logger.ErrorFormat("Windows feature 'GPMC' do not seem to be installed on this machine: {0}. This command can only be executed on a Windows Server Operating System with GPMC feature installed.", Environment.MachineName); return 1; } if (!Directory.Exists(reportFolder)) { _logger.ErrorFormat("Report folder '{0}' does not exists", reportFolder); return 1; } _logger.Info("Clean report folder for existing reports"); var existingReportFiles = Directory.GetFiles(reportFolder, "*.xml",SearchOption.TopDirectoryOnly); foreach (var existingReportFile in existingReportFiles) { File.Delete(existingReportFile); } _logger.Info("Report all GPOs..."); var gpDomain = new GPDomain(); var gpoCollection = gpDomain.GetAllGpos(); foreach (var gpo in gpoCollection) { var gpoGuid = gpo.Id; var gpoReport = gpo.GenerateReport(ReportType.Xml); var gpoReportFile = Path.Combine(reportFolder, gpoGuid + ".xml"); using (var sw = new StreamWriter(gpoReportFile, false, Encoding.UTF8)) { if (removeReadTimestamp) { gpoReport = RemoveReadTimeStamp(gpoReport); } sw.Write(gpoReport); } } return returnValue; }
public static void Main(string[] args) { // Pobranie biezacej domeny i jej GPO GPDomain dom = new GPDomain(); GpoCollection gpos = dom.GetAllGpos(); foreach (Gpo gpo in gpos) { // iteracja po GPO i generacja raportow XML-owych dla GPO string xml = gpo.GenerateReport(ReportType.Xml); XmlDocument xd = new XmlDocument(); xd.LoadXml(xml); XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable); xnm.AddNamespace("q1", "http://www.microsoft.com/GroupPolicy/Settings/Security"); // Wyszuwanie tych polityk, ktore dotycza LmCompatibilityLevel XmlNodeList nl = xd.SelectNodes("//*[q1:KeyName[text()[contains(.,'LmCompatibilityLevel')]]]", xnm); if (nl.Count > 0) { // Wypisanie danych polityki i jej ustawien odpowiedzialnych za LmCompatibilityLevel Console.WriteLine("\n " + gpo.DisplayName); for (int i = 0; i< nl.Count; i++) { // Dla kazdego pasujacego elementu wypisanie info XmlNode node = nl.Item(i); String keyName = node.SelectSingleNode("//q1:KeyName", xnm).InnerText; String name = node.SelectSingleNode("//q1:Name", xnm).InnerText; String displayString = node.SelectSingleNode("//q1:DisplayString", xnm).InnerText; Console.WriteLine("Policy: " + name); Console.WriteLine("Policy Setting: " + displayString); } } else { // Wyswietlenie polityki, ktora nie zawierala ustawienia Console.WriteLine("\n #" + gpo.DisplayName + " - nie zawiera"); } } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
private void NextButton_Click(object sender, EventArgs e) { if (state == 0) { /*StreamReader sr = new StreamReader(path); * string line = sr.ReadLine(); * string[] parsed = line.Split(new String[] { " = " }, StringSplitOptions.None); * while (line != null) * { * parsed = line.Split(new String[] { " = " },StringSplitOptions.None); * if (parsed.Length > 1) { * Policies.Add(parsed[0], Int32.Parse(parsed[parsed.Length - 1])); * } * * /*parsed = line.Split(new Char[] { ' ', '=', ' ' }); * if (parsed.Length > 1) * { * if (Policies.ContainsKey(parsed[0])) * { * advice.Add(Policies[parsed[0]].DynamicInvoke(Int32.Parse(parsed[parsed.Length-1])) + "\n"); * } * }*/ // line = sr.ReadLine(); // } //label1.Text = advice; var guid = new Guid("31B2F340-016D-11D2-945F-00C04FB984F9"); var domain = new GPDomain(System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName); gpo = domain.GetGpo(guid); var gpoReport = gpo.GenerateReport(ReportType.Xml); //gpb = gpo.Backup(Path.GetTempPath(), null); //path = Path.GetTempPath() + "{" + gpb.Id + "}\\DomainSysvol\\GPO\\Machine\\microsoft\\windows nt\\SecEdit\\GptTmpl.inf"; //StreamReader sr = new StreamReader(path); XmlDocument doc = new XmlDocument(); doc.LoadXml(gpoReport); List <XmlNode> extensionData = new List <XmlNode>(); XmlNode extIter = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData"); while (extIter != null) { if (extIter.Name == "ExtensionData") { extensionData.Add(extIter); } extIter = extIter.NextSibling; } extIter = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData"); while (extIter != null) { if (extIter.Name == "ExtensionData") { extensionData.Add(extIter); } extIter = extIter.NextSibling; } /*XmlNode extensiondata1 = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData"); * MessageBox.Show(extensiondata1.ToString()); * XmlNode extensiondata5 = extensiondata1.NextSibling.NextSibling.NextSibling.NextSibling; * MessageBox.Show(extensiondata5.ToString()); * XmlNode extensiondata6 = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData"); * MessageBox.Show(extensiondata6.ToString()); * MessageBox.Show(extensions.ToString());*/ var i = 2; foreach (XmlNode data in extensionData) { XmlNode extensions = data.FirstChild; if (data == extensionData[0]) { foreach (XmlNode pol in extensions) { if (pol.Name == "q1:Account") { var name = GetChildByName(pol, "q1:Name"); var num = GetChildByName(pol, "q1:SettingNumber"); var tf = GetChildByName(pol, "q1:SettingBoolean"); if (name != null && num != null) { Policies.Add(pol.ChildNodes[0].InnerText, Int32.Parse(pol.ChildNodes[1].InnerText)); } if (name != null && tf != null) { if (tf.InnerText == "true") { Policies.Add(pol.ChildNodes[0].InnerText, 1); } else { Policies.Add(pol.ChildNodes[0].InnerText, 0); } } } } } else { foreach (XmlNode pol in extensions) { if (pol.Name == "q" + i.ToString() + ":Policy") { var name = GetChildByName(pol, "q" + i.ToString() + ":Name"); var state = GetChildByName(pol, "q" + i.ToString() + ":State"); if (name != null && state != null) { if (state.InnerText == "Enabled") { Policies.Add(pol.ChildNodes[0].InnerText, 1); } else if (state.InnerText == "Disabled") { Policies.Add(pol.ChildNodes[0].InnerText, 0); } else { Policies.Add(pol.ChildNodes[0].InnerText, 2); } } } } i++; } } XmlDocument policies = new XmlDocument(); //XmlDocument updater = new XmlDocument(); Assembly assembly = Assembly.GetExecutingAssembly(); var a = assembly.GetManifestResourceNames(); Stream stream = assembly.GetManifestResourceStream("WindowsFormsApp1.PoliciesXML.xml"); //Stream stream1 = assembly.GetManifestResourceStream("WindowsFormsApp1.UpdaterXML.xml"); policies.Load(stream); //updater.Load(stream1); XmlNode p = policies.ChildNodes[1].ChildNodes[0]; //XmlNode u = updater.ChildNodes[1].ChildNodes[0]; while (p != null) { if (p.NodeType.ToString() == "Element") { Pols.Add(new Policy(p)); } p = p.NextSibling; //u = u.NextSibling; } maxPage = Pols.Count; state = 1; GuidanceButton.Visible = true; // UpdateButton.Visible = true; MarkAsDone.Visible = true; NextButton.Text = "Next >>"; } if (state == 1) { if (page == maxPage) { InfoBox.Text = ""; state = 2; PrevButton.Visible = false; GuidanceButton.Visible = false; //UpdateButton.Visible = false; } else { var name = Pols[page].getName(); if (Policies.ContainsKey(name)) { InfoBox.Text = Pols[page].check(Policies[name]).Trim(); } else { InfoBox.Text = Pols[page].useDefault().Trim(); } if (Pols[page].guidance() == "") { GuidanceButton.Visible = false; } else { GuidanceButton.Visible = true; } page += 1; if (page == maxPage) { NextButton.Text = "Finish"; } if (page == 2) { PrevButton.Visible = true; } } } else { this.Close(); } }
private void NextButton_Click(object sender, EventArgs e) { if (state == 0) { //Gets GP XML var guid = new Guid("31B2F340-016D-11D2-945F-00C04FB984F9"); var domain = new GPDomain(System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName); gpo = domain.GetGpo(guid); var gpoReport = gpo.GenerateReport(ReportType.Xml); XmlDocument doc = new XmlDocument(); doc.LoadXml(gpoReport); //Parses GP XML List <XmlNode> extensionData = new List <XmlNode>(); XmlNode extIter = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData"); while (extIter != null) { if (extIter.Name == "ExtensionData") { extensionData.Add(extIter); } extIter = extIter.NextSibling; } extIter = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData"); while (extIter != null) { if (extIter.Name == "ExtensionData") { extensionData.Add(extIter); } extIter = extIter.NextSibling; } var i = 2; foreach (XmlNode data in extensionData) { XmlNode extensions = data.FirstChild; if (data == extensionData[0]) { foreach (XmlNode pol in extensions) { if (pol.Name == "q1:Account") { var name = GetChildByName(pol, "q1:Name"); var num = GetChildByName(pol, "q1:SettingNumber"); var tf = GetChildByName(pol, "q1:SettingBoolean"); if (name != null && num != null) { Policies.Add(pol.ChildNodes[0].InnerText, Int32.Parse(pol.ChildNodes[1].InnerText)); } if (name != null && tf != null) { if (tf.InnerText == "true") { Policies.Add(pol.ChildNodes[0].InnerText, 1); } else { Policies.Add(pol.ChildNodes[0].InnerText, 0); } } } } } else { if (GetChildByName(data, "Name") != null && GetChildByName(data, "Name").InnerText == "Windows Firewall") { //Special case as the firewall is displayed differently foreach (XmlNode pol in extensions) { if (pol.Name == "q" + i + ":DomainProfile") { var lpm = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge"); var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0; Policies.Add("Domain Profile - Apply local firewall rules", lpmState); var efw = GetChildByName(pol, "q" + i + ":EnableFirewall"); var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0; Policies.Add("Domain Profile", efwState); //var ibc = GetChildByName(pol, "q5:DefaultInboundAction"); //var ibcState = (ibc == null) ? 0 : (ibc.InnerText=="true")?1:0; //var obc = GetChildByName(pol, "q5:DefaultOutboundAction"); //var obcState = (obc == null) ? 0 : (obc.InnerText == "true") ? 1 : 0; } else if (pol.Name == "q" + i + ":PrivateProfile") { var lpm = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge"); var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0; Policies.Add("Private Profile - Apply local firewall rules", lpmState); var efw = GetChildByName(pol, "q" + i + ":EnableFirewall"); var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0; Policies.Add("Private Profile", efwState); } else if (pol.Name == "q5:PublicProfile") { var lpm = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge"); var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0; Policies.Add("Public Profile - Apply local firewall rules", lpmState); var efw = GetChildByName(pol, "q" + i + ":EnableFirewall"); var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0; Policies.Add("Public Profile", efwState); } } } else { foreach (XmlNode pol in extensions) { if (pol.Name == "q" + i.ToString() + ":Policy") { //Parses enabled/disabled/not configured to numbers var name = GetChildByName(pol, "q" + i.ToString() + ":Name"); var state = GetChildByName(pol, "q" + i.ToString() + ":State"); if (name != null && state != null) { if (state.InnerText == "Enabled") { Policies.Add(pol.ChildNodes[0].InnerText, 1); } else if (state.InnerText == "Disabled") { Policies.Add(pol.ChildNodes[0].InnerText, 0); } else { Policies.Add(pol.ChildNodes[0].InnerText, 2); } } } } } i++; } } //Parses PoliciesXML to the dictionary to generate the pages XmlDocument policies = new XmlDocument(); Assembly assembly = Assembly.GetExecutingAssembly(); var a = assembly.GetManifestResourceNames(); Stream stream = assembly.GetManifestResourceStream("WindowsFormsApp1.PoliciesXML.xml"); policies.Load(stream); XmlNode p = policies.ChildNodes[1].ChildNodes[0]; while (p != null) { if (p.NodeType.ToString() == "Element") { Pols.Add(new PolicySection(p)); } p = p.NextSibling; } //Configure first page state = 1; GuidanceButton.Visible = true; MarkAsDone.Visible = true; NextButton.Text = "Next >>"; this.Text = Pols[section].name; } //If within pages if (state == 1) { if (!Pols[section].isPolicyAt(page - 1) && page != 0 && section == Pols.Count - 1) { //Exit to finish page InfoBox.Text = ""; state = 2; PrevButton.Visible = false; GuidanceButton.Visible = false; MarkAsDone.Visible = false; NameLabel.Text = ""; NextButton.Text = "Finish"; } else { if (!Pols[section].isPolicyAt(page - 1) && page != 0) { //Change section section += 1; this.Text = Pols[section].name; page = 0; } if (page == 0) { //Display header page for page 0 NameLabel.Text = ""; InfoBox.Text = Pols[section].headerText(Policies); if (Pols[section].isSafe()) { SkipButton.Visible = true; } else { SkipButton.Visible = false; } GuidanceButton.Visible = false; page += 1; MarkAsDone.Visible = false; } else { //Display individual policy page SkipButton.Visible = false; var currentPolicy = Pols[section].policyAt(page - 1); var name = currentPolicy.getName(); NameLabel.Text = name; InfoBox.Height = 329 - NameLabel.Height; //Avoids height overflow on name InfoBox.Top = 18 + NameLabel.Height; if (Policies.ContainsKey(name)) { //Use setting found InfoBox.Text = currentPolicy.check(Policies[name]).Trim(); NameLabel.ForeColor = (currentPolicy.isRecommended(Policies[name])) ? currentPolicy.recColour() : currentPolicy.nonRecColour(); } else { //Otherwise use default InfoBox.Text = currentPolicy.useDefault().Trim(); NameLabel.ForeColor = (currentPolicy.isRecommended(-1)) ? currentPolicy.recColour() : currentPolicy.nonRecColour(); } if (currentPolicy.guidance() == "") { GuidanceButton.Visible = false; } else { GuidanceButton.Visible = true; } page += 1; PrevButton.Visible = true; MarkAsDone.Visible = true; //Very last page show finish instead of next if (!Pols[section].isPolicyAt(page - 1) && section == Pols.Count - 1) { NextButton.Text = "Finish"; MarkAsDone.Visible = false; } } } } else { this.Close(); } }