|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using System.Linq; |
| 3 | +using Confuser.Core; |
| 4 | +using Confuser.Core.Services; |
| 5 | +using dnlib.DotNet; |
| 6 | +using dnlib.DotNet.Emit; |
| 7 | + |
| 8 | +namespace Confuser.Protections { |
| 9 | + internal sealed class HardeningPhase : ProtectionPhase { |
| 10 | + //private new HardeningComponent Parent => (HardeningComponent)base.Parent; |
| 11 | + |
| 12 | + /// <inheritdoc /> |
| 13 | + [SuppressMessage("ReSharper", "SuggestBaseTypeForParameter")] |
| 14 | + public HardeningPhase(HardeningProtection parent) : base(parent) { } |
| 15 | + |
| 16 | + /// <inheritdoc /> |
| 17 | + public override ProtectionTargets Targets => ProtectionTargets.Modules; |
| 18 | + |
| 19 | + /// <inheritdoc /> |
| 20 | + public override string Name => "Hardening Phase"; |
| 21 | + |
| 22 | + /// <inheritdoc /> |
| 23 | + public override bool ProcessAll => true; |
| 24 | + |
| 25 | + /// <inheritdoc /> |
| 26 | + protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { |
| 27 | + foreach (var module in parameters.Targets.OfType<ModuleDef>()) |
| 28 | + HardenMethod(context, module); |
| 29 | + } |
| 30 | + |
| 31 | + private static void HardenMethod(ConfuserContext context, ModuleDef module) { |
| 32 | + var cctor = module.GlobalType.FindStaticConstructor(); |
| 33 | + if (cctor == null) { |
| 34 | + context.Logger.Debug("No .cctor containing protection code found. Nothing to do."); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (!cctor.HasBody || !cctor.Body.HasInstructions) return; |
| 39 | + |
| 40 | + var marker = context.Registry.GetService<IMarkerService>(); |
| 41 | + var instructions = cctor.Body.Instructions; |
| 42 | + for (var i = instructions.Count - 1; i >= 0; i--) { |
| 43 | + if (instructions[i].OpCode.Code != Code.Call) continue; |
| 44 | + if (!(instructions[i].Operand is MethodDef targetMethod)) continue; |
| 45 | + if (!targetMethod.IsStatic || targetMethod.DeclaringType != module.GlobalType) continue; |
| 46 | + if (!marker.IsMarked(targetMethod) || !(marker.GetHelperParent(targetMethod) is Protection protection)) continue; |
| 47 | + |
| 48 | + // Resource protection needs to rewrite the method during the write phase. Not compatible! |
| 49 | + if (protection.FullId.Equals(ResourceProtection._FullId)) continue; |
| 50 | + |
| 51 | + cctor.Body.MergeCall(instructions[i]); |
| 52 | + targetMethod.DeclaringType.Methods.Remove(targetMethod); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments