-
Notifications
You must be signed in to change notification settings - Fork 0
/
modSystem.cs
266 lines (239 loc) · 13.3 KB
/
modSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using Microsoft.VisualBasic.Devices;
using Microsoft.Win32;
namespace XRFAgent
{
internal class modSystem
{
/// <summary>
/// Loading the system module is NOT NEEDED
/// </summary>
public static void Load() { }
/// <summary>
/// Unloading the system module is NOT NEEDED
/// </summary>
public static void Unload() { }
/// <summary>
/// Collects the list of installed applications, updates the local table, and sends to the server.
/// </summary>
/// <returns>Result of new applications detected</returns>
public static string GetInstalledSoftware()
{
try
{
List<RegistryKey> UninstallKeys = new List<RegistryKey>() { Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"), Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall") };
RegistryKey SoftwareKey;
string SoftwareKeyName;
string NewSoftware = ""; string NewSoftwareJSON = "{\"newsoftware\":[";
int count = 0;
int result = 0;
modDatabase.InstalledSoftware SoftwareObj;
foreach (RegistryKey UninstallKey in UninstallKeys)
{
foreach (string ProgramKey in UninstallKey.GetSubKeyNames())
{
SoftwareKey = UninstallKey.OpenSubKey(ProgramKey);
if (SoftwareKey != null)
{
SoftwareKeyName = SoftwareKey.GetValue("DisplayName")?.ToString() ?? "";
if (SoftwareKeyName != "")
{
SoftwareObj = new modDatabase.InstalledSoftware { Name = SoftwareKeyName, Version = SoftwareKey.GetValue("DisplayVersion")?.ToString() ?? "", Publisher = SoftwareKey.GetValue("Publisher")?.ToString() ?? "", InstallDate = SoftwareKey.GetValue("InstallDate")?.ToString() ?? "" };
result = modDatabase.UpdateSoftware(SoftwareObj);
if (result == 0)
{
NewSoftware = NewSoftware + SoftwareKeyName + ", ";
NewSoftwareJSON = NewSoftwareJSON + JsonSerializer.Serialize(SoftwareObj) + ",";
result = modDatabase.AddSoftware(SoftwareObj);
}
count++;
}
}
}
}
if (NewSoftware != "")
{
NewSoftware = NewSoftware.Substring(0, NewSoftware.Length - 2);
NewSoftwareJSON = NewSoftwareJSON.Substring(0, NewSoftwareJSON.Length - 1) + "]}";
modLogging.LogEvent("Detected new software installed: " + NewSoftware, EventLogEntryType.Information, 6051);
modSync.SendMessage("server", "nodedata", "newsoftware", NewSoftwareJSON);
}
return "Installed Applications: " + count.ToString();
}
catch (Exception err)
{
modLogging.LogEvent("Unable to get registry information: " + err.Message + "\n\n" + err.StackTrace, EventLogEntryType.Error, 6032);
return "Registry error";
}
}
/// <summary>
/// Drops the installed software inventory, then gathers an updated version.
/// </summary>
/// <returns>Result of new applications detected</returns>
public static string ResetInstalledSoftware()
{
modLogging.LogEvent("Reset installed software inventory.", EventLogEntryType.Information, 6052);
modDatabase.TruncateSoftware();
return GetInstalledSoftware();
}
/// <summary>
/// Collects some general system information
/// </summary>
/// <returns>(string) Result</returns>
public static string GetSystemDetails()
{
try
{
string SystemDetailsJSON = "{\"systemdetails\":[";
modDatabase.Config ConfigObj;
ConfigObj = new modDatabase.Config { Key = "System_Hostname", Value = Environment.MachineName };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
RegistryKey currentVersion = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MICROSOFT\Windows NT\CurrentVersion");
ConfigObj = new modDatabase.Config { Key = "System_LastKnownWindowsVersion", Value = currentVersion.GetValue("CurrentMajorVersionNumber").ToString() + "." + currentVersion.GetValue("CurrentMinorVersionNumber").ToString() + "." + currentVersion.GetValue("CurrentBuild").ToString() + "." + currentVersion.GetValue("UBR").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_OSProductName", Value = currentVersion.GetValue("ProductName").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
RegistryKey systemHardware = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS");
ConfigObj = new modDatabase.Config { Key = "System_BaseBoardManufacturer", Value = systemHardware.GetValue("BaseBoardManufacturer").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_BaseBoardProduct", Value = systemHardware.GetValue("BaseBoardProduct").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_SystemManufacturer", Value = systemHardware.GetValue("SystemManufacturer").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_SystemProductName", Value = systemHardware.GetValue("SystemProductName").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
RegistryKey systemCPU = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
ConfigObj = new modDatabase.Config { Key = "System_ProcessorName", Value = systemCPU.GetValue("ProcessorNameString").ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ComputerInfo VBCI = new ComputerInfo();
ConfigObj = new modDatabase.Config { Key = "System_TotalPhysicalMemory", Value = VBCI.TotalPhysicalMemory.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
DriveInfo[] allDrives = DriveInfo.GetDrives();
string dLetter;
foreach (DriveInfo d in allDrives)
{
dLetter = d.Name.Substring(0, 1);
ConfigObj = new modDatabase.Config { Key = "System_Drive_" + dLetter + "_Type", Value = d.DriveType.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
if (d.IsReady == true && d.DriveType == DriveType.Fixed)
{
ConfigObj = new modDatabase.Config { Key = "System_Drive_" + dLetter + "_Label", Value = d.VolumeLabel };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_Drive_" + dLetter + "_TotalSize", Value = d.TotalSize.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "System_Drive_" + dLetter + "_TotalFreeSpace", Value = d.TotalFreeSpace.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
}
}
if (File.Exists(@"C:\HAC\HAController.exe"))
{
ConfigObj = new modDatabase.Config { Key = "HAController_Version", Value = FileVersionInfo.GetVersionInfo(@"C:\HAC\HAController.exe").FileVersion };
modDatabase.AddOrUpdateConfig(ConfigObj);
SystemDetailsJSON = SystemDetailsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
}
SystemDetailsJSON = SystemDetailsJSON.Substring(0, SystemDetailsJSON.Length - 1) + "]}";
modSync.SendMessage("server", "nodedata", "systemdetails", SystemDetailsJSON);
return "System details updated";
}
catch (Exception err)
{
modLogging.LogEvent("Unable to get registry information: " + err.Message + "\n\n" + err.StackTrace, EventLogEntryType.Error, 6032);
return "Registry error";
}
}
public static int InstallWindowsUpdates()
{
try
{
if (File.Exists(Properties.Settings.Default.Tools_FolderURI + "WindowsUpdatePush.exe") == false)
{
int installResult = modUpdate.InstallWindowsUpdatePush();
if (installResult == -1)
{
return -1;
}
}
Process UpdateRunner = new Process();
UpdateRunner.StartInfo.UseShellExecute = false;
UpdateRunner.StartInfo.RedirectStandardOutput = true;
UpdateRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
UpdateRunner.StartInfo.FileName = Properties.Settings.Default.Tools_FolderURI + "WindowsUpdatePush.exe";
UpdateRunner.Start();
UpdateRunner.WaitForExit();
modLogging.LogEvent(UpdateRunner.StandardOutput.ReadToEnd(), EventLogEntryType.Information, 6042);
return UpdateRunner.ExitCode;
}
catch(Exception err)
{
modLogging.LogEvent("Windows Update error: " + err.Message, EventLogEntryType.Error, 6041);
return -1;
}
}
/// <summary>
/// Reboots the host computer
/// </summary>
/// <returns>(string) Response</returns>
public static string RebootHost()
{
try
{
Process.Start("shutdown", "-r");
return "Reboot started";
}
catch(Exception err)
{
modLogging.LogEvent("Unable to initiate reboot: " + err.Message, EventLogEntryType.Error, 6031);
return "Reboot error";
}
}
/// <summary>
/// Shuts down the host computer
/// </summary>
/// <returns>(string) Response</returns>
public static string ShutdownHost()
{
try
{
Process.Start("shutdown", "-s");
return "Shutdown started";
}
catch(Exception err)
{
modLogging.LogEvent("Unable to initiate shutdown: " + err.Message, EventLogEntryType.Error, 6031);
return "Shutdown failed";
}
}
/// <summary>
/// Configures the system Run dialog, which is frequently used by phone scammers
/// </summary>
/// <param name="action">(string) "enable" or "disable"</param>
/// <returns>(string) Response</returns>
public static string ConfigureRunDialog(string action)
{
int newvalue = 0;
if (action == "disable") { newvalue = 1; }
RegistryKey explorerPolicies = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer", true);
explorerPolicies.SetValue("NoRun", newvalue, RegistryValueKind.DWord);
explorerPolicies.Close();
modSync.SendSingleConfig("Security_RunDialog", action + "d");
return "Run dialog " + action + "d";
}
}
}