Add CflowDeobfuscator
This commit is contained in:
parent
bc5d829714
commit
c65b76583c
|
@ -38,9 +38,11 @@
|
||||||
<Compile Include="Blocks.cs" />
|
<Compile Include="Blocks.cs" />
|
||||||
<Compile Include="BlocksSorter.cs" />
|
<Compile Include="BlocksSorter.cs" />
|
||||||
<Compile Include="cflow\BlockCflowDeobfuscator.cs" />
|
<Compile Include="cflow\BlockCflowDeobfuscator.cs" />
|
||||||
|
<Compile Include="cflow\CflowDeobfuscator.cs" />
|
||||||
<Compile Include="cflow\CflowUtils.cs" />
|
<Compile Include="cflow\CflowUtils.cs" />
|
||||||
<Compile Include="cflow\DeadCodeRemover.cs" />
|
<Compile Include="cflow\DeadCodeRemover.cs" />
|
||||||
<Compile Include="cflow\DeadStoreRemover.cs" />
|
<Compile Include="cflow\DeadStoreRemover.cs" />
|
||||||
|
<Compile Include="cflow\ICflowDeobfuscator.cs" />
|
||||||
<Compile Include="cflow\InstructionEmulator.cs" />
|
<Compile Include="cflow\InstructionEmulator.cs" />
|
||||||
<Compile Include="cflow\Int32Value.cs" />
|
<Compile Include="cflow\Int32Value.cs" />
|
||||||
<Compile Include="cflow\Int64Value.cs" />
|
<Compile Include="cflow\Int64Value.cs" />
|
||||||
|
|
|
@ -33,6 +33,7 @@ namespace de4dot.blocks.cflow {
|
||||||
MethodCallInliner methodCallInliner = new MethodCallInliner();
|
MethodCallInliner methodCallInliner = new MethodCallInliner();
|
||||||
|
|
||||||
public bool InlineMethods { get; set; }
|
public bool InlineMethods { get; set; }
|
||||||
|
public bool InlineInstanceMethods { get; set; }
|
||||||
|
|
||||||
public void init(Blocks blocks) {
|
public void init(Blocks blocks) {
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
|
@ -54,7 +55,7 @@ namespace de4dot.blocks.cflow {
|
||||||
|
|
||||||
if (InlineMethods) {
|
if (InlineMethods) {
|
||||||
foreach (var block in allBlocks) {
|
foreach (var block in allBlocks) {
|
||||||
methodCallInliner.init(blocks, block);
|
methodCallInliner.init(blocks, block, InlineInstanceMethods);
|
||||||
changed |= methodCallInliner.deobfuscate();
|
changed |= methodCallInliner.deobfuscate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
62
blocks/cflow/CflowDeobfuscator.cs
Normal file
62
blocks/cflow/CflowDeobfuscator.cs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
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;
|
||||||
|
using Mono.Cecil;
|
||||||
|
using Mono.Cecil.Cil;
|
||||||
|
|
||||||
|
namespace de4dot.blocks.cflow {
|
||||||
|
public class CflowDeobfuscator : ICflowDeobfuscator {
|
||||||
|
BlocksCflowDeobfuscator cflowDeobfuscator = new BlocksCflowDeobfuscator();
|
||||||
|
|
||||||
|
public bool InlineMethods { get; set; }
|
||||||
|
public bool InlineInstanceMethods { get; set; }
|
||||||
|
|
||||||
|
public CflowDeobfuscator() {
|
||||||
|
InlineMethods = true;
|
||||||
|
InlineInstanceMethods = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deobfuscateCflow(MethodDefinition method) {
|
||||||
|
deobfuscate(method, (blocks) => {
|
||||||
|
cflowDeobfuscator.InlineMethods = InlineMethods;
|
||||||
|
cflowDeobfuscator.InlineInstanceMethods = InlineInstanceMethods;
|
||||||
|
cflowDeobfuscator.init(blocks);
|
||||||
|
cflowDeobfuscator.deobfuscate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool hasNonEmptyBody(MethodDefinition method) {
|
||||||
|
return method.Body != null && method.Body.Instructions.Count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deobfuscate(MethodDefinition method, Action<Blocks> handler) {
|
||||||
|
if (hasNonEmptyBody(method)) {
|
||||||
|
var blocks = new Blocks(method);
|
||||||
|
|
||||||
|
handler(blocks);
|
||||||
|
|
||||||
|
IList<Instruction> allInstructions;
|
||||||
|
IList<ExceptionHandler> allExceptionHandlers;
|
||||||
|
blocks.getCode(out allInstructions, out allExceptionHandlers);
|
||||||
|
DotNetUtils.restoreBody(method, allInstructions, allExceptionHandlers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
blocks/cflow/ICflowDeobfuscator.cs
Normal file
26
blocks/cflow/ICflowDeobfuscator.cs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
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 Mono.Cecil;
|
||||||
|
|
||||||
|
namespace de4dot.blocks.cflow {
|
||||||
|
public interface ICflowDeobfuscator {
|
||||||
|
void deobfuscateCflow(MethodDefinition method);
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,11 +29,13 @@ namespace de4dot.blocks.cflow {
|
||||||
Blocks blocks;
|
Blocks blocks;
|
||||||
Block block;
|
Block block;
|
||||||
int iteration;
|
int iteration;
|
||||||
|
bool inlineInstanceMethods;
|
||||||
|
|
||||||
public void init(Blocks blocks, Block block) {
|
public void init(Blocks blocks, Block block, bool inlineInstanceMethods) {
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
this.block = block;
|
this.block = block;
|
||||||
this.iteration = 0;
|
this.iteration = 0;
|
||||||
|
this.inlineInstanceMethods = inlineInstanceMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool deobfuscate() {
|
public bool deobfuscate() {
|
||||||
|
@ -50,6 +52,14 @@ namespace de4dot.blocks.cflow {
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool canInline(MethodDefinition method) {
|
||||||
|
if (method.IsStatic)
|
||||||
|
return true;
|
||||||
|
if (method.IsVirtual)
|
||||||
|
return false;
|
||||||
|
return inlineInstanceMethods;
|
||||||
|
}
|
||||||
|
|
||||||
bool inlineMethod(Instruction callInstr, int instrIndex) {
|
bool inlineMethod(Instruction callInstr, int instrIndex) {
|
||||||
var method = callInstr.Operand as MethodDefinition;
|
var method = callInstr.Operand as MethodDefinition;
|
||||||
if (method == null)
|
if (method == null)
|
||||||
|
@ -57,7 +67,7 @@ namespace de4dot.blocks.cflow {
|
||||||
if (MemberReferenceHelper.compareMethodReferenceAndDeclaringType(method, blocks.Method))
|
if (MemberReferenceHelper.compareMethodReferenceAndDeclaringType(method, blocks.Method))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!method.IsStatic)
|
if (!canInline(method))
|
||||||
return false;
|
return false;
|
||||||
var body = method.Body;
|
var body = method.Body;
|
||||||
if (body == null)
|
if (body == null)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user