Skip to content

Commit 3da25ba

Browse files
committed
Merge remote-tracking branch 'origin/master'
Conflicts: CFGGenerator.php test/test.php utils/multiBlockHandlerUtils.class.php
2 parents c82b3fb + 82b78e4 commit 3da25ba

File tree

37 files changed

+134
-100
lines changed

37 files changed

+134
-100
lines changed

CFGGenerator.php

+38-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function getBranches($node){
8787
$catch_branch = new Branch($catch->type, $catch->stmts) ;
8888
array_push($branches, $catch_branch) ;
8989
}
90-
9190
break ;
9291

9392
case 'Expr_Ternary':
@@ -276,6 +275,12 @@ private function assignHandler($node,$block,$dataFlow,$type){
276275
if($part && $part->getType() == "Expr_Ternary"){
277276
BIFuncUtils::ternaryHandler($type, $part, $dataFlow) ;
278277
}
278+
279+
//处理双引号中包含的变量
280+
if($part && $part->getType() == "Scalar_Encapsed"){
281+
$symbol = SymbolUtils::getSymbolByNode($part) ;
282+
$dataFlow->setValue($symbol) ;
283+
}
279284

280285

281286
}//else
@@ -485,7 +490,8 @@ public function functionHandler($node, $block, $fileSummary){
485490
$funcName = NodeUtils::getNodeFunctionName($node);
486491
//判断是否为sink函数,返回格式为array(true,funcname) or array(false)
487492
$ret = NodeUtils::isSinkFunction($funcName, $scan_type);
488-
if($ret[0] != null){
493+
494+
if($ret[0] != null && $ret[0] === true){
489495
//如果发现了sink调用,启动污点分析
490496
$analyser = new TaintAnalyser() ;
491497
//获取危险参数的位置
@@ -495,7 +501,6 @@ public function functionHandler($node, $block, $fileSummary){
495501
}
496502
//获取到危险参数位置的变量
497503
$argArr = NodeUtils::getFuncParamsByPos($node, $argPosition);
498-
499504
//遍历危险参数名,调用污点分析函数
500505
if(count($argArr) > 0){
501506
foreach ($argArr as $item){
@@ -511,15 +516,14 @@ public function functionHandler($node, $block, $fileSummary){
511516

512517
}
513518
}else{
514-
515519
//如果不是sink调用,启动过程间分析
516520
$context = Context::getInstance() ;
517521
$funcBody = $context->getClassMethodBody(
518522
$funcName,
519523
$this->fileSummary->getPath(),
520524
$this->fileSummary->getIncludeMap()
521525
);
522-
526+
523527
//check
524528
if(!$funcBody || !is_object($funcBody)) return ;
525529

@@ -558,9 +562,10 @@ public function functionHandler($node, $block, $fileSummary){
558562
if($funcBody->getType() == "Stmt_ClassMethod"){
559563
$funcBody->stmts = $funcBody->stmts[0] ;
560564
}
561-
565+
562566
//构建相应方法体的block和summary
563567
$nextblock = $this->CFGBuilder($funcBody->stmts, NULL, NULL, NULL) ;
568+
564569
//ret危险参数的位置比如:array(0)
565570
$ret = $this->sinkFunctionHandler($funcBody, $nextblock, $block);
566571

@@ -766,8 +771,8 @@ public function CFGBuilder($nodes,$condition,$pEntryBlock,$pNextBlock){
766771
//print_r($currBlock->getBlockSummary()) ;
767772
return $currBlock ;
768773
}else{
769-
$currBlock->addNode($node);
770-
//print_r($currBlock->getBlockSummary()) ;
774+
$currBlock->addNode($node);
775+
//print_r($currBlock->getBlockSummary()) ;
771776
}
772777
}
773778

@@ -1024,10 +1029,9 @@ public function leaveNode(Node $node){
10241029
* @return array
10251030
*/
10261031
public function sinkMultiBlockTraceback($argName,$block,$flowsNum=0){
1027-
//print_r("enter sinkMultiBlockTraceback<br/>");
10281032
$mulitBlockHandlerUtils = new multiBlockHandlerUtils($block);
10291033
$blockList = $mulitBlockHandlerUtils->getPathArr();
1030-
1034+
10311035
$flows = $block->getBlockSummary()->getDataFlowMap();
10321036
//当前块flows没有遍历完
10331037
if(count($flows) != $flowsNum)
@@ -1148,10 +1152,19 @@ public function sinkTracebackBlock($argName,$block,$flowsNum){
11481152
// echo "<pre>" ;
11491153

11501154

1155+
11511156
// //从用户那接受项目路径
11521157
// $project_path = 'C:/Users/xyw55/Desktop/test/74cms_3.3';
11531158
// //$project_path = "D:/MySoftware/wamp/www/code/phpvulhunter/test/test.php" ;
1159+
1160+
1161+
// //从用户那接受项目路径
1162+
// $project_path = 'E:/School_of_software/information_security/PHPVulScanner_project/simple-log_v1.3.12/upload/';
1163+
// $project_path = "D:/MySoftware/wamp/www/code/phpvulhunter/test/test.php" ;
1164+
// $project_path = "E:/School_of_software/information_security/PHPVulScanner_project/74cms_3.3/" ;
1165+
11541166
// $allFiles = FileUtils::getPHPfile($project_path);
1167+
11551168
// //初始化
11561169
// $initModule = new InitModule() ;
11571170
// $initModule->init($project_path, $allFiles) ;
@@ -1172,7 +1185,21 @@ public function sinkTracebackBlock($argName,$block,$flowsNum){
11721185
// $pEntryBlock->is_entry = true ;
11731186
// $ret = $cfg->CFGBuilder($nodes, NULL, NULL, NULL) ;
11741187

1175-
//echo '456';
1188+
1189+
// $cfg = new CFGGenerator() ;
1190+
// $visitor = new MyVisitor() ;
1191+
// $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative) ;
1192+
// $traverser = new PhpParser\NodeTraverser ;
1193+
// $path = CURR_PATH . '/test/test.php';
1194+
// $cfg->getFileSummary()->setPath($path);
1195+
// $code = file_get_contents($path);
1196+
// $stmts = $parser->parse($code) ;
1197+
// $traverser->addVisitor($visitor) ;
1198+
// $traverser->traverse($stmts) ;
1199+
// $nodes = $visitor->getNodes() ;
1200+
// $pEntryBlock = new BasicBlock() ;
1201+
// $pEntryBlock->is_entry = true ;
1202+
// $ret = $cfg->CFGBuilder($nodes, NULL, NULL, NULL) ;
11761203

11771204

11781205

analyser/TaintAnalyser.class.php

+33-71
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,6 @@ public function getPathArr() {
3333
return $this->pathArr;
3434
}
3535

36-
/**
37-
* 判断字符串needle是否是str的开头
38-
* @param string $str
39-
* @param string $needle
40-
* @return boolean
41-
*/
42-
private function startWith($str, $needle) {
43-
if(strpos($str, $needle) === 0){
44-
return true ;
45-
}else{
46-
return false ;
47-
}
48-
}
49-
50-
/**
51-
* 判断target是否是source的结尾
52-
* @param string $source
53-
* @param string $target
54-
* @return boolean
55-
*/
56-
private function endsWith($source, $target){
57-
if(strrchr($source,$target) == $target){
58-
return true ;
59-
}else{
60-
return false ;
61-
}
62-
}
6336

6437
/**
6538
* 根据变量的节点返回变量的名称
@@ -110,8 +83,8 @@ public function addTypeByVars(&$vars){
11083
continue ;
11184
}
11285
//判断是否被单引号包裹
113-
$is_start_with = $this->startWith($vars[$i-1]->getValue(), "'");
114-
$is_end_with = $this->endsWith($vars[$i+1]->getValue(), "'") ;
86+
$is_start_with = CommonUtils::startWith($vars[$i-1]->getValue(), "'");
87+
$is_end_with = CommonUtils::endsWith($vars[$i+1]->getValue(), "'") ;
11588
if($is_start_with != -1 && $is_end_with != -1){
11689
$vars[$i]->setType("valueInt") ;
11790
}
@@ -157,6 +130,7 @@ public function getVarsByFlow($flow){
157130

158131
/**
159132
* 获取当前基本块的所有前驱基本块
133+
* 逆序排列
160134
* @param BasicBlock $block
161135
* @return Array 返回前驱基本块集合$this->pathArr
162136
* 使用该方法时,需要对类属性$this->pathArr进行初始化
@@ -246,6 +220,7 @@ public function currBlockTaintHandler($block,$node,$argName,$fileSummary, $flowN
246220
//报告漏洞
247221
$path = $fileSummary->getPath() ;
248222
$this->report($path, $path, $node, $flow->getLocation(), $type) ;
223+
continue ;
249224
}else{
250225
//首先进行文件夹的分析
251226
$this->multiFileHandler($block, $varName, $node, $fileSummary) ;
@@ -267,41 +242,32 @@ public function currBlockTaintHandler($block,$node,$argName,$fileSummary, $flowN
267242
* @param Node $node 调用sink的node
268243
* @param FileSummary $fileSummary 当前文件的文件摘要
269244
*/
270-
public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNum=0){
245+
public function multiBlockHandler($block, $argName, $node, $fileSummary){
271246
if($this->pathArr){
272247
$this->pathArr = array() ;
273248
}
274249

275250
$this->getPrevBlocks($block) ;
276251
$block_list = $this->pathArr ;
277252

278-
//单基本块进入
253+
//单基本块进入 算法停止
279254
if(empty($block_list)){
255+
// 首先,在当前基本块中探测变量,如果有source和不完整的santi则报告漏洞
256+
$this->currBlockTaintHandler($block, $node, $argName, $fileSummary) ;
280257
return ;
281258
}
282259

283260
!empty($block) && array_push($block_list, $block) ;
284261

285-
//如果前驱基本块为空,说明完成回溯,算法停止
286-
if($block_list == null || count($block_list) == 0){
287-
return ;
288-
}
289-
290262
foreach($block_list as $bitem){
291263
//处理非平行结构的前驱基本块
292264
if(!is_array($bitem)){
293265
$flows = $bitem->getBlockSummary()->getDataFlowMap() ;
294266
$flows = array_reverse($flows) ;
295-
$tempNum = $flowNum ;
296-
while ($tempNum){
297-
$tempNum --;
298-
array_shift($flows);
299-
}
300267
//如果flow中没有信息,则换下一个基本块
301268
if($flows == null){
302269
//找到新的argName
303270
foreach ($block->getBlockSummary()->getDataFlowMap() as $flow){
304-
$flowNum ++;
305271
if($flow->getName() == $argName){
306272
if(is_object($flow->getLocation())){
307273
$target = $flow->getLocation() ;
@@ -325,27 +291,25 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
325291
$path = $fileSummary->getPath() ;
326292
$type = TypeUtils::getTypeByFuncName(NodeUtils::getNodeFunctionName($node)) ;
327293
$this->report($path, $path, $node, $flow->getLocation(),$type) ;
328-
//return true ;
294+
continue ;
329295
}
330-
$this->multiBlockHandler($bitem, $varName, $node, $fileSummary,$flowNum) ;
296+
$this->multiBlockHandler($bitem, $varName, $node, $fileSummary) ;
331297
}
332298
return ;
333299
}else{
334300
//在最初block中,argName没有变化则直接递归
335-
336301
if($block_list == null){
337302
return ;
338303
}else{
339-
$this->multiBlockHandler($bitem, $argName, $node, $fileSummary,$flowNum) ;
340-
return ;
304+
$this->multiBlockHandler($bitem, $argName, $node, $fileSummary) ;
305+
return ;
341306
}
342307

343308
}
344309
}
345310
}else{
346311
//对于每个flow,寻找变量argName
347312
foreach ($flows as $flow){
348-
$flowNum ++;
349313
if($flow->getName() == $argName){
350314
//处理净化信息,如果被编码或者净化则返回safe
351315
//先对左边的变量进行查询
@@ -376,6 +340,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
376340
//报告漏洞
377341
$path = $fileSummary->getPath() ;
378342
$this->report($path, $path, $node, $flow->getLocation(), $type) ;
343+
continue ;
379344
}else{
380345
//首先进行文件夹的分析
381346
//首先根据fileSummary获取到fileSummaryMap
@@ -392,8 +357,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
392357
$bitem,
393358
$varName,
394359
$node,
395-
$fileSummary,
396-
$flowNum
360+
$fileSummary
397361
) ;
398362
}
399363
}
@@ -404,15 +368,11 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
404368
}
405369

406370
}else if(is_array($bitem) && count($block_list) > 0){
371+
$bitem = array_reverse($bitem) ;
407372
//是平行结构
408-
foreach ($bitem as $block_item){
373+
foreach ($bitem as $block_item){
409374
$flows = $block_item->getBlockSummary()->getDataFlowMap() ;
410375
$flows = array_reverse($flows) ;
411-
$tempNum = $flowNum ;
412-
while ($tempNum){
413-
$tempNum --;
414-
array_shift($flows);
415-
}
416376
//如果flow中没有信息,则换下一个基本块
417377
if($flows == null){
418378
//找到新的argName
@@ -432,15 +392,20 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
432392
}
433393

434394
$vars = $this->getVarsByFlow($flow) ;
395+
435396
foreach ($vars as $var){
397+
if($var instanceof ValueSymbol){
398+
continue ;
399+
}
436400
$varName = $this->getVarName($var) ;
437401
if(in_array($varName, $this->sourcesArr)){
438402
//报告漏洞
439403
$path = $fileSummary->getPath() ;
440404
$this->report($path, $path, $node, $flow->getLocation(),$type) ;
441-
return ;
405+
continue ;
406+
}else{
407+
$this->multiBlockHandler($block_item, $varName, $node,$fileSummary) ;
442408
}
443-
$this->multiBlockHandler($block_item, $varName, $node,$fileSummary) ;
444409
}
445410
return ;
446411
}else{
@@ -452,7 +417,6 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
452417
}else{
453418
//对于每个flow,寻找变量argName
454419
foreach ($flows as $flow){
455-
$flowNum ++;
456420
if($flow->getName() == $argName){
457421
//处理净化信息,如果被编码或者净化则返回safe
458422
//先对左边的变量进行查询
@@ -483,7 +447,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
483447
//报告漏洞
484448
$path = $fileSummary->getPath() ;
485449
$this->report($path, $path, $node, $flow->getLocation(),$type) ;
486-
//return true ;
450+
continue ;
487451
}else{
488452
//首先进行文件夹的分析
489453
//首先根据fileSummary获取到fileSummaryMap
@@ -492,17 +456,15 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
492456
$block,
493457
$varName,
494458
$node,
495-
$fileSummaryMap,
496-
$flowNum
459+
$fileSummaryMap
497460
) ;
498461

499462
//文件间分析失败,递归
500463
$ret = $this->multiBlockHandler(
501464
$block_item,
502465
$varName,
503466
$node,
504-
$fileSummary,
505-
$flowNum
467+
$fileSummary
506468
) ;
507469
}
508470
}
@@ -684,7 +646,7 @@ public function analysis($block, $node, $argName, $fileSummary){
684646
$block_list = $this->pathArr ;
685647
array_push($block_list, $block) ;
686648
//首先,在当前基本块中探测变量,如果有source和不完整的santi则报告漏洞
687-
$this->currBlockTaintHandler($block, $node, $argName, $fileSummary) ;
649+
//$this->currBlockTaintHandler($block, $node, $argName, $fileSummary) ;
688650
//多个基本块的处理
689651
$this->pathArr = array() ;
690652
$this->multiBlockHandler($block, $argName, $node, $fileSummary) ;
@@ -700,12 +662,12 @@ public function analysis($block, $node, $argName, $fileSummary){
700662
* @param string 漏洞的类型
701663
*/
702664
public function report($node_path, $var_path, $node, $var, $type){
703-
echo "<pre>" ;
704-
echo "有漏洞=====>". $type ."<br/>" ;
705-
echo "漏洞变量:<br/>" ;
706-
print_r($var) ;
707-
echo "漏洞节点:<br/>" ;
708-
print_r($node) ;
665+
// echo "<pre>" ;
666+
// echo "有漏洞=====>". $type ."<br/>" ;
667+
// echo "漏洞变量:<br/>" ;
668+
// print_r($var) ;
669+
// echo "漏洞节点:<br/>" ;
670+
// print_r($node) ;
709671

710672
//获取结果集上下文
711673
$resultContext = ResultContext::getInstance() ;

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin8_15

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_article.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_explain.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_hotword.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_hrtools.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_link.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_notice.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_openconnect.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_admin_admin_page.php

Whitespace-only changes.

data/resultConetxtSerialData/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_plus_ajax_street.php

Whitespace-only changes.

data/serialdata/E__School_of_software_information_security_PHPVulScanner_project_74cms_3.3_

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)