Skip to content

Commit 89501e6

Browse files
author
RandomCrocodile
committed
fix(core): don't create PDB files when debug=false (upstream mkaring#532)
Cherry-pick of mkaring#532 changes: - Skip PDB file creation in Debug phase when project debug flag is false - Remove hardening instruction count limit (>150) that restricted method inlining - Pin decrypted assembly bytes with GCHandle to prevent GC relocation during load
1 parent b0f2caa commit 89501e6

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

Confuser.Core/ConfuserEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ static void WriteModule(ConfuserContext context) {
421421

422422
static void Debug(ConfuserContext context) {
423423
context.Logger.Info("Finalizing...");
424+
if(!context.Project.Debug)
425+
return;
424426
for (int i = 0; i < context.OutputModules.Count; i++) {
425427
if (context.OutputSymbols[i] == null)
426428
continue;

Confuser.Protections/HardeningPhase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ private static void HardenMethod(ConfuserContext context, ModuleDef module) {
4848
// Resource protection needs to rewrite the method during the write phase. Not compatible!
4949
if (protection.FullId.Equals(ResourceProtection._FullId)) continue;
5050

51-
// Skip large methods to avoid aggregating suspicious patterns in .cctor
52-
if (targetMethod.HasBody && targetMethod.Body.Instructions.Count > 150) continue;
53-
5451
cctor.Body.MergeCall(instructions[i]);
5552
targetMethod.DeclaringType.Methods.Remove(targetMethod);
5653
}

Confuser.Runtime/Compressor.Compat.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4+
using System.Runtime.InteropServices;
45
using System.Text;
56

67
namespace Confuser.Runtime {
78
internal static class CompressorCompat {
89
static byte[] key;
910

10-
static byte[] Decrypt(uint[] data, uint seed) {
11+
static GCHandle Decrypt(uint[] data, uint seed) {
1112
var w = new uint[0x10];
1213
var k = new uint[0x10];
1314
ulong s = seed;
@@ -34,23 +35,26 @@ static byte[] Decrypt(uint[] data, uint seed) {
3435
byte[] j = Lzma.Decompress(b);
3536
Array.Clear(b, 0, b.Length);
3637

38+
GCHandle g = GCHandle.Alloc(j, GCHandleType.Pinned);
3739
var z = (uint)(s % 0x8a5cb7);
3840
for (int i = 0; i < j.Length; i++) {
3941
j[i] ^= (byte)s;
4042
if ((i & 0xff) == 0)
4143
s = (s * s) % 0x8a5cb7;
4244
}
43-
return j;
45+
return g;
4446
}
4547

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

51-
byte[] b = Decrypt(q, (uint)Mutation.KeyI1);
53+
GCHandle h = Decrypt(q, (uint)Mutation.KeyI1);
54+
var b = (byte[])h.Target;
5255
Assembly a = Assembly.Load(b);
5356
Array.Clear(b, 0, b.Length);
57+
h.Free();
5458
Array.Clear(q, 0, q.Length);
5559

5660
var m = typeof(CompressorCompat).Module;
@@ -89,9 +93,12 @@ static Assembly Resolve(object sender, ResolveEventArgs e) {
8993
uint s = 0x6fff61;
9094
foreach (byte c in b)
9195
s = s * 0x5e3f1f + c;
92-
byte[] f = Decrypt(d, s);
96+
GCHandle h = Decrypt(d, s);
97+
98+
var f = (byte[])h.Target;
9399
Assembly a = Assembly.Load(f);
94100
Array.Clear(f, 0, f.Length);
101+
h.Free();
95102
Array.Clear(d, 0, d.Length);
96103

97104
return a;

Confuser.Runtime/Compressor.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4+
using System.Runtime.InteropServices;
45
using System.Text;
56

67
namespace Confuser.Runtime {
78
internal static class Compressor {
89
static byte[] key;
910

10-
static byte[] Decrypt(uint[] data, uint seed) {
11+
static GCHandle Decrypt(uint[] data, uint seed) {
1112
var w = new uint[0x10];
1213
var k = new uint[0x10];
1314
ulong s = seed;
@@ -34,13 +35,14 @@ static byte[] Decrypt(uint[] data, uint seed) {
3435
byte[] j = Lzma.Decompress(b);
3536
Array.Clear(b, 0, b.Length);
3637

38+
GCHandle g = GCHandle.Alloc(j, GCHandleType.Pinned);
3739
var z = (uint)(s % 0x8a5cb7);
3840
for (int i = 0; i < j.Length; i++) {
3941
j[i] ^= (byte)s;
4042
if ((i & 0xff) == 0)
4143
s = (s * s) % 0x8a5cb7;
4244
}
43-
return j;
45+
return g;
4446
}
4547

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

5153
Assembly a = Assembly.GetExecutingAssembly();
5254
Module n = a.ManifestModule;
53-
byte[] b = Decrypt(q, (uint)Mutation.KeyI1);
55+
GCHandle h = Decrypt(q, (uint)Mutation.KeyI1);
56+
var b = (byte[])h.Target;
5457
Module m = a.LoadModule("koi", b);
5558

5659
Array.Clear(b, 0, b.Length);
60+
h.Free();
5761
Array.Clear(q, 0, q.Length);
5862

5963
key = n.ResolveSignature(Mutation.KeyI2);
@@ -94,9 +98,12 @@ static Assembly Resolve(object sender, ResolveEventArgs e) {
9498
uint s = 0x6fff61;
9599
foreach (byte c in b)
96100
s = s * 0x5e3f1f + c;
97-
byte[] f = Decrypt(d, s);
101+
GCHandle h = Decrypt(d, s);
102+
103+
var f = (byte[])h.Target;
98104
Assembly a = Assembly.Load(f);
99105
Array.Clear(f, 0, f.Length);
106+
h.Free();
100107
Array.Clear(d, 0, d.Length);
101108

102109
return a;

0 commit comments

Comments
 (0)