Ejemplo n.º 1
0
        /// <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());
            }
        }