/// <summary> /// Obtain a Byte Array List Representing PkeyConfig for Microsoft Office PIDX Check from Program /// </summary> /// <param name="product">Name of Microsoft Office Product to Choose based on selection</param> /// <returns>Byte Array Representations of PkeyConfig.xrm-ms files</returns> public static List <byte[]> GetPkeyConfigOffice(string product = "") { // Choose by Product if Provided if (String.IsNullOrWhiteSpace(product)) { // Get Microsoft Office Edition product = OfficeVersion.GetOfficeName(); } // Choose PkeyConfigs based on Microsoft Office Edition switch (product) { case OfficeVersion.Office2010: return(new List <byte[]> { Resources.pkeyconfig_office2010, Resources.pkeyconfig_office2010csvlk, Resources.pkeyconfig_office2010web }); case OfficeVersion.Office2013: return(new List <byte[]> { Resources.pkeyconfig_office2013, Resources.pkeyconfig_office2013csvlk, Resources.pkeyconfig_office2013web }); case OfficeVersion.Office2016: return(new List <byte[]> { Resources.pkeyconfig_office2016, Resources.pkeyconfig_office2016csvlk, Resources.pkeyconfig_office2016web }); case OfficeVersion.Office2019: return(new List <byte[]> { Resources.pkeyconfig_office2019, Resources.pkeyconfig_office2019csvlk //Resources.pkeyconfig_office2019web }); default: throw new ApplicationException("No PkeyConfig Matching this Product!"); } }
/// <summary> /// Install KMS Keys Automatically and Perform Activation of All KMS Licenses /// </summary> /// <param name="licenses">List of all Licenses</param> /// <param name="minimalOutput">Reduce the Amount of Output During Activation</param> /// <param name="kmsServer">KMS Host to Connect To</param> /// <param name="kmsPort">KMS Port to Connect To</param> /// <param name="kmsPid">KMS PID to apply to KMSEmulator</param> /// <param name="kmsHwid">KMS Hardware ID to apply to KMSEmulator</param> /// <param name="useKMSEmulator">Start a KMSEmulator Process</param> /// <param name="removeKMSConnection">Remove KMS Host and Port after Activation</param> /// <param name="killProcessOnPort">Force Start KMSEmulator by Killing Other Processes usign the KMS Port</param> /// <param name="useDLLInjection">Use DLL Injection to Force Localhost KMS Activation</param> /// <param name="useTAPAdapter">Use TAP Adapter to Force Localhost KMS Activation</param> /// <param name="useWinDivert">Use WinDivert Client to Force Localhost KMS Activation</param> /// <param name="localHostBypassIPAddress">IP Address of TAP Adapter NIC or WinDivert Client</param> /// <param name="localHostBypassIPSubnet">Subnet Mask for TAP Adapter or WinDivert Client Network</param> /// <returns>Activation Result of all KMS Licenses</returns> public static string RunEZActivator(LicenseList licenses, bool minimalOutput = true, string kmsServer = "127.0.0.2", int kmsPort = 1688, string kmsPid = "RandomKMSPID", string kmsHwid = "364F463A8863D35F", bool useKMSEmulator = true, bool removeKMSConnection = false, bool killProcessOnPort = false, bool useDLLInjection = false, bool useTAPAdapter = false, bool useWinDivert = false, string localHostBypassIPAddress = "10.3.0.1", string localHostBypassIPSubnet = "255.255.255.0") { // Show Activation Errors if No Licenses Exist if (licenses.GetListUnlicensed().Count == 0 && licenses.GetListLicensed().Count == 0) { return(LicenseErrorCode.ErrBroken); } // Show Activation Errors if No KMS Licenses Exist if (licenses.GetListKMS().Count == 0) { return(LicenseErrorCode.ErrNoKMS); } using (StringWriter output = new StringWriter()) { // Activated Windows Check if (licenses is LicenseListWindows && licenses.GetListPermanentlyActivated().Count > 0) { output.WriteLine("----------------------------------------"); output.WriteLine("Windows is already permanently activated."); output.WriteLine("----------------------------------------"); return(output.ToString()); } // Get All Possible KMS Keys KeyList keys = new KeyList(); if (licenses is LicenseListOffice) { keys = KeyBase.GetApplicableKeysList(OfficeVersion.GetOfficeName()); } else if (licenses is LicenseListWindows) { keys = KeyBase.GetApplicableKeysList(OSVersion.GetWindowsName()); } // Remove Trial Keys bool removedTrialKeys = false; output.WriteLine("----------------------------------------"); output.WriteLine("Removing Any Trial/Grace Keys."); foreach (LicenseInstance licenseKeys in licenses.GetListLicensed()) { if (licenseKeys.LicenseDescription.ToUpper().Contains("TRIAL") || licenseKeys.LicenseDescription.ToUpper().Contains("GRACE")) { if (licenses is LicenseListOffice) { KeyInstaller.UnInstallKeyByKeyOffice(licenseKeys.PartialProductKey); output.WriteLine("Removed Key for: " + licenseKeys.LicenseFamily + " (" + licenseKeys.PartialProductKey + ")."); removedTrialKeys = true; } else if (licenses is LicenseListWindows) { KeyInstaller.UnInstallKeyByKeyOffice(licenseKeys.PartialProductKey); output.WriteLine("Removed Key for: " + licenseKeys.LicenseFamily + " (" + licenseKeys.PartialProductKey + ")."); removedTrialKeys = true; } } } if (removedTrialKeys) { licenses.Refresh(); } // Install Uninstalled KMS Keys by SKUID Match bool installedKMSKeys = false; output.WriteLine("----------------------------------------"); output.WriteLine("Installing Any Matching Volume Keys."); if (licenses is LicenseListWindows && OSVersion.GetWindowsNumber() >= 10 && OSVersion.GetWindowsBuildNumber() >= 14393) { // Determine KMS Key if (licenses.GetListLicensed().Count > 0) { // SKU Match Existing Key foreach (LicenseInstance license in licenses.GetListLicensed()) { string editionId = license.LicenseFamily; try { // Check Key bool installKey = true; List <string> skuidListMatched = keys.GetSKUIDs(editionId); foreach (LicenseInstance licensed in licenses.GetListLicensed()) { if (skuidListMatched.Contains(licensed.SKUID)) { installKey = false; break; } } // Install Key if (installKey) { // Get All SKUIDs List <string> skuidListAll = new List <string>(); foreach (LicenseInstance licensed in licenses.GetListFull()) { skuidListAll.Add(licensed.SKUID); } // Get Matched Key foreach (string skuid in skuidListMatched) { if (skuidListAll.Contains(skuid)) { output.WriteLine("Installing " + keys.GetProductName(skuid) + " KMS Key (" + keys.GetProductKey(skuid) + ")."); KeyInstaller.InstallKeyWindows(keys.GetProductKey(skuid)); output.WriteLine("<Product key installation successful>"); installedKMSKeys = true; break; } } } } catch (Exception ex) { output.WriteLine(ex.Message); } } } else { // Edition ID Check using (RegistryKey registrySubKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true)) { if (registrySubKey != null && registrySubKey.GetValue("EditionID") != null) { string editionId = registrySubKey.GetValue("EditionID").ToString(); try { if (keys.GetProductKey(editionId) != string.Empty) { output.WriteLine("Installing " + keys.GetProductName(editionId) + " KMS Key (" + keys.GetProductKey(editionId) + ")."); KeyInstaller.InstallKeyWindows(keys.GetProductKey(editionId)); output.WriteLine("<Product key installation successful>"); installedKMSKeys = true; } } catch (Exception ex) { output.WriteLine(ex.Message); } } } } } else { foreach (LicenseInstance license in licenses.GetListUnlicensed()) { if (keys.GetSKUIDs().Contains(license.SKUID)) { try { output.WriteLine("Installing " + keys.GetProductName(license.SKUID) + " KMS Key (" + keys.GetProductKey(license.SKUID) + ")."); if (licenses is LicenseListOffice) { KeyInstaller.InstallKeyOffice(keys.GetProductKey(license.SKUID)); output.WriteLine("<Product key installation successful>"); installedKMSKeys = true; } else if (licenses is LicenseListWindows) { KeyInstaller.InstallKeyWindows(keys.GetProductKey(license.SKUID)); output.WriteLine("<Product key installation successful>"); installedKMSKeys = true; } } catch (Exception ex) { output.WriteLine(ex.Message); } } } } output.WriteLine("----------------------------------------"); if (installedKMSKeys) { licenses.Refresh(); } // Perform Activation if (licenses is LicenseListOffice) { output.WriteLine("Attempting To Activate Microsoft Office"); output.Write(ActivationState.AttemptActivationOffice(licenses, minimalOutput, kmsServer, kmsPort, kmsPid, kmsHwid, useKMSEmulator, removeKMSConnection, killProcessOnPort, useDLLInjection, useTAPAdapter, useWinDivert, localHostBypassIPAddress, localHostBypassIPSubnet)); } else if (licenses is LicenseListWindows) { output.WriteLine("Attempting To Activate Microsoft Windows"); output.Write(ActivationState.AttemptActivationWindows(licenses, minimalOutput, kmsServer, kmsPort, kmsPid, kmsHwid, useKMSEmulator, removeKMSConnection, killProcessOnPort, useDLLInjection, useTAPAdapter, useWinDivert, localHostBypassIPAddress, localHostBypassIPSubnet)); } return(output.ToString()); } }
/// <summary> /// Uninstall AutoRearm and Remove Scheduled Task /// </summary> /// <returns>Output of Uninstallation Process</returns> public static string UninstallAutoRearm() { using (StringWriter output = new StringWriter()) { // Get Program File Path try { AutoRearmInstallPath = GetAutoRearmPath(); } catch (Exception) { output.WriteLine("Failed to access AutoRearm installation path!"); return(output.ToString()); } // Restore AutoRearm Backup try { string restoreoutput = LicenseBackup.RestoreOfficeTrustedStore("AutoRearmBackup", AutoRearmInstallPath + "Backups\\" + OfficeVersion.GetOfficeName() + Path.DirectorySeparatorChar); if (restoreoutput.Contains("Failed")) { throw new Exception(); } output.WriteLine("Restored License Backup from when you installed AutoRearm"); } catch (Exception) { output.WriteLine("Failed to Restore License Backup from when you installed AutoRearm"); } // Delete AutoRearm Backups folder try { CommonUtilities.FolderDelete(AutoRearmInstallPath + "Backups\\" + OfficeVersion.GetOfficeName() + Path.DirectorySeparatorChar); output.WriteLine("Deleted " + AutoRearmInstallPath + "Backups\\" + OfficeVersion.GetOfficeName() + Path.DirectorySeparatorChar); } catch (Exception) { output.WriteLine("Failed to delete AutoRearm Backups directory"); } // Delete Program File try { CommonUtilities.FileDelete(AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); output.WriteLine("Deleted " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } catch (Exception) { output.WriteLine("Failed to delete " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } // Delete Program Log try { CommonUtilities.FileDelete(AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmLogFileName); output.WriteLine("Deleted " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmLogFileName); } catch (Exception) { output.WriteLine("Failed to delete " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmLogFileName); } // Delete Scheduled Tasks try { DeleteAutoRearmNormalTask(); output.WriteLine("Deleted AutoRearm Scheduled Tasks"); } catch (Exception) { output.WriteLine("Failed to delete AutoRearm Scheduled Tasks"); } // Remove Windows Defender Exclusion if (OSVersion.GetWindowsNumber() >= 10.0) { CommonUtilities.ExecuteCommand("powershell.exe Remove-MpPreference -ExclusionPath " + CommonUtilities.EscapePath(AutoRearmInstallPath) + " -Force", true); } if (output.ToString().Contains("Failed")) { output.Write("AutoRearm failed to uninstall!"); } else { // Delete Install Folder If Empty CommonUtilities.DeleteEmptyFolders(AutoRearmInstallPath + Path.DirectorySeparatorChar); output.Write("AutoRearm was succesfully uninstalled."); } return(output.ToString()); } }
/// <summary> /// Install AutoRearm and Setup Scheduled Task /// </summary> /// <param name="installPath">Path to write Program File</param> /// <param name="autoRearm">Byte Array representation of AutoRearm Program File</param> /// <returns>Output of Installation Process</returns> public static string InstallAutoRearm(string installPath, byte[] autoRearm) { using (StringWriter output = new StringWriter()) { LicenseList licenses = new LicenseListOffice(); // Show Activation Errors if No Licenses or Keys Exist if (licenses.GetListUnlicensed().Count == 0 && licenses.GetListLicensed().Count == 0) { output.WriteLine(LicenseErrorCode.ErrBroken); return(output.ToString()); } if (licenses.GetListUnlicensed().Count > 0 && licenses.GetListLicensed().Count == 0) { output.WriteLine(LicenseErrorCode.ErrKeyless); return(output.ToString()); } // Get Rearm Count try { int rearmCount = RearmOffice.GetRearmCount(); if (rearmCount < 1) { output.Write("You must have at least 1 available rearm to use this tool." + Environment.NewLine + "You have " + rearmCount + " rearms."); return(output.ToString()); } output.WriteLine("You have " + rearmCount + " rearms."); } catch (Exception) { output.Write("Failed to check rearm count. AutoRearm cannot be installed"); return(output.ToString()); } // Create Program File try { try { AutoRearmInstallPath = installPath; // Create Windows Defender Exclusion if (OSVersion.GetWindowsNumber() >= 10.0) { CommonUtilities.ExecuteCommand("powershell.exe Add-MpPreference -ExclusionPath " + CommonUtilities.EscapePath(AutoRearmInstallPath) + " -Force", true); } // Create AutoRearm CommonUtilities.FileCreate(AutoRearmFileName, autoRearm, AutoRearmInstallPath); output.WriteLine("Created " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } catch (Exception) { AutoRearmInstallPath = Environment.GetEnvironmentVariable("windir") + Path.DirectorySeparatorChar + AutoRearmProcessName; output.WriteLine("Could not access AutoRearm path set in Settings, using default installation path."); CommonUtilities.FileCreate(AutoRearmFileName, autoRearm, AutoRearmInstallPath); output.WriteLine("Created " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } } catch (Exception) { // Remove Windows Defender Exclusion if (OSVersion.GetWindowsNumber() >= 10.0) { CommonUtilities.ExecuteCommand("powershell.exe Remove-MpPreference -ExclusionPath " + CommonUtilities.EscapePath(AutoRearmInstallPath) + " -Force", true); } output.WriteLine("Failed to create " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); output.Write("AutoRearm failed to install!"); return(output.ToString()); } // Create Scheduled Tasks try { MakeAutoRearmNormalTask(); output.WriteLine("Created AutoRearm Scheduled Tasks"); } catch (Exception ex) { output.WriteLine("Failed to create AutoRearm Scheduled Tasks"); output.WriteLine(ex.Message); output.WriteLine("Cleaning up as AutoRearm is partially installed"); // Delete Program File try { CommonUtilities.FileDelete(AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); output.WriteLine("Deleted " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } catch (Exception) { output.WriteLine("Failed to delete " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } // Remove Windows Defender Exclusion if (OSVersion.GetWindowsNumber() >= 10.0) { CommonUtilities.ExecuteCommand("powershell.exe Remove-MpPreference -ExclusionPath " + CommonUtilities.EscapePath(AutoRearmInstallPath) + " -Force", true); } output.Write("AutoRearm failed to install!"); return(output.ToString()); } // Make License Backup try { LicenseBackup.Backup("AutoRearmBackup", AutoRearmInstallPath + "\\Backups\\" + OfficeVersion.GetOfficeName() + Path.DirectorySeparatorChar, licenses); output.WriteLine("Created License Backup Of Current Configuration"); } catch (Exception) { output.WriteLine("Failed to create License Backup Of Current Configuration"); output.WriteLine("Cleaning up as AutoRearm is partially installed"); // Delete Program File try { CommonUtilities.FileDelete(AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); output.WriteLine("Deleted " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } catch (Exception) { output.WriteLine("Failed to delete " + AutoRearmInstallPath + Path.DirectorySeparatorChar + AutoRearmFileName); } // Delete Scheduled Tasks try { DeleteAutoRearmNormalTask(); output.WriteLine("Deleted AutoRearm Scheduled Tasks"); } catch (Exception) { output.WriteLine("Failed to delete AutoRearm Scheduled Tasks"); } // Remove Windows Defender Exclusion if (OSVersion.GetWindowsNumber() >= 10.0) { CommonUtilities.ExecuteCommand("powershell.exe Remove-MpPreference -ExclusionPath " + CommonUtilities.EscapePath(AutoRearmInstallPath) + " -Force", true); } output.Write("AutoRearm failed to install!"); return(output.ToString()); } output.Write("AutoRearm was succesfully installed."); return(output.ToString()); } }
/// <summary> /// Launch AutoRearm /// </summary> public void RunAutoRearm() { // Start Logging string logPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + AutoRearmInstaller.AutoRearmInstaller.AutoRearmLogFileName; // Delete Log if Greater than 10MB FileInfo logFile = new FileInfo(logPath); if (logFile.Exists && logFile.Length >= 10485760) { CommonUtilities.FileDelete(logPath); } // Access Log using (_log = new StreamWriter(logPath, true)) { // Get Lowest Grace Period if (OfficeVersion.IsOfficeSupported()) { // Rearm if we only have 5 days left LicenseList licenses = new LicenseListOffice(); if (RearmBase.LowestGraceDays(licenses) < 6) { // Get the Program File Version FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location); WriteLine("------------------------------------" + Environment.NewLine + "AutoRearm " + myFileVersionInfo.FileVersion + " Ran At " + DateTime.Now + "."); // Rearm if Possible, Restore Backup if Rearm Fails WriteLine("Rearming Office because grace period is almost expired."); if (RearmOffice.Rearm().Contains("failed")) { WriteLine("Failed to Rearm. Restoring License Backup."); string restoreoutput = LicenseBackup.RestoreOfficeTrustedStore("AutoRearmBackup", Environment.CurrentDirectory + "\\Backups\\" + OfficeVersion.GetOfficeName() + Path.DirectorySeparatorChar); if (restoreoutput.Contains("Failed") == false) { WriteLine("Successfully Restored License Backup. Rearming Office."); // Wait and Cycle OSPPSVC Services.StopOSPPSVC(); Thread.Sleep(2000); Services.StartOSPPSVC(); Thread.Sleep(2000); if (RearmOffice.Rearm().Contains("failed")) { WriteLine("Failed to Rearm."); } else { WriteLine("Successfully Rearmed."); } } } else { WriteLine("Successfully Rearmed."); } } } else { WriteLine("Office Is Not Installed!"); } } // Remake AutoRearm Tasks AutoRearmInstaller.AutoRearmInstaller.AutoRearmInstallPath = Environment.CurrentDirectory; //AutoRearmInstaller.AutoRearmInstaller.DeleteAutoRearmNormalTask(); AutoRearmInstaller.AutoRearmInstaller.MakeAutoRearmNormalTask(); }