de4dot-cex/de4dot.code/deobfuscators/Skater_NET/EnumClassFinder.cs

82 lines
2.1 KiB
C#
Raw Permalink Normal View History

2011-12-31 20:14:02 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-31 20:14:02 +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 dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-31 20:14:02 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Skater_NET {
class EnumClassFinder {
2012-11-07 00:15:11 +08:00
ModuleDefMD module;
FieldDef enumField;
2011-12-31 20:14:02 +08:00
2012-11-07 00:15:11 +08:00
public EnumClassFinder(ModuleDefMD module) {
2011-12-31 20:14:02 +08:00
this.module = module;
2013-01-19 20:03:57 +08:00
Find();
2011-12-31 20:14:02 +08:00
}
2013-01-19 20:03:57 +08:00
void Find() {
2011-12-31 20:14:02 +08:00
foreach (var type in module.Types) {
if (type.HasEvents || type.HasProperties)
continue;
if (type.Methods.Count != 1)
continue;
if (type.Fields.Count != 1)
continue;
var method = type.Methods[0];
if (method.Name != ".ctor")
continue;
var field = type.Fields[0];
2013-01-19 20:03:57 +08:00
var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
2011-12-31 20:14:02 +08:00
if (fieldType == null)
continue;
if (!fieldType.IsEnum)
continue;
enumField = field;
return;
}
}
2013-01-19 20:03:57 +08:00
public void Deobfuscate(Blocks blocks) {
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
2011-12-31 20:14:02 +08:00
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldsfld = instrs[i];
if (ldsfld.OpCode.Code != Code.Ldsfld)
continue;
var ldci4 = instrs[i + 1];
2013-01-19 20:03:57 +08:00
if (!ldci4.IsLdcI4())
2011-12-31 20:14:02 +08:00
continue;
var stfld = instrs[i + 2];
if (stfld.OpCode.Code != Code.Stfld)
continue;
2012-11-07 00:15:11 +08:00
var field = stfld.Operand as IField;
if (!FieldEqualityComparer.CompareDeclaringTypes.Equals(enumField, field))
2011-12-31 20:14:02 +08:00
continue;
2013-01-19 20:03:57 +08:00
block.Remove(i, 3);
2011-12-31 20:14:02 +08:00
i--;
}
}
}
}
}