Skip to content
This repository has been archived by the owner on Jan 27, 2019. It is now read-only.

Commit

Permalink
Add packer compatibility mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yck1509 committed Oct 9, 2015
1 parent 310db56 commit 469682c
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 10 deletions.
16 changes: 13 additions & 3 deletions Confuser.Protections/Compress/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ protected override void Pack(ConfuserContext context, ProtectionParameters param

ModuleDefMD originModule = context.Modules[ctx.ModuleIndex];
ctx.OriginModuleDef = originModule;

var stubModule = new ModuleDefUser(ctx.ModuleName, originModule.Mvid, originModule.CorLibTypes.AssemblyRef);
ctx.Assembly.Modules.Insert(0, stubModule);
if (ctx.CompatMode) {
var assembly = new AssemblyDefUser(originModule.Assembly);
assembly.Name += ".cr";
assembly.Modules.Add(stubModule);
}
else {
ctx.Assembly.Modules.Insert(0, stubModule);
ImportAssemblyTypeReferences(originModule, stubModule);
}
stubModule.Characteristics = originModule.Characteristics;
stubModule.Cor20HeaderFlags = originModule.Cor20HeaderFlags;
stubModule.Cor20HeaderRuntimeVersion = originModule.Cor20HeaderRuntimeVersion;
Expand All @@ -70,7 +79,6 @@ protected override void Pack(ConfuserContext context, ProtectionParameters param
stubModule.RuntimeVersion = originModule.RuntimeVersion;
stubModule.TablesHeaderVersion = originModule.TablesHeaderVersion;
stubModule.Win32Resources = originModule.Win32Resources;
ImportAssemblyTypeReferences(originModule, stubModule);

InjectStub(context, ctx, parameters, stubModule);

Expand Down Expand Up @@ -186,7 +194,9 @@ void InjectStub(ConfuserContext context, CompressorContext compCtx, ProtectionPa
var rt = context.Registry.GetService<IRuntimeService>();
RandomGenerator random = context.Registry.GetService<IRandomService>().GetRandomGenerator(Id);
var comp = context.Registry.GetService<ICompressionService>();
IEnumerable<IDnlibDef> defs = InjectHelper.Inject(rt.GetRuntimeType("Confuser.Runtime.Compressor"), stubModule.GlobalType, stubModule);

var rtType = rt.GetRuntimeType(compCtx.CompatMode ? "Confuser.Runtime.CompressorCompat" : "Confuser.Runtime.Compressor");
IEnumerable<IDnlibDef> defs = InjectHelper.Inject(rtType, stubModule.GlobalType, stubModule);

switch (parameters.GetParameter(context, context.CurrentModule, "key", Mode.Normal)) {
case Mode.Normal:
Expand Down
1 change: 1 addition & 0 deletions Confuser.Protections/Compress/CompressorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class CompressorContext {
public string ModuleName;
public byte[] OriginModule;
public ModuleDef OriginModuleDef;
public bool CompatMode;

public byte[] Encrypt(ICompressionService compress, byte[] data, uint seed, Action<double> progressFunc) {
data = (byte[])data.Clone();
Expand Down
14 changes: 8 additions & 6 deletions Confuser.Protections/Compress/ExtractPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
if (isExe) {
var ctx = new CompressorContext {
ModuleIndex = context.CurrentModuleIndex,
Assembly = context.CurrentModule.Assembly
Assembly = context.CurrentModule.Assembly,
CompatMode = parameters.GetParameter(context, null, "compat", false)
};
context.Annotations.Set(context, Compressor.ContextKey, ctx);

ctx.ModuleName = context.CurrentModule.Name;
context.CurrentModule.Name = "koi";

ctx.EntryPoint = context.CurrentModule.EntryPoint;
context.CurrentModule.EntryPoint = null;

ctx.Kind = context.CurrentModule.Kind;
context.CurrentModule.Kind = ModuleKind.NetModule;

if (!ctx.CompatMode) {
context.CurrentModule.Name = "koi";
context.CurrentModule.EntryPoint = null;
context.CurrentModule.Kind = ModuleKind.NetModule;
}

context.CurrentModuleWriterListener.OnWriterEvent += new ResourceRecorder(ctx, context.CurrentModule).OnWriterEvent;
}
Expand Down
6 changes: 5 additions & 1 deletion Confuser.Protections/Compress/StubProtection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected override void Initialize(ConfuserContext context) {
}

protected override void PopulatePipeline(ProtectionPipeline pipeline) {
pipeline.InsertPreStage(PipelineStage.Inspection, new InjPhase(this));
if (!ctx.CompatMode)
pipeline.InsertPreStage(PipelineStage.Inspection, new InjPhase(this));
pipeline.InsertPostStage(PipelineStage.BeginModule, new SigPhase(this));
}

Expand Down Expand Up @@ -96,6 +97,9 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
uint rid = writer.MetaData.TablesHeap.StandAloneSigTable.Add(new RawStandAloneSigRow(blob));
Debug.Assert((0x11000000 | rid) == prot.ctx.KeyToken);

if (prot.ctx.CompatMode)
return;

// Add File reference
byte[] hash = SHA1.Create().ComputeHash(prot.ctx.OriginModule);
uint hashBlob = writer.MetaData.BlobHeap.Add(hash);
Expand Down
109 changes: 109 additions & 0 deletions Confuser.Runtime/Compressor.Compat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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) {
var w = new uint[0x10];
var k = new uint[0x10];
ulong s = seed;
for (int i = 0; i < 0x10; i++) {
s = (s * s) % 0x143fc089;
k[i] = (uint)s;
w[i] = (uint)((s * s) % 0x444d56fb);
}
Mutation.Crypt(w, k);
Array.Clear(k, 0, 0x10);

var b = new byte[data.Length << 2];
uint h = 0;
for (int i = 0; i < data.Length; i++) {
uint d = data[i] ^ w[i & 0xf];
w[i & 0xf] = (w[i & 0xf] ^ d) + 0x3ddb2819;
b[h + 0] = (byte)(d >> 0);
b[h + 1] = (byte)(d >> 8);
b[h + 2] = (byte)(d >> 16);
b[h + 3] = (byte)(d >> 24);
h += 4;
}
Array.Clear(w, 0, 0x10);
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;
}

[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;
Assembly a = Assembly.Load(b);
Array.Clear(b, 0, b.Length);
h.Free();
Array.Clear(q, 0, q.Length);

var m = typeof(CompressorCompat).Module;
key = m.ResolveSignature(Mutation.KeyI2);
AppDomain.CurrentDomain.AssemblyResolve += Resolve;

MethodBase e = a.ManifestModule.ResolveMethod(key[0] | (key[1] << 8) | (key[2] << 16) | (key[3] << 24));
var g = new object[e.GetParameters().Length];
if (g.Length != 0)
g[0] = args;
object r = e.Invoke(null, g);
if (r is int)
return (int)r;
return 0;
}

static Assembly Resolve(object sender, ResolveEventArgs e) {
byte[] b = Encoding.UTF8.GetBytes(new AssemblyName(e.Name).FullName.ToUpperInvariant());

Stream m = null;
if (b.Length + 4 <= key.Length) {
for (int i = 0; i < b.Length; i++)
b[i] *= key[i + 4];
string n = Convert.ToBase64String(b);
m = Assembly.GetEntryAssembly().GetManifestResourceStream(n);
}
if (m != null) {
var d = new uint[m.Length >> 2];
var t = new byte[0x100];
int r;
int o = 0;
while ((r = m.Read(t, 0, 0x100)) > 0) {
Buffer.BlockCopy(t, 0, d, o, r);
o += r;
}
uint s = 0x6fff61;
foreach (byte c in b)
s = s * 0x5e3f1f + c;
GCHandle h = Decrypt(d, s);

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

return a;
}
return null;
}
}
}
1 change: 1 addition & 0 deletions Confuser.Runtime/Confuser.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Compressor.cs" />
<Compile Include="Constant.cs" />
<Compile Include="AntiTamper.JIT.cs" />
<Compile Include="Compressor.Compat.cs" />
<Compile Include="Lzma.cs" />
<Compile Include="Mutation.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 469682c

Please sign in to comment.