-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathanalyze_func.php
More file actions
279 lines (256 loc) · 10.8 KB
/
analyze_func.php
File metadata and controls
279 lines (256 loc) · 10.8 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
require 'global.php';
ini_set('display_errors', 0); // 禁用错误输出
error_reporting(E_ERROR | E_PARSE); // 仅显示致命错误和解析错误
function scanDirectory($dir)
{
$phpFiles = [];
$frameworks = [];
try {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
if ($file->isFile() && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$phpFiles[] = $file->getRealPath();
}
$filename = strtolower($file->getFilename());
if (strpos($filename, 'psrserverrequestresolver.php') !== false && !in_array('Symfony', $frameworks)) {
$frameworks[] = 'Symfony';
}
if (strpos($filename, 'think') !== false && !in_array('ThinkPHP', $frameworks)) {
$frameworks[] = 'ThinkPHP';
}
if (strpos($filename, 'welcome.blade.php') !== false && !in_array('Laravel', $frameworks)) {
$frameworks[] = 'Laravel';
}
if (strpos($filename, 'yii') !== false && !in_array('Yii', $frameworks)) {
$frameworks[] = 'Yii';
}
if (strpos($filename, 'dev_error_stacktrace.php') !== false && !in_array('CakePHP', $frameworks)) {
$frameworks[] = 'CakePHP';
}
}
} catch (Exception $e) {
error_log("目录扫描错误: " . $e->getMessage());
}
return ['phpFiles' => $phpFiles, 'frameworks' => $frameworks];
}
function handleDirectoryScan($dir)
{
$scanResult = scanDirectory($dir);
$phpFiles = $scanResult['phpFiles'];
$frameworks = $scanResult['frameworks'];
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
$traverser = new PhpParser\NodeTraverser();
$NodeInitVisitor = new NodeInitVisitor();
$traverser->addVisitor($NodeInitVisitor);
$AstToIrConverter = new AstToIrConverter();
foreach ($phpFiles as $filePath) {
try {
$code = file_get_contents($filePath);
$code = preg_replace('/\belse\s+if\b/', 'elseif', $code);
$parseResult = $parser->parse($code);
$NodeInitVisitor = new NodeInitVisitor();
$traverser->removeVisitor($NodeInitVisitor);
$traverser->addVisitor($NodeInitVisitor);
$traverser->traverse($parseResult);
$nodes = $NodeInitVisitor->getNodes();
$AstToIrConverter->build_funcTable($nodes, $filePath);
} catch (Throwable $e) {
echo "[Error] Failed to parse file: $filePath\n";
echo "Reason: " . $e->getMessage() . "\n\n";
}
}
$func_table = $AstToIrConverter->funcs;
foreach ($func_table as $key => $obj) {
if ($obj->flag == 'null') {
if ($func_table[md5($obj->func_name)]->func_stmt != null) {
try {
$AstToIrConverter = new AstToIrConverter();
$NodeInitVisitor = new NodeInitVisitor();
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor($NodeInitVisitor);
$traverser->traverse($func_table[md5($obj->func_name)]->func_stmt);
$nodes = $NodeInitVisitor->getNodes();
$quads = $AstToIrConverter->FuncParse($nodes, $func_table[md5($obj->func_name)]->func_param);
$flow_graph = new ControlFlowGraph();
$flow_graph->FuncProcess($quads, $func_table, $obj->func_name);
} catch (Exception $e) {
error_log("DFS 错误: {$obj->func_name}: {$e->getMessage()}");
}
} else {
$obj->flag == 'false;';
}
}
}
$results = [];
$symbolFunctions = [];
foreach ($func_table as $key => $obj) {
if ($obj->flag == 'true') {
foreach ($obj->sink as $sink) {
$symbolFunctions[$obj->func_name] = "'" . $obj->func_name . "'" . ' => ' . "'" . $sink->type . "'" . ',';
$results[] = [
'funcName' => $obj->func_name,
'type' => $sink->type,
'name' => $sink->name,
'linenum' => $sink->sink_num,
'path' => $obj->file_path,
'code' => getHighlightedCode($obj->file_path, $sink)
];
}
}
}
return [
'results' => formatResults($results),
'frameworks' => count($frameworks) > 0 ? "检测到框架: " . implode(', ', $frameworks) : "未检测到框架。",
'symbolFunctions' => array_values($symbolFunctions)
];
}
function getHighlightedCode($path, $sink)
{
$code = file($path, FILE_IGNORE_NEW_LINES);
$lines = [];
foreach ($code as $i => $line) {
$linenum = $i + 1;
$escaped = htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
if ($linenum == $sink->sink_num) {
$lines[] = "<span style='color: red; font-weight: bold;'>{$linenum} {$escaped}</span>";
} elseif (in_array($linenum, $sink->linenum)) {
$lines[] = "<span style='color: orange; font-weight: bold;'>{$linenum} {$escaped}</span>";
} else {
$lines[] = "{$linenum} {$escaped}";
}
}
return implode("\n", $lines);
}
function formatResults($info)
{
$detailsHtml = '<table class="table"><thead><tr>
<th>#</th><th>SinkFunc</th><th>Type</th><th>Function</th><th>Line</th><th>Path</th><th>Detail</th>
</tr></thead><tbody>';
foreach ($info as $index => $item) {
$modalId = "modal-{$index}";
$detailsHtml .= "<tr>
<td>" . ($index + 1) . "</td>
<td>{$item['funcName']}</td>
<td>{$item['type']}</td>
<td>{$item['name']}</td>
<td>{$item['linenum']}</td>
<td>{$item['path']}</td>
<td>
<button class='btn btn-primary' data-toggle='modal' data-target='#{$modalId}'>Details</button>
<div class='modal fade' id='{$modalId}' tabindex='-1' role='dialog'>
<div class='modal-dialog modal-lg' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title'>Code Details</h5>
<button type='button' class='close' data-dismiss='modal'>×</button>
</div>
<div class='modal-body'>
<pre><code>{$item['code']}</code></pre>
</div>
</div>
</div>
</div>
</td>
</tr>";
}
$detailsHtml .= '</tbody></table>';
return $detailsHtml;
}
function handleSingleFile($filePath)
{
$frameworks = [];
$phpFiles = [$filePath];
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
$traverser = new PhpParser\NodeTraverser();
$NodeInitVisitor = new NodeInitVisitor();
$traverser->addVisitor($NodeInitVisitor);
$AstToIrConverter = new AstToIrConverter();
foreach ($phpFiles as $filePath) {
$code = file_get_contents($filePath);
$code = preg_replace('/\belse\s+if\b/', 'elseif', $code);
$parseResult = $parser->parse($code);
$NodeInitVisitor = new NodeInitVisitor();
$traverser->removeVisitor($NodeInitVisitor);
$traverser->addVisitor($NodeInitVisitor);
$traverser->traverse($parseResult);
$nodes = $NodeInitVisitor->getNodes();
$AstToIrConverter->build_funcTable($nodes, $filePath);
}
$func_table = $AstToIrConverter->funcs;
foreach ($func_table as $key => $obj) {
if ($obj->flag == 'null') {
if ($func_table[md5($obj->func_name)]->func_stmt != null) {
try {
$AstToIrConverter = new AstToIrConverter();
$NodeInitVisitor = new NodeInitVisitor();
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor($NodeInitVisitor);
$traverser->traverse($func_table[md5($obj->func_name)]->func_stmt);
$nodes = $NodeInitVisitor->getNodes();
$quads = $AstToIrConverter->FuncParse($nodes, $func_table[md5($obj->func_name)]->func_param);
$flow_graph = new ControlFlowGraph();
$flow_graph->FuncProcess($quads, $func_table, $obj->func_name);
} catch (Exception $e) {
error_log("DFS 错误: {$obj->func_name}: {$e->getMessage()}");
}
} else {
$obj->flag == 'false;';
}
}
}
$results = [];
$symbolFunctions = [];
foreach ($func_table as $key => $obj) {
if ($obj->flag == 'true') {
foreach ($obj->sink as $sink) {
$symbolFunctions[$obj->func_name] = "'" . $obj->func_name . "'" . ' => ' . "'" . $sink->type . "'" . ',';
$results[] = [
'funcName' => $obj->func_name,
'type' => $sink->type,
'name' => $sink->name,
'linenum' => $sink->sink_num,
'path' => $obj->file_path,
'code' => getHighlightedCode($obj->file_path, $sink)
];
}
}
}
return [
'results' => formatResults($results),
'frameworks' => count($frameworks) > 0 ? "检测到框架: " . implode(', ', $frameworks) : "未检测到框架。",
'symbolFunctions' => array_values($symbolFunctions)
];
}
$inputPath = $_POST['path'] ?? null;
if ($inputPath) {
if (is_dir($inputPath)) {
$results = handleDirectoryScan($inputPath);
echo json_encode([
'status' => 'success',
'frameworks' => $results['frameworks'],
'results' => [['status' => 'success', 'details' => $results['results']]],
'symbolFunctions' => $results['symbolFunctions']
]);
} elseif (is_file($inputPath) && pathinfo($inputPath, PATHINFO_EXTENSION) === 'php') {
$results = handleSingleFile($inputPath);
echo json_encode([
'status' => 'success',
'frameworks' => $results['frameworks'],
'results' => [['status' => 'success', 'details' => $results['results']]],
'symbolFunctions' => $results['symbolFunctions']
]);
} else {
echo json_encode([
'status' => 'error',
'type' => 'InvalidPath',
'message' => '无效路径,请输入有效的PHP文件或目录路径。',
]);
}
} else {
echo json_encode([
'status' => 'error',
'type' => 'InvalidPath',
'message' => '请输入文件或目录路径。',
]);
}