de4dot-cex/de4dot.code/deobfuscators/Agile_NET/vm/CilOperandInstructionRestorer.cs

130 lines
3.2 KiB
C#
Raw Normal View History

2012-04-06 00:06:56 +08:00
/*
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 dot10.DotNet;
using dot10.DotNet.Emit;
2012-04-06 00:06:56 +08:00
using de4dot.blocks;
2012-11-06 23:38:39 +08:00
namespace de4dot.code.deobfuscators.Agile_NET.vm {
2012-04-06 00:06:56 +08:00
// Tries to restore the operands of the following CIL instructions:
// ldelema
// ldobj
// stobj
class CilOperandInstructionRestorer {
MethodDef method;
2012-04-06 00:06:56 +08:00
public bool restore(MethodDef method) {
2012-04-06 00:06:56 +08:00
this.method = method;
bool atLeastOneFailed = false;
2012-04-06 00:06:56 +08:00
if (method == null || method.Body == null)
2012-04-07 12:47:19 +08:00
return !atLeastOneFailed;
2012-04-06 00:06:56 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.Operand != null)
continue;
2012-11-07 12:17:45 +08:00
TypeSig operandType = null;
2012-04-06 00:06:56 +08:00
switch (instr.OpCode.Code) {
case Code.Ldelema:
2012-11-07 12:17:45 +08:00
var arrayType = MethodStack.getLoadedType(method, instrs, i, 1) as SZArraySig;
2012-04-06 00:06:56 +08:00
if (arrayType == null)
break;
2012-11-07 12:17:45 +08:00
operandType = arrayType.Next;
2012-04-06 00:06:56 +08:00
break;
case Code.Ldobj:
operandType = getPtrElementType(MethodStack.getLoadedType(method, instrs, i, 0));
2012-04-06 00:06:56 +08:00
break;
case Code.Stobj:
operandType = MethodStack.getLoadedType(method, instrs, i, 0);
if (!isValidType(operandType))
operandType = getPtrElementType(MethodStack.getLoadedType(method, instrs, i, 1));
2012-04-06 00:06:56 +08:00
break;
default:
continue;
}
if (!isValidType(operandType)) {
atLeastOneFailed = true;
2012-04-06 00:06:56 +08:00
continue;
}
2012-04-06 00:06:56 +08:00
2012-11-07 12:17:45 +08:00
instr.Operand = operandType.ToTypeDefOrRef();
2012-04-06 00:06:56 +08:00
}
2012-04-07 12:47:19 +08:00
return !atLeastOneFailed;
2012-04-06 00:06:56 +08:00
}
2012-11-07 12:17:45 +08:00
static TypeSig getPtrElementType(TypeSig type) {
if (type == null)
return null;
2012-11-07 12:17:45 +08:00
if (type.IsPointer || type.IsByRef)
return type.Next;
return null;
}
2012-11-07 12:17:45 +08:00
bool isValidType(TypeSig type) {
type = type.RemovePinnedAndModifiers();
2012-04-06 00:06:56 +08:00
if (type == null)
return false;
2012-11-07 12:17:45 +08:00
if (type.ElementType == ElementType.Void)
2012-04-06 00:06:56 +08:00
return false;
while (type != null) {
2012-11-07 12:17:45 +08:00
switch (type.ElementType) {
case ElementType.SZArray:
case ElementType.Array:
case ElementType.GenericInst:
case ElementType.Ptr:
case ElementType.Class:
case ElementType.ValueType:
case ElementType.FnPtr:
2012-04-06 00:06:56 +08:00
break;
2012-11-07 12:17:45 +08:00
case ElementType.MVar:
var gmvar = (GenericMVar)type;
if (gmvar.Number >= method.MethodSig.GetGenParamCount())
2012-04-06 00:06:56 +08:00
return false;
break;
2012-11-07 12:17:45 +08:00
case ElementType.Var:
var gvar = (GenericVar)type;
var dt = method.DeclaringType;
2012-11-17 06:50:52 +08:00
if (dt == null || gvar.Number >= dt.GenericParameters.Count)
2012-11-07 12:17:45 +08:00
return false;
break;
case ElementType.ByRef:
2012-04-06 00:06:56 +08:00
default:
return false;
}
2012-11-07 12:17:45 +08:00
if (type.Next == null)
2012-04-06 00:06:56 +08:00
break;
2012-11-07 12:17:45 +08:00
type = type.Next;
2012-04-06 00:06:56 +08:00
}
return type != null;
}
}
}