2011-11-06 19:19:26 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-11-06 19:19:26 +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;
|
2012-04-26 23:14:54 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-04-22 22:18:41 +08:00
|
|
|
|
using System.IO;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2012-04-22 20:16:33 +08:00
|
|
|
|
using de4dot.blocks;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
2012-11-06 23:38:39 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Agile_NET {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
class CliSecureRtType {
|
2012-11-07 12:17:45 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef cliSecureRtType;
|
|
|
|
|
MethodDef postInitializeMethod;
|
|
|
|
|
MethodDef initializeMethod;
|
|
|
|
|
MethodDef stringDecrypterMethod;
|
|
|
|
|
MethodDef loadMethod;
|
2012-04-22 22:18:41 +08:00
|
|
|
|
bool foundSig;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
2012-04-22 22:18:41 +08:00
|
|
|
|
get { return foundSig || cliSecureRtType != null; }
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return cliSecureRtType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef StringDecrypterMethod {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return stringDecrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef PostInitializeMethod {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return postInitializeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef InitializeMethod {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return initializeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef LoadMethod {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return loadMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 12:17:45 +08:00
|
|
|
|
public CliSecureRtType(ModuleDefMD module) {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 12:17:45 +08:00
|
|
|
|
public CliSecureRtType(ModuleDefMD module, CliSecureRtType oldOne) {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
this.module = module;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
cliSecureRtType = Lookup(oldOne.cliSecureRtType, "Could not find CliSecureRt type");
|
|
|
|
|
postInitializeMethod = Lookup(oldOne.postInitializeMethod, "Could not find postInitializeMethod method");
|
|
|
|
|
initializeMethod = Lookup(oldOne.initializeMethod, "Could not find initializeMethod method");
|
|
|
|
|
stringDecrypterMethod = Lookup(oldOne.stringDecrypterMethod, "Could not find stringDecrypterMethod method");
|
|
|
|
|
loadMethod = Lookup(oldOne.loadMethod, "Could not find loadMethod method");
|
2012-04-23 20:40:46 +08:00
|
|
|
|
foundSig = oldOne.foundSig;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
T Lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
|
|
|
|
|
return DeobUtils.Lookup(module, def, errorMessage);
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find(byte[] moduleBytes) {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
if (cliSecureRtType != null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Find2())
|
2012-04-22 20:16:33 +08:00
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Find3())
|
2012-04-26 07:56:59 +08:00
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FindNativeCode(moduleBytes);
|
2012-04-22 20:16:33 +08:00
|
|
|
|
}
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
2012-04-26 07:16:08 +08:00
|
|
|
|
static readonly string[] requiredFields1 = new string[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
};
|
|
|
|
|
static readonly string[] requiredFields2 = new string[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
};
|
|
|
|
|
static readonly string[] requiredFields3 = new string[] {
|
2012-04-26 20:46:22 +08:00
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
};
|
|
|
|
|
static readonly string[] requiredFields4 = new string[] {
|
2012-04-26 07:16:08 +08:00
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool Find2() {
|
|
|
|
|
foreach (var cctor in DeobUtils.GetInitCctors(module, 3)) {
|
|
|
|
|
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, cctor)) {
|
2012-04-25 19:32:14 +08:00
|
|
|
|
var type = calledMethod.DeclaringType;
|
2012-04-26 07:16:08 +08:00
|
|
|
|
if (type.IsPublic)
|
|
|
|
|
continue;
|
|
|
|
|
var fieldTypes = new FieldTypes(type);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!fieldTypes.Exactly(requiredFields1) && !fieldTypes.Exactly(requiredFields2) &&
|
|
|
|
|
!fieldTypes.Exactly(requiredFields3) && !fieldTypes.Exactly(requiredFields4))
|
2012-04-25 19:32:14 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!HasInitializeMethod(type, "_Initialize") && !HasInitializeMethod(type, "_Initialize64"))
|
2012-04-25 19:32:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypterMethod = FindStringDecrypterMethod(type);
|
2012-04-25 19:32:14 +08:00
|
|
|
|
initializeMethod = calledMethod;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
postInitializeMethod = FindMethod(type, "System.Void", "PostInitialize", "()");
|
|
|
|
|
loadMethod = FindMethod(type, "System.IntPtr", "Load", "()");
|
2012-04-25 19:32:14 +08:00
|
|
|
|
cliSecureRtType = type;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-04-22 20:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool Find3() {
|
2012-04-26 07:56:59 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.Fields.Count != 1)
|
|
|
|
|
continue;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
if (type.Fields[0].FieldSig.GetFieldType().GetFullName() != "System.Byte[]")
|
2012-04-26 07:56:59 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (type.Methods.Count != 2)
|
|
|
|
|
continue;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (type.FindStaticConstructor() == null)
|
2012-04-26 07:56:59 +08:00
|
|
|
|
continue;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
var cs = type.FindMethod("cs");
|
2012-04-26 07:56:59 +08:00
|
|
|
|
if (cs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
stringDecrypterMethod = cs;
|
|
|
|
|
cliSecureRtType = type;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef FindStringDecrypterMethod(TypeDef type) {
|
2012-04-26 07:16:08 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String)"))
|
2012-04-26 07:16:08 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef FindMethod(TypeDef type, string returnType, string name, string parameters) {
|
2012-04-26 07:16:08 +08:00
|
|
|
|
var methodName = returnType + " " + type.FullName + "::" + name + parameters;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic)
|
|
|
|
|
continue;
|
|
|
|
|
if (method.FullName != methodName)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool HasInitializeMethod(TypeDef type, string name) {
|
|
|
|
|
var method = DotNetUtils.GetPInvokeMethod(type, name);
|
2012-04-26 07:16:08 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return false;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
var sig = method.MethodSig;
|
|
|
|
|
if (sig.Params.Count != 1)
|
2012-04-26 07:16:08 +08:00
|
|
|
|
return false;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
if (sig.Params[0].GetElementType() != ElementType.I)
|
2012-04-26 07:16:08 +08:00
|
|
|
|
return false;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
var retType = sig.RetType.GetElementType();
|
|
|
|
|
if (retType != ElementType.Void && retType != ElementType.I4)
|
2012-04-26 07:16:08 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool FindNativeCode(byte[] moduleBytes) {
|
|
|
|
|
var bytes = moduleBytes != null ? moduleBytes : DeobUtils.ReadModule(module);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
using (var peImage = new MyPEImage(bytes))
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return foundSig = MethodsDecrypter.Detect(peImage);
|
2012-04-22 22:18:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool IsAtLeastVersion50() {
|
|
|
|
|
return DotNetUtils.HasPinvokeMethod(cliSecureRtType, "LoadLibraryA");
|
2012-04-23 03:03:06 +08:00
|
|
|
|
}
|
2012-04-26 22:53:47 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void FindStringDecrypterMethod() {
|
2012-04-26 22:53:47 +08:00
|
|
|
|
if (cliSecureRtType != null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.Fields.Count != 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (type.Methods.Count != 1)
|
|
|
|
|
continue;
|
|
|
|
|
var cs = type.Methods[0];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!IsOldStringDecrypterMethod(cs))
|
2012-04-26 22:53:47 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
cliSecureRtType = type;
|
|
|
|
|
stringDecrypterMethod = cs;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool IsOldStringDecrypterMethod(MethodDef method) {
|
2012-04-26 22:53:47 +08:00
|
|
|
|
if (method == null || method.Body == null || !method.IsStatic)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String)"))
|
2012-04-26 22:53:47 +08:00
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(method, 0xFF))
|
2012-04-26 22:53:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|