Esempio n. 1
0
        /// <summary>
        /// Rearm Microsoft Office
        /// </summary>
        /// <returns>Output of Rearm Result and Any Errors</returns>
        public static string Rearm()
        {
            try
            {
                using (StringWriter output = new StringWriter())
                {
                    // Get Rearm Tool Path
                    if (String.IsNullOrWhiteSpace(_ospprearm))
                    {
                        _ospprearm = GetRearmToolPath();
                    }

                    // Rearm with SKUIDs if Microsoft Office uses Windows Licensing Services
                    if (OfficeVersion.IsOfficeSPP())
                    {
                        LicenseList licenses = new LicenseListOffice();

                        output.WriteLine("---Processing--------------------------");
                        output.Write("----------------------------------------");
                        foreach (LicenseInstance license in licenses.GetListLicensed())
                        {
                            output.WriteLine();
                            output.WriteLine("Installed license detected - attempting to rearm the following product:");
                            output.WriteLine("Name: " + license.LicenseName);
                            output.WriteLine("Description: " + license.LicenseDescription);
                            output.WriteLine("SKU ID: " + license.SKUID);
                            Result result = CommonUtilities.ExecuteCommand(_ospprearm + " {" + license.SKUID + "}", true, true);

                            if (result.HasError)
                            {
                                // Show Rearm Error
                                string errorCode = Regex.Match(result.Error, "[0][x][A-Fa-f0-9]{8}").ToString().ToUpper().Replace("X", "x");
                                output.WriteLine("ERROR CODE: " + errorCode);
                                output.WriteLine("ERROR TEXT: " + LicenseErrorCode.GetErrorDescription(errorCode));
                                output.Write("<Product rearm failed>" + Environment.NewLine + "----------------------------------------");
                            }
                            else
                            {
                                output.Write("<Product rearm successful>" + Environment.NewLine + "----------------------------------------");
                            }
                        }
                    }
                    // Rearm without SKUIDs
                    else
                    {
                        Result result = CommonUtilities.ExecuteCommand(_ospprearm, true, true);

                        if (result.HasError)
                        {
                            output.WriteLine("<Microsoft Office rearm failed.>");
                            // Show Rearm Error
                            string errorCode = Regex.Match(result.Error, "[0][x][A-Fa-f0-9]{8}").ToString().ToUpper().Replace("X", "x");
                            output.WriteLine("ERROR CODE: " + errorCode);
                            output.WriteLine("ERROR TEXT: " + LicenseErrorCode.GetErrorDescription(errorCode));
                        }
                        else
                        {
                            output.WriteLine("<Microsoft Office rearm successful.>");
                        }
                    }
                    return(output.ToString());
                }
            }
            catch (COMException ex)
            {
                return("<Microsoft Office rearm failed>" + Environment.NewLine + LicenseErrorCode.GetErrorDescription("0x" + ex.ErrorCode.ToString("X8")));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Launch AutoKMS
        /// </summary>
        public void RunAutoKMS()
        {
            // Start Logging
            string logPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + AutoKMSInstaller.AutoKMSInstaller.AutoKMSLogFileName;

            // 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))
            {
                WriteLine("----------------------------------------");

                // Get the Program File Version
                FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
                WriteLine("AutoKMS " + myFileVersionInfo.FileVersion + " Ran At " + DateTime.Now + ".");

                // Check for Recent BSOD
                if (BSODTest() && Settings.Default.LocalHostBypassUseDLLInjection)
                {
                    // Log Early Termination
                    WriteLine("A recent unexpected shutdown or BSOD occured.");
                    WriteLine("AutoKMS will not continue until the next Scheduled run.");
                    WriteLine("----------------------------------------");

                    // Remake AutoKMS Tasks
                    if (AutoKMSInstaller.AutoKMSInstaller.IsAutoKMSCustomTaskInstalled() == false)
                    {
                        AutoKMSInstaller.AutoKMSInstaller.AutoKMSInstallPath = Environment.CurrentDirectory;
                        AutoKMSInstaller.AutoKMSInstaller.MakeAutoKMSNormalTask();
                    }
                    return;
                }

                // EZ-Activator Check
                bool     ezActivator = false;
                String[] arguments   = Environment.GetCommandLineArgs();
                foreach (string argument in arguments)
                {
                    if (String.Compare(argument, "/EZActivator", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        ezActivator = true;
                        break;
                    }
                }

                // Show Splash Screen
                Thread splashScreenThread = null;
                if (ezActivator)
                {
                    splashScreenThread = new Thread(ShowSplashScreen);
                    splashScreenThread.Start();
                }

                // Activate Microsoft Office
                if (OfficeVersion.IsOfficeSupported())
                {
                    // Only Activate KMS Licenses
                    LicenseList licenses = new LicenseListOffice();
                    if (!ezActivator)
                    {
                        licenses.List = licenses.GetListLicensed();
                        licenses.List = licenses.GetListKMS();
                    }
                    else
                    {
                        licenses.List = licenses.GetListKMS();
                    }

                    if (licenses.List.Count > 0)
                    {
                        // Handle KMS Server Service
                        if (Regex.IsMatch(Settings.Default.KMSServerOffice, @"^(127(\.\d+){1,3}|[0:]+1|localhost)$") || String.Compare(Settings.Default.KMSServerOffice, Environment.MachineName, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            if (KMSServerServiceInstaller.KMSServerServiceInstaller.IsKMSServerServiceInstalled())
                            {
                                Services.StartService(KMSServerServiceInstaller.KMSServerServiceInstaller.KMSServerServiceName);
                                Settings.Default.UseKMSEmulator = false;
                                WriteLine("Using KMS Server Service.");
                            }
                            else if (Settings.Default.UseKMSEmulator)
                            {
                                WriteLine("Using built-in KMS Server Emulator.");
                            }
                            else
                            {
                                WriteLine("WARNING: Attempting LocalHost KMS Activation without KMS Server Service or built-in KMS Server Emulator!");
                            }
                        }
                        else
                        {
                            WriteLine("Using KMS Server over the Network.");
                        }

                        if (!ezActivator)
                        {
                            WriteLine("Attempting To Activate Microsoft Office");
                            Write(ActivationState.AttemptActivationOffice(licenses, true, Settings.Default.KMSServerOffice, Settings.Default.KMSPortOffice, Settings.Default.KMSPIDOffice, Settings.Default.KMSHardwareID, Settings.Default.UseKMSEmulator, Settings.Default.RemoveKMSConnection, Settings.Default.KillProcessOnPort, Settings.Default.LocalHostBypassUseDLLInjection, Settings.Default.LocalHostBypassUseTAPAdapter, Settings.Default.LocalHostBypassUseWinDivertClient, Settings.Default.LocalHostBypassIPAddress, Settings.Default.LocalHostBypassIPSubnet));
                            WriteLine(string.Empty);
                        }
                        else
                        {
                            Write(EZActivator.RunEZActivator(licenses, true, Settings.Default.KMSServerOffice, Settings.Default.KMSPortOffice, Settings.Default.KMSPIDOffice, Settings.Default.KMSHardwareID, Settings.Default.UseKMSEmulator, Settings.Default.RemoveKMSConnection, Settings.Default.KillProcessOnPort, Settings.Default.LocalHostBypassUseDLLInjection, Settings.Default.LocalHostBypassUseTAPAdapter, Settings.Default.LocalHostBypassUseWinDivertClient, Settings.Default.LocalHostBypassIPAddress, Settings.Default.LocalHostBypassIPSubnet));
                            WriteLine(string.Empty);
                        }
                    }
                }
                // Activate Microsoft Windows
                if (OSVersion.IsWindowsSupported())
                {
                    // Only Activate KMS Licenses
                    LicenseList licenses = new LicenseListWindows();
                    if (!ezActivator)
                    {
                        licenses.List = licenses.GetListLicensed();
                        licenses.List = licenses.GetListKMS();
                    }
                    else
                    {
                        licenses.List = licenses.GetListKMS();
                    }

                    if (licenses.List.Count > 0)
                    {
                        // Handle KMS Server Service
                        if (Regex.IsMatch(Settings.Default.KMSServerWindows, @"^(127(\.\d+){1,3}|[0:]+1|localhost)$") || String.Compare(Settings.Default.KMSServerWindows, Environment.MachineName, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            if (KMSServerServiceInstaller.KMSServerServiceInstaller.IsKMSServerServiceInstalled())
                            {
                                Services.StartService(KMSServerServiceInstaller.KMSServerServiceInstaller.KMSServerServiceName);
                                Settings.Default.UseKMSEmulator = false;
                                WriteLine("Using KMS Server Service.");
                            }
                            else if (Settings.Default.UseKMSEmulator)
                            {
                                WriteLine("Using built-in KMS Server Emulator.");
                            }
                            else
                            {
                                WriteLine("WARNING: Attempting LocalHost KMS Activation without KMS Server Service or built-in KMS Server Emulator!");
                            }
                        }
                        else
                        {
                            WriteLine("Using KMS Server over the Network.");
                        }

                        if (!ezActivator)
                        {
                            WriteLine("Attempting To Activate Microsoft Windows");
                            Write(ActivationState.AttemptActivationWindows(licenses, true, Settings.Default.KMSServerWindows, Settings.Default.KMSPortWindows, Settings.Default.KMSPIDWindows, Settings.Default.KMSHardwareID, Settings.Default.UseKMSEmulator, Settings.Default.RemoveKMSConnection, Settings.Default.KillProcessOnPort, Settings.Default.LocalHostBypassUseDLLInjection, Settings.Default.LocalHostBypassUseTAPAdapter, Settings.Default.LocalHostBypassUseWinDivertClient, Settings.Default.LocalHostBypassIPAddress, Settings.Default.LocalHostBypassIPSubnet));
                            WriteLine(string.Empty);
                        }
                        else
                        {
                            Write(EZActivator.RunEZActivator(licenses, true, Settings.Default.KMSServerWindows, Settings.Default.KMSPortWindows, Settings.Default.KMSPIDWindows, Settings.Default.KMSHardwareID, Settings.Default.UseKMSEmulator, Settings.Default.RemoveKMSConnection, Settings.Default.KillProcessOnPort, Settings.Default.LocalHostBypassUseDLLInjection, Settings.Default.LocalHostBypassUseTAPAdapter, Settings.Default.LocalHostBypassUseWinDivertClient, Settings.Default.LocalHostBypassIPAddress, Settings.Default.LocalHostBypassIPSubnet));
                            WriteLine(string.Empty);
                        }
                    }
                }

                // Hide Splash Screen
                if (ezActivator)
                {
                    splashScreenThread.Abort();
                }
            }

            // Remake AutoKMS Tasks
            if (AutoKMSInstaller.AutoKMSInstaller.IsAutoKMSCustomTaskInstalled() == false)
            {
                AutoKMSInstaller.AutoKMSInstaller.AutoKMSInstallPath = Environment.CurrentDirectory;
                AutoKMSInstaller.AutoKMSInstaller.MakeAutoKMSNormalTask();
            }
        }
Esempio n. 3
0
        /// <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());
            }
        }