Fix incompatibility between resource protection and compressor packer

Fix #96, #95, #60
This commit is contained in:
yck1509 2014-09-30 18:44:36 +08:00
Родитель c62cf3fdcc
Коммит 940c9c67c8
3 изменённых файлов: 60 добавлений и 6 удалений

Просмотреть файл

@ -73,7 +73,8 @@ namespace Confuser.Protections.Resources {
}
private void InjectHelpers(ConfuserContext context, ICompressionService compression, IRuntimeService rt, REContext moduleCtx) {
IEnumerable<IDnlibDef> members = InjectHelper.Inject(rt.GetRuntimeType("Confuser.Runtime.Resource"), context.CurrentModule.GlobalType, context.CurrentModule);
var rtName = context.Packer != null ? "Confuser.Runtime.Resource_Packer" : "Confuser.Runtime.Resource";
IEnumerable<IDnlibDef> members = InjectHelper.Inject(rt.GetRuntimeType(rtName), context.CurrentModule.GlobalType, context.CurrentModule);
foreach (IDnlibDef member in members) {
if (member.Name == "Initialize")
moduleCtx.InitMethod = (MethodDef)member;

Просмотреть файл

@ -29,9 +29,11 @@ namespace Confuser.Protections.Resources {
if (e.WriterEvent == ModuleWriterEvent.MDBeginAddResources) {
ctx.Context.CheckCancellation();
ctx.Context.Logger.Debug("Encrypting resources...");
bool hasPacker = ctx.Context.Packer != null;
List<EmbeddedResource> resources = ctx.Module.Resources.OfType<EmbeddedResource>().ToList();
ctx.Module.Resources.RemoveWhere(res => res is EmbeddedResource);
if (!hasPacker)
ctx.Module.Resources.RemoveWhere(res => res is EmbeddedResource);
// move resources
string asmName = ctx.Name.RandomName(RenameMode.Letters);
@ -43,10 +45,12 @@ namespace Confuser.Protections.Resources {
ModuleDef module = assembly.ManifestModule;
assembly.ManifestModule.Kind = ModuleKind.Dll;
var asmRef = new AssemblyRefUser(module.Assembly);
foreach (EmbeddedResource res in resources) {
res.Attributes = ManifestResourceAttributes.Public;
module.Resources.Add(res);
ctx.Module.Resources.Add(new AssemblyLinkedResource(res.Name, asmRef, res.Attributes));
if (!hasPacker) {
foreach (EmbeddedResource res in resources) {
res.Attributes = ManifestResourceAttributes.Public;
module.Resources.Add(res);
ctx.Module.Resources.Add(new AssemblyLinkedResource(res.Name, asmRef, res.Attributes));
}
}
byte[] moduleBuff;
using (var ms = new MemoryStream()) {

Просмотреть файл

@ -49,4 +49,53 @@ namespace Confuser.Runtime {
}
}
internal static class Resource_Packer {
private static Assembly c;
// Hmm... Too lazy.
private static void Initialize() {
var l = (uint)Mutation.KeyI0;
uint[] q = Mutation.Placeholder(new uint[Mutation.KeyI0]);
var k = new uint[0x10];
var n = (uint)Mutation.KeyI1;
for (int i = 0; i < 0x10; i++) {
n ^= n >> 13;
n ^= n << 25;
n ^= n >> 27;
k[i] = n;
}
int s = 0, d = 0;
var w = new uint[0x10];
var o = new byte[l * 4];
while (s < l) {
for (int j = 0; j < 0x10; j++)
w[j] = q[s + j];
Mutation.Crypt(w, k);
for (int j = 0; j < 0x10; j++) {
uint e = w[j];
o[d++] = (byte)e;
o[d++] = (byte)(e >> 8);
o[d++] = (byte)(e >> 16);
o[d++] = (byte)(e >> 24);
k[j] ^= e;
}
s += 0x10;
}
c = Assembly.Load(Lzma.Decompress(o));
AppDomain.CurrentDomain.ResourceResolve += Handler;
}
private static Assembly Handler(object sender, ResolveEventArgs args) {
var n = c.GetManifestResourceNames();
if (Array.IndexOf(n, args.Name) != -1)
return c;
return null;
}
}
}