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

153 lines
4.2 KiB
C#
Raw Normal View History

2012-02-09 01:40:24 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-02-09 01:40:24 +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.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-02-09 01:40:24 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CodeVeil {
class TamperDetection {
2012-11-08 16:48:05 +08:00
ModuleDefMD module;
2012-02-09 01:40:24 +08:00
MainType mainType;
TypeDef tamperDetectionType;
List<MethodDef> tamperDetectionMethods = new List<MethodDef>();
2012-02-09 01:40:24 +08:00
public TypeDef Type {
2012-02-09 01:40:24 +08:00
get { return tamperDetectionType; }
}
public List<MethodDef> Methods {
2012-02-09 01:40:24 +08:00
get { return tamperDetectionMethods; }
}
2012-11-08 16:48:05 +08:00
public TamperDetection(ModuleDefMD module, MainType mainType) {
2012-02-09 01:40:24 +08:00
this.module = module;
this.mainType = mainType;
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
2012-02-09 01:40:24 +08:00
if (!mainType.Detected)
return;
if (mainType.TamperCheckMethod == null)
return;
2013-01-19 20:03:57 +08:00
FindTamperDetectionTypes();
2012-02-09 01:40:24 +08:00
}
2013-01-19 20:03:57 +08:00
void FindTamperDetectionTypes() {
2012-02-09 01:40:24 +08:00
foreach (var type in module.Types) {
if (!type.HasNestedTypes)
continue;
2012-11-17 06:50:52 +08:00
if ((type.Attributes & ~TypeAttributes.Sealed) != 0)
2012-02-09 01:40:24 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!CheckTamperDetectionClasses(type.NestedTypes))
2012-02-09 01:40:24 +08:00
continue;
tamperDetectionType = type;
2013-01-19 20:03:57 +08:00
FindTamperDetectionMethods();
2012-02-09 01:40:24 +08:00
return;
}
}
2013-01-19 20:03:57 +08:00
void FindTamperDetectionMethods() {
2012-02-09 01:40:24 +08:00
foreach (var type in tamperDetectionType.NestedTypes) {
foreach (var method in type.Methods) {
if (!method.IsStatic || method.Body == null)
continue;
if (method.Name == ".cctor")
continue;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.IsMethod(method, "System.Void", "()"))
2012-02-09 01:40:24 +08:00
tamperDetectionMethods.Add(method);
}
}
}
2013-01-19 20:03:57 +08:00
bool CheckTamperDetectionClasses(IEnumerable<TypeDef> types) {
2012-02-09 01:40:24 +08:00
foreach (var type in types) {
2013-01-19 20:03:57 +08:00
if (!IsTamperDetectionClass(type))
2012-02-09 01:40:24 +08:00
return false;
}
return true;
}
2013-01-19 20:03:57 +08:00
bool IsTamperDetectionClass(TypeDef type) {
2012-11-08 16:48:05 +08:00
if (type.BaseType == null || type.BaseType.FullName != "System.Object")
2012-02-09 01:40:24 +08:00
return false;
2012-11-17 06:50:52 +08:00
if ((type.Attributes & ~TypeAttributes.Sealed) != TypeAttributes.NestedAssembly)
2012-02-09 01:40:24 +08:00
return false;
MethodDef cctor = null, initMethod = null;
2012-02-09 01:40:24 +08:00
foreach (var method in type.Methods) {
2013-01-19 20:03:57 +08:00
if (InvalidMethodsFinder.IsInvalidMethod(method))
2012-02-12 23:25:12 +08:00
continue;
2012-02-09 01:40:24 +08:00
if (!method.IsStatic || method.Body == null)
return false;
if (method.Name == ".cctor")
cctor = method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Void", "()"))
2012-02-09 01:40:24 +08:00
initMethod = method;
}
if (cctor == null || initMethod == null)
return false;
2013-01-19 20:03:57 +08:00
if (!CallsMainTypeTamperCheckMethod(cctor))
2012-02-09 01:40:24 +08:00
return false;
return true;
}
2013-01-19 20:03:57 +08:00
bool CallsMainTypeTamperCheckMethod(MethodDef method) {
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
2012-03-18 03:36:41 +08:00
if (calledMethod == mainType.TamperCheckMethod)
2012-02-09 01:40:24 +08:00
return true;
}
var instructions = method.Body.Instructions;
for (int i = 0; i < instructions.Count; i++) {
2013-01-19 20:03:57 +08:00
var instrs = DotNetUtils.GetInstructions(instructions, i, OpCodes.Ldtoken, OpCodes.Call, OpCodes.Call, OpCodes.Ldc_I8, OpCodes.Call);
if (instrs == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckInvokeCall(instrs[1], "System.Type", "(System.RuntimeTypeHandle)"))
continue;
2013-01-19 20:03:57 +08:00
if (!CheckInvokeCall(instrs[2], "System.Reflection.Assembly", "(System.Object)"))
continue;
2013-01-19 20:03:57 +08:00
if (!CheckInvokeCall(instrs[4], "System.Void", "(System.Reflection.Assembly,System.UInt64)"))
continue;
return true;
}
2012-02-09 01:40:24 +08:00
return false;
}
2013-01-19 20:03:57 +08:00
static bool CheckInvokeCall(Instruction instr, string returnType, string parameters) {
var method = instr.Operand as MethodDef;
if (method == null)
return false;
if (method.Name != "Invoke")
return false;
2013-01-19 20:03:57 +08:00
return DotNetUtils.IsMethod(method, returnType, parameters);
}
2012-02-09 01:40:24 +08:00
}
}