de4dot-cex/de4dot.code/deobfuscators/CryptoObfuscator/TamperDetection.cs

162 lines
4.9 KiB
C#
Raw Normal View History

/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
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 dnlib.DotNet;
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CryptoObfuscator {
class TamperDetection {
2012-11-09 05:24:13 +08:00
ModuleDefMD module;
TypeDef tamperType;
MethodDef tamperMethod;
2012-03-17 02:13:27 +08:00
FrameworkType frameworkType;
public bool Detected {
get { return tamperMethod != null; }
}
public TypeDef Type {
2011-10-23 15:03:00 +08:00
get { return tamperType; }
}
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) {
this.module = module;
2013-01-19 20:03:57 +08:00
frameworkType = DotNetUtils.GetFrameworkType(module);
}
2013-01-19 20:03:57 +08:00
public void Find() {
if (Find(module.EntryPoint))
return;
2013-01-19 20:03:57 +08:00
if (Find(DotNetUtils.GetModuleTypeCctor(module)))
2011-12-15 23:16:21 +08:00
return;
}
2013-01-19 20:03:57 +08:00
bool Find(MethodDef methodToCheck) {
2011-12-15 23:16:21 +08:00
if (methodToCheck == null)
return false;
2013-01-19 20:03:57 +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:
2013-01-19 20:03:57 +08:00
result = FindDesktop(method);
2012-03-16 05:36:23 +08:00
break;
2012-03-17 02:13:27 +08:00
case FrameworkType.Silverlight:
2013-01-19 20:03:57 +08:00
result = FindSilverlight(method);
2012-03-16 05:36:23 +08:00
break;
2012-03-17 02:13:27 +08:00
case FrameworkType.CompactFramework:
2013-01-19 20:03:57 +08:00
result = FindCompactFramework(method);
2012-03-16 05:36:23 +08:00
break;
}
2012-03-16 05:36:23 +08:00
if (!result)
continue;
2012-03-16 05:36:23 +08:00
tamperType = method.DeclaringType;
tamperMethod = method;
2011-12-15 23:16:21 +08:00
return true;
}
2011-12-15 23:16:21 +08:00
return false;
}
2012-03-16 05:36:23 +08:00
2013-01-19 20:03:57 +08:00
bool FindDesktop(MethodDef method) {
2012-03-16 05:36:23 +08:00
var type = method.DeclaringType;
2013-01-19 20:03:57 +08:00
if (!method.IsStatic || !DotNetUtils.IsMethod(method, "System.Void", "()"))
2012-03-16 05:36:23 +08:00
return false;
2014-05-09 21:59:50 +08:00
if (type.Methods.Count < 3 || type.Methods.Count > 31)
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetPInvokeMethod(type, "mscoree", "StrongNameSignatureVerificationEx") != null) {
2012-03-16 05:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.GetPInvokeMethod(type, "mscoree", "CLRCreateInstance") != null) {
2012-03-16 05:36:23 +08:00
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",
};
2013-01-19 20:03:57 +08:00
bool FindSilverlight(MethodDef method) {
if (!new LocalTypes(method).Exactly(requiredLocals_sl))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Int32 System.String::get_Length()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Reflection.Assembly System.Reflection.Assembly::GetExecutingAssembly()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.String System.Reflection.Assembly::get_FullName()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.CallsMethod(method, "System.String", "(System.Reflection.Assembly)")) {
2012-03-16 05:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.CallsMethod(method, "System.String System.Reflection.AssemblyName::get_Name()")) {
2012-03-16 05:36:23 +08:00
}
else
return false;
return true;
}
static string[] requiredLocals_cf = new string[] {
"System.Boolean",
"System.Byte[]",
"System.Int32",
"System.String",
};
2013-01-19 20:03:57 +08:00
bool FindCompactFramework(MethodDef method) {
if (!new LocalTypes(method).Exactly(requiredLocals_cf))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Int32 System.String::get_Length()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Reflection.Assembly System.Reflection.Assembly::GetExecutingAssembly()"))
2012-03-16 05:36:23 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.CallsMethod(method, "System.Byte[]", "(System.Reflection.Assembly)") &&
DotNetUtils.CallsMethod(method, "System.String", "(System.Reflection.Assembly)")) {
2012-03-16 05:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.CallsMethod(method, "System.Reflection.AssemblyName System.Reflection.Assembly::GetName()") &&
DotNetUtils.CallsMethod(method, "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()")) {
2012-03-16 05:36:23 +08:00
}
else
return false;
return true;
}
}
}