forked from mkaring/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWatermarkingProtection.cs
More file actions
92 lines (75 loc) · 3.49 KB
/
Copy pathWatermarkingProtection.cs
File metadata and controls
92 lines (75 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Linq;
using Confuser.Core.Services;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using Microsoft.Extensions.Logging;
namespace Confuser.Core {
public sealed class WatermarkingProtection : Protection {
public const string _Id = "watermark";
public const string _FullId = "Cx.Watermark";
/// <inheritdoc />
public override string Name => "Watermarking";
/// <inheritdoc />
public override string Description =>
"This applies a watermark to the assembly, showing that ConfuserEx protected the assembly. So people try to reverse the obfuscation know to just give up.";
/// <inheritdoc />
public override string Id => _Id;
/// <inheritdoc />
public override string FullId => _FullId;
/// <inheritdoc />
protected internal override void Initialize(ConfuserContext context) { }
/// <inheritdoc />
protected internal override void PopulatePipeline(ProtectionPipeline pipeline) =>
pipeline.InsertPostStage(PipelineStage.EndModule, new WatermarkingPhase(this));
/// <inheritdoc />
public override ProtectionPreset Preset => ProtectionPreset.None;
private sealed class WatermarkingPhase : ProtectionPhase {
/// <inheritdoc />
public WatermarkingPhase(ConfuserComponent parent) : base(parent) { }
/// <inheritdoc />
public override ProtectionTargets Targets => ProtectionTargets.Modules;
/// <inheritdoc />
public override string Name => "Apply watermark";
/// <inheritdoc />
protected internal override void Execute(ConfuserContext context, ProtectionParameters parameters) {
var marker = context.Registry.GetService<IMarkerService>();
context.Logger.LogDebug("Watermarking...");
foreach (var module in parameters.Targets.OfType<ModuleDef>()) {
// Both are configurable so users can brand (or disguise) the watermark instead of
// carrying the identifiable default ConfuserEx fingerprint.
var attributeName = parameters.GetParameter(context, module, "attributeName", "ConfusedByAttribute");
if (string.IsNullOrEmpty(attributeName))
attributeName = "ConfusedByAttribute";
var text = parameters.GetParameter(context, module, "text", ConfuserEngine.Version);
var attrRef = module.CorLibTypes.GetTypeRef("System", "Attribute");
var attrType = module.FindNormal(attributeName);
if (attrType == null) {
attrType = new TypeDefUser("", attributeName, attrRef);
module.Types.Add(attrType);
marker.Mark(attrType, Parent);
}
var ctor = attrType.FindInstanceConstructors()
.FirstOrDefault(m => m.Parameters.Count == 1 && m.Parameters[0].Type == module.CorLibTypes.String);
if (ctor == null) {
ctor = new MethodDefUser(
".ctor",
MethodSig.CreateInstance(module.CorLibTypes.Void, module.CorLibTypes.String),
MethodImplAttributes.Managed,
MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName) {
Body = new CilBody { MaxStack = 1 }
};
ctor.Body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
ctor.Body.Instructions.Add(OpCodes.Call.ToInstruction(new MemberRefUser(module, ".ctor",
MethodSig.CreateInstance(module.CorLibTypes.Void), attrRef)));
ctor.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
attrType.Methods.Add(ctor);
marker.Mark(ctor, Parent);
}
var attr = new CustomAttribute(ctor);
attr.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, text));
module.CustomAttributes.Add(attr);
}
}
}
}
}