2012-04-06 00:06:56 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-04-06 00:06:56 +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;
|
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 {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef method;
|
2012-04-06 00:06:56 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool Restore(MethodDef method) {
|
2012-04-06 00:06:56 +08:00
|
|
|
|
this.method = method;
|
2012-04-06 18:33:39 +08:00
|
|
|
|
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:
|
2013-01-19 20:03:57 +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:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
operandType = GetPtrElementType(MethodStack.GetLoadedType(method, instrs, i, 0));
|
2012-04-06 00:06:56 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Stobj:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!IsValidType(operandType)) {
|
2012-04-06 18:33:39 +08:00
|
|
|
|
atLeastOneFailed = true;
|
2012-04-06 00:06:56 +08:00
|
|
|
|
continue;
|
2012-04-06 18:33:39 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static TypeSig GetPtrElementType(TypeSig type) {
|
2012-04-06 21:38:44 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return null;
|
2012-11-07 12:17:45 +08:00
|
|
|
|
if (type.IsPointer || type.IsByRef)
|
|
|
|
|
return type.Next;
|
2012-04-06 21:38:44 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool IsValidType(TypeSig type) {
|
2012-11-07 12:17:45 +08:00
|
|
|
|
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-11-20 08:14:05 +08:00
|
|
|
|
case ElementType.Void:
|
|
|
|
|
case ElementType.Boolean:
|
|
|
|
|
case ElementType.Char:
|
|
|
|
|
case ElementType.I1:
|
|
|
|
|
case ElementType.U1:
|
|
|
|
|
case ElementType.I2:
|
|
|
|
|
case ElementType.U2:
|
|
|
|
|
case ElementType.I4:
|
|
|
|
|
case ElementType.U4:
|
|
|
|
|
case ElementType.I8:
|
|
|
|
|
case ElementType.U8:
|
|
|
|
|
case ElementType.R4:
|
|
|
|
|
case ElementType.R8:
|
|
|
|
|
case ElementType.TypedByRef:
|
|
|
|
|
case ElementType.I:
|
|
|
|
|
case ElementType.U:
|
|
|
|
|
case ElementType.String:
|
|
|
|
|
case ElementType.Object:
|
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-11-20 08:14:05 +08:00
|
|
|
|
case ElementType.CModOpt:
|
|
|
|
|
case ElementType.CModReqd:
|
|
|
|
|
case ElementType.Pinned:
|
|
|
|
|
case ElementType.Sentinel:
|
|
|
|
|
case ElementType.ValueArray:
|
|
|
|
|
case ElementType.R:
|
|
|
|
|
case ElementType.End:
|
|
|
|
|
case ElementType.Internal:
|
|
|
|
|
case ElementType.Module:
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|