2011-11-06 19:19:26 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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-22 22:18:41 +08:00
|
|
|
|
using System.IO;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
using Mono.Cecil;
|
2012-04-22 20:16:33 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-04-22 22:18:41 +08:00
|
|
|
|
using de4dot.PE;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CliSecure {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
class CliSecureRtType {
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
TypeDefinition cliSecureRtType;
|
|
|
|
|
MethodDefinition postInitializeMethod;
|
|
|
|
|
MethodDefinition initializeMethod;
|
|
|
|
|
MethodDefinition stringDecrypterMethod;
|
|
|
|
|
MethodDefinition 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TypeDefinition Type {
|
|
|
|
|
get { return cliSecureRtType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition StringDecrypterMethod {
|
|
|
|
|
get { return stringDecrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition PostInitializeMethod {
|
|
|
|
|
get { return postInitializeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition InitializeMethod {
|
|
|
|
|
get { return initializeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition LoadMethod {
|
|
|
|
|
get { return loadMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CliSecureRtType(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CliSecureRtType(ModuleDefinition module, CliSecureRtType oldOne) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T lookup<T>(T def, string errorMessage) where T : MemberReference {
|
|
|
|
|
return DeobUtils.lookup(module, def, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
if (cliSecureRtType != null)
|
|
|
|
|
return;
|
2012-04-22 20:16:33 +08:00
|
|
|
|
if (find2())
|
|
|
|
|
return;
|
2012-04-26 07:56:59 +08:00
|
|
|
|
if (find3())
|
|
|
|
|
return;
|
2012-04-22 22:18:41 +08:00
|
|
|
|
findNativeCode();
|
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[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
};
|
2012-04-22 20:16:33 +08:00
|
|
|
|
bool find2() {
|
2012-04-25 19:32:14 +08:00
|
|
|
|
foreach (var cctor in DeobUtils.getInitCctors(module, 3)) {
|
|
|
|
|
foreach (var calledMethod in DotNetUtils.getCalledMethods(module, cctor)) {
|
|
|
|
|
var type = calledMethod.DeclaringType;
|
2012-04-26 07:16:08 +08:00
|
|
|
|
if (type.IsPublic)
|
|
|
|
|
continue;
|
|
|
|
|
var fieldTypes = new FieldTypes(type);
|
|
|
|
|
if (!fieldTypes.exactly(requiredFields1) && !fieldTypes.exactly(requiredFields2) && !fieldTypes.exactly(requiredFields3))
|
2012-04-25 19:32:14 +08:00
|
|
|
|
continue;
|
2012-04-26 07:16:08 +08:00
|
|
|
|
if (!hasInitializeMethod(type, "_Initialize"))
|
2012-04-25 19:32:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-04-26 07:16:08 +08:00
|
|
|
|
stringDecrypterMethod = findStringDecrypterMethod(type);
|
2012-04-25 19:32:14 +08:00
|
|
|
|
initializeMethod = calledMethod;
|
2012-04-26 07:16:08 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 07:56:59 +08:00
|
|
|
|
bool find3() {
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.Fields.Count != 1)
|
|
|
|
|
continue;
|
|
|
|
|
if (type.Fields[0].FieldType.FullName != "System.Byte[]")
|
|
|
|
|
continue;
|
|
|
|
|
if (type.Methods.Count != 2)
|
|
|
|
|
continue;
|
|
|
|
|
if (DotNetUtils.getMethod(type, ".cctor") == null)
|
|
|
|
|
continue;
|
|
|
|
|
var cs = DotNetUtils.getMethod(type, "cs");
|
|
|
|
|
if (cs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
stringDecrypterMethod = cs;
|
|
|
|
|
cliSecureRtType = type;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 07:16:08 +08:00
|
|
|
|
static MethodDefinition findStringDecrypterMethod(TypeDefinition type) {
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.String", "(System.String)"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static MethodDefinition findMethod(TypeDefinition type, string returnType, string name, string parameters) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool hasInitializeMethod(TypeDefinition type, string name) {
|
|
|
|
|
var method = DotNetUtils.getPInvokeMethod(type, name);
|
|
|
|
|
if (method == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Parameters.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Parameters[0].ParameterType.FullName != "System.IntPtr")
|
|
|
|
|
return false;
|
|
|
|
|
var retType = method.MethodReturnType.ReturnType.FullName;
|
|
|
|
|
if (retType != "System.Void" && retType != "System.Int32")
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-22 22:18:41 +08:00
|
|
|
|
bool findNativeCode() {
|
|
|
|
|
if ((module.Attributes & ModuleAttributes.ILOnly) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-04-23 20:25:12 +08:00
|
|
|
|
using (var file = new FileStream(module.FullyQualifiedName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
|
|
|
|
|
var peImage = new PeImage(file);
|
|
|
|
|
return foundSig = MethodsDecrypter.detect(peImage);
|
|
|
|
|
}
|
2012-04-22 22:18:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 03:03:06 +08:00
|
|
|
|
public bool isAtLeastVersion50() {
|
2012-04-26 07:16:08 +08:00
|
|
|
|
return DotNetUtils.hasPinvokeMethod(cliSecureRtType, "LoadLibraryA");
|
2012-04-23 03:03:06 +08:00
|
|
|
|
}
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|