Skip to content

Commit 44f8cc4

Browse files
authored
Merge 52ad70a into 56558b5
2 parents 56558b5 + 52ad70a commit 44f8cc4

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

Confuser.Core/ConfuserEngine.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ static void RunInternal(ConfuserParameters parameters, CancellationToken token)
8787
bool ok = false;
8888
try {
8989
// Enable watermarking by default
90-
context.Project.Rules.Insert(0, new Rule {new SettingItem<Protection>(WatermarkingProtection._Id)});
90+
context.Project.Rules.Insert(0, new Rule {
91+
new SettingItem<Protection>(WatermarkingProtection._Id),
92+
new SettingItem<Protection>("harden")
93+
});
9194

9295
var asmResolver = new AssemblyResolver();
9396
asmResolver.EnableTypeDefCache = true;

Confuser.Core/DnlibUtils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ public static void MergeCall(this CilBody targetBody, Instruction callInstructio
535535
targetBody.Variables.Add(local.Value);
536536

537537
// Nop the call
538-
int index = targetBody.Instructions.IndexOf(callInstruction);
539-
targetBody.Instructions[index++].OpCode = OpCodes.Nop;
538+
int index = targetBody.Instructions.IndexOf(callInstruction) + 1;
539+
callInstruction.OpCode = OpCodes.Nop;
540+
callInstruction.Operand = null;
540541
var afterIndex = targetBody.Instructions[index];
541542

542543
// Find Exception handler index

Confuser.Core/Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static string EncodeString(byte[] buff, char[] charset) {
155155

156156
/// <summary>
157157
/// Returns a new string in which all occurrences of a specified string in
158-
/// <paramref name="str" /><paramref name="str" /> are replaced with another specified string.
158+
/// <paramref name="str" /> are replaced with another specified string.
159159
/// </summary>
160160
/// <returns>
161161
/// A <see cref="string" /> equivalent to <paramref name="str" /> but with all instances of
@@ -233,4 +233,4 @@ public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, ILo
233233
logger.EndProgress();
234234
}
235235
}
236-
}
236+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Confuser.Core;
3+
4+
namespace Confuser.Protections {
5+
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated by reflection.")]
6+
internal sealed class HardeningProtection : Protection {
7+
/// <inheritdoc />
8+
public override string Name => "Protection Hardening";
9+
10+
/// <inheritdoc />
11+
public override string Description => "This component improves the protection code, making it harder to circumvent it.";
12+
13+
/// <inheritdoc />
14+
public override string Id => "harden";
15+
16+
/// <inheritdoc />
17+
public override string FullId => "Cx.Harden";
18+
19+
/// <inheritdoc />
20+
protected override void Initialize(ConfuserContext context) { }
21+
22+
/// <inheritdoc />
23+
protected override void PopulatePipeline(ProtectionPipeline pipeline) =>
24+
pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new HardeningPhase(this));
25+
26+
/// <inheritdoc />
27+
public override ProtectionPreset Preset => ProtectionPreset.None;
28+
}
29+
}

0 commit comments

Comments
 (0)