using System.Diagnostics; using System.Reflection; using System.Security.Principal; namespace AiQ_GUI { internal class Windows { private static readonly string[] targetProcesses = ["IP_Tool", "Rapier", "IPConfig", "BackdoorGUI"]; // Closes other MAV and Rudstone tools. public static async Task CloseProcesses() { IEnumerable tasks = Process.GetProcesses() .Where(p => targetProcesses.Any(tp => p.ProcessName.Contains(tp))) .Select(clsProcess => { try { clsProcess.CloseMainWindow(); } catch { } return Task.CompletedTask; }); await Task.WhenAll(tasks).ConfigureAwait(false); // Run all tasks concurrently } public static void StartAsAdmin(string ExeLoc) { Logging.LogMessage($"Starting exe from {ExeLoc}"); ProcessStartInfo processInfo = new(ExeLoc) { UseShellExecute = true, Verb = "runas" }; try { Process.Start(processInfo); Properties.Settings.Default.FirstRun = false; Properties.Settings.Default.Save(); Application.Exit(); // Exit now that we have admin rights version } catch (Exception ex) { Logging.LogErrorMessage("Failed to restart with admin rights. " + ex.Message); MessageBox.Show("Sorry, but I don't seem to be able to start this program with administrator rights!"); } } public static void UpdateFirewall() { WindowsPrincipal wp = new(WindowsIdentity.GetCurrent()); bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator); string ExeLoc = Assembly.GetEntryAssembly().Location.Replace("dll", "exe"); // Sometimes trys to open the dll instead of exe if (Properties.Settings.Default.FirstRun && !runAsAdmin) // On first run, put into admin mode to allow defender. { StartAsAdmin(ExeLoc); } else if (Properties.Settings.Default.FirstRun && runAsAdmin) { try { // Use dynamic for COM interop Type ruleType = Type.GetTypeFromProgID("HNetCfg.FWRule"); Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); dynamic firewallRule = Activator.CreateInstance(ruleType); dynamic firewallPolicy = Activator.CreateInstance(policyType); firewallRule.ApplicationName = ExeLoc; firewallRule.Action = 1; // NET_FW_ACTION_ALLOW firewallRule.Description = "Programmatically added rule to allow the GUI to work"; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.Name = "AiQ_GUI"; firewallRule.Protocol = 17; // UDP firewallPolicy.Rules.Add(firewallRule); Properties.Settings.Default.FirstRun = false; Properties.Settings.Default.Save(); Application.Restart(); // Restart to not be in admin mode } catch (Exception ex) { Logging.LogErrorMessage("Failed to install firewall. " + ex.Message); MessageBox.Show("Sorry, but I couldn't install the firewall rule!"); } } } } }