2025-04-30 21:47:21 +08:00
|
|
|
using dnlib.DotNet;
|
|
|
|
using dnlib.DotNet.Emit;
|
|
|
|
using Obfuz;
|
2025-05-10 19:50:03 +08:00
|
|
|
using Obfuz.Settings;
|
2025-04-30 21:47:21 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2025-05-10 19:50:03 +08:00
|
|
|
using System.Linq;
|
2025-04-30 21:47:21 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
2025-05-10 19:09:44 +08:00
|
|
|
namespace Obfuz.ObfusPasses.FieldEncrypt
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
|
|
|
|
2025-05-10 19:09:44 +08:00
|
|
|
public class FieldEncryptPass : InstructionObfuscationPassBase
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
2025-05-10 19:50:03 +08:00
|
|
|
private readonly List<string> _configFiles;
|
2025-05-11 20:12:33 +08:00
|
|
|
private readonly int _encryptionLevel;
|
2025-05-10 19:50:03 +08:00
|
|
|
private IEncryptPolicy _encryptionPolicy;
|
2025-05-10 19:09:44 +08:00
|
|
|
private IFieldEncryptor _memoryEncryptor;
|
2025-04-30 21:47:21 +08:00
|
|
|
|
2025-05-12 22:01:35 +08:00
|
|
|
public override ObfuscationPassType Type => ObfuscationPassType.FieldEncrypt;
|
|
|
|
|
2025-05-10 19:50:03 +08:00
|
|
|
public FieldEncryptPass(FieldEncryptSettings settings)
|
|
|
|
{
|
|
|
|
_configFiles = settings.configFiles.ToList();
|
2025-05-11 20:12:33 +08:00
|
|
|
_encryptionLevel = settings.encryptionLevel;
|
2025-05-10 19:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool NeedProcessNotObfuscatedAssembly => true;
|
|
|
|
|
2025-05-13 08:49:57 +08:00
|
|
|
public override void Start()
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
2025-05-13 08:49:57 +08:00
|
|
|
var ctx = ObfuscationPassContext.Current;
|
|
|
|
_memoryEncryptor = new DefaultFieldEncryptor(ctx.random, ctx.encryptor, ctx.moduleEntityManager, _encryptionLevel);
|
2025-05-10 19:50:03 +08:00
|
|
|
_encryptionPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
|
2025-04-30 21:47:21 +08:00
|
|
|
}
|
|
|
|
|
2025-05-13 08:49:57 +08:00
|
|
|
public override void Stop()
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool NeedObfuscateMethod(MethodDef method)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-05-10 19:09:44 +08:00
|
|
|
private bool IsSupportedFieldType(TypeSig type)
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
type = type.RemovePinnedAndModifiers();
|
|
|
|
switch (type.ElementType)
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
case ElementType.I4:
|
|
|
|
case ElementType.I8:
|
|
|
|
case ElementType.U4:
|
|
|
|
case ElementType.U8:
|
|
|
|
case ElementType.R4:
|
|
|
|
case ElementType.R8:
|
|
|
|
return true;
|
|
|
|
default: return false;
|
2025-04-30 21:47:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool TryObfuscateInstruction(MethodDef callingMethod, Instruction inst, IList<Instruction> instructions, int instructionIndex, List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
|
|
|
|
{
|
|
|
|
Code code = inst.OpCode.Code;
|
2025-05-10 19:09:44 +08:00
|
|
|
if (!(inst.Operand is IField field) || !field.IsField)
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2025-05-10 19:09:44 +08:00
|
|
|
FieldDef fieldDef = field.ResolveFieldDefThrow();
|
|
|
|
if (!IsSupportedFieldType(fieldDef.FieldSig.Type) || !_encryptionPolicy.NeedEncrypt(fieldDef))
|
2025-04-30 21:47:21 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case Code.Ldfld:
|
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
_memoryEncryptor.Decrypt(callingMethod, fieldDef, outputInstructions, inst);
|
2025-04-30 21:47:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Code.Stfld:
|
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
_memoryEncryptor.Encrypt(callingMethod, fieldDef, outputInstructions, inst);
|
2025-04-30 21:47:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Code.Ldsfld:
|
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
_memoryEncryptor.Decrypt(callingMethod, fieldDef, outputInstructions, inst);
|
2025-04-30 21:47:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Code.Stsfld:
|
|
|
|
{
|
2025-05-10 19:09:44 +08:00
|
|
|
_memoryEncryptor.Encrypt(callingMethod, fieldDef, outputInstructions, inst);
|
2025-04-30 21:47:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Code.Ldflda:
|
|
|
|
case Code.Ldsflda:
|
|
|
|
{
|
|
|
|
throw new System.Exception($"You shouldn't get reference to memory encryption field: {field}");
|
|
|
|
}
|
|
|
|
default: return false;
|
|
|
|
}
|
2025-05-03 20:42:08 +08:00
|
|
|
//Debug.Log($"memory encrypt field: {field}");
|
2025-04-30 21:47:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|