2011-12-29 15:26:36 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-12-29 15:26:36 +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;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-12-29 15:26:36 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Goliath_NET {
|
|
|
|
|
class StrongNameChecker {
|
2012-11-18 10:02:12 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef strongNameType;
|
|
|
|
|
MethodDef strongNameCheckMethod;
|
2011-12-29 15:26:36 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return strongNameType != null;}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
get { return strongNameType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef CheckerMethod {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
get { return strongNameCheckMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 10:02:12 +08:00
|
|
|
|
public StrongNameChecker(ModuleDefMD module) {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.HasFields || type.HasEvents || type.HasProperties)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var checkMethod = GetAntiTamperingDetectionMethod(type);
|
2011-12-29 15:26:36 +08:00
|
|
|
|
if (checkMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.GetMethod(type, "System.Byte[]", "(System.Reflection.Assembly)") == null)
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.GetMethod(type, "System.String", "(System.Collections.Generic.Stack`1<System.Int32>)") == null)
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.GetMethod(type, "System.Int32", "(System.Int32,System.Byte[])") == null)
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
strongNameType = type;
|
|
|
|
|
strongNameCheckMethod = checkMethod;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
MethodDef GetAntiTamperingDetectionMethod(TypeDef type) {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
var requiredLocals = new string[] {
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
"System.Collections.Generic.Stack`1<System.Int32>",
|
|
|
|
|
};
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "(System.Type)"))
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new LocalTypes(method).All(requiredLocals))
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!HasThrow(method))
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool HasThrow(MethodDef method) {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code == Code.Throw)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool Deobfuscate(Blocks blocks) {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
if (blocks.Method.Name != ".cctor" && blocks.Method.Name != ".ctor")
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldtoken = instrs[i];
|
|
|
|
|
if (ldtoken.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call1 = instrs[i + 1];
|
|
|
|
|
if (call1.OpCode.Code != Code.Call && call1.OpCode.Code != Code.Callvirt)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(call1.Operand as IMethod, "System.Type", "(System.RuntimeTypeHandle)"))
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call2 = instrs[i + 2];
|
|
|
|
|
if (call2.OpCode.Code != Code.Call && call2.OpCode.Code != Code.Callvirt)
|
|
|
|
|
continue;
|
2012-11-18 10:02:12 +08:00
|
|
|
|
if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(call2.Operand as IMethod, strongNameCheckMethod))
|
2011-12-29 15:26:36 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
block.Remove(i, 3);
|
2011-12-29 15:26:36 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|