From 82c90498a640084e30f97b778afaffee31380e5b Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 20 Jul 2019 00:28:35 +0200 Subject: [PATCH] Misc corrections --- ArchiSteamFarm/ArchiCryptoHelper.cs | 72 ++++++++++++++--------------- ArchiSteamFarm/ArchiHandler.cs | 19 ++++++-- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/ArchiSteamFarm/ArchiCryptoHelper.cs b/ArchiSteamFarm/ArchiCryptoHelper.cs index 611b5266a..444feb443 100644 --- a/ArchiSteamFarm/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/ArchiCryptoHelper.cs @@ -31,14 +31,14 @@ namespace ArchiSteamFarm { public static class ArchiCryptoHelper { private static byte[] EncryptionKey = Encoding.UTF8.GetBytes(nameof(ArchiSteamFarm)); - internal static string BruteforceSteamParentalCode(byte[] passwordHash, byte[] salt, bool derivedKey = true) { + internal static string BruteforceSteamParentalCode(byte[] passwordHash, byte[] salt, bool scrypt = true) { if ((passwordHash == null) || (salt == null)) { ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); return null; } - return derivedKey ? BruteforceSteamParentalCodeDerived(passwordHash, salt) : BruteforceSteamParentalCodePbkdf2(passwordHash, salt); + return scrypt ? BruteforceSteamParentalCodeScrypt(passwordHash, salt) : BruteforceSteamParentalCodePbkdf2(passwordHash, salt); } internal static string Decrypt(ECryptoMethod cryptoMethod, string encrypted) { @@ -93,40 +93,6 @@ namespace ArchiSteamFarm { EncryptionKey = Encoding.UTF8.GetBytes(key); } - private static string BruteforceSteamParentalCodeDerived(byte[] passwordHash, byte[] salt) { - if ((passwordHash == null) || (salt == null)) { - ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); - - return null; - } - - byte[] password = new byte[4]; - - for (char a = '0'; a <= '9'; a++) { - password[0] = (byte) a; - - for (char b = '0'; b <= '9'; b++) { - password[1] = (byte) b; - - for (char c = '0'; c <= '9'; c++) { - password[2] = (byte) c; - - for (char d = '0'; d <= '9'; d++) { - password[3] = (byte) d; - - byte[] passwordHashTry = SCrypt.ComputeDerivedKey(password, salt, 8192, 8, 1, null, passwordHash.Length); - - if (passwordHashTry.SequenceEqual(passwordHash)) { - return Encoding.UTF8.GetString(password); - } - } - } - } - } - - return null; - } - private static string BruteforceSteamParentalCodePbkdf2(byte[] passwordHash, byte[] salt) { if ((passwordHash == null) || (salt == null)) { ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); @@ -163,6 +129,40 @@ namespace ArchiSteamFarm { return null; } + private static string BruteforceSteamParentalCodeScrypt(byte[] passwordHash, byte[] salt) { + if ((passwordHash == null) || (salt == null)) { + ASF.ArchiLogger.LogNullError(nameof(passwordHash) + " || " + nameof(salt)); + + return null; + } + + byte[] password = new byte[4]; + + for (char a = '0'; a <= '9'; a++) { + password[0] = (byte) a; + + for (char b = '0'; b <= '9'; b++) { + password[1] = (byte) b; + + for (char c = '0'; c <= '9'; c++) { + password[2] = (byte) c; + + for (char d = '0'; d <= '9'; d++) { + password[3] = (byte) d; + + byte[] passwordHashTry = SCrypt.ComputeDerivedKey(password, salt, 8192, 8, 1, null, passwordHash.Length); + + if (passwordHashTry.SequenceEqual(passwordHash)) { + return Encoding.UTF8.GetString(password); + } + } + } + } + } + + return null; + } + private static string DecryptAES(string encrypted) { if (string.IsNullOrEmpty(encrypted)) { ASF.ArchiLogger.LogNullError(nameof(encrypted)); diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index 8016ec717..4e334737c 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -25,6 +25,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Net; +using System.Security.Cryptography; using System.Threading.Tasks; using ArchiSteamFarm.CMsgs; using ArchiSteamFarm.Localization; @@ -665,15 +666,15 @@ namespace ArchiSteamFarm { return (false, null); } - bool derivedKey; + bool scrypt; switch (body.settings.passwordhashtype) { case 4: - derivedKey = false; + scrypt = false; break; case 6: - derivedKey = true; + scrypt = true; break; default: @@ -695,7 +696,15 @@ namespace ArchiSteamFarm { } if (i >= steamParentalCode.Length) { - byte[] passwordHash = derivedKey ? SCrypt.ComputeDerivedKey(password, body.settings.salt, 8192, 8, 1, null, body.settings.passwordhash.Length) : SCrypt.GetEffectivePbkdf2Salt(password, body.settings.salt, 8192, 8, 1, null); + byte[] passwordHash; + + if (scrypt) { + passwordHash = SCrypt.ComputeDerivedKey(password, body.settings.salt, 8192, 8, 1, null, body.settings.passwordhash.Length); + } else { + using (KeyedHashAlgorithm hmacAlgorithm = KeyedHashAlgorithm.Create()) { + passwordHash = Pbkdf2.ComputeDerivedKey(hmacAlgorithm, body.settings.salt, 10000, body.settings.passwordhash.Length); + } + } if (passwordHash.SequenceEqual(body.settings.passwordhash)) { return (true, steamParentalCode); @@ -705,7 +714,7 @@ namespace ArchiSteamFarm { ArchiLogger.LogGenericInfo(Strings.PleaseWait); - steamParentalCode = ArchiCryptoHelper.BruteforceSteamParentalCode(body.settings.passwordhash, body.settings.salt, derivedKey); + steamParentalCode = ArchiCryptoHelper.BruteforceSteamParentalCode(body.settings.passwordhash, body.settings.salt, scrypt); ArchiLogger.LogGenericInfo(Strings.Done);