Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Confuser.Protections/HardeningPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,32 @@
HardenMethod(context, module);
}

private static void HardenMethod(ConfuserContext context, ModuleDef module) {
var cctor = module.GlobalType.FindStaticConstructor();
if (cctor == null) {
context.Logger.Debug("No .cctor containing protection code found. Nothing to do.");
return;
}

if (!cctor.HasBody || !cctor.Body.HasInstructions) return;

var marker = context.Registry.GetService<IMarkerService>();
var instructions = cctor.Body.Instructions;
for (var i = instructions.Count - 1; i >= 0; i--) {
if (instructions[i].OpCode.Code != Code.Call) continue;
if (!(instructions[i].Operand is MethodDef targetMethod)) continue;
if (!targetMethod.IsStatic || targetMethod.DeclaringType != module.GlobalType) continue;
if (!marker.IsMarked(targetMethod) || !(marker.GetHelperParent(targetMethod) is Protection protection)) continue;

// Resource protection needs to rewrite the method during the write phase. Not compatible!
if (protection.FullId.Equals(ResourceProtection._FullId)) continue;
if (protection.FullId.Equals(ResourceProtection._FullId)) continue;

// Skip large methods to avoid aggregating suspicious patterns in .cctor
if (targetMethod.HasBody && targetMethod.Body.Instructions.Count > 150) continue;

cctor.Body.MergeCall(instructions[i]);
targetMethod.DeclaringType.Methods.Remove(targetMethod);
}
}
}

Check notice on line 58 in Confuser.Protections/HardeningPhase.cs

View check run for this annotation

codefactor.io / CodeFactor

Confuser.Protections/HardeningPhase.cs#L31-L58

Complex Method
}
17 changes: 5 additions & 12 deletions Confuser.Runtime/Compressor.Compat.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Confuser.Runtime {
internal static class CompressorCompat {
static byte[] key;

static GCHandle Decrypt(uint[] data, uint seed) {
static byte[] Decrypt(uint[] data, uint seed) {
var w = new uint[0x10];
var k = new uint[0x10];
ulong s = seed;
Expand All @@ -35,26 +34,23 @@ static GCHandle Decrypt(uint[] data, uint seed) {
byte[] j = Lzma.Decompress(b);
Array.Clear(b, 0, b.Length);

GCHandle g = GCHandle.Alloc(j, GCHandleType.Pinned);
var z = (uint)(s % 0x8a5cb7);
for (int i = 0; i < j.Length; i++) {
j[i] ^= (byte)s;
if ((i & 0xff) == 0)
s = (s * s) % 0x8a5cb7;
}
return g;
return j;
}

[STAThread]
static int Main(string[] args) {
var l = (uint)Mutation.KeyI0;
uint[] q = Mutation.Placeholder(new uint[Mutation.KeyI0]);

GCHandle h = Decrypt(q, (uint)Mutation.KeyI1);
var b = (byte[])h.Target;
byte[] b = Decrypt(q, (uint)Mutation.KeyI1);
Assembly a = Assembly.Load(b);
Array.Clear(b, 0, b.Length);
h.Free();
Array.Clear(q, 0, q.Length);

var m = typeof(CompressorCompat).Module;
Expand Down Expand Up @@ -93,12 +89,9 @@ static Assembly Resolve(object sender, ResolveEventArgs e) {
uint s = 0x6fff61;
foreach (byte c in b)
s = s * 0x5e3f1f + c;
GCHandle h = Decrypt(d, s);

var f = (byte[])h.Target;
byte[] f = Decrypt(d, s);
Assembly a = Assembly.Load(f);
Array.Clear(f, 0, f.Length);
h.Free();
Array.Clear(d, 0, d.Length);

return a;
Expand Down
17 changes: 5 additions & 12 deletions Confuser.Runtime/Compressor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Confuser.Runtime {
internal static class Compressor {
static byte[] key;

static GCHandle Decrypt(uint[] data, uint seed) {
static byte[] Decrypt(uint[] data, uint seed) {
var w = new uint[0x10];
var k = new uint[0x10];
ulong s = seed;
Expand All @@ -35,14 +34,13 @@ static GCHandle Decrypt(uint[] data, uint seed) {
byte[] j = Lzma.Decompress(b);
Array.Clear(b, 0, b.Length);

GCHandle g = GCHandle.Alloc(j, GCHandleType.Pinned);
var z = (uint)(s % 0x8a5cb7);
for (int i = 0; i < j.Length; i++) {
j[i] ^= (byte)s;
if ((i & 0xff) == 0)
s = (s * s) % 0x8a5cb7;
}
return g;
return j;
}

[STAThread]
Expand All @@ -52,12 +50,10 @@ static int Main(string[] args) {

Assembly a = Assembly.GetExecutingAssembly();
Module n = a.ManifestModule;
GCHandle h = Decrypt(q, (uint)Mutation.KeyI1);
var b = (byte[])h.Target;
byte[] b = Decrypt(q, (uint)Mutation.KeyI1);
Module m = a.LoadModule("koi", b);

Array.Clear(b, 0, b.Length);
h.Free();
Array.Clear(q, 0, q.Length);

key = n.ResolveSignature(Mutation.KeyI2);
Expand Down Expand Up @@ -98,12 +94,9 @@ static Assembly Resolve(object sender, ResolveEventArgs e) {
uint s = 0x6fff61;
foreach (byte c in b)
s = s * 0x5e3f1f + c;
GCHandle h = Decrypt(d, s);

var f = (byte[])h.Target;
byte[] f = Decrypt(d, s);
Assembly a = Assembly.Load(f);
Array.Clear(f, 0, f.Length);
h.Free();
Array.Clear(d, 0, d.Length);

return a;
Expand Down