Beispiel #1
0
        internal static string EncryptKey(string key)
        {
            var keyChars = ObscuredString.Encrypt(key.ToCharArray(), GetCryptoKey());

            key = Base64Utils.ToBase64(keyChars);
            return(key);
        }
Beispiel #2
0
        private static IEnumerable <AllowedAssembly> LoadAndParseLegacyWhitelist()
        {
            var result = new List <AllowedAssembly>();

            string[] separator = { InjectionConstants.DataSeparator };

            var fs = new FileStream(InjectionConstants.LegacyWhitelistRelativePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            var br = new BinaryReader(fs);

            try
            {
                var count = br.ReadInt32();

                for (var i = 0; i < count; i++)
                {
                    var line = br.ReadString();
                    line = new string(ObscuredString.Encrypt(line, ACTkConstants.StringKey));
                    var strArr       = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    var stringsCount = strArr.Length;
                    if (stringsCount > 1)
                    {
                        var assemblyName = strArr[0];

                        var hashes = new int[stringsCount - 1];
                        for (var j = 1; j < stringsCount; j++)
                        {
                            var parseResult = 0;
                            var success     = int.TryParse(strArr[j], out parseResult);
                            if (success)
                            {
                                hashes[j - 1] = parseResult;
                            }
                            else
                            {
                                Debug.LogError(ACTkConstants.LogPrefix + "Could not parse value: " + strArr[j] +
                                               ", line:\n" + line);
                            }
                        }

                        result.Add(new AllowedAssembly(assemblyName, hashes));
                    }
                    else
                    {
                        Debug.LogWarning(EditorTools.ConstructError("Error parsing whitelist file line!"));
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(ACTkConstants.LogPrefix + "Error while reading legacy whitelist:\n" + e);
            }
            finally
            {
                br.Close();
                fs.Close();
            }

            return(result);
        }
        private static void OnHashesGenerated(BuildReport report, BuildHashes[] hashedBuilds)
        {
            Debug.Log("CodeHashGeneratorListener example listener saying hello.");

            var whitelistedHashes = string.Empty;

            // Upload hashes to the server or do anything you would like to.
            //
            // Note, you may have multiple builds each with own hashes in some cases after build,
            // e.g. when using "Split APKs by target architecture" option.
            foreach (var hashedBuild in hashedBuilds)
            {
                hashedBuild.PrintToConsole();

                whitelistedHashes += hashedBuild.SummaryHash + GenuineValidatorExample.Separator;

                var fileHashes       = hashedBuild.FileHashes;
                var fileHashesLength = fileHashes.Length;
                for (var i = 0; i < fileHashesLength; i++)
                {
                    var fileHash = fileHashes[i];
                    whitelistedHashes += fileHash.Hash;

                    if (i != fileHashesLength - 1)
                    {
                        whitelistedHashes += GenuineValidatorExample.Separator;
                    }
                }
            }

            // for example, you may put hashes next to the standalone build to compare them offline
            // just as a proof of concept, but please consider uploading your hashes to the server
            // and make comparison on the server-side instead when possible to add cheaters some more pain

            var outputFolder = Path.GetDirectoryName(report.summary.outputPath);

            if (string.IsNullOrEmpty(outputFolder) || !Directory.Exists(outputFolder))
            {
                Debug.LogError(ACTkConstants.LogPrefix + "Couldn't find build folder!");
                return;
            }

            var filePath = Path.Combine(outputFolder, GenuineValidatorExample.FileName);

            // encrypt to hide hashes from the eye
            var encryptedValue = ObscuredString.Encrypt(whitelistedHashes, GenuineValidatorExample.StringKey);

            // now just get raw bytes and write them to the file to compare hashes in runtime
            var bytes = GenuineValidatorExample.UnicodeCharsToBytes(encryptedValue);

            File.WriteAllBytes(filePath, bytes);
        }
        private static void OnHashesGenerate(BuildReport report, Dictionary <string, string> buildHashes)
        {
            Debug.Log("CodeHashGeneratorListener example listener saying hello.");

            // Upload hashes to the server or do anything you would like to.
            //
            // Note, you may have multiple builds each with own hash in some cases after build,
            // e.g. when using "Split APKs by target architecture" option.
            foreach (var buildHash in buildHashes)
            {
                Debug.Log("Build: " + buildHash.Key + "\n" +
                          "Hash: " + buildHash.Value);
            }

            // for example, you may put hash next to the standalone build to compare it offline
            // just as a proof of concept, please consider uploading your hash to the server
            // and make comparison on the server-side to add some more pain to the cheaters\

            var firstBuildHash = buildHashes.FirstOrDefault().Value;

            if (string.IsNullOrEmpty(firstBuildHash))
            {
                Debug.LogError(ACTkConstants.LogPrefix + "Couldn't find first build hash!");
                return;
            }

            var outputFolder = Path.GetDirectoryName(report.summary.outputPath);

            if (string.IsNullOrEmpty(outputFolder) || !Directory.Exists(outputFolder))
            {
                Debug.LogError(ACTkConstants.LogPrefix + "Couldn't find build folder!");
                return;
            }

            var filePath          = Path.Combine(outputFolder, GenuineValidatorExample.FileName);
            var hashOfTheHashHaha = GenuineValidatorExample.GetHash(firstBuildHash + GenuineValidatorExample.HashSalt);

            // let's put together build hash with its hash and encrypt it using constant key
            var encryptedValue = ObscuredString.Encrypt(firstBuildHash + GenuineValidatorExample.Separator + hashOfTheHashHaha, GenuineValidatorExample.StringKey);

            // now just get raw bytes and write them to the file to compare hash in runtime
            var bytes = GenuineValidatorExample.UnicodeCharsToBytes(encryptedValue);

            File.WriteAllBytes(filePath, bytes);
        }
        private static void WriteAllowedAssemblies(List <AllowedAssembly> assemblies)
        {
            Directory.CreateDirectory(InjectionConstants.ResourcesFolder);
            var bw = new BinaryWriter(new FileStream(InjectionConstants.DataFilePath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.Unicode);

            bw.Write(assemblies.Count);

#if ACTK_DEBUG_VERBOSE
            sw.Stop();
            Debug.Log(ACTkConstants.LogPrefix + "Writing assemblies data, count: " + assemblies.Count);
            sw.Start();
#endif

            foreach (var assembly in assemblies)
            {
                var name   = assembly.name;
                var hashes = "";

                for (var j = 0; j < assembly.hashes.Length; j++)
                {
                    hashes += assembly.hashes[j].ToString(CultureInfo.InvariantCulture);
                    if (j < assembly.hashes.Length - 1)
                    {
                        hashes += InjectionConstants.DataSeparator;
                    }
                }

                var line = ObscuredString.Encrypt(name + InjectionConstants.DataSeparator + hashes, ACTkConstants.StringKey);

#if ACTK_DEBUG_PARANIOD
                sw.Stop();
                Debug.Log(ACTkConstants.LogPrefix + "Writing assembly:\n" + name + InjectionConstants.DataSeparator + hashes + "\n" +
                          new string(line) + ", length: " + line.Length);
                sw.Start();
#endif
                bw.Write(line.Length);
                bw.Write(line, 0, line.Length);
            }

            bw.Close();
        }