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.

190 lines
6.4 KiB

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