Esempio n. 1
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            if (EnvironmentUtil.IsRunningLegacyOwin)
            {
                //Still running legacy Owin and using the DPAPI, we don't want to migrate
                logger.Debug(ID + " - Running Owin, no need to migrate from DPAPI");
                return(false);
            }

            Version dotNetVersion = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework.Version;
            bool    isWindows     = Environment.OSVersion.Platform == PlatformID.Win32NT;

            if (!isWindows && dotNetVersion.Major < 4)
            {
                // User isn't running Windows, but is running on .NET Core framework, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            object passwordPropertyValue = null;
            string passwordValue         = "";

            try
            {
                passwordPropertyValue = configData.GetType().GetProperty("Password").GetValue(configData, null);
                passwordValue         = passwordPropertyValue.GetType().GetProperty("Value").GetValue(passwordPropertyValue, null).ToString();
            }
            catch (Exception)
            {
                logger.Debug($"Unable to source password for [{ID}] while attempting migration, likely a public tracker");
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex);
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.GetType().GetProperty("Value").SetValue(passwordPropertyValue, unprotectedPassword);
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception);
                    }
                }
            }

            return(false);
        }
Esempio n. 2
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            bool isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;

            if (!isWindows && DotNetCoreUtil.IsRunningOnDotNetCore)
            {
                // User isn't running Windows, but is running on .NET Core framework, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            StringItem passwordPropertyValue = null;
            string     passwordValue         = "";

            try
            {
                // try dynamic items first (e.g. all cardigann indexers)
                passwordPropertyValue = (StringItem)configData.GetDynamicByName("password");

                if (passwordPropertyValue == null) // if there's no dynamic password try the static property
                {
                    passwordPropertyValue = (StringItem)configData.GetType().GetProperty("Password").GetValue(configData, null);

                    // protection is based on the item.Name value (property name might be different, example: Abnormal), so check the Name again
                    if (!string.Equals(passwordPropertyValue.Name, "password", StringComparison.InvariantCultureIgnoreCase))
                    {
                        logger.Debug($"Skipping non default password property (unencrpyted password) for [{ID}] while attempting migration");
                        return(false);
                    }
                }

                passwordValue = passwordPropertyValue.Value;
            }
            catch (Exception)
            {
                logger.Debug($"Unable to source password for [{ID}] while attempting migration, likely a tracker without a password setting");
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex);
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.Value = unprotectedPassword;
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception);
                    }
                }
            }

            return(false);
        }
Esempio n. 3
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            var  currentAssembly   = Assembly.GetExecutingAssembly();
            bool runningLegacyOwin = new StackTrace().GetFrames()
                                     .Select(x => x.GetMethod().ReflectedType.Assembly).Distinct()
                                     .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName == currentAssembly.FullName))
                                     .Where(x => x.ManifestModule.Name == "Jackett.dll" || x.ManifestModule.Name == "JackettConsole.exe")
                                     .Count() == 2;

            if (runningLegacyOwin)
            {
                //Still running legacy Owin and using the DPAPI, we don't want to migrate
                logger.Debug(ID + " - Running Owin, no need to migrate from DPAPI");
                return(false);
            }

            Version dotNetVersion = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework.Version;
            bool    isWindows     = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

            if (!isWindows && dotNetVersion.Major < 4)
            {
                // User isn't running Windows, but is running on .NET Core framewrok, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            object passwordPropertyValue = null;
            string passwordValue         = "";

            try
            {
                passwordPropertyValue = configData.GetType().GetProperty("Password").GetValue(configData, null);
                passwordValue         = passwordPropertyValue.GetType().GetProperty("Value").GetValue(passwordPropertyValue, null).ToString();
            }
            catch (Exception ex)
            {
                logger.Warn($"Attempt to source password from json failed - {ID} : " + ex.ToString());
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex.ToString());
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.GetType().GetProperty("Value").SetValue(passwordPropertyValue, unprotectedPassword);
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception.ToString());
                    }
                }
            }

            return(false);
        }