forked from ganlvtech/php-mfenc-decompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
111 lines (100 loc) · 4.38 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
use Ganlv\MfencDecompiler\Decompiler;
use Ganlv\MfencDecompiler\DfsDisassembler;
use Ganlv\MfencDecompiler\DirectedGraphStructureSimplifier;
use Ganlv\MfencDecompiler\Disassembler1;
use Ganlv\MfencDecompiler\Disassembler2;
use Ganlv\MfencDecompiler\GraphViewer;
use Ganlv\MfencDecompiler\Helper;
use Ganlv\MfencDecompiler\VmDecompiler;
require 'vendor/autoload.php';
ini_set('xdebug.max_nesting_level', 1000000);
$functionIndex = 10;
if (isset($_GET['flowchart'])) {
$functionIndex = $_GET['flowchart'];
$graph = getGraph($functionIndex);
$simplifier = new DirectedGraphStructureSimplifier($graph);
$graph = $simplifier->simplify();
header('Content-Type: text/plain; charset=UTF-8');
echo Helper::graphToFlowchart($graph);
return;
}
if (isset($_GET['id'])) {
$functionIndex = $_GET['id'];
}
$dissectInstructions = getStructuredInstructions($functionIndex);
$decompiler = new Decompiler($dissectInstructions);
try {
$decompiler->decompile();
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL, PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL, PHP_EOL;
}
$ast = $decompiler->ast;
echo Helper::prettyPrintFile($ast);
$nodeVisitor = new \Ganlv\MfencDecompiler\NodeVisitors\GetAllEipsNodeVisitor();
Helper::traverseAst($nodeVisitor, $ast);
$eips = $nodeVisitor->eips;
file_put_contents("runtime/$functionIndex.structure.summary.txt", Helper::printStructuredInstructionsIsUsed(getStructuredInstructions($functionIndex), $eips, true));
$stack = $decompiler->stack;
$ast = \Ganlv\MfencDecompiler\Beautifier::beautify($ast);
file_put_contents("runtime/$functionIndex.decompiled.1.php", Helper::prettyPrintFile($ast));
function getAst()
{
if (!file_exists("runtime/ast.serialize.txt")) {
$original = file_get_contents('tests/keke_xzhseo.class.php');
$ast = Helper::parseCode($original);
file_put_contents("runtime/ast.serialize.txt", serialize($ast));
} else {
$ast = unserialize(file_get_contents("runtime/ast.serialize.txt"));
}
return $ast;
}
function getInstructions($functionIndex)
{
if (!file_exists("runtime/$functionIndex.instructions.serialize.txt")) {
$ast = getAst();
$ast = $ast[11]->stmts[$functionIndex]->stmts;
$vmStart = VmDecompiler::findVmStart($ast);
$vmVariables = VmDecompiler::findVmVariables($ast, $vmStart);
$vmMemoryData = VmDecompiler::findVmMemoryData($ast, $vmStart);
$disassembler = new Disassembler1($vmMemoryData['memory_data']);
$disassembler2 = new Disassembler2($vmVariables);
$dfsDisassembler = new DfsDisassembler($disassembler, $disassembler2);
$dfsDisassembler->disassemble();
$instructions = $dfsDisassembler->getInstructions();
file_put_contents("runtime/$functionIndex.instructions.serialize.txt", serialize($instructions));
file_put_contents("runtime/$functionIndex.instructions.txt", Helper::printInstructions($instructions, true, true));
} else {
$instructions = unserialize(file_get_contents("runtime/$functionIndex.instructions.serialize.txt"));
}
return $instructions;
}
function getGraph($functionIndex)
{
if (!file_exists("runtime/$functionIndex.graph.serialize.txt")) {
$instructions = getInstructions($functionIndex);
$graph = GraphViewer::toDirectedGraph($instructions);
$graph->simplify();
file_put_contents("runtime/$functionIndex.graph.serialize.txt", serialize($graph));
file_put_contents("runtime/$functionIndex.graph.txt", Helper::printDirectedGraph($graph, true));
} else {
$graph = unserialize(file_get_contents("runtime/$functionIndex.graph.serialize.txt"));
}
return $graph;
}
function getStructuredInstructions($functionIndex)
{
if (!file_exists("runtime/$functionIndex.structure.serialize.txt")) {
$graph = getGraph($functionIndex);
$simplifier = new DirectedGraphStructureSimplifier($graph);
$graph = $simplifier->simplify();
assert(count($graph->getVerticesId()) === 1);
$instructions = $graph->getVertex(0);
file_put_contents("runtime/$functionIndex.structure.serialize.txt", serialize($instructions));
file_put_contents("runtime/$functionIndex.structure.txt", Helper::printStructuredInstructions($instructions, true));
} else {
$instructions = unserialize(file_get_contents("runtime/$functionIndex.structure.serialize.txt"));
}
return $instructions;
}