2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-09-22 10:55:30 +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 Mono.Cecil;
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
class ExceptionLoggerRemover {
|
|
|
|
|
Dictionary<MethodReference, bool> exceptionLoggerMethods = new Dictionary<MethodReference, bool>();
|
|
|
|
|
|
|
|
|
|
public int NumRemovedExceptionLoggers { get; set; }
|
|
|
|
|
|
|
|
|
|
public void add(MethodDefinition exceptionLogger) {
|
|
|
|
|
exceptionLoggerMethods[exceptionLogger] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool find(Blocks blocks, out TryBlock tryBlock) {
|
|
|
|
|
tryBlock = null;
|
|
|
|
|
|
|
|
|
|
foreach (var bb in blocks.MethodBlocks.BaseBlocks) {
|
|
|
|
|
tryBlock = bb as TryBlock;
|
|
|
|
|
if (tryBlock == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (tryBlock.TryHandlerBlocks.Count != 1)
|
|
|
|
|
continue;
|
|
|
|
|
var catchBlock = tryBlock.TryHandlerBlocks[0];
|
|
|
|
|
if (catchBlock.HandlerType != ExceptionHandlerType.Catch ||
|
2012-01-17 09:54:48 +08:00
|
|
|
|
catchBlock.CatchType.FullName != "System.Exception") {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (catchBlock.BaseBlocks.Count != 1)
|
|
|
|
|
continue;
|
|
|
|
|
var handlerBlock = catchBlock.BaseBlocks[0] as HandlerBlock;
|
|
|
|
|
if (handlerBlock == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
int calls = 0;
|
|
|
|
|
Instr callInstr = null;
|
|
|
|
|
bool failed = false;
|
|
|
|
|
foreach (var bb2 in handlerBlock.BaseBlocks) {
|
|
|
|
|
var block = bb2 as Block;
|
|
|
|
|
if (block == null) {
|
|
|
|
|
failed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
foreach (var instr in block.Instructions) {
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Call:
|
|
|
|
|
case Code.Calli:
|
|
|
|
|
case Code.Callvirt:
|
|
|
|
|
calls++;
|
|
|
|
|
callInstr = instr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (failed || calls != 1 || callInstr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
|
|
|
|
var calledMethod = callInstr.Operand as MethodReference;
|
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (!isExceptionLogger(calledMethod))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 09:54:48 +08:00
|
|
|
|
protected virtual bool isExceptionLogger(MethodReference method) {
|
|
|
|
|
return exceptionLoggerMethods.ContainsKey(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool HasExceptionLoggers {
|
|
|
|
|
get { return exceptionLoggerMethods.Count != 0; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
|
public bool remove(Blocks blocks) {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (!HasExceptionLoggers)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
TryBlock tryBlock;
|
|
|
|
|
if (!find(blocks, out tryBlock))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
blocks.MethodBlocks.removeTryBlock(tryBlock);
|
|
|
|
|
NumRemovedExceptionLoggers++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|