Skip to content

Commit ca85475

Browse files
committed
add sanitization in fileSummary
1 parent fcddc76 commit ca85475

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

CFGGenerator.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ private function assignHandler($node,$block,$dataFlow,$type){
165165
}else{
166166
return ;
167167
}
168-
169168

170169
//处理$GLOBALS的赋值
171170
//$GLOBAL['name'] = "chongrui" ; 数据流信息为 $name = "chongrui" ;
@@ -1043,9 +1042,20 @@ public function sinkTracebackBlock($argName,$block,$flowsNum){
10431042
}
10441043
}
10451044

1045+
10461046
echo "<pre>" ;
10471047
//从用户那接受项目路径
10481048
$project_path = 'F:/wamp/www/phpvulhunter/test';
1049+
//初始化
1050+
$initModule = new InitModule() ;
1051+
$initModule->init($project_path) ;
1052+
1053+
//$path = 'F:/wamp/www/phpvulhunter/test/simple_demo.php';
1054+
//$absPath = $path;
1055+
//$ret = FileSummaryGenerator::getFileSummary($absPath);
1056+
//print_r($ret);
1057+
//FileSummaryGenerator::getIncludeFilesDataFlows($ret);
1058+
10491059
$cfg = new CFGGenerator() ;
10501060
$visitor = new MyVisitor() ;
10511061
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative) ;
@@ -1067,7 +1077,7 @@ public function sinkTracebackBlock($argName,$block,$flowsNum){
10671077
$sanitiFuncContext = UserSanitizeFuncConetxt::getInstance();
10681078
// print_r($sanitiFuncContext);
10691079
$sinkContext = UserDefinedSinkContext::getInstance();
1070-
//print_r($sinkContext);
1080+
// print_r($sinkContext);
10711081
// $context = Context::getInstance() ;
10721082
// $funcName = "goods:buy";
10731083
// $funcBody = $context->getClassMethodBody($funcName,$path,$fileSummary->getIncludeMap());

FileSummaryGenerator.php

+44-15
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public static function getIncludeFilesDataFlows($fileSummary){
1515
$fileSummaryContext = FileSummaryContext::getInstance();
1616
$ret = $fileSummaryContext->findSummaryByPath($absPath);
1717
if ($ret){
18+
//查看此文件是否有include文件
19+
$pRetFlows = self::getIncludeFilesDataFlows($ret);
20+
$retFlows = array_merge($pRetFlows, $retFlows);
21+
1822
$dataFlows = $ret->getFlowsMap();
1923
$retFlows = array_merge($dataFlows, $retFlows);
2024
}else{
21-
$fileSummary = self::getFileSummary($absPath);
22-
if ($fileSummary)
23-
$retFlows = array_merge($fileSummary->getFlowsMap(), $retFlows);
25+
$includeFileSummary = self::getFileSummary($absPath);
26+
if ($includeFileSummary)
27+
$retFlows = array_merge($includeFileSummary->getFlowsMap(), $retFlows);
2428
}
2529
}
2630
//return all files dataFlows
@@ -45,17 +49,33 @@ public static function getFileSummary($absPath){
4549

4650
$fileSummary = new FileSummary();
4751
$fileSummary->setPath($absPath);
52+
53+
$currBlock = new BasicBlock() ;
54+
foreach ($nodes as $node){
55+
//搜集节点中的require include require_once include_once的PHP文件名称
56+
$fileSummary->addIncludeToMap(NodeUtils::getNodeIncludeInfo($node)) ;
57+
58+
if(!is_object($node)) continue ;
59+
60+
//不分析函数定义
61+
if($node->getType() == "Stmt_Function"){
62+
continue ;
63+
}
64+
$currBlock->addNode($node);
65+
}
66+
67+
4868
$fileSummaryGenerator = new FileSummaryGenerator();
49-
$fileSummaryGenerator->simulate($nodes, $fileSummary);
69+
$fileSummaryGenerator->simulate($currBlock, $fileSummary);
5070
return $fileSummary;
5171
}
5272

5373
/**
5474
* 得到该文件的dataFlows
5575
* @param Nodes $nodes
5676
*/
57-
public function simulate($nodes, $fileSummary){
58-
77+
public function simulate($block, $fileSummary){
78+
$nodes = $block->getContainedNodes();
5979
//循环nodes集合,搜集信息加入到中
6080
foreach ($nodes as $node){
6181
//搜集节点中的require include require_once include_once的PHP文件名称
@@ -65,20 +85,22 @@ public function simulate($nodes, $fileSummary){
6585
//处理赋值语句
6686
case 'Expr_Assign':
6787
$dataFlow = new DataFlow() ;
68-
$this->assignHandler($node, $dataFlow, "left") ;
69-
$this->assignHandler($node, $dataFlow, "right") ;
88+
$this->assignHandler($node, $dataFlow, "left", $block, $fileSummary) ;
89+
$this->assignHandler($node, $dataFlow, "right", $block, $fileSummary) ;
7090
//处理完一条赋值语句,加入DataFlowMap
7191
$fileSummary->addDataFlow($dataFlow);
92+
$block->getBlockSummary()->addDataFlowItem($dataFlow);
7293
break ;
7394

7495
//处理字符串连接赋值
7596
//$sql .= "from users where"生成sql => "from users where"
7697
case 'Expr_AssignOp_Concat':
7798
$dataFlow = new DataFlow() ;
78-
$this->assignConcatHandler($node, $dataFlow, "left") ;
79-
$this->assignConcatHandler($node, $dataFlow, "right") ;
99+
$this->assignConcatHandler($node, $dataFlow, "left", $block, $fileSummary) ;
100+
$this->assignConcatHandler($node, $dataFlow, "right", $block, $fileSummary) ;
80101
//处理完一条赋值语句,加入DataFlowMap
81102
$fileSummary->addDataFlow($dataFlow);
103+
$block->getBlockSummary()->addDataFlowItem($dataFlow);
82104
break ;
83105
default:
84106
break;
@@ -92,7 +114,7 @@ public function simulate($nodes, $fileSummary){
92114
* @param DataFlow $dataFlow
93115
* @param string $type
94116
*/
95-
public function assignHandler($node, $dataFlow, $type){
117+
public function assignHandler($node, $dataFlow, $type, $block, $fileSummary){
96118
$part = null ;
97119
if($type == "left"){
98120
$part = $node->var ;
@@ -174,15 +196,22 @@ public function assignHandler($node, $dataFlow, $type){
174196
}else if($type == "right"){
175197
$dataFlow->setValue($concat) ;
176198
}
177-
}else{
199+
}elseif($part && $part->getType() == "Expr_Ternary"){
200+
//处理三元表达式
201+
$ter_symbol = new MutipleSymbol() ;
202+
$ter_symbol->setItemByNode($part) ;
203+
if($type == 'right'){
204+
$dataFlow->setValue($ter_symbol) ;
205+
}
206+
}else{
178207
//不属于已有的任何一个symbol类型,如函数调用
179208
if($part && $part->getType() == "Expr_FuncCall"){
180209
if($type == "left"){
181210
$dataFlow->setLocation($arr) ;
182211
$dataFlow->setName(NodeUtils::getNodeStringName($part)) ;
183212
}else if($type == "right"){
184213
//处理净化信息和编码信息
185-
//SanitizationHandler::setSanitiInfo($part, $dataFlow, $this->fileSummary) ;
214+
SanitizationHandler::setSanitiInfo($part, $dataFlow, $block, $fileSummary) ;
186215
//EncodingHandler::setEncodeInfo($part, $dataFlow) ;
187216
}
188217
}
@@ -196,8 +225,8 @@ public function assignHandler($node, $dataFlow, $type){
196225
* @param DataFlow $dataFlow
197226
* @param string $type
198227
*/
199-
private function assignConcatHandler($node, $dataFlow, $type){
200-
$this->assignHandler($node, $dataFlow, $type) ;
228+
private function assignConcatHandler($node, $dataFlow, $type, $block, $fileSummary){
229+
$this->assignHandler($node, $dataFlow, $type, $block, $fileSummary) ;
201230
}
202231

203232
}

symbols/SanitizationHandler.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SanitizationHandler {
1515
* @param Node $node
1616
* @param 数据流 $dataFlow
1717
*/
18-
public static function setSanitiInfo($node,$dataFlow,$block, $fileSummary ){
18+
public static function setSanitiInfo($node, $dataFlow, $block, $fileSummary ){
1919
$dataFlows = $block->getBlockSummary()->getDataFlowMap();
2020
$sanitiInfo = self::SantiniFuncHandler($node, $fileSummary);
2121
//print_r($sanitiInfo);

0 commit comments

Comments
 (0)