- Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathFileSummaryGenerator.php
More file actions
Latest commit
253 lines (225 loc) · 8.71 KB
/
Copy pathFileSummaryGenerator.php
File metadata and controls
253 lines (225 loc) · 8.71 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
<?php
class FileSummaryGenerator {
/**
* 获取当前文件include的所有文件的summary
* @param FileSummary $fileSummary
* @return array(fileSummarys)
*/
publicstaticfunctiongetIncludeFilesDataFlows($fileSummary){
if (!is_object($fileSummary)){
return;
}
//1.得到include files
$includeFiles = $fileSummary->getIncludeMap();
$currentFilePath = $fileSummary->getPath();
//2.foreach() files
$retFileSummary = array();
foreach ($includeFilesas$rpath){
$absPath = FileUtils::getAbsPath($currentFilePath, $rpath);
if (!$absPath){
continue;
}
// 查看是否在fileSummaryContext中
// 得到DataFlows
$fileSummaryContext = FileSummaryContext::getInstance();
$ret = $fileSummaryContext->findSummaryByPath($absPath);
if (is_object($ret)){
//查看此文件是否有include文件
$pRetFiles = self::getIncludeFilesDataFlows($ret);
$retFileSummary = array_merge($pRetFiles, $retFileSummary);
$retFileSummary = array_merge(array($ret), $retFileSummary);
}else{
$includeFileSummary = self::getFileSummary($absPath);
if (is_object($includeFileSummary)){
$pRetFiles = self::getIncludeFilesDataFlows($includeFileSummary);
$retFileSummary = array_merge($pRetFiles, $retFileSummary);
$retFileSummary = array_merge(array($includeFileSummary), $retFileSummary);
}
}
}
//return all files summary
//include在前的,在数组前面
return$retFileSummary;
}
/**
* 得到一个文件基本信息FileSummary,包括
* @param string $absPath
*/
publicstaticfunctiongetFileSummary($absPath){
if (!$absPath){
return ;
}
$visitor = newMyVisitor() ;
$parser = newPhpParser\Parser(newPhpParser\Lexer\Emulative) ;
$traverser = newPhpParser\NodeTraverser ;
$code = file_get_contents($absPath);
try {
$stmts = $parser->parse($code);
} catch (Exception$e) {
return ;
}
$traverser->addVisitor($visitor) ;
$traverser->traverse($stmts) ;
$nodes = $visitor->getNodes() ;
$fileSummary = newFileSummary();
$fileSummary->setPath($absPath);
$currBlock = newBasicBlock() ;
foreach ($nodesas$node){
if(!is_object($node)) continue ;
//不分析函数定义
if($node->getType() == "Stmt_Function"){
continue ;
}
$currBlock->addNode($node);
}
$fileSummaryGenerator = newFileSummaryGenerator();
$fileSummaryGenerator->simulate($currBlock, $fileSummary);
return$fileSummary;
}
/**
* 得到该文件的dataFlows
* @param Nodes $nodes
*/
publicfunctionsimulate($block, $fileSummary){
$nodes = $block->getContainedNodes();
//循环nodes集合,搜集信息加入到中
foreach ($nodesas$node){
//搜集节点中的require include require_once include_once的PHP文件名称
$fileSummary->addIncludeToMap(NodeUtils::getNodeIncludeInfo($node)) ;
switch ($node->getType()){
//处理赋值语句
case'Expr_Assign':
$dataFlow = newDataFlow() ;
$this->assignHandler($node, $dataFlow, "left", $block, $fileSummary) ;
$this->assignHandler($node, $dataFlow, "right", $block, $fileSummary) ;
//处理完一条赋值语句,加入DataFlowMap
$fileSummary->addDataFlow($dataFlow);
$block->getBlockSummary()->addDataFlowItem($dataFlow);
break ;
//处理字符串连接赋值
//$sql .= "from users where"生成sql => "from users where"
case'Expr_AssignOp_Concat':
$dataFlow = newDataFlow() ;
$this->assignConcatHandler($node, $dataFlow, "left", $block, $fileSummary) ;
$this->assignConcatHandler($node, $dataFlow, "right", $block, $fileSummary) ;
//处理完一条赋值语句,加入DataFlowMap
$fileSummary->addDataFlow($dataFlow);
$block->getBlockSummary()->addDataFlowItem($dataFlow);
break ;
default:
break;
}
}
}
/**
* 处理赋值的assign语句,添加至dataFlows中
* @param AST $node
* @param DataFlow $dataFlow
* @param string $type
*/
publicfunctionassignHandler($node, $dataFlow, $type, $block, $fileSummary){
$part = null ;
if($type == "left"){
$part = $node->var ;
}elseif($type == "right"){
$part = $node->expr ;
}else{
return ;
}
//处理$GLOBALS的赋值
//$GLOBAL['name'] = "chongrui" ; 数据流信息为 $name = "chongrui" ;
if ($part && SymbolUtils::isArrayDimFetch($part) && (substr(NodeUtils::getNodeStringName($part),0,7)=="GLOBALS")){
//加入dataFlow
$arr = newArrayDimFetchSymbol() ;
$arr->setValue($part) ;
if($type == "left"){
$dataFlow->setLocation($arr) ;
$dataFlow->setName(NodeUtils::getNodeGLOBALSNodeName($part)) ;
}elseif($type == "right"){
$dataFlow->setValue($arr) ;
}
return ;
}
//处理赋值语句,存放在DataFlow
//处理赋值语句的左边
if($part && SymbolUtils::isValue($part)){
//在DataFlow加入Location以及name
$vs = newValueSymbol() ;
$vs->setValueByNode($part) ;
if($type == "left"){
$dataFlow->setLocation($vs) ;
$dataFlow->setName($part->name) ;
}elseif($type == "right"){
$dataFlow->setValue($vs) ;
}
}elseif ($part && SymbolUtils::isVariable($part)){
//加入dataFlow
$vars = newVariableSymbol() ;
$vars->setValue($part);
if($type == "left"){
$dataFlow->setLocation($vars) ;
$dataFlow->setName($part->name) ;
}elseif($type == "right"){
$dataFlow->setValue($part) ;
}
}elseif ($part && SymbolUtils::isConstant($part)){
//加入dataFlow
$con = newConstantSymbol() ;
$con->setValueByNode($part) ;
$con->setName($part->name->parts[0]) ;
if($type == "left"){
$dataFlow->setLocation($con) ;
$dataFlow->setName($part->name) ;
}elseif($type == "right"){
$dataFlow->setValue($con) ;
}
}elseif ($part && SymbolUtils::isArrayDimFetch($part)){
//加入dataFlow
$arr = newArrayDimFetchSymbol() ;
$arr->setValue($part) ;
if($type == "left"){
$dataFlow->setLocation($arr) ;
$dataFlow->setName(NodeUtils::getNodeStringName($part)) ;
}elseif($type == "right"){
$dataFlow->setValue($arr) ;
}
}elseif ($part && SymbolUtils::isConcat($part)){
$concat = newConcatSymbol() ;
$concat->setItemByNode($part) ;
if($type == "left"){
$dataFlow->setLocation($concat) ;
$dataFlow->setName($part->name) ;
}elseif($type == "right"){
$dataFlow->setValue($concat) ;
}
}else{
//不属于已有的任何一个symbol类型,如函数调用
if($part && ($part->getType() == "Expr_FuncCall" ||
$part->getType() == "Expr_MethodCall" ||
$part->getType() == "Expr_StaticCall")){
if($type == "left"){
$dataFlow->setLocation($arr) ;
$dataFlow->setName(NodeUtils::getNodeStringName($part)) ;
}elseif($type == "right"){
//处理净化信息和编码信息
SanitizationHandler::setSanitiInfo($part, $dataFlow, $block, $fileSummary) ;
EncodingHandler::setEncodeInfo($part, $dataFlow, $block, $fileSummary) ;
}
}
//处理三元表达式
if($part && $part->getType() == "Expr_Ternary"){
BIFuncUtils::ternaryHandler($type, $part, $dataFlow) ;
}
}
}
/**
* 处理赋值的concat语句,添加至dataFlows中
* @param AST $node
* @param DataFlow $dataFlow
* @param string $type
*/
privatefunctionassignConcatHandler($node, $dataFlow, $type, $block, $fileSummary){
$this->assignHandler($node, $dataFlow, $type, $block, $fileSummary) ;
}
}
?>