2011-10-17 06:22:22 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011 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 System.Collections.Generic;
|
2011-10-18 14:28:23 +08:00
|
|
|
|
using Mono.Cecil.Cil;
|
2011-10-17 06:22:22 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.blocks.cflow {
|
2011-10-19 05:31:50 +08:00
|
|
|
|
public class BlocksCflowDeobfuscator {
|
2011-10-19 07:53:42 +08:00
|
|
|
|
BlockCflowDeobfuscator blockCflowDeobfuscator = new BlockCflowDeobfuscator();
|
2011-10-17 06:22:22 +08:00
|
|
|
|
Blocks blocks;
|
|
|
|
|
int numRemovedDeadBlocks;
|
|
|
|
|
|
|
|
|
|
public int NumberOfRemovedDeadBlocks {
|
|
|
|
|
get { return numRemovedDeadBlocks; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init(Blocks blocks) {
|
|
|
|
|
this.blocks = blocks;
|
|
|
|
|
numRemovedDeadBlocks = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deobfuscate() {
|
2011-10-19 05:31:50 +08:00
|
|
|
|
var allBlocks = new List<Block>();
|
|
|
|
|
var switchCflowDeobfuscator = new SwitchCflowDeobfuscator();
|
2011-10-19 21:49:19 +08:00
|
|
|
|
var deadCodeRemover = new DeadCodeRemover();
|
2011-10-22 00:02:58 +08:00
|
|
|
|
var deadStoreRemover = new DeadStoreRemover();
|
|
|
|
|
var stLdlocFixer = new StLdlocFixer();
|
2011-10-17 06:22:22 +08:00
|
|
|
|
bool changed;
|
|
|
|
|
do {
|
|
|
|
|
changed = false;
|
2011-10-19 07:53:42 +08:00
|
|
|
|
removeDeadBlocks();
|
|
|
|
|
mergeBlocks();
|
2011-10-19 05:31:50 +08:00
|
|
|
|
|
2011-10-22 00:02:58 +08:00
|
|
|
|
blocks.MethodBlocks.getAllBlocks(allBlocks);
|
2011-10-19 05:31:50 +08:00
|
|
|
|
|
|
|
|
|
foreach (var block in allBlocks) {
|
2011-10-18 14:28:23 +08:00
|
|
|
|
var lastInstr = block.LastInstr;
|
|
|
|
|
if (!DotNetUtils.isConditionalBranch(lastInstr.OpCode.Code) && lastInstr.OpCode.Code != Code.Switch)
|
|
|
|
|
continue;
|
2011-10-19 07:53:42 +08:00
|
|
|
|
blockCflowDeobfuscator.init(block, blocks.Method.Parameters, blocks.Locals);
|
|
|
|
|
changed |= blockCflowDeobfuscator.deobfuscate();
|
2011-10-17 06:22:22 +08:00
|
|
|
|
}
|
2011-10-19 05:31:50 +08:00
|
|
|
|
|
|
|
|
|
switchCflowDeobfuscator.init(blocks, allBlocks);
|
|
|
|
|
changed |= switchCflowDeobfuscator.deobfuscate();
|
2011-10-19 21:49:19 +08:00
|
|
|
|
|
2011-10-22 00:02:58 +08:00
|
|
|
|
deadStoreRemover.init(blocks, allBlocks);
|
|
|
|
|
changed |= deadStoreRemover.remove();
|
|
|
|
|
|
2011-10-19 21:49:19 +08:00
|
|
|
|
deadCodeRemover.init(allBlocks);
|
|
|
|
|
changed |= deadCodeRemover.remove();
|
2011-10-22 00:02:58 +08:00
|
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
|
stLdlocFixer.init(allBlocks, blocks.Locals);
|
|
|
|
|
changed |= stLdlocFixer.fix();
|
|
|
|
|
}
|
2011-10-17 06:22:22 +08:00
|
|
|
|
} while (changed);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-19 05:31:50 +08:00
|
|
|
|
bool removeDeadBlocks() {
|
|
|
|
|
int count = new DeadBlocksRemover(blocks.MethodBlocks).remove();
|
|
|
|
|
numRemovedDeadBlocks += count;
|
|
|
|
|
return count > 0;
|
2011-10-17 06:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-19 05:31:50 +08:00
|
|
|
|
bool mergeBlocks() {
|
|
|
|
|
bool changed = false;
|
2011-10-17 06:22:22 +08:00
|
|
|
|
foreach (var scopeBlock in getAllScopeBlocks(blocks.MethodBlocks))
|
2011-10-19 05:31:50 +08:00
|
|
|
|
changed |= scopeBlock.mergeBlocks() > 0;
|
|
|
|
|
return changed;
|
2011-10-17 06:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<ScopeBlock> getAllScopeBlocks(ScopeBlock scopeBlock) {
|
|
|
|
|
var list = new List<ScopeBlock>();
|
|
|
|
|
list.Add(scopeBlock);
|
|
|
|
|
list.AddRange(scopeBlock.getAllScopeBlocks());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|