de4dot-cex/de4dot.code/deobfuscators/Goliath_NET/LocalsRestorer.cs

151 lines
3.9 KiB
C#
Raw Normal View History

2011-12-29 15:26:36 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 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/>.
*/
using System.Collections.Generic;
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 LocalsRestorer {
2012-11-18 10:02:12 +08:00
ModuleDefMD module;
2012-11-22 16:14:51 +08:00
TypeDefDict<Info> typeToInfo = new TypeDefDict<Info>();
2011-12-29 15:26:36 +08:00
class Info {
public TypeDef type;
2012-11-18 10:02:12 +08:00
public TypeSig localType;
2011-12-29 15:26:36 +08:00
public bool referenced = false;
2012-11-18 10:02:12 +08:00
public Info(TypeDef type, TypeSig localType) {
2011-12-29 15:26:36 +08:00
this.type = type;
this.localType = localType;
}
}
public List<TypeDef> Types {
2011-12-29 15:26:36 +08:00
get {
var list = new List<TypeDef>(typeToInfo.Count);
2013-01-19 20:03:57 +08:00
foreach (var info in typeToInfo.GetValues()) {
2011-12-29 15:26:36 +08:00
if (info.referenced)
list.Add(info.type);
}
return list;
}
}
2012-11-18 10:02:12 +08:00
public LocalsRestorer(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.GetTypes())
2013-01-19 20:03:57 +08:00
Initialize(type);
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
void Initialize(TypeDef type) {
2011-12-29 15:26:36 +08:00
if (type.HasEvents || type.HasProperties)
return;
if (!type.IsValueType)
return;
if (type.Methods.Count != 1)
return;
var ctor = type.Methods[0];
if (ctor.Name != ".ctor" || ctor.Body == null || ctor.IsStatic)
return;
2012-11-18 10:02:12 +08:00
var sig = ctor.MethodSig;
if (sig == null || sig.Params.Count != 1)
2011-12-29 15:26:36 +08:00
return;
2012-11-18 10:02:12 +08:00
var ctorParam = sig.Params[0];
2011-12-29 15:26:36 +08:00
if (type.Fields.Count != 1)
return;
var typeField = type.Fields[0];
if (typeField.IsStatic)
return;
2012-11-18 10:02:12 +08:00
if (!new SigComparer().Equals(ctorParam, typeField.FieldType))
2011-12-29 15:26:36 +08:00
return;
2013-01-19 20:03:57 +08:00
typeToInfo.Add(ctor.DeclaringType, new Info(ctor.DeclaringType, typeField.FieldType));
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
public void Deobfuscate(Blocks blocks) {
2011-12-29 15:26:36 +08:00
var instrsToRemove = new List<int>();
2013-01-19 20:03:57 +08:00
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
2011-12-29 15:26:36 +08:00
instrsToRemove.Clear();
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
int indexToRemove;
2012-11-18 10:02:12 +08:00
ITypeDefOrRef type;
Local local = null;
2011-12-29 15:26:36 +08:00
if (instr.OpCode.Code == Code.Newobj) {
if (i + 1 >= instrs.Count)
continue;
2012-11-18 10:02:12 +08:00
var ctor = instr.Operand as IMethod;
2011-12-29 15:26:36 +08:00
if (ctor == null || ctor.DeclaringType == null)
continue;
if (ctor.Name != ".ctor")
continue;
var next = instrs[i + 1];
2013-01-19 20:03:57 +08:00
if (!next.IsStloc() && !next.IsLeave() && next.OpCode.Code != Code.Pop)
2011-12-29 15:26:36 +08:00
continue;
indexToRemove = i;
type = ctor.DeclaringType;
2013-01-19 20:03:57 +08:00
if (next.IsStloc())
local = Instr.GetLocalVar(blocks.Locals, next);
2011-12-29 15:26:36 +08:00
}
else if (instr.OpCode.Code == Code.Ldfld) {
if (i == 0)
continue;
var ldloc = instrs[i - 1];
2013-01-19 20:03:57 +08:00
if (!ldloc.IsLdloc())
2011-12-29 15:26:36 +08:00
continue;
2012-11-18 10:02:12 +08:00
var field = instr.Operand as IField;
2011-12-29 15:26:36 +08:00
if (field == null || field.DeclaringType == null)
continue;
indexToRemove = i;
type = field.DeclaringType;
2013-01-19 20:03:57 +08:00
local = Instr.GetLocalVar(blocks.Locals, ldloc);
2011-12-29 15:26:36 +08:00
}
else
continue;
if (type == null)
continue;
2013-01-19 20:03:57 +08:00
var info = typeToInfo.Find(type);
2011-12-29 15:26:36 +08:00
if (info == null)
continue;
info.referenced = true;
instrsToRemove.Add(indexToRemove);
if (local != null)
2012-11-18 10:02:12 +08:00
local.Type = info.localType;
2011-12-29 15:26:36 +08:00
}
2012-11-18 10:02:12 +08:00
if (instrsToRemove.Count > 0)
2013-01-19 20:03:57 +08:00
block.Remove(instrsToRemove);
2011-12-29 15:26:36 +08:00
}
}
}
}