142 lines
3.7 KiB
C#
142 lines
3.7 KiB
C#
/*
|
|
Copyright (C) 2011-2012 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 System;
|
|
using System.Collections.Generic;
|
|
using dnlib.DotNet;
|
|
using de4dot.blocks;
|
|
|
|
namespace de4dot.code.deobfuscators.MaxtoCode {
|
|
class MainType {
|
|
ModuleDefMD module;
|
|
TypeDef mcType;
|
|
bool isOld;
|
|
|
|
public bool IsOld {
|
|
get { return isOld; }
|
|
}
|
|
|
|
public TypeDef Type {
|
|
get { return mcType; }
|
|
}
|
|
|
|
public IEnumerable<MethodDef> InitMethods {
|
|
get {
|
|
var list = new List<MethodDef>();
|
|
if (mcType == null)
|
|
return list;
|
|
foreach (var method in mcType.Methods) {
|
|
if (method.IsStatic && DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
list.Add(method);
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
|
|
public bool Detected {
|
|
get { return mcType != null; }
|
|
}
|
|
|
|
public MainType(ModuleDefMD module) {
|
|
this.module = module;
|
|
}
|
|
|
|
public MainType(ModuleDefMD module, MainType oldOne) {
|
|
this.module = module;
|
|
this.mcType = lookup(oldOne.mcType, "Could not find main type");
|
|
}
|
|
|
|
T lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
|
|
return DeobUtils.lookup(module, def, errorMessage);
|
|
}
|
|
|
|
public void find() {
|
|
foreach (var cctor in DeobUtils.getInitCctors(module, 3)) {
|
|
if (checkCctor(cctor))
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool checkCctor(MethodDef cctor) {
|
|
foreach (var method in DotNetUtils.getCalledMethods(module, cctor)) {
|
|
if (method.Name != "Startup")
|
|
continue;
|
|
if (!DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
continue;
|
|
|
|
ModuleRef module1, module2;
|
|
bool isOldTmp;
|
|
if (!checkType(method.DeclaringType, out module1, out module2, out isOldTmp))
|
|
continue;
|
|
|
|
mcType = method.DeclaringType;
|
|
isOld = isOldTmp;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool checkType(TypeDef type, out ModuleRef module1, out ModuleRef module2, out bool isOld) {
|
|
module1 = module2 = null;
|
|
isOld = false;
|
|
|
|
if (type.FindMethod("Startup") == null)
|
|
return false;
|
|
|
|
var pinvokes = getPinvokes(type);
|
|
var pinvokeList = getPinvokeList(pinvokes, "CheckRuntime");
|
|
if (pinvokeList == null)
|
|
return false;
|
|
if (getPinvokeList(pinvokes, "MainDLL") == null)
|
|
return false;
|
|
|
|
// Newer versions (3.4+ ???) also have GetModuleBase()
|
|
isOld = getPinvokeList(pinvokes, "GetModuleBase") == null;
|
|
|
|
module1 = pinvokeList[0].ImplMap.Module;
|
|
module2 = pinvokeList[1].ImplMap.Module;
|
|
return true;
|
|
}
|
|
|
|
static Dictionary<string, List<MethodDef>> getPinvokes(TypeDef type) {
|
|
var pinvokes = new Dictionary<string, List<MethodDef>>(StringComparer.Ordinal);
|
|
foreach (var method in type.Methods) {
|
|
var info = method.ImplMap;
|
|
if (info == null || UTF8String.IsNullOrEmpty(info.Name))
|
|
continue;
|
|
List<MethodDef> list;
|
|
if (!pinvokes.TryGetValue(info.Name.String, out list))
|
|
pinvokes[info.Name.String] = list = new List<MethodDef>();
|
|
list.Add(method);
|
|
}
|
|
return pinvokes;
|
|
}
|
|
|
|
static List<MethodDef> getPinvokeList(Dictionary<string, List<MethodDef>> pinvokes, string methodName) {
|
|
List<MethodDef> list;
|
|
if (!pinvokes.TryGetValue(methodName, out list))
|
|
return null;
|
|
if (list.Count != 2)
|
|
return null;
|
|
return list;
|
|
}
|
|
}
|
|
}
|