obfuz/Editor/ObfusPasses/ConstObfus/DefaultDataObfuscator.cs

90 lines
3.7 KiB
C#
Raw Normal View History

2025-04-21 21:02:47 +08:00
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2025-04-22 19:34:46 +08:00
using Obfuz.Emit;
2025-05-07 19:39:09 +08:00
using Obfuz.Encryption;
2025-04-22 19:34:46 +08:00
using Obfuz.Utils;
2025-04-21 21:02:47 +08:00
using System;
using System.Collections.Generic;
2025-05-04 19:55:10 +08:00
namespace Obfuz.ObfusPasses.ConstObfus
2025-04-21 21:02:47 +08:00
{
public class DefaultDataObfuscator : IDataObfuscator
{
2025-04-22 19:34:46 +08:00
private readonly IRandom _random;
private readonly RandomDataNodeCreator _nodeCreator;
private readonly RvaDataAllocator _rvaDataAllocator;
private readonly ConstFieldAllocator _constFieldAllocator;
2025-04-23 18:58:44 +08:00
private readonly IEncryptor _encryptor;
2025-04-22 19:34:46 +08:00
public DefaultDataObfuscator()
{
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
2025-04-23 18:58:44 +08:00
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
2025-04-22 19:34:46 +08:00
_nodeCreator = new RandomDataNodeCreator(_random);
2025-04-23 18:58:44 +08:00
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor);
_constFieldAllocator = new ConstFieldAllocator(_random);
2025-04-22 19:34:46 +08:00
}
2025-04-22 08:13:58 +08:00
private void CompileNode(IDataNode node, MethodDef method, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
var ctx = new CompileContext
{
method = method,
2025-04-22 10:42:58 +08:00
output = obfuscatedInstructions,
2025-04-22 19:34:46 +08:00
rvaDataAllocator = _rvaDataAllocator,
constFieldAllocator = _constFieldAllocator,
};
node.Compile(ctx);
}
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions)
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value);
CompileNode(node, method, obfuscatedInstructions);
2025-04-22 10:42:58 +08:00
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value));
2025-04-21 21:02:47 +08:00
}
2025-04-22 08:13:58 +08:00
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
2025-04-23 10:28:27 +08:00
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int64, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value));
2025-04-21 21:02:47 +08:00
}
2025-04-22 08:13:58 +08:00
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float32, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, value));
2025-04-21 21:02:47 +08:00
}
2025-04-22 08:13:58 +08:00
public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float64, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, value));
2025-04-21 21:02:47 +08:00
}
2025-04-22 08:13:58 +08:00
public void ObfuscateBytes(MethodDef method, Array value, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
2025-04-23 13:46:50 +08:00
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Bytes, value);
CompileNode(node, method, obfuscatedInstructions);
//throw new NotSupportedException();
2025-04-22 08:13:58 +08:00
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value.Length));
2025-04-21 21:02:47 +08:00
}
2025-04-22 08:13:58 +08:00
public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions)
2025-04-21 21:02:47 +08:00
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value));
2025-04-21 21:02:47 +08:00
}
2025-04-22 19:34:46 +08:00
2025-04-28 11:37:48 +08:00
public void Done()
2025-04-22 19:34:46 +08:00
{
2025-04-22 22:53:51 +08:00
_rvaDataAllocator.Done();
_constFieldAllocator.Done();
2025-04-22 19:34:46 +08:00
}
2025-04-21 21:02:47 +08:00
}
}