提交部分MemoryEncryption的代码

backup
walon 2025-04-30 21:47:21 +08:00
parent fa4eb5db21
commit bf9dbefda9
6 changed files with 153 additions and 29 deletions

View File

@ -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<Instruction> instructions, int instructionIndex, List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
{
throw new System.NotImplementedException();
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,9 @@
using dnlib.DotNet;
namespace Obfuz.MemEncrypt
{
public abstract class EncryptionPolicyBase : IEncryptionPolicy
{
public abstract bool NeedEncrypt(FieldDef field);
}
}

View File

@ -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);
}
}

View File

@ -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<Instruction> instructions, int instructionIndex, List<Instruction> outputInstructions, List<Instruction> 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;
}
}
}

View File

@ -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());