2011-10-22 20:31:38 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-10-22 20:31:38 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-10-22 23:13:28 +08:00
|
|
|
|
using System;
|
2012-07-02 20:26:00 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-11-09 05:24:13 +08:00
|
|
|
|
using dot10.IO;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
class ResourceResolver {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
ResourceDecrypter resourceDecrypter;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef resolverType;
|
|
|
|
|
MethodDef resolverMethod;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
ResolverVersion resolverVersion = ResolverVersion.V1;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
bool mergedIt = false;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
|
2011-12-15 23:16:21 +08:00
|
|
|
|
enum ResolverVersion {
|
2012-03-15 16:15:12 +08:00
|
|
|
|
None,
|
2011-12-15 23:16:21 +08:00
|
|
|
|
V1,
|
|
|
|
|
V2,
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public ResourceResolver(ModuleDefMD module, ResourceDecrypter resourceDecrypter) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.resourceDecrypter = resourceDecrypter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
2012-03-15 16:35:44 +08:00
|
|
|
|
var cctor = DotNetUtils.getModuleTypeCctor(module);
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-03-18 03:36:41 +08:00
|
|
|
|
foreach (var method in DotNetUtils.getCalledMethods(module, cctor)) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (method.Name == ".cctor" || method.Name == ".ctor")
|
|
|
|
|
continue;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-03-15 16:15:12 +08:00
|
|
|
|
if (checkType(method))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-22 23:13:28 +08:00
|
|
|
|
public EmbeddedResource mergeResources() {
|
|
|
|
|
if (mergedIt)
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-07-02 20:26:00 +08:00
|
|
|
|
var resource = DotNetUtils.getResource(module, getResourceNames()) as EmbeddedResource;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
if (resource == null)
|
2011-10-23 23:23:33 +08:00
|
|
|
|
return null;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
|
2012-11-20 08:15:27 +08:00
|
|
|
|
resource.Data.Position = 0;
|
2012-11-09 05:24:13 +08:00
|
|
|
|
DeobUtils.decryptAndAddResources(module, resource.Name.String, () => resourceDecrypter.decrypt(resource.Data.CreateStream()));
|
2011-10-22 23:13:28 +08:00
|
|
|
|
mergedIt = true;
|
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 20:26:00 +08:00
|
|
|
|
IEnumerable<string> getResourceNames() {
|
|
|
|
|
var names = new List<string>();
|
|
|
|
|
|
2011-12-15 23:16:21 +08:00
|
|
|
|
switch (resolverVersion) {
|
2012-07-02 20:26:00 +08:00
|
|
|
|
case ResolverVersion.V1:
|
2012-11-09 05:24:13 +08:00
|
|
|
|
names.Add(module.Assembly.Name.String);
|
2012-07-02 20:26:00 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ResolverVersion.V2:
|
2012-11-09 05:24:13 +08:00
|
|
|
|
names.Add(string.Format("{0}{0}{0}", module.Assembly.Name.String));
|
|
|
|
|
names.Add(string.Format("{0}&", module.Assembly.Name.String));
|
2012-07-02 20:26:00 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown version");
|
2011-12-15 23:16:21 +08:00
|
|
|
|
}
|
2012-07-02 20:26:00 +08:00
|
|
|
|
|
|
|
|
|
return names;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool checkType(MethodDef initMethod) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (!initMethod.HasBody)
|
|
|
|
|
return false;
|
2012-03-15 16:15:12 +08:00
|
|
|
|
if (DotNetUtils.findFieldType(initMethod.DeclaringType, "System.Reflection.Assembly", true) == null)
|
2011-10-22 20:31:38 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-03-15 16:15:12 +08:00
|
|
|
|
resolverVersion = checkSetupMethod(initMethod);
|
|
|
|
|
if (resolverVersion == ResolverVersion.None)
|
2012-11-17 06:50:52 +08:00
|
|
|
|
resolverVersion = checkSetupMethod(initMethod.DeclaringType.FindStaticConstructor());
|
2012-03-15 16:15:12 +08:00
|
|
|
|
if (resolverVersion == ResolverVersion.None)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
resolverType = initMethod.DeclaringType;
|
|
|
|
|
resolverMethod = initMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
ResolverVersion checkSetupMethod(MethodDef setupMethod) {
|
2012-03-15 16:15:12 +08:00
|
|
|
|
var instructions = setupMethod.Body.Instructions;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
int foundCount = 0;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
for (int i = 0; i < instructions.Count; i++) {
|
|
|
|
|
var instrs = DotNetUtils.getInstructions(instructions, i, OpCodes.Ldnull, OpCodes.Ldftn, OpCodes.Newobj);
|
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
IMethod methodRef;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
var ldftn = instrs[1];
|
|
|
|
|
var newobj = instrs[2];
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
methodRef = ldftn.Operand as IMethod;
|
|
|
|
|
if (methodRef == null || !new SigComparer().Equals(setupMethod.DeclaringType, methodRef.DeclaringType))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
methodRef = newobj.Operand as IMethod;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (methodRef == null || methodRef.FullName != "System.Void System.ResolveEventHandler::.ctor(System.Object,System.IntPtr)")
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-12-15 23:16:21 +08:00
|
|
|
|
foundCount++;
|
|
|
|
|
}
|
|
|
|
|
if (foundCount == 0)
|
2012-03-15 16:15:12 +08:00
|
|
|
|
return ResolverVersion.None;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
|
|
|
|
|
switch (foundCount) {
|
2012-03-15 16:15:12 +08:00
|
|
|
|
case 1: return ResolverVersion.V1;
|
|
|
|
|
case 2: return ResolverVersion.V2;
|
|
|
|
|
default: return ResolverVersion.None;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|