You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.3 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CDPShared
  9. {
  10. public class DataGather
  11. {
  12. private CircleAPIHelper _apiHelper;
  13. List<string> _meta;
  14. List<string> _head;
  15. List<string> _tail;
  16. public List<string> Meta
  17. {
  18. get
  19. {
  20. return _meta;
  21. }
  22. }
  23. public DataGather(CircleAPIHelper apiHelper)
  24. {
  25. _apiHelper = apiHelper;
  26. _meta = new List<string>();
  27. _head = new List<string>();
  28. _tail = new List<string>();
  29. }
  30. public void AddHead(string name, string value)
  31. {
  32. _head.Add(name + "|" + value);
  33. }
  34. public void AddTail(string name, string value)
  35. {
  36. _tail.Add(name + "|" + value);
  37. }
  38. public List<string> Gather()
  39. {
  40. try
  41. {
  42. _meta.AddRange(_head);
  43. _meta.AddRange(GetSystemWMIStuff());
  44. _meta.AddRange(GetSecurityWMIStuff());
  45. _meta.Add(GetCountry());
  46. _meta.Add($"OS username|{Environment.UserDomainName + "\\" + Environment.UserName}");
  47. _meta.Add($"Machine name|{Environment.MachineName}");
  48. _meta.AddRange(_tail);
  49. }
  50. catch (Exception e)
  51. {
  52. }
  53. return _meta;
  54. }
  55. string GetCountry()
  56. {
  57. CultureInfo culture = CultureInfo.CurrentCulture;
  58. RegionInfo region = new RegionInfo(culture.LCID);
  59. return $"Country|{region.DisplayName}";
  60. }
  61. List<string> GetSystemWMIStuff()
  62. {
  63. List<string> systemWmiStuff = new List<string>();
  64. try
  65. {
  66. // Connect to the WMI namespace
  67. ManagementScope scope = new ManagementScope(@"\\.\root\CIMV2");
  68. scope.Connect();
  69. string wql = @"SELECT * FROM Win32_OperatingSystem";
  70. // Create a new WMI query object and execute the query
  71. ObjectQuery query = new ObjectQuery(wql);
  72. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  73. ManagementObjectCollection results = searcher.Get();
  74. foreach (ManagementObject result in results)
  75. {
  76. string OS = result.Properties["Caption"].Value.ToString();
  77. systemWmiStuff.Add($"Operating system|{OS}");
  78. }
  79. // IP Addresses
  80. wql = @"SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'";
  81. // Create a new WMI query object and execute the query
  82. query = new ObjectQuery(wql);
  83. searcher = new ManagementObjectSearcher(scope, query);
  84. results = searcher.Get();
  85. foreach (ManagementObject result in results)
  86. {
  87. string[] ipAddresses = (string[])result["IPAddress"];
  88. if (ipAddresses != null && ipAddresses.Length > 0)
  89. {
  90. systemWmiStuff.Add($"IP Address|{ipAddresses[0]}");
  91. }
  92. }
  93. // TimeZone
  94. wql = @"SELECT * FROM Win32_TimeZone";
  95. // Create a new WMI query object and execute the query
  96. query = new ObjectQuery(wql);
  97. searcher = new ManagementObjectSearcher(scope, query);
  98. results = searcher.Get();
  99. foreach (ManagementObject result in results)
  100. {
  101. string timeZoneName = (string)result["Caption"];
  102. systemWmiStuff.Add($"TimeZone|{timeZoneName}");
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. MinLogging.LogIt(e.Message);
  108. }
  109. return systemWmiStuff;
  110. }
  111. List<string> GetSecurityWMIStuff()
  112. {
  113. List<string> securityWMIStuff = new List<string>();
  114. try
  115. {
  116. // Connect to the WMI namespace
  117. ManagementScope scope = new ManagementScope(@"\\.\root\SecurityCenter2");
  118. scope.Connect();
  119. // WMI query to get the current patch level for the antivirus program
  120. string wql = @"SELECT * FROM AntiVirusProduct";
  121. // Create a new WMI query object and execute the query
  122. ObjectQuery query = new ObjectQuery(wql);
  123. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
  124. ManagementObjectCollection results = searcher.Get();
  125. foreach (ManagementObject result in results)
  126. {
  127. string name = result.Properties["displayName"].Value.ToString();
  128. string signatureDate = result.Properties["timestamp"].Value.ToString();
  129. uint state = Convert.ToUInt32(result.Properties["productState"].Value);
  130. uint avState = (state >> 12) & 0xf; // https://mcpforlife.com/2020/04/14/how-to-resolve-this-state-value-of-av-providers/
  131. string running = "";
  132. switch (avState)
  133. {
  134. case 0:
  135. running = "Off";
  136. break;
  137. case 1:
  138. running = "On";
  139. break;
  140. case 2:
  141. running = "Snoozed";
  142. break;
  143. case 3:
  144. running = "Expired";
  145. break;
  146. default:
  147. running = "Unknown";
  148. break;
  149. }
  150. string avLine = string.Format($"AntiVirus|{name} Signature file: {signatureDate} Active: {running}");
  151. securityWMIStuff.Add(avLine);
  152. }
  153. }
  154. catch (Exception e)
  155. {
  156. MinLogging.LogIt(e.Message);
  157. }
  158. return securityWMIStuff;
  159. }
  160. }
  161. }