de4dot-cex/de4dot.code/deobfuscators/MaxtoCode/MainType.cs

151 lines
4.0 KiB
C#
Raw Normal View History

2012-02-20 11:55:59 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-02-20 11:55:59 +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;
using System.Collections.Generic;
using dnlib.DotNet;
2012-02-20 11:55:59 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.MaxtoCode {
class MainType {
2012-11-18 14:34:51 +08:00
ModuleDefMD module;
TypeDef mcType;
2012-02-22 19:14:15 +08:00
bool isOld;
2013-10-21 00:09:38 +08:00
ModuleRef runtimeModule1, runtimeModule2;
2012-02-22 19:14:15 +08:00
public bool IsOld {
get { return isOld; }
}
2012-02-20 11:55:59 +08:00
public TypeDef Type {
2012-02-21 18:57:47 +08:00
get { return mcType; }
}
public IEnumerable<MethodDef> InitMethods {
2012-02-21 18:57:47 +08:00
get {
var list = new List<MethodDef>();
2012-02-21 18:57:47 +08:00
if (mcType == null)
return list;
foreach (var method in mcType.Methods) {
2013-01-19 20:03:57 +08:00
if (method.IsStatic && DotNetUtils.IsMethod(method, "System.Void", "()"))
2012-02-21 18:57:47 +08:00
list.Add(method);
}
return list;
}
}
2013-10-21 00:09:38 +08:00
public IEnumerable<ModuleRef> RuntimeModuleRefs {
get {
if (runtimeModule1 != null)
yield return runtimeModule1;
if (runtimeModule2 != null)
yield return runtimeModule2;
}
}
2012-02-20 11:55:59 +08:00
public bool Detected {
get { return mcType != null; }
}
2012-11-18 14:34:51 +08:00
public MainType(ModuleDefMD module) {
2012-02-20 11:55:59 +08:00
this.module = module;
}
2012-11-18 14:34:51 +08:00
public MainType(ModuleDefMD module, MainType oldOne) {
this.module = module;
2013-01-19 20:03:57 +08:00
this.mcType = Lookup(oldOne.mcType, "Could not find main type");
}
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);
}
2013-01-19 20:03:57 +08:00
public void Find() {
foreach (var cctor in DeobUtils.GetInitCctors(module, 3)) {
if (CheckCctor(cctor))
2012-03-25 02:14:16 +08:00
break;
}
}
2013-01-19 20:03:57 +08:00
bool CheckCctor(MethodDef cctor) {
foreach (var method in DotNetUtils.GetCalledMethods(module, cctor)) {
2012-02-20 11:55:59 +08:00
if (method.Name != "Startup")
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Void", "()"))
2012-02-20 11:55:59 +08:00
continue;
2012-02-22 19:14:15 +08:00
bool isOldTmp;
2013-10-21 00:09:38 +08:00
if (!CheckType(method.DeclaringType, out runtimeModule1, out runtimeModule2, out isOldTmp))
2012-03-25 02:14:16 +08:00
continue;
2012-02-20 11:55:59 +08:00
mcType = method.DeclaringType;
2012-02-22 19:14:15 +08:00
isOld = isOldTmp;
2012-03-25 02:14:16 +08:00
return true;
2012-02-20 11:55:59 +08:00
}
2012-03-25 02:14:16 +08:00
return false;
2012-02-20 11:55:59 +08:00
}
2013-01-19 20:03:57 +08:00
static bool CheckType(TypeDef type, out ModuleRef module1, out ModuleRef module2, out bool isOld) {
2012-02-20 11:55:59 +08:00
module1 = module2 = null;
2012-02-22 19:14:15 +08:00
isOld = false;
2012-02-20 11:55:59 +08:00
2012-11-18 14:34:51 +08:00
if (type.FindMethod("Startup") == null)
2012-02-20 11:55:59 +08:00
return false;
2013-01-19 20:03:57 +08:00
var pinvokes = GetPinvokes(type);
var pinvokeList = GetPinvokeList(pinvokes, "CheckRuntime");
2012-02-20 11:55:59 +08:00
if (pinvokeList == null)
return false;
2013-01-19 20:03:57 +08:00
if (GetPinvokeList(pinvokes, "MainDLL") == null)
2012-02-20 11:55:59 +08:00
return false;
2012-02-22 19:14:15 +08:00
// Newer versions (3.4+ ???) also have GetModuleBase()
2013-01-19 20:03:57 +08:00
isOld = GetPinvokeList(pinvokes, "GetModuleBase") == null;
2012-02-20 11:55:59 +08:00
2012-11-18 14:34:51 +08:00
module1 = pinvokeList[0].ImplMap.Module;
module2 = pinvokeList[1].ImplMap.Module;
2012-02-20 11:55:59 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
static Dictionary<string, List<MethodDef>> GetPinvokes(TypeDef type) {
var pinvokes = new Dictionary<string, List<MethodDef>>(StringComparer.Ordinal);
2012-02-20 11:55:59 +08:00
foreach (var method in type.Methods) {
2012-11-18 14:34:51 +08:00
var info = method.ImplMap;
if (info == null || UTF8String.IsNullOrEmpty(info.Name))
2012-02-20 11:55:59 +08:00
continue;
List<MethodDef> list;
2012-11-18 14:34:51 +08:00
if (!pinvokes.TryGetValue(info.Name.String, out list))
pinvokes[info.Name.String] = list = new List<MethodDef>();
2012-02-20 11:55:59 +08:00
list.Add(method);
}
return pinvokes;
}
2013-01-19 20:03:57 +08:00
static List<MethodDef> GetPinvokeList(Dictionary<string, List<MethodDef>> pinvokes, string methodName) {
List<MethodDef> list;
2012-02-20 11:55:59 +08:00
if (!pinvokes.TryGetValue(methodName, out list))
return null;
if (list.Count != 2)
return null;
return list;
}
}
}