2012-08-21 21:11:14 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-08-21 21:11:14 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
|
|
|
|
class MethodsDecrypter {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef decrypterType;
|
|
|
|
|
MethodDef decryptMethod;
|
|
|
|
|
MethodDef decrypterCctor;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
EmbeddedResource resource;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
List<TypeDef> delegateTypes = new List<TypeDef>();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
get { return decrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public IEnumerable<TypeDef> DelegateTypes {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
get { return delegateTypes; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return resource; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return decrypterType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public MethodsDecrypter(ModuleDefMD module) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Check(type))
|
2012-08-21 21:11:14 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Collections.Generic.Dictionary`2<System.Int32,System.Int32>",
|
|
|
|
|
"System.ModuleHandle",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool Check(TypeDef type) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (type.NestedTypes.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
if (type.Fields.Count != 3)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).All(requiredFields))
|
2012-08-21 21:11:14 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-17 06:50:52 +08:00
|
|
|
|
var cctor = type.FindStaticConstructor();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var decryptMethodTmp = FindDecryptMethod(type);
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (decryptMethodTmp == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
decryptMethod = decryptMethodTmp;
|
|
|
|
|
decrypterCctor = cctor;
|
|
|
|
|
decrypterType = type;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredLocals = new string[] {
|
|
|
|
|
"System.Delegate",
|
|
|
|
|
"System.ModuleHandle",
|
|
|
|
|
"System.Reflection.Emit.DynamicILInfo",
|
|
|
|
|
"System.Reflection.Emit.DynamicMethod",
|
|
|
|
|
"System.Reflection.FieldInfo",
|
|
|
|
|
"System.Reflection.FieldInfo[]",
|
|
|
|
|
"System.Reflection.MethodBase",
|
|
|
|
|
"System.Reflection.MethodBody",
|
|
|
|
|
"System.Type",
|
|
|
|
|
"System.Type[]",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef FindDecryptMethod(TypeDef type) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new LocalTypes(method).All(requiredLocals))
|
2012-08-21 21:11:14 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "(System.Int32,System.Int32,System.Int32)"))
|
2012-08-21 21:11:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-29 01:51:25 +08:00
|
|
|
|
public void Decrypt(ResourceDecrypter resourceDecrypter, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (decryptMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
resource = CoUtils.GetResource(module, decrypterCctor);
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (resource == null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var decrypted = resourceDecrypter.Decrypt(resource.GetResourceStream());
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var reader = MemoryImageStream.Create(decrypted);
|
2012-08-21 21:11:14 +08:00
|
|
|
|
int numEncrypted = reader.ReadInt32();
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Restoring {0} encrypted methods", numEncrypted);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.Instance.Indent();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
for (int i = 0; i < numEncrypted; i++) {
|
|
|
|
|
int delegateTypeToken = reader.ReadInt32();
|
|
|
|
|
uint codeOffset = reader.ReadUInt32();
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var origOffset = reader.Position;
|
|
|
|
|
reader.Position = codeOffset;
|
2013-09-29 01:51:25 +08:00
|
|
|
|
Decrypt(reader, delegateTypeToken, simpleDeobfuscator);
|
2012-11-09 05:24:13 +08:00
|
|
|
|
reader.Position = origOffset;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.Instance.DeIndent();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-29 01:51:25 +08:00
|
|
|
|
void Decrypt(IBinaryReader reader, int delegateTypeToken, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var delegateType = module.ResolveToken(delegateTypeToken) as TypeDef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (delegateType == null)
|
|
|
|
|
throw new ApplicationException("Couldn't find delegate type");
|
|
|
|
|
|
|
|
|
|
int delToken, encMethToken, encDeclToken;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetTokens(delegateType, out delToken, out encMethToken, out encDeclToken))
|
2012-08-21 21:11:14 +08:00
|
|
|
|
throw new ApplicationException("Could not find encrypted method tokens");
|
|
|
|
|
if (delToken != delegateTypeToken)
|
|
|
|
|
throw new ApplicationException("Invalid delegate type token");
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var encType = module.ResolveToken(encDeclToken) as ITypeDefOrRef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (encType == null)
|
|
|
|
|
throw new ApplicationException("Invalid declaring type token");
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var encMethod = module.ResolveToken(encMethToken) as MethodDef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (encMethod == null)
|
|
|
|
|
throw new ApplicationException("Invalid encrypted method token");
|
|
|
|
|
|
|
|
|
|
var bodyReader = new MethodBodyReader(module, reader);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bodyReader.Read(encMethod);
|
2012-11-09 05:24:13 +08:00
|
|
|
|
bodyReader.RestoreMethod(encMethod);
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Restored method {0} ({1:X8}). Instrs:{2}, Locals:{3}, Exceptions:{4}",
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Utils.RemoveNewlines(encMethod.FullName),
|
2012-11-02 22:57:11 +08:00
|
|
|
|
encMethod.MDToken.ToInt32(),
|
2012-08-21 21:11:14 +08:00
|
|
|
|
encMethod.Body.Instructions.Count,
|
2012-11-20 00:58:34 +08:00
|
|
|
|
encMethod.Body.Variables.Count,
|
2012-08-21 21:11:14 +08:00
|
|
|
|
encMethod.Body.ExceptionHandlers.Count);
|
|
|
|
|
delegateTypes.Add(delegateType);
|
2013-09-29 01:51:25 +08:00
|
|
|
|
simpleDeobfuscator.MethodModified(encMethod);
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool GetTokens(TypeDef delegateType, out int delegateToken, out int encMethodToken, out int encDeclaringTypeToken) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
delegateToken = 0;
|
|
|
|
|
encMethodToken = 0;
|
|
|
|
|
encDeclaringTypeToken = 0;
|
|
|
|
|
|
2012-11-17 06:50:52 +08:00
|
|
|
|
var cctor = delegateType.FindStaticConstructor();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var instrs = cctor.Body.Instructions;
|
2012-08-22 02:17:21 +08:00
|
|
|
|
for (int i = 0; i < instrs.Count - 3; i++) {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
var ldci4_1 = instrs[i];
|
2012-11-09 05:24:13 +08:00
|
|
|
|
if (!ldci4_1.IsLdcI4())
|
2012-08-21 21:11:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4_2 = instrs[i + 1];
|
2012-11-09 05:24:13 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-08-21 21:11:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4_3 = instrs[i + 2];
|
2012-11-09 05:24:13 +08:00
|
|
|
|
if (!ldci4_3.IsLdcI4())
|
2012-08-21 21:11:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 3];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (calledMethod != decryptMethod)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
delegateToken = ldci4_1.GetLdcI4Value();
|
|
|
|
|
encMethodToken = ldci4_2.GetLdcI4Value();
|
|
|
|
|
encDeclaringTypeToken = ldci4_3.GetLdcI4Value();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:17:21 +08:00
|
|
|
|
return false;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|