obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs

155 lines
6.9 KiB
C#
Raw Normal View History

2025-04-21 21:02:47 +08:00
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using Obfuz.Emit;
2025-05-09 19:26:59 +08:00
using Obfuz.ObfusPasses.ConstEncrypt.Policies;
2025-05-08 12:36:06 +08:00
using Obfuz.Settings;
2025-04-21 21:02:47 +08:00
using System;
2025-04-21 09:57:34 +08:00
using System.Collections.Generic;
using System.Linq;
2025-04-21 21:02:47 +08:00
using System.Runtime.CompilerServices;
2025-04-21 09:57:34 +08:00
using System.Text;
using System.Threading.Tasks;
2025-04-23 13:46:50 +08:00
using UnityEngine.Assertions;
2025-04-21 09:57:34 +08:00
2025-05-09 19:26:59 +08:00
namespace Obfuz.ObfusPasses.ConstEncrypt
2025-04-21 09:57:34 +08:00
{
2025-04-21 21:02:47 +08:00
2025-05-09 19:26:59 +08:00
public class ConstEncryptPass : BasicBlockObfuscationPassBase
2025-04-21 09:57:34 +08:00
{
2025-05-08 12:36:06 +08:00
private readonly string _configFile;
2025-05-09 19:26:59 +08:00
private IEncryptPolicy _dataObfuscatorPolicy;
private IConstEncryptor _dataObfuscator;
2025-04-21 21:02:47 +08:00
2025-05-09 19:26:59 +08:00
public ConstEncryptPass(ConstEncryptSettings settings)
2025-05-08 12:36:06 +08:00
{
_configFile = settings.configFile;
}
2025-05-04 19:24:14 +08:00
public override void Start(ObfuscationPassContext ctx)
2025-04-21 21:02:47 +08:00
{
2025-05-09 19:26:59 +08:00
_dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFile);
_dataObfuscator = new DefaultConstEncryptor();
2025-04-21 21:02:47 +08:00
}
2025-05-04 19:24:14 +08:00
public override void Stop(ObfuscationPassContext ctx)
2025-04-21 21:02:47 +08:00
{
2025-04-28 11:37:48 +08:00
_dataObfuscator.Done();
2025-04-21 21:02:47 +08:00
}
protected override bool NeedObfuscateMethod(MethodDef method)
2025-04-21 09:57:34 +08:00
{
return _dataObfuscatorPolicy.NeedObfuscateMethod(method);
2025-04-21 21:02:47 +08:00
}
2025-04-21 09:57:34 +08:00
protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, BasicBlock block, int instructionIndex,
List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
2025-04-21 21:02:47 +08:00
{
bool currentInLoop = block.inLoop;
ConstCachePolicy constCachePolicy = _dataObfuscatorPolicy.GetMethodConstCachePolicy(method);
switch (inst.OpCode.OperandType)
2025-04-21 21:02:47 +08:00
{
case OperandType.InlineI:
case OperandType.InlineI8:
case OperandType.ShortInlineI:
case OperandType.ShortInlineR:
case OperandType.InlineR:
2025-04-21 21:02:47 +08:00
{
bool needCache = currentInLoop ? constCachePolicy.cacheConstInLoop : constCachePolicy.cacheConstNotInLoop;
object operand = inst.Operand;
if (operand is int)
2025-04-21 21:02:47 +08:00
{
int value = (int)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is sbyte)
{
int value = (sbyte)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is byte)
{
int value = (byte)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is long)
{
long value = (long)operand;
if (_dataObfuscatorPolicy.NeedObfuscateLong(method, currentInLoop, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateLong(method, needCache, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is float)
2025-04-21 21:02:47 +08:00
{
float value = (float)operand;
if (_dataObfuscatorPolicy.NeedObfuscateFloat(method, currentInLoop, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateFloat(method, needCache, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is double)
2025-04-23 13:46:50 +08:00
{
double value = (double)operand;
if (_dataObfuscatorPolicy.NeedObfuscateDouble(method, currentInLoop, value))
2025-04-23 13:46:50 +08:00
{
_dataObfuscator.ObfuscateDouble(method, needCache, value, outputInstructions);
return true;
2025-04-23 13:46:50 +08:00
}
}
return false;
2025-04-21 21:02:47 +08:00
}
case OperandType.InlineString:
2025-04-21 21:02:47 +08:00
{
//RuntimeHelpers.InitializeArray
string value = (string)inst.Operand;
if (_dataObfuscatorPolicy.NeedObfuscateString(method, currentInLoop, value))
{
bool needCache = currentInLoop ? constCachePolicy.cacheStringInLoop : constCachePolicy.cacheStringNotInLoop;
_dataObfuscator.ObfuscateString(method, needCache, value, outputInstructions);
return true;
}
return false;
2025-04-21 21:02:47 +08:00
}
case OperandType.InlineMethod:
{
if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
{
Instruction prevInst = block.instructions[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;
}
}
}
return false;
}
default: return false;
2025-04-22 08:13:58 +08:00
}
2025-04-21 09:57:34 +08:00
}
}
}