diff --git a/Editor/DynamicMemoryEncryption/DynamicMemoryEncryptionPass.cs b/Editor/DynamicMemoryEncryption/DynamicMemoryEncryptionPass.cs deleted file mode 100644 index 9480a3c..0000000 --- a/Editor/DynamicMemoryEncryption/DynamicMemoryEncryptionPass.cs +++ /dev/null @@ -1,29 +0,0 @@ -using dnlib.DotNet; -using dnlib.DotNet.Emit; -using Obfuz; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class DynamicMemoryEncryptionPass : MethodBodyObfuscationPassBase -{ - public override void Start(ObfuscatorContext ctx) - { - - } - - public override void Stop(ObfuscatorContext ctx) - { - - } - - protected override bool NeedObfuscateMethod(MethodDef method) - { - return true; - } - - protected override bool TryObfuscateInstruction(MethodDef callingMethod, Instruction inst, IList instructions, int instructionIndex, List outputInstructions, List totalFinalInstructions) - { - throw new System.NotImplementedException(); - } -} diff --git a/Editor/MemEncrypt/ConfigEncryptionPolicy.cs b/Editor/MemEncrypt/ConfigEncryptionPolicy.cs new file mode 100644 index 0000000..3fd650f --- /dev/null +++ b/Editor/MemEncrypt/ConfigEncryptionPolicy.cs @@ -0,0 +1,39 @@ +using dnlib.DotNet; + +namespace Obfuz.MemEncrypt +{ + public class ConfigEncryptionPolicy : EncryptionPolicyBase + { + + private bool IsSupportedFieldType(TypeSig type) + { + type = type.RemovePinnedAndModifiers(); + switch (type.ElementType) + { + case ElementType.I4: + case ElementType.I8: + case ElementType.U4: + case ElementType.U8: + case ElementType.R4: + case ElementType.R8: + return true; + default: return false; + } + } + + public override bool NeedEncrypt(FieldDef field) + { + TypeDef type = field.DeclaringType; + if (!IsSupportedFieldType(field.FieldType)) + { + return false; + } + // TODO + if (type.Name == "EncryptField" || type.Name == "EncryptProperty") + { + return true; + } + return false; + } + } +} diff --git a/Editor/MemEncrypt/EncryptionPolicyBase.cs b/Editor/MemEncrypt/EncryptionPolicyBase.cs new file mode 100644 index 0000000..3552081 --- /dev/null +++ b/Editor/MemEncrypt/EncryptionPolicyBase.cs @@ -0,0 +1,9 @@ +using dnlib.DotNet; + +namespace Obfuz.MemEncrypt +{ + public abstract class EncryptionPolicyBase : IEncryptionPolicy + { + public abstract bool NeedEncrypt(FieldDef field); + } +} diff --git a/Editor/MemEncrypt/IEncryptionPolicy.cs b/Editor/MemEncrypt/IEncryptionPolicy.cs new file mode 100644 index 0000000..fb167b5 --- /dev/null +++ b/Editor/MemEncrypt/IEncryptionPolicy.cs @@ -0,0 +1,14 @@ +using dnlib.DotNet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.MemEncrypt +{ + public interface IEncryptionPolicy + { + bool NeedEncrypt(FieldDef field); + } +} diff --git a/Editor/MemEncrypt/MemoryEncryptionPass.cs b/Editor/MemEncrypt/MemoryEncryptionPass.cs new file mode 100644 index 0000000..06eb3b2 --- /dev/null +++ b/Editor/MemEncrypt/MemoryEncryptionPass.cs @@ -0,0 +1,90 @@ +using dnlib.DotNet; +using dnlib.DotNet.Emit; +using Obfuz; +using Obfuz.MemEncrypt; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Obfuz.MemEncrypt +{ + + public class MemoryEncryptionPass : MethodBodyObfuscationPassBase + { + private readonly IEncryptionPolicy _encryptionPolicy = new ConfigEncryptionPolicy(); + + public override void Start(ObfuscatorContext ctx) + { + + } + + public override void Stop(ObfuscatorContext ctx) + { + + } + + protected override bool NeedObfuscateMethod(MethodDef method) + { + return true; + } + + private FieldDef TryResolveFieldDef(IField field) + { + if (field is FieldDef fieldDef) + { + return fieldDef; + } + if (field is MemberRef memberRef) + { + return memberRef.ResolveFieldDef(); + } + throw new System.Exception($"Cannot resolve field: {field}"); + } + + protected override bool TryObfuscateInstruction(MethodDef callingMethod, Instruction inst, IList instructions, int instructionIndex, List outputInstructions, List totalFinalInstructions) + { + Code code = inst.OpCode.Code; + if (!(inst.Operand is IField field)) + { + return false; + } + FieldDef fieldDef = TryResolveFieldDef(field); + if (fieldDef == null) + { + return false; + } + if (!_encryptionPolicy.NeedEncrypt(fieldDef)) + { + return false; + } + switch (code) + { + case Code.Ldfld: + { + break; + } + case Code.Stfld: + { + break; + } + case Code.Ldsfld: + { + break; + } + case Code.Stsfld: + { + break; + } + case Code.Ldflda: + case Code.Ldsflda: + { + throw new System.Exception($"You shouldn't get reference to memory encryption field: {field}"); + } + default: return false; + } + Debug.Log($"memory encrypt field: {field}"); + outputInstructions.Add(inst); + return true; + } + } +} diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index 40cc3a6..4c6808d 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -46,6 +46,7 @@ namespace Obfuz _obfuscationAssemblyNames = options.obfuscationAssemblyNames; _assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.assemblySearchDirs.ToArray())); + _pipeline.AddPass(new MemoryEncryptionPass()); _pipeline.AddPass(new ProxyCallPass()); _pipeline.AddPass(new ExprObfuscationPass()); _pipeline.AddPass(new DataVirtualizationPass());