@@ -33,33 +33,6 @@ public function getPathArr() {
33
33
return $ this ->pathArr ;
34
34
}
35
35
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
- }
63
36
64
37
/**
65
38
* 根据变量的节点返回变量的名称
@@ -110,8 +83,8 @@ public function addTypeByVars(&$vars){
110
83
continue ;
111
84
}
112
85
//判断是否被单引号包裹
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 (), "' " ) ;
115
88
if ($ is_start_with != -1 && $ is_end_with != -1 ){
116
89
$ vars [$ i ]->setType ("valueInt " ) ;
117
90
}
@@ -157,6 +130,7 @@ public function getVarsByFlow($flow){
157
130
158
131
/**
159
132
* 获取当前基本块的所有前驱基本块
133
+ * 逆序排列
160
134
* @param BasicBlock $block
161
135
* @return Array 返回前驱基本块集合$this->pathArr
162
136
* 使用该方法时,需要对类属性$this->pathArr进行初始化
@@ -246,6 +220,7 @@ public function currBlockTaintHandler($block,$node,$argName,$fileSummary, $flowN
246
220
//报告漏洞
247
221
$ path = $ fileSummary ->getPath () ;
248
222
$ this ->report ($ path , $ path , $ node , $ flow ->getLocation (), $ type ) ;
223
+ continue ;
249
224
}else {
250
225
//首先进行文件夹的分析
251
226
$ this ->multiFileHandler ($ block , $ varName , $ node , $ fileSummary ) ;
@@ -267,41 +242,32 @@ public function currBlockTaintHandler($block,$node,$argName,$fileSummary, $flowN
267
242
* @param Node $node 调用sink的node
268
243
* @param FileSummary $fileSummary 当前文件的文件摘要
269
244
*/
270
- public function multiBlockHandler ($ block , $ argName , $ node , $ fileSummary, $ flowNum = 0 ){
245
+ public function multiBlockHandler ($ block , $ argName , $ node , $ fileSummary ){
271
246
if ($ this ->pathArr ){
272
247
$ this ->pathArr = array () ;
273
248
}
274
249
275
250
$ this ->getPrevBlocks ($ block ) ;
276
251
$ block_list = $ this ->pathArr ;
277
252
278
- //单基本块进入
253
+ //单基本块进入 算法停止
279
254
if (empty ($ block_list )){
255
+ // 首先,在当前基本块中探测变量,如果有source和不完整的santi则报告漏洞
256
+ $ this ->currBlockTaintHandler ($ block , $ node , $ argName , $ fileSummary ) ;
280
257
return ;
281
258
}
282
259
283
260
!empty ($ block ) && array_push ($ block_list , $ block ) ;
284
261
285
- //如果前驱基本块为空,说明完成回溯,算法停止
286
- if ($ block_list == null || count ($ block_list ) == 0 ){
287
- return ;
288
- }
289
-
290
262
foreach ($ block_list as $ bitem ){
291
263
//处理非平行结构的前驱基本块
292
264
if (!is_array ($ bitem )){
293
265
$ flows = $ bitem ->getBlockSummary ()->getDataFlowMap () ;
294
266
$ flows = array_reverse ($ flows ) ;
295
- $ tempNum = $ flowNum ;
296
- while ($ tempNum ){
297
- $ tempNum --;
298
- array_shift ($ flows );
299
- }
300
267
//如果flow中没有信息,则换下一个基本块
301
268
if ($ flows == null ){
302
269
//找到新的argName
303
270
foreach ($ block ->getBlockSummary ()->getDataFlowMap () as $ flow ){
304
- $ flowNum ++;
305
271
if ($ flow ->getName () == $ argName ){
306
272
if (is_object ($ flow ->getLocation ())){
307
273
$ target = $ flow ->getLocation () ;
@@ -325,27 +291,25 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
325
291
$ path = $ fileSummary ->getPath () ;
326
292
$ type = TypeUtils::getTypeByFuncName (NodeUtils::getNodeFunctionName ($ node )) ;
327
293
$ this ->report ($ path , $ path , $ node , $ flow ->getLocation (),$ type ) ;
328
- //return true ;
294
+ continue ;
329
295
}
330
- $ this ->multiBlockHandler ($ bitem , $ varName , $ node , $ fileSummary, $ flowNum ) ;
296
+ $ this ->multiBlockHandler ($ bitem , $ varName , $ node , $ fileSummary ) ;
331
297
}
332
298
return ;
333
299
}else {
334
300
//在最初block中,argName没有变化则直接递归
335
-
336
301
if ($ block_list == null ){
337
302
return ;
338
303
}else {
339
- $ this ->multiBlockHandler ($ bitem , $ argName , $ node , $ fileSummary, $ flowNum ) ;
340
- return ;
304
+ $ this ->multiBlockHandler ($ bitem , $ argName , $ node , $ fileSummary ) ;
305
+ return ;
341
306
}
342
307
343
308
}
344
309
}
345
310
}else {
346
311
//对于每个flow,寻找变量argName
347
312
foreach ($ flows as $ flow ){
348
- $ flowNum ++;
349
313
if ($ flow ->getName () == $ argName ){
350
314
//处理净化信息,如果被编码或者净化则返回safe
351
315
//先对左边的变量进行查询
@@ -376,6 +340,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
376
340
//报告漏洞
377
341
$ path = $ fileSummary ->getPath () ;
378
342
$ this ->report ($ path , $ path , $ node , $ flow ->getLocation (), $ type ) ;
343
+ continue ;
379
344
}else {
380
345
//首先进行文件夹的分析
381
346
//首先根据fileSummary获取到fileSummaryMap
@@ -392,8 +357,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
392
357
$ bitem ,
393
358
$ varName ,
394
359
$ node ,
395
- $ fileSummary ,
396
- $ flowNum
360
+ $ fileSummary
397
361
) ;
398
362
}
399
363
}
@@ -404,15 +368,11 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
404
368
}
405
369
406
370
}else if (is_array ($ bitem ) && count ($ block_list ) > 0 ){
371
+ $ bitem = array_reverse ($ bitem ) ;
407
372
//是平行结构
408
- foreach ($ bitem as $ block_item ){
373
+ foreach ($ bitem as $ block_item ){
409
374
$ flows = $ block_item ->getBlockSummary ()->getDataFlowMap () ;
410
375
$ flows = array_reverse ($ flows ) ;
411
- $ tempNum = $ flowNum ;
412
- while ($ tempNum ){
413
- $ tempNum --;
414
- array_shift ($ flows );
415
- }
416
376
//如果flow中没有信息,则换下一个基本块
417
377
if ($ flows == null ){
418
378
//找到新的argName
@@ -432,15 +392,20 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
432
392
}
433
393
434
394
$ vars = $ this ->getVarsByFlow ($ flow ) ;
395
+
435
396
foreach ($ vars as $ var ){
397
+ if ($ var instanceof ValueSymbol){
398
+ continue ;
399
+ }
436
400
$ varName = $ this ->getVarName ($ var ) ;
437
401
if (in_array ($ varName , $ this ->sourcesArr )){
438
402
//报告漏洞
439
403
$ path = $ fileSummary ->getPath () ;
440
404
$ this ->report ($ path , $ path , $ node , $ flow ->getLocation (),$ type ) ;
441
- return ;
405
+ continue ;
406
+ }else {
407
+ $ this ->multiBlockHandler ($ block_item , $ varName , $ node ,$ fileSummary ) ;
442
408
}
443
- $ this ->multiBlockHandler ($ block_item , $ varName , $ node ,$ fileSummary ) ;
444
409
}
445
410
return ;
446
411
}else {
@@ -452,7 +417,6 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
452
417
}else {
453
418
//对于每个flow,寻找变量argName
454
419
foreach ($ flows as $ flow ){
455
- $ flowNum ++;
456
420
if ($ flow ->getName () == $ argName ){
457
421
//处理净化信息,如果被编码或者净化则返回safe
458
422
//先对左边的变量进行查询
@@ -483,7 +447,7 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
483
447
//报告漏洞
484
448
$ path = $ fileSummary ->getPath () ;
485
449
$ this ->report ($ path , $ path , $ node , $ flow ->getLocation (),$ type ) ;
486
- //return true ;
450
+ continue ;
487
451
}else {
488
452
//首先进行文件夹的分析
489
453
//首先根据fileSummary获取到fileSummaryMap
@@ -492,17 +456,15 @@ public function multiBlockHandler($block, $argName, $node, $fileSummary, $flowNu
492
456
$ block ,
493
457
$ varName ,
494
458
$ node ,
495
- $ fileSummaryMap ,
496
- $ flowNum
459
+ $ fileSummaryMap
497
460
) ;
498
461
499
462
//文件间分析失败,递归
500
463
$ ret = $ this ->multiBlockHandler (
501
464
$ block_item ,
502
465
$ varName ,
503
466
$ node ,
504
- $ fileSummary ,
505
- $ flowNum
467
+ $ fileSummary
506
468
) ;
507
469
}
508
470
}
@@ -684,7 +646,7 @@ public function analysis($block, $node, $argName, $fileSummary){
684
646
$ block_list = $ this ->pathArr ;
685
647
array_push ($ block_list , $ block ) ;
686
648
//首先,在当前基本块中探测变量,如果有source和不完整的santi则报告漏洞
687
- $ this ->currBlockTaintHandler ($ block , $ node , $ argName , $ fileSummary ) ;
649
+ // $this->currBlockTaintHandler($block, $node, $argName, $fileSummary) ;
688
650
//多个基本块的处理
689
651
$ this ->pathArr = array () ;
690
652
$ this ->multiBlockHandler ($ block , $ argName , $ node , $ fileSummary ) ;
@@ -700,12 +662,12 @@ public function analysis($block, $node, $argName, $fileSummary){
700
662
* @param string 漏洞的类型
701
663
*/
702
664
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) ;
709
671
710
672
//获取结果集上下文
711
673
$ resultContext = ResultContext::getInstance () ;
0 commit comments