de4dot-cex/de4dot.blocks/cflow/BlocksCflowDeobfuscator.cs

189 lines
5.6 KiB
C#
Raw Normal View History

2011-10-17 06:22:22 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-10-17 06:22:22 +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 dnlib.DotNet.Emit;
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-17 06:22:22 +08:00
Blocks blocks;
2011-10-22 00:05:24 +08:00
List<Block> allBlocks = new List<Block>();
List<IBlocksDeobfuscator> userBlocksDeobfuscators = new List<IBlocksDeobfuscator>();
List<IBlocksDeobfuscator> ourBlocksDeobfuscators = new List<IBlocksDeobfuscator>();
2011-10-17 06:22:22 +08:00
2015-10-30 05:36:17 +08:00
public BlocksCflowDeobfuscator()
: this(false) {
}
2015-10-30 05:36:17 +08:00
public BlocksCflowDeobfuscator(bool disableNewCFCode) {
Initialize(disableNewCFCode);
}
public BlocksCflowDeobfuscator(IEnumerable<IBlocksDeobfuscator> blocksDeobfuscator)
: this(blocksDeobfuscator, false) {
}
public BlocksCflowDeobfuscator(IEnumerable<IBlocksDeobfuscator> blocksDeobfuscator, bool disableNewCFCode) {
Initialize(disableNewCFCode);
2013-01-19 20:03:57 +08:00
Add(blocksDeobfuscator);
}
2015-10-30 05:36:17 +08:00
void Initialize(bool disableNewCFCode) {
2013-04-30 18:15:07 +08:00
ourBlocksDeobfuscators.Add(new BlockCflowDeobfuscator { ExecuteIfNotModified = false });
ourBlocksDeobfuscators.Add(new SwitchCflowDeobfuscator { ExecuteIfNotModified = false });
ourBlocksDeobfuscators.Add(new DeadStoreRemover { ExecuteIfNotModified = false });
ourBlocksDeobfuscators.Add(new DeadCodeRemover { ExecuteIfNotModified = false });
2015-10-30 05:36:17 +08:00
ourBlocksDeobfuscators.Add(new ConstantsFolder { ExecuteIfNotModified = true, DisableNewCode = disableNewCFCode });
2013-04-30 18:15:07 +08:00
ourBlocksDeobfuscators.Add(new StLdlocFixer { ExecuteIfNotModified = true });
ourBlocksDeobfuscators.Add(new DupBlockCflowDeobfuscator { ExecuteIfNotModified = true });
}
2013-01-19 20:03:57 +08:00
public void Add(IEnumerable<IBlocksDeobfuscator> blocksDeobfuscators) {
foreach (var bd in blocksDeobfuscators)
2013-01-19 20:03:57 +08:00
Add(bd);
}
2013-01-19 20:03:57 +08:00
public void Add(IBlocksDeobfuscator blocksDeobfuscator) {
if (blocksDeobfuscator != null)
userBlocksDeobfuscators.Add(blocksDeobfuscator);
}
2013-01-19 20:03:57 +08:00
public void Initialize(Blocks blocks) {
2011-10-17 06:22:22 +08:00
this.blocks = blocks;
}
2013-01-19 20:03:57 +08:00
public void Deobfuscate() {
2013-04-30 18:15:07 +08:00
bool modified;
2011-10-22 02:14:25 +08:00
int iterations = -1;
2013-01-19 20:03:57 +08:00
DeobfuscateBegin(userBlocksDeobfuscators);
DeobfuscateBegin(ourBlocksDeobfuscators);
2011-10-17 06:22:22 +08:00
do {
2011-10-22 02:14:25 +08:00
iterations++;
2013-04-30 18:15:07 +08:00
modified = false;
2013-01-19 20:03:57 +08:00
RemoveDeadBlocks();
MergeBlocks();
2011-10-19 05:31:50 +08:00
2013-01-19 20:03:57 +08:00
blocks.MethodBlocks.GetAllBlocks(allBlocks);
2011-10-19 05:31:50 +08:00
2011-10-22 02:14:25 +08:00
if (iterations == 0)
2013-04-30 18:15:07 +08:00
modified |= FixDotfuscatorLoop();
2011-10-22 02:14:25 +08:00
2013-04-30 18:15:07 +08:00
modified |= Deobfuscate(userBlocksDeobfuscators, allBlocks);
modified |= Deobfuscate(ourBlocksDeobfuscators, allBlocks);
modified |= DeobfuscateIfNotModified(modified, userBlocksDeobfuscators, allBlocks);
modified |= DeobfuscateIfNotModified(modified, ourBlocksDeobfuscators, allBlocks);
} while (modified);
}
2011-10-19 05:31:50 +08:00
2013-01-19 20:03:57 +08:00
void DeobfuscateBegin(IEnumerable<IBlocksDeobfuscator> bds) {
foreach (var bd in bds)
2013-01-19 20:03:57 +08:00
bd.DeobfuscateBegin(blocks);
}
2012-01-09 08:25:25 +08:00
2013-01-19 20:03:57 +08:00
bool Deobfuscate(IEnumerable<IBlocksDeobfuscator> bds, List<Block> allBlocks) {
2013-04-30 18:15:07 +08:00
bool modified = false;
foreach (var bd in bds) {
2013-04-30 18:15:07 +08:00
if (bd.ExecuteIfNotModified)
continue;
2013-04-30 18:15:07 +08:00
modified |= bd.Deobfuscate(allBlocks);
}
2013-04-30 18:15:07 +08:00
return modified;
}
2013-04-30 18:15:07 +08:00
bool DeobfuscateIfNotModified(bool modified, IEnumerable<IBlocksDeobfuscator> bds, List<Block> allBlocks) {
foreach (var bd in bds) {
2013-04-30 18:15:07 +08:00
if (modified)
break;
2013-04-30 18:15:07 +08:00
if (!bd.ExecuteIfNotModified)
continue;
2013-04-30 18:15:07 +08:00
modified |= bd.Deobfuscate(allBlocks);
}
2013-04-30 18:15:07 +08:00
return modified;
2011-10-17 06:22:22 +08:00
}
2011-10-22 02:14:25 +08:00
// Hack for old Dotfuscator
2013-01-19 20:03:57 +08:00
bool FixDotfuscatorLoop() {
2011-10-22 02:14:25 +08:00
/*
blk1:
...
ldc.i4.x
blk2:
dup
dup
ldc.i4.y
some_op
bcc blk2
blk3:
pop
...
*/
2013-04-30 18:15:07 +08:00
bool modified = false;
2011-10-22 02:14:25 +08:00
foreach (var block in allBlocks) {
if (block.Instructions.Count != 5)
continue;
var instructions = block.Instructions;
if (instructions[0].OpCode.Code != Code.Dup)
continue;
if (instructions[1].OpCode.Code != Code.Dup)
continue;
2013-01-19 20:03:57 +08:00
if (!instructions[2].IsLdcI4())
2011-10-22 02:14:25 +08:00
continue;
if (instructions[3].OpCode.Code != Code.Sub && instructions[3].OpCode.Code != Code.Add)
continue;
if (instructions[4].OpCode.Code != Code.Blt && instructions[4].OpCode.Code != Code.Blt_S &&
instructions[4].OpCode.Code != Code.Bgt && instructions[4].OpCode.Code != Code.Bgt_S)
continue;
if (block.Sources.Count != 2)
continue;
var prev = block.Sources[0];
if (prev == block)
prev = block.Sources[1];
2013-01-19 20:03:57 +08:00
if (prev == null || !prev.LastInstr.IsLdcI4())
2011-10-22 02:14:25 +08:00
continue;
var next = block.FallThrough;
if (next.FirstInstr.OpCode.Code != Code.Pop)
continue;
2013-01-19 20:03:57 +08:00
block.ReplaceLastInstrsWithBranch(5, next);
2013-04-30 18:15:07 +08:00
modified = true;
2011-10-22 02:14:25 +08:00
}
2013-04-30 18:15:07 +08:00
return modified;
2011-10-22 02:14:25 +08:00
}
2013-01-19 20:03:57 +08:00
bool RemoveDeadBlocks() {
return new DeadBlocksRemover(blocks.MethodBlocks).Remove() > 0;
2011-10-17 06:22:22 +08:00
}
2013-01-19 20:03:57 +08:00
bool MergeBlocks() {
2013-04-30 18:15:07 +08:00
bool modified = false;
2013-01-19 20:03:57 +08:00
foreach (var scopeBlock in GetAllScopeBlocks(blocks.MethodBlocks))
2013-04-30 18:15:07 +08:00
modified |= scopeBlock.MergeBlocks() > 0;
return modified;
2011-10-17 06:22:22 +08:00
}
2013-01-19 20:03:57 +08:00
IEnumerable<ScopeBlock> GetAllScopeBlocks(ScopeBlock scopeBlock) {
2011-10-17 06:22:22 +08:00
var list = new List<ScopeBlock>();
list.Add(scopeBlock);
2013-01-19 20:03:57 +08:00
list.AddRange(scopeBlock.GetAllScopeBlocks());
2011-10-17 06:22:22 +08:00
return list;
}
}
}