diff --git a/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs b/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs index b115eb7..77fa5a7 100644 --- a/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs +++ b/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs @@ -42,6 +42,8 @@ namespace Obfuz.ObfusPasses.ConstEncrypt return _dataObfuscatorPolicy.NeedObfuscateMethod(method); } + private readonly HashSet _encryptedRvaFields = new HashSet(); + protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, BasicBlock block, int instructionIndex, IList globalInstructions, List outputInstructions, List totalFinalInstructions) { @@ -127,25 +129,34 @@ namespace Obfuz.ObfusPasses.ConstEncrypt } case OperandType.InlineMethod: { - if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") - { - Instruction prevInst = globalInstructions[instructionIndex - 1]; - if (prevInst.OpCode.Code == Code.Ldtoken) - { - IField rvaField = (IField)prevInst.Operand; - FieldDef ravFieldDef = rvaField.ResolveFieldDefThrow(); - byte[] data = ravFieldDef.InitialValue; - if (data != null && _dataObfuscatorPolicy.NeedObfuscateArray(method, currentInLoop, data)) - { - // remove prev ldtoken instruction - Assert.AreEqual(Code.Ldtoken, totalFinalInstructions[totalFinalInstructions.Count - 1].OpCode.Code); - totalFinalInstructions.RemoveAt(totalFinalInstructions.Count - 1); - bool needCache = currentInLoop ? constCachePolicy.cacheStringInLoop : constCachePolicy.cacheStringNotInLoop; - _dataObfuscator.ObfuscateBytes(method, needCache, data, outputInstructions); - return true; - } - } - } + //if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") + //{ + // Instruction prevInst = globalInstructions[instructionIndex - 1]; + // if (prevInst.OpCode.Code == Code.Ldtoken) + // { + // IField rvaField = (IField)prevInst.Operand; + // FieldDef ravFieldDef = rvaField.ResolveFieldDefThrow(); + // byte[] data = ravFieldDef.InitialValue; + // if (data != null && _dataObfuscatorPolicy.NeedObfuscateArray(method, currentInLoop, data)) + // { + // if (_encryptedRvaFields.Add(ravFieldDef)) + // { + + // } + + // // remove prev ldtoken instruction + // Assert.AreEqual(Code.Ldtoken, totalFinalInstructions.Last().OpCode.Code); + // //totalFinalInstructions.RemoveAt(totalFinalInstructions.Count - 1); + // // dup arr argument for decryption operation + // totalFinalInstructions.Insert(totalFinalInstructions.Count - 1, Instruction.Create(OpCodes.Dup)); + // totalFinalInstructions.Add(inst.Clone()); + // //bool needCache = currentInLoop ? constCachePolicy.cacheStringInLoop : constCachePolicy.cacheStringNotInLoop; + // bool needCache = false; + // _dataObfuscator.ObfuscateBytes(method, needCache, data, outputInstructions); + // return true; + // } + // } + //} return false; } default: return false; diff --git a/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs b/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs index 5e63c81..c7ab506 100644 --- a/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs +++ b/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs @@ -132,27 +132,28 @@ namespace Obfuz.ObfusPasses.ConstEncrypt public void ObfuscateBytes(MethodDef method, bool needCacheValue, byte[] value, List obfuscatedInstructions) { - if (needCacheValue) - { - FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value); - obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField)); - return; - } + throw new NotSupportedException("ObfuscateBytes is not supported yet."); + //if (needCacheValue) + //{ + // FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value); + // obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField)); + // return; + //} - int ops = GenerateEncryptionOperations(); - int salt = GenerateSalt(); - byte[] encryptedValue = _encryptor.Encrypt(value, 0, value.Length, ops, salt); - Assert.IsTrue(encryptedValue.Length % 4 == 0); - RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue); + //int ops = GenerateEncryptionOperations(); + //int salt = GenerateSalt(); + //byte[] encryptedValue = _encryptor.Encrypt(value, 0, value.Length, ops, salt); + //Assert.IsTrue(encryptedValue.Length % 4 == 0); + //RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue); - DefaultMetadataImporter importer = GetModuleMetadataImporter(method); - obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field)); - obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset)); - // should use value.Length, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length. - obfuscatedInstructions.Add(Instruction.CreateLdcI4(value.Length)); - obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); - obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); - obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaBytes)); + //DefaultMetadataImporter importer = GetModuleMetadataImporter(method); + //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field)); + //obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset)); + //// should use value.Length, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length. + //obfuscatedInstructions.Add(Instruction.CreateLdcI4(value.Length)); + //obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); + //obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); + //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaBytes)); } public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List obfuscatedInstructions)