修复常量加密bytes类型的bug。暂时先注释掉这个支持

backup
walon 2025-05-12 10:36:18 +08:00
parent 4986705b95
commit eec68decac
2 changed files with 50 additions and 38 deletions

View File

@ -42,6 +42,8 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
return _dataObfuscatorPolicy.NeedObfuscateMethod(method); return _dataObfuscatorPolicy.NeedObfuscateMethod(method);
} }
private readonly HashSet<FieldDef> _encryptedRvaFields = new HashSet<FieldDef>();
protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, BasicBlock block, int instructionIndex, IList<Instruction> globalInstructions, protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, BasicBlock block, int instructionIndex, IList<Instruction> globalInstructions,
List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions) List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
{ {
@ -127,25 +129,34 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
} }
case OperandType.InlineMethod: case OperandType.InlineMethod:
{ {
if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") //if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
{ //{
Instruction prevInst = globalInstructions[instructionIndex - 1]; // Instruction prevInst = globalInstructions[instructionIndex - 1];
if (prevInst.OpCode.Code == Code.Ldtoken) // if (prevInst.OpCode.Code == Code.Ldtoken)
{ // {
IField rvaField = (IField)prevInst.Operand; // IField rvaField = (IField)prevInst.Operand;
FieldDef ravFieldDef = rvaField.ResolveFieldDefThrow(); // FieldDef ravFieldDef = rvaField.ResolveFieldDefThrow();
byte[] data = ravFieldDef.InitialValue; // byte[] data = ravFieldDef.InitialValue;
if (data != null && _dataObfuscatorPolicy.NeedObfuscateArray(method, currentInLoop, data)) // if (data != null && _dataObfuscatorPolicy.NeedObfuscateArray(method, currentInLoop, data))
{ // {
// remove prev ldtoken instruction // if (_encryptedRvaFields.Add(ravFieldDef))
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; // // 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; return false;
} }
default: return false; default: return false;

View File

@ -132,27 +132,28 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
public void ObfuscateBytes(MethodDef method, bool needCacheValue, byte[] value, List<Instruction> obfuscatedInstructions) public void ObfuscateBytes(MethodDef method, bool needCacheValue, byte[] value, List<Instruction> obfuscatedInstructions)
{ {
if (needCacheValue) throw new NotSupportedException("ObfuscateBytes is not supported yet.");
{ //if (needCacheValue)
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value); //{
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField)); // FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
return; // obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
} // return;
//}
int ops = GenerateEncryptionOperations(); //int ops = GenerateEncryptionOperations();
int salt = GenerateSalt(); //int salt = GenerateSalt();
byte[] encryptedValue = _encryptor.Encrypt(value, 0, value.Length, ops, salt); //byte[] encryptedValue = _encryptor.Encrypt(value, 0, value.Length, ops, salt);
Assert.IsTrue(encryptedValue.Length % 4 == 0); //Assert.IsTrue(encryptedValue.Length % 4 == 0);
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue); //RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
DefaultMetadataImporter importer = GetModuleMetadataImporter(method); //DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field)); //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset)); //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. //// 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(value.Length));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops)); //obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt)); //obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaBytes)); //obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFromRvaBytes));
} }
public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions) public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions)