de4dot-cex/de4dot.code/deobfuscators/Confuser/ConstantsDecrypterUtils.cs

131 lines
4.2 KiB
C#
Raw Normal View History

2012-08-06 02:25:43 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-08-06 02:25:43 +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-23 04:08:29 +08:00
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-08-06 02:25:43 +08:00
namespace de4dot.code.deobfuscators.Confuser {
static class ConstantsDecrypterUtils {
2013-01-19 20:09:49 +08:00
public static FieldDef FindDictField(MethodDef method, TypeDef declaringType) {
2012-08-06 02:25:43 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
2012-11-19 06:42:43 +08:00
var ctor = newobj.Operand as IMethod;
2012-08-06 02:25:43 +08:00
if (ctor == null || ctor.FullName != "System.Void System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
2012-11-19 06:42:43 +08:00
var field = stsfld.Operand as FieldDef;
2012-08-06 02:25:43 +08:00
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>")
continue;
return field;
}
return null;
}
2013-01-20 22:59:30 +08:00
public static FieldDef FindDataField_v18_r75367(MethodDef method, TypeDef declaringType) {
2012-08-06 02:25:43 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var callvirt = instrs[i];
if (callvirt.OpCode.Code != Code.Callvirt)
continue;
2012-11-19 06:42:43 +08:00
var calledMethod = callvirt.Operand as IMethod;
2012-08-06 02:25:43 +08:00
if (calledMethod == null || calledMethod.FullName != "System.Byte[] System.IO.MemoryStream::ToArray()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
2012-11-19 06:42:43 +08:00
var field = stsfld.Operand as FieldDef;
2012-08-06 02:25:43 +08:00
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Byte[]")
continue;
return field;
}
return null;
}
2013-01-20 22:59:30 +08:00
// Normal ("safe") mode only (not dynamic or native)
public static FieldDef FindDataField_v19_r77172(MethodDef method, TypeDef declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldloc = instrs[i];
if (!ldloc.IsLdloc())
continue;
var local = ldloc.GetLocal(method.Body.Variables);
if (local == null || local.Type.GetFullName() != "System.Byte[]")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDef;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Byte[]")
continue;
return field;
}
return null;
}
2013-01-19 20:09:49 +08:00
public static FieldDef FindStreamField(MethodDef method, TypeDef declaringType) {
return FindStreamField(method, declaringType, "System.IO.Stream");
}
2013-01-19 20:09:49 +08:00
public static FieldDef FindMemoryStreamField(MethodDef method, TypeDef declaringType) {
return FindStreamField(method, declaringType, "System.IO.MemoryStream");
}
2013-01-19 20:09:49 +08:00
public static FieldDef FindStreamField(MethodDef method, TypeDef declaringType, string fieldTypeName) {
2012-08-06 02:25:43 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
2012-11-19 06:42:43 +08:00
var ctor = newobj.Operand as IMethod;
2012-08-06 02:25:43 +08:00
if (ctor == null || ctor.FullName != "System.Void System.IO.MemoryStream::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
2012-11-19 06:42:43 +08:00
var field = stsfld.Operand as FieldDef;
2012-08-06 02:25:43 +08:00
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != fieldTypeName)
2012-08-06 02:25:43 +08:00
continue;
return field;
}
return null;
}
}
}