2025-10-29 20:40:37 +08:00
|
|
|
|
// Copyright 2025 Code Philosophy
|
|
|
|
|
|
//
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
2025-06-20 16:56:14 +08:00
|
|
|
|
using dnlib.DotNet.Emit;
|
2025-06-22 10:39:31 +08:00
|
|
|
|
using Obfuz.Data;
|
2025-06-20 16:56:14 +08:00
|
|
|
|
using Obfuz.Emit;
|
|
|
|
|
|
using Obfuz.Utils;
|
2025-06-22 10:39:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-06-17 20:21:28 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.ObfusPasses.ExprObfus.Obfuscators
|
|
|
|
|
|
{
|
|
|
|
|
|
class AdvancedObfuscator : BasicObfuscator
|
|
|
|
|
|
{
|
2025-06-20 18:58:54 +08:00
|
|
|
|
protected bool GenerateIdentityTransformForArgument(Instruction inst, EvalDataType op, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
2025-06-20 16:56:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
IRandom random = ctx.localRandom;
|
2025-07-02 18:57:53 +08:00
|
|
|
|
ConstFieldAllocator constFieldAllocator = ctx.constFieldAllocator;
|
2025-06-20 18:23:38 +08:00
|
|
|
|
switch (op)
|
2025-06-20 16:56:14 +08:00
|
|
|
|
{
|
2025-06-20 18:23:38 +08:00
|
|
|
|
case EvalDataType.Int32:
|
|
|
|
|
|
{
|
|
|
|
|
|
// = x + y = x + (y * a + b) * ra + (-b * ra)
|
|
|
|
|
|
int a = random.NextInt() | 0x1;
|
|
|
|
|
|
int ra = MathUtil.ModInverse32(a);
|
|
|
|
|
|
int b = random.NextInt();
|
|
|
|
|
|
int b_ra = -b * ra;
|
|
|
|
|
|
float constProbability = 0.5f;
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstInt(a, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstInt(b, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstInt(ra, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstInt(b_ra, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
|
|
|
|
|
outputInsts.Add(inst.Clone());
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case EvalDataType.Int64:
|
|
|
|
|
|
{
|
|
|
|
|
|
// = x + y = x + (y * a + b) * ra + (-b * ra)
|
|
|
|
|
|
long a = random.NextLong() | 0x1L;
|
|
|
|
|
|
long ra = MathUtil.ModInverse64(a);
|
|
|
|
|
|
long b = random.NextLong();
|
|
|
|
|
|
long b_ra = -b * ra;
|
|
|
|
|
|
float constProbability = 0.5f;
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstLong(a, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstLong(b, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstLong(ra, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstLong(b_ra, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
|
|
|
|
|
outputInsts.Add(inst.Clone());
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case EvalDataType.Float:
|
|
|
|
|
|
{
|
|
|
|
|
|
// = x + y = x + (y + a) * b; a = 0.0f, b = 1.0f
|
|
|
|
|
|
float a = 0.0f;
|
|
|
|
|
|
float b = 1.0f;
|
|
|
|
|
|
float constProbability = 0f;
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstFloat(a, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstFloat(b, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
|
|
|
|
|
outputInsts.Add(inst.Clone());
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case EvalDataType.Double:
|
2025-06-20 16:56:14 +08:00
|
|
|
|
{
|
2025-06-20 18:23:38 +08:00
|
|
|
|
// = x + y = x + (y + a) * b; a = 0.0, b = 1.0
|
|
|
|
|
|
double a = 0.0;
|
|
|
|
|
|
double b = 1.0;
|
|
|
|
|
|
float constProbability = 0f;
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstDouble(a, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Add));
|
2025-06-21 09:20:36 +08:00
|
|
|
|
ConstObfusUtil.LoadConstDouble(b, random, constProbability, constFieldAllocator, outputInsts);
|
2025-06-20 18:23:38 +08:00
|
|
|
|
outputInsts.Add(Instruction.Create(OpCodes.Mul));
|
|
|
|
|
|
outputInsts.Add(inst.Clone());
|
2025-06-20 16:56:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
default: return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 18:23:38 +08:00
|
|
|
|
public override bool ObfuscateBasicUnaryOp(Instruction inst, EvalDataType op, EvalDataType ret, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GenerateIdentityTransformForArgument(inst, op, outputInsts, ctx) || base.ObfuscateBasicUnaryOp(inst, op, ret, outputInsts, ctx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 16:56:14 +08:00
|
|
|
|
public override bool ObfuscateBasicBinOp(Instruction inst, EvalDataType op1, EvalDataType op2, EvalDataType ret, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
|
|
|
|
|
{
|
2025-06-20 18:23:38 +08:00
|
|
|
|
return GenerateIdentityTransformForArgument(inst, op2, outputInsts, ctx) || base.ObfuscateBasicBinOp(inst, op1, op2, ret, outputInsts, ctx);
|
2025-06-20 16:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool ObfuscateUnaryBitwiseOp(Instruction inst, EvalDataType op, EvalDataType ret, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
|
|
|
|
|
{
|
2025-06-20 18:23:38 +08:00
|
|
|
|
return GenerateIdentityTransformForArgument(inst, op, outputInsts, ctx) || base.ObfuscateUnaryBitwiseOp(inst, op, ret, outputInsts, ctx);
|
2025-06-20 16:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool ObfuscateBinBitwiseOp(Instruction inst, EvalDataType op1, EvalDataType op2, EvalDataType ret, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
|
|
|
|
|
{
|
2025-06-22 10:39:31 +08:00
|
|
|
|
return GenerateIdentityTransformForArgument(inst, op2, outputInsts, ctx) || base.ObfuscateBinBitwiseOp(inst, op1, op2, ret, outputInsts, ctx);
|
2025-06-20 16:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool ObfuscateBitShiftOp(Instruction inst, EvalDataType op1, EvalDataType op2, EvalDataType ret, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
2025-06-17 20:21:28 +08:00
|
|
|
|
{
|
2025-06-22 10:39:31 +08:00
|
|
|
|
return GenerateIdentityTransformForArgument(inst, op2, outputInsts, ctx) || base.ObfuscateBitShiftOp(inst, op1, op2, ret, outputInsts, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|