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;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
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 {
|
2012-01-28 09:54:12 +08:00
|
|
|
|
MethodDefinitionAndDeclaringTypeDict<bool> exceptionLoggerMethods = new MethodDefinitionAndDeclaringTypeDict<bool>();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
public int NumRemovedExceptionLoggers { get; set; }
|
|
|
|
|
|
2012-11-01 23:42:02 +08:00
|
|
|
|
public void add(MethodDef exceptionLogger) {
|
2012-01-28 09:54:12 +08:00
|
|
|
|
exceptionLoggerMethods.add(exceptionLogger, true);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2012-11-02 14:36:02 +08:00
|
|
|
|
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;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
var calledMethod = callInstr.Operand as IMethod;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
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-11-01 23:42:02 +08:00
|
|
|
|
protected virtual bool isExceptionLogger(IMethod method) {
|
2012-01-28 09:54:12 +08:00
|
|
|
|
return exceptionLoggerMethods.find(method);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|