2011-10-23 00:13:13 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-10-23 00:13:13 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
2011-10-23 00:13:13 +08:00
|
|
|
|
class TamperDetection {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef tamperType;
|
|
|
|
|
MethodDef tamperMethod;
|
2012-03-17 02:13:27 +08:00
|
|
|
|
FrameworkType frameworkType;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return tamperMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return tamperType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return tamperMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public TamperDetection(ModuleDefMD module) {
|
2011-10-23 00:13:13 +08:00
|
|
|
|
this.module = module;
|
2012-03-17 02:13:27 +08:00
|
|
|
|
frameworkType = DotNetUtils.getFrameworkType(module);
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
2011-12-15 23:16:21 +08:00
|
|
|
|
if (find(module.EntryPoint))
|
2011-10-23 00:13:13 +08:00
|
|
|
|
return;
|
2012-01-22 18:15:14 +08:00
|
|
|
|
if (find(DotNetUtils.getModuleTypeCctor(module)))
|
2011-12-15 23:16:21 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool find(MethodDef methodToCheck) {
|
2011-12-15 23:16:21 +08:00
|
|
|
|
if (methodToCheck == null)
|
|
|
|
|
return false;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
|
2012-03-18 03:36:41 +08:00
|
|
|
|
foreach (var method in DotNetUtils.getCalledMethods(module, methodToCheck)) {
|
2012-03-16 05:36:23 +08:00
|
|
|
|
bool result = false;
|
2012-03-17 02:13:27 +08:00
|
|
|
|
switch (frameworkType) {
|
|
|
|
|
case FrameworkType.Desktop:
|
2012-03-16 05:36:23 +08:00
|
|
|
|
result = findDesktop(method);
|
|
|
|
|
break;
|
2012-03-17 02:13:27 +08:00
|
|
|
|
case FrameworkType.Silverlight:
|
2012-03-16 05:36:23 +08:00
|
|
|
|
result = findSilverlight(method);
|
|
|
|
|
break;
|
2012-03-17 02:13:27 +08:00
|
|
|
|
case FrameworkType.CompactFramework:
|
2012-03-16 05:36:23 +08:00
|
|
|
|
result = findCompactFramework(method);
|
|
|
|
|
break;
|
2012-03-15 05:23:19 +08:00
|
|
|
|
}
|
2012-03-16 05:36:23 +08:00
|
|
|
|
if (!result)
|
2011-10-23 00:13:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-03-16 05:36:23 +08:00
|
|
|
|
tamperType = method.DeclaringType;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
tamperMethod = method;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
return true;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
2011-12-15 23:16:21 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
2012-03-16 05:36:23 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool findDesktop(MethodDef method) {
|
2012-03-16 05:36:23 +08:00
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
return false;
|
2013-01-15 08:37:21 +08:00
|
|
|
|
if (type.Methods.Count < 3 || type.Methods.Count > 24)
|
2012-03-16 05:36:23 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (DotNetUtils.getPInvokeMethod(type, "mscoree", "StrongNameSignatureVerificationEx") != null) {
|
|
|
|
|
}
|
|
|
|
|
else if (DotNetUtils.getPInvokeMethod(type, "mscoree", "CLRCreateInstance") != null) {
|
|
|
|
|
if (type.NestedTypes.Count != 3)
|
|
|
|
|
return false;
|
|
|
|
|
if (!type.NestedTypes[0].IsInterface || !type.NestedTypes[1].IsInterface || !type.NestedTypes[2].IsInterface)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string[] requiredLocals_sl = new string[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.Reflection.AssemblyName",
|
|
|
|
|
"System.String",
|
|
|
|
|
};
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool findSilverlight(MethodDef method) {
|
2012-03-16 05:36:23 +08:00
|
|
|
|
if (!new LocalTypes(method).exactly(requiredLocals_sl))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Int32 System.String::get_Length()"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Reflection.Assembly System.Reflection.Assembly::GetExecutingAssembly()"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.String System.Reflection.Assembly::get_FullName()"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()"))
|
|
|
|
|
return false;
|
|
|
|
|
if (DotNetUtils.callsMethod(method, "System.String", "(System.Reflection.Assembly)")) {
|
|
|
|
|
}
|
|
|
|
|
else if (DotNetUtils.callsMethod(method, "System.String System.Reflection.AssemblyName::get_Name()")) {
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string[] requiredLocals_cf = new string[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.String",
|
|
|
|
|
};
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool findCompactFramework(MethodDef method) {
|
2012-03-16 05:36:23 +08:00
|
|
|
|
if (!new LocalTypes(method).exactly(requiredLocals_cf))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Int32 System.String::get_Length()"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)"))
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.callsMethod(method, "System.Reflection.Assembly System.Reflection.Assembly::GetExecutingAssembly()"))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (DotNetUtils.callsMethod(method, "System.Byte[]", "(System.Reflection.Assembly)") &&
|
|
|
|
|
DotNetUtils.callsMethod(method, "System.String", "(System.Reflection.Assembly)")) {
|
|
|
|
|
}
|
|
|
|
|
else if (DotNetUtils.callsMethod(method, "System.Reflection.AssemblyName System.Reflection.Assembly::GetName()") &&
|
|
|
|
|
DotNetUtils.callsMethod(method, "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()")) {
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|