-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphptickler.py
1378 lines (1176 loc) · 54.8 KB
/
phptickler.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, re, json, threading, queue, requests, ast, argparse, sys, html, datetime
from typing import List, Dict, Set, Any, Optional
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from enum import Enum
import xml.etree.ElementTree as ET
from abc import ABC, abstractmethod
from colorama import init, Fore, Style
from pathlib import Path
class SeverityLevel(Enum):
CRITICAL = 5
HIGH = 4
MEDIUM = 3
LOW = 2
INFO = 1
@dataclass
class VulnerabilityResult:
type: str
severity: SeverityLevel
line_number: int
line_content: str
function: str
description: str
remediation: str
cwe_id: str
references: List[str]
owasp_category: str = None
fix_snippet: str = None
framework: str = None
@dataclass
class TaintSource:
source_type: str
match: str
line_number: int
content: str = ''
@dataclass
class TaintSink:
sink_type: str
match: str
line_number: int
content: str = ''
class VulnerabilityScanner(ABC):
@abstractmethod
def scan(self, content: str) -> List[VulnerabilityResult]:
pass
class StaticAnalysisScanner(VulnerabilityScanner):
def __init__(self):
self.sql_injection_sinks = [
r'SELECT.*FROM.*WHERE.*=\s*[\'"]\s*\$(?:_GET|_POST|REQUEST)',
r'SELECT.*FROM.*WHERE.*=\s*\$(?!_SERVER)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
r'SELECT.*FROM.*WHERE.*=\s*[\'"]\s*\$.*[\'"]',
r'SELECT.*FROM.*WHERE.*=\s*\$.*(?<!escaped_)',
r'mysqli_query\s*\([^,]+,\s*[\'"][^\'"].*\$.*[\'"]',
r'mysqli_query\s*\([^,]+,\s*\$.*\)',
r'mysql_query\s*\([\'"][^\'"].*\$.*[\'"]',
r'\$.*->query\s*\([\'"][^\'"].*\$.*[\'"]'
]
self.rce_sinks = [
r'system\s*\([^)]*\$',
r'exec\s*\([^)]*\$',
r'shell_exec\s*\([^)]*\$',
r'passthru\s*\([^)]*\$',
r'`.*\$.*`',
r'popen\s*\([^)]*\$',
r'proc_open\s*\([^)]*\$',
r'eval\s*\([^)]*\$',
r'assert\s*\([^)]*\$',
r'create_function\s*\([^)]*\$'
]
self.xss_sinks = [
r'echo\s+[^;]*\$_(?:GET|POST|REQUEST)',
r'echo\s+[^;]*\$(?!_SERVER)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
r'print\s+[^;]*\$_(?:GET|POST|REQUEST)',
r'print\s+[^;]*\$(?!_SERVER)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
r'<.*?>\s*\$.*?</.*?>',
r'innerHTML\s*=\s*[\'"].*?\$'
]
self.file_upload_sinks = [
r'move_uploaded_file\s*\(',
r'\$_FILES\s*\[',
r'copy\s*\([^,]*\$_FILES',
r'file_put_contents\s*\([^,]*\$_FILES',
r'fwrite\s*\([^,]*\$_FILES'
]
self.lfi_sinks = [
r'include\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'require\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'include_once\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'require_once\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'file_get_contents\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'fopen\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'readfile\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'file\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)'
]
self.safe_sanitization = {
'sql': [
r'mysqli_real_escape_string',
r'mysql_real_escape_string',
r'PDO::prepare',
r'mysqli_prepare',
r'bind_param'
],
'xss': [
r'htmlspecialchars\s*\(',
r'htmlentities\s*\(',
r'strip_tags\s*\('
],
'file': [
r'pathinfo\s*\(',
r'filesize\s*\(',
r'mime_content_type\s*\(',
r'is_uploaded_file\s*\('
],
'path': [
r'basename\s*\(',
r'realpath\s*\(',
r'dirname\s*\('
]
}
self.dangerous_extensions = [
r'\.php',
r'\.phtml',
r'\.php3',
r'\.php4',
r'\.php5',
r'\.php7',
r'\.pht',
r'\.phar',
r'\.exe',
r'\.sh',
r'\.asp',
r'\.aspx',
r'\.jsp',
r'\.cgi'
]
self.path_traversal_sinks = [
r'(?:fopen|file_get_contents|file_put_contents|readfile)\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST|_FILES)',
r'(?:opendir|scandir|dir)\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST|_FILES)',
r'(?:include|require|include_once|require_once)\s*\(\s*[\'"]?\$(?:_GET|_POST|REQUEST)',
r'move_uploaded_file\s*\(\s*\$_FILES.*?,\s*(?:[\'"].*?\$(?:_GET|_POST|REQUEST)|[^,]*?\.\.)',
r'[\'"](?:\.\./|\.\.\\\|\.\/|\.\\)+.*?\$(?:_GET|_POST|REQUEST)',
r'\$(?:path|dir|directory|file|filename)\s*=\s*[\'"].*?\$(?:_GET|_POST|REQUEST)',
r'\$target(?:_dir|_path|_file)\s*=\s*[\'"].*?\$(?:_GET|_POST|REQUEST)',
r'basename\s*\(\s*\$(?:_GET|_POST|REQUEST)\[.*?\]',
r'realpath\s*\(\s*\$(?:_GET|_POST|REQUEST)\[.*?\]'
]
self.safe_path_patterns = [
r'<link[^>]+href=[\'"]\.\./',
r'<script[^>]+src=[\'"]\.\./',
r'<img[^>]+src=[\'"]\.\./',
r'href=[\'"]\.\./',
r'location\.href=[\'"]\.\./\w+',
r'onclick=[\'"]location\.href=[\'"]\.\./\w+'
]
self.path_traversal_sanitization = [
r'realpath\s*\(',
r'basename\s*\(',
r'dirname\s*\(',
r'pathinfo\s*\(',
r'str_replace\s*\(\s*[\'"]\.\.[\'"]\s*,\s*[\'"][\'"]\s*,',
r'strstr\s*\([^,]+,\s*[\'"]\.\.[\'"]\s*\)\s*===\s*false',
r'strpos\s*\([^,]+,\s*[\'"]\.\.[\'"]\s*\)\s*===\s*false',
r'preg_replace\s*\(\s*[\'"](?:#|/)\.\./[\'"]',
r'(?:DIRECTORY_SEPARATOR|PATH_SEPARATOR)',
r'is_dir\s*\(',
r'is_file\s*\(',
r'file_exists\s*\('
]
self.path_traversal_sinks = [
r'move_uploaded_file\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]tmp_name[\'"]\]\s*,\s*(?:[^,]*?\.\.|\$(?:_GET|_POST|REQUEST|_FILES))',
r'\$target(?:_dir|_file|_path)\s*=\s*[\'"].*?\$_FILES\[[\'"].*?[\'"]\]\[[\'"]name[\'"]\]',
r'basename\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]name[\'"]\]\s*\)',
r'[\'"]uploads\/[\'"]?\s*\.\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]name[\'"]\]',
r'[\'"]uploads\/[\'"]?\s*\.\s*basename\s*\(\s*\$_FILES',
r'(?<!realpath\()(?<!is_uploaded_file\()\$_FILES\[[\'"].*?[\'"]\]\[[\'"](?:name|tmp_name)[\'"]\]',
r'\$target.*?=.*?[\'"].*?\/.*?\$_FILES',
r'pathinfo\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]name[\'"]\]\s*\)',
r'(?<!realpath\()\$target_(?:dir|file|path)\s*=\s*[\'"].*?\/.*?\$'
]
self.file_upload_sanitization = [
r'is_uploaded_file\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]tmp_name[\'"]\]\s*\)',
r'realpath\s*\(\s*\$target(?:_dir|_file|_path)\s*\)',
r'pathinfo\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]name[\'"]\]\s*,\s*PATHINFO_(?:BASENAME|EXTENSION)\s*\)',
r'mime_content_type\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]tmp_name[\'"]\]\s*\)',
r'getimagesize\s*\(\s*\$_FILES\[[\'"].*?[\'"]\]\[[\'"]tmp_name[\'"]\]\s*\)'
]
def _check_vulnerability(self, line: str, line_number: int, patterns: List[str],
vuln_type: str, severity: SeverityLevel,
cwe_id: str, variables: Dict) -> Optional[VulnerabilityResult]:
for pattern in patterns:
if re.search(pattern, line):
is_vulnerable = False
used_var = None
if re.search(r'\$_(?:GET|POST|REQUEST)', line):
is_vulnerable = True
else:
for var in variables:
if f'${var}' in line:
used_var = variables[var]
if not used_var.get(f'{vuln_type.lower()}_sanitized', False):
is_vulnerable = True
break
if is_vulnerable:
return self._create_vulnerability_result(
vuln_type, severity, line_number, line, used_var, cwe_id
)
return None
def _create_vulnerability_result(self, vuln_type: str, severity: SeverityLevel,
line_number: int, line: str, var_info: Optional[dict],
cwe_id: str) -> VulnerabilityResult:
vuln = VulnerabilityResult(
type=vuln_type,
severity=severity,
line_number=line_number,
line_content=line.strip(),
function="",
description=f"Potential {vuln_type} vulnerability detected",
remediation=f"Please review and secure the {vuln_type.lower()} implementation",
cwe_id=cwe_id,
references=["https://owasp.org/www-project-top-ten/"],
owasp_category="A03:2021-Injection"
)
print(f"Created vulnerability: {vuln_type} on line {line_number}")
return vuln
def scan(self, content: str) -> List[VulnerabilityResult]:
vulnerabilities = []
lines = content.split('\n')
variables = self._track_variables(lines)
file_operations = self._track_file_operations(lines)
for i, line in enumerate(lines, 1):
if vuln := self._check_vulnerability(line, i, self.sql_injection_sinks,
"SQL Injection", SeverityLevel.CRITICAL,
"CWE-89", variables):
vulnerabilities.append(vuln)
if vuln := self._check_vulnerability(line, i, self.rce_sinks,
"RCE", SeverityLevel.CRITICAL,
"CWE-78", variables):
vulnerabilities.append(vuln)
if vuln := self._check_vulnerability(line, i, self.xss_sinks,
"Cross-Site Scripting", SeverityLevel.HIGH,
"CWE-79", variables):
vulnerabilities.append(vuln)
if vuln := self._check_vulnerability(line, i, self.file_upload_sinks,
"File Upload", SeverityLevel.HIGH,
"CWE-434", variables):
vulnerabilities.append(vuln)
if vuln := self._check_vulnerability(line, i, self.lfi_sinks,
"Local File Inclusion", SeverityLevel.CRITICAL,
"CWE-98", variables):
vulnerabilities.append(vuln)
if self._is_path_traversal_vulnerable(line, file_operations):
vuln = VulnerabilityResult(
type="Path Traversal",
severity=SeverityLevel.CRITICAL,
line_number=i,
line_content=line.strip(),
function="",
description="Path Traversal vulnerability detected: Unsanitized user input in file path operations",
remediation=(
"1. Use basename() to extract filename\n"
"2. Implement realpath() to resolve and validate the final path\n"
"3. Validate against a whitelist of allowed directories\n"
"4. Remove directory traversal sequences before processing\n"
"5. Store files outside of web root"
),
cwe_id="CWE-22",
references=["https://owasp.org/www-community/attacks/Path_Traversal"],
owasp_category="A01:2021-Path Traversal"
)
vulnerabilities.append(vuln)
vuln = self._check_file_upload_path_traversal(line, content)
if vuln:
vuln.line_number = i
vulnerabilities.append(vuln)
return vulnerabilities
def _track_variables(self, lines: List[str]) -> Dict:
variables = {}
for i, line in enumerate(lines, 1):
input_assign = re.search(r'\$(\w+)\s*=\s*\$_(?:GET|POST|REQUEST)\[[\'"](.*?)[\'"]\]', line)
if input_assign:
var_name = input_assign.group(1)
variables[var_name] = {
'line': i,
'sanitized': False,
'source': line.strip()
}
for var in variables:
if f'${var}' in line:
for vuln_type, patterns in self.safe_sanitization.items():
for pattern in patterns:
if re.search(pattern + r'.*\$' + var, line):
variables[var][f'{vuln_type}_sanitized'] = True
break
return variables
def _track_file_operations(self, lines: List[str]) -> Dict:
operations = {}
for i, line in enumerate(lines, 1):
if any(re.search(pattern, line) for pattern in self.path_traversal_sinks):
operations[i] = {
'type': 'file_operation',
'content': line.strip(),
'sanitized': any(re.search(pattern, line) for pattern in self.path_traversal_sanitization),
'validation': []
}
if re.search(r'realpath\s*\(', line):
operations[i]['validation'].append('realpath')
if re.search(r'basename\s*\(', line):
operations[i]['validation'].append('basename')
if re.search(r'is_dir\s*\(|is_file\s*\(', line):
operations[i]['validation'].append('existence_check')
return operations
def _is_path_traversal_vulnerable(self, line: str, operations: Dict) -> bool:
if any(re.search(pattern, line) for pattern in self.safe_path_patterns):
return False
has_traversal_pattern = any(re.search(pattern, line) for pattern in self.path_traversal_sinks)
if not has_traversal_pattern:
return False
has_sanitization = any(re.search(pattern, line) for pattern in self.path_traversal_sanitization)
if has_sanitization:
return False
has_user_input = re.search(r'\$(?:_GET|_POST|REQUEST|_FILES)', line)
if not has_user_input:
return False
return True
def _generate_path_traversal_description(self, line: str, operations: Dict) -> str:
description = "Path Traversal vulnerability detected: "
if 'move_uploaded_file' in line:
description += "Unsafe file upload location allows directory traversal"
elif 'include' in line or 'require' in line:
description += "Unsanitized file inclusion enables path traversal"
elif any(op in line for op in ['fopen', 'file_get_contents', 'readfile']):
description += "Unsanitized file operations enable path traversal"
else:
description += "Unsanitized path manipulation enables directory traversal"
return description
def _generate_path_traversal_remediation(self) -> str:
return (
"1. Use realpath() to resolve and validate the final path\n"
"2. Implement basename() to extract the file name from the path\n"
"3. Validate file paths and use realpath() to prevent directory traversal\n"
"4. Use whitelisting and realpath() to validate file paths\n"
"5. Implement proper input validation and sanitization"
)
def _check_file_upload_path_traversal(self, line: str, content: str) -> Optional[VulnerabilityResult]:
"""Specific check for path traversal in file uploads"""
if not re.search(r'\$_FILES\[', line):
return None
if re.search(r'move_uploaded_file\s*\(', line):
has_path_validation = any([
re.search(pattern, content) for pattern in [
r'realpath\s*\(\s*\$target',
r'basename\s*\(\s*\$_FILES',
r'is_uploaded_file\s*\(\s*\$_FILES'
]
])
has_traversal_prevention = any([
re.search(pattern, content) for pattern in [
r'strpos\s*\([^,]+,\s*[\'"]\.\.[\'"]\s*\)\s*===\s*false',
r'str_replace\s*\(\s*[\'"]\.\.[\'"]\s*,\s*[\'"][\'"]\s*,',
r'preg_replace\s*\(\s*[\'"]#/\.\./#[\'"]\s*,\s*[\'"][\'"]\s*,'
]
])
if not (has_path_validation and has_traversal_prevention):
return VulnerabilityResult(
type="Path Traversal in File Upload",
severity=SeverityLevel.CRITICAL,
line_number=0,
line_content=line.strip(),
function="",
description=(
"Path Traversal vulnerability in file upload: Missing proper path validation and "
"directory traversal prevention. Attackers could potentially upload files to unauthorized locations."
),
remediation=(
"1. Use basename() to extract filename\n"
"2. Implement realpath() to resolve and validate the final path\n"
"3. Validate file paths against a whitelist of allowed directories\n"
"4. Implement proper directory traversal prevention\n"
"5. Use is_uploaded_file() to validate upload source\n"
"6. Store files in a directory outside web root\n"
"7. Implement proper file type validation"
),
cwe_id="CWE-22",
references=["https://owasp.org/www-community/attacks/Path_Traversal"],
owasp_category="A01:2021-Path Traversal"
)
return None
class PHPVulnerabilityScanner:
def __init__(self, config: dict = None):
self.config = config or {}
self.excluded_paths = set(self.config.get('excluded_paths', []))
self.verbosity = self.config.get('verbosity', 1)
self.max_threads = self.config.get('max_threads', 4)
self.scan_queue = queue.Queue()
self.results = {}
self.scanners = {
'static': StaticAnalysisScanner(),
'dependency': DependencyScanner(),
'config': ConfigurationScanner(),
}
def scan_project(self, directory: str) -> Dict[str, List[VulnerabilityResult]]:
"""Scan entire project using multiple threads."""
print(f"Starting scan in directory: {directory}")
php_files_found = 0
with ThreadPoolExecutor(max_workers=self.max_threads) as executor:
futures = []
for root, _, files in os.walk(directory):
if any(excluded in root for excluded in self.excluded_paths):
print(f"Skipping excluded directory: {root}")
continue
for file in files:
if file.endswith('.php'):
php_files_found += 1
filepath = os.path.join(root, file)
print(f"Queuing file for scanning: {filepath}")
future = executor.submit(self._scan_file, filepath)
futures.append(future)
for future in futures:
future.result()
print(f"Total PHP files found: {php_files_found}")
print(f"Total files with vulnerabilities: {len(self.results)}")
return self.results
def _scan_file(self, filepath: str):
"""Scan individual file with all available scanners."""
try:
print(f"\n=== Scanning file: {filepath} ===")
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
file_results = []
static_scanner = self.scanners['static']
try:
results = static_scanner.scan(content)
if results:
print(f"Found {len(results)} vulnerabilities with static scanner")
file_results.extend(results)
else:
print("No vulnerabilities found with static scanner")
except Exception as e:
print(f"Error in static scanner: {str(e)}")
import traceback
print(traceback.format_exc())
for scanner_name, scanner in self.scanners.items():
if scanner_name == 'static':
continue
try:
print(f"\nRunning {scanner_name} scanner...")
results = scanner.scan(content)
if results:
print(f"Found {len(results)} vulnerabilities with {scanner_name} scanner")
file_results.extend(results)
else:
print(f"No vulnerabilities found with {scanner_name} scanner")
except Exception as e:
print(f"Error in {scanner_name} scanner: {str(e)}")
if file_results:
self.results[filepath] = file_results
print(f"Found {len(file_results)} total vulnerabilities in {filepath}")
else:
print("No vulnerabilities found in this file")
except Exception as e:
print(f"Error scanning {filepath}: {str(e)}")
import traceback
print(traceback.format_exc())
def _track_variables(self, source_var: str, sink: TaintSink, variables: Dict[str, List[str]], content: str) -> List[str]:
"""Track tainted variables from source to sink"""
tainted_vars = set([source_var])
for var, assignments in variables.items():
for assignment in assignments:
if any(tv in assignment for tv in tainted_vars):
tainted_vars.add(var)
if any(f in assignment.lower() for f in ['concat', 'join', 'append', 'replace']):
if any(tv in assignment for tv in tainted_vars):
tainted_vars.add(var)
sink_vars = set()
if sink.content:
for var in tainted_vars:
if var in sink.content:
sink_vars.add(var)
return list(sink_vars)
class PHPNode:
"""Represents a node in the PHP code."""
def __init__(self, type: str, name: str, line: int, content: str, children=None):
self.type = type
self.name = name
self.line = line
self.content = content
self.children = children or []
def walk(self):
"""Generator to walk through all nodes."""
yield self
for child in self.children:
yield from child.walk()
class TaintFlow:
def __init__(self, source: TaintSource, sink: TaintSink, variables: List[str]):
self.source = source
self.sink = sink
self.variables = variables
self.is_vulnerable = False
self.simulation_results = []
class EnhancedTaintAnalyzer:
def __init__(self):
self.source_patterns = {
'user_input_GET': r'\$_GET\[([\'"].*?[\'"]*)\]',
'user_input_POST': r'\$_POST\[([\'"].*?[\'"]*)\]',
'user_input_FILES': r'\$_FILES\[([\'"].*?[\'"]*)\]',
'user_input_REQUEST': r'\$_REQUEST\[([\'"].*?[\'"]*)\]',
'user_input_COOKIE': r'\$_COOKIE\[([\'"].*?[\'"]*)\]',
'user_input_SERVER': r'\$_SERVER\[([\'"].*?[\'"]*)\]',
'user_input_raw': r'file_get_contents\([\'"]php://input[\'"]\)',
}
self.sink_patterns = {
'xss': r'echo|print|printf|<\?=|\$_GET|\$_POST',
'sql': r'mysql_query|mysqli_query|PDO::query|sqlite_query|\$conn->query',
'file': r'file_get_contents|fopen|readfile|include|require|include_once|require_once',
'cmd': r'exec|system|shell_exec|passthru|`.*`|popen|proc_open',
'code': r'eval|assert|create_function|unserialize',
'xpath': r'xpath|query\s*\(',
'ldap': r'ldap_search|ldap_bind',
'reflection': r'ReflectionClass|ReflectionFunction|ReflectionMethod'
}
def _extract_variable_name(self, match_text: str) -> str:
"""Extract variable name from a regex match"""
if match_text.startswith('$_'):
return match_text.split('[')[1].strip('"[]')
if '=' in match_text:
return match_text.split('=')[0].strip('$ ')
if '(' in match_text:
return match_text.split('(')[1].strip('$) ')
return match_text.strip('$ ')
def _analyze_flow(self, source: TaintSource, sink: TaintSink,
variables: Dict[str, List[str]], content: str) -> TaintFlow:
"""Analyze the flow between a source and sink"""
taint_flow = TaintFlow(source, sink, [])
source_var = self._extract_variable_name(source.match)
if source_var:
taint_flow.variables = self._track_variables(source_var, sink, variables, content)
if self._is_vulnerable_flow(taint_flow, content):
taint_flow.is_vulnerable = True
return taint_flow
def _track_variables(self, source_var: str, sink: TaintSink, variables: Dict[str, List[str]], content: str) -> List[str]:
"""Track variables from source to sink to detect taint flow"""
tainted_vars = set([source_var])
for var, assignments in variables.items():
for assignment in assignments:
if any(tv in assignment for tv in tainted_vars):
tainted_vars.add(var)
if any(f in assignment.lower() for f in ['concat', 'join', 'append', 'replace']):
if any(tv in assignment for tv in tainted_vars):
tainted_vars.add(var)
sink_vars = set()
if sink.content:
for var in tainted_vars:
if var in sink.content:
sink_vars.add(var)
return list(sink_vars)
def _is_vulnerable_flow(self, taint_flow: TaintFlow, content: str) -> bool:
"""Enhanced vulnerability detection logic"""
sanitization_functions = [
'htmlspecialchars', 'htmlentities', 'strip_tags',
'mysqli_real_escape_string', 'addslashes', 'escapeshellarg',
'escapeshellcmd', 'filter_var'
]
start_pos = taint_flow.source.line_number
end_pos = taint_flow.sink.line_number
code_segment = '\n'.join(content.split('\n')[start_pos-1:end_pos])
for func in sanitization_functions:
if func in code_segment:
return False
if any(var in taint_flow.sink.content for var in taint_flow.variables):
return True
return True
def _find_sources(self, content: str) -> List[TaintSource]:
"""Find all potential taint sources in the code"""
sources = []
for source_type, pattern in self.source_patterns.items():
matches = re.finditer(pattern, content, re.IGNORECASE)
for match in matches:
line_number = content.count('\n', 0, match.start()) + 1
sources.append(TaintSource(
source_type=source_type,
match=match.group(0),
line_number=line_number,
content=content[match.start():match.end()]
))
return sources
def _find_sinks(self, content: str) -> List[TaintSink]:
"""Find all potential taint sinks in the code"""
sinks = []
for sink_type, pattern in self.sink_patterns.items():
matches = re.finditer(pattern, content, re.IGNORECASE)
for match in matches:
line_number = content.count('\n', 0, match.start()) + 1
sinks.append(TaintSink(
sink_type=sink_type,
match=match.group(0),
line_number=line_number,
content=content[match.start():match.end()]
))
return sinks
def analyze(self, content: str) -> List[VulnerabilityResult]:
"""Enhanced analysis with better vulnerability detection"""
vulnerabilities = []
sources = self._find_sources(content)
sinks = self._find_sinks(content)
variables = self._track_all_variables(content)
for source in sources:
for sink in sinks:
tainted_vars = self._track_variables(
source_var=self._extract_variable_name(source.match),
sink=sink,
variables=variables,
content=content
)
if tainted_vars:
taint_flow = TaintFlow(source, sink, tainted_vars)
if self._is_vulnerable_flow(taint_flow, content):
severity = self._determine_severity(source, sink)
vuln = VulnerabilityResult(
type=f"{sink.sink_type}_vulnerability",
severity=severity,
line_number=sink.line_number,
line_content=sink.content,
function=self._get_function_context(content, sink.line_number),
description=self._generate_description(source, sink),
remediation=self._generate_remediation(sink.sink_type),
cwe_id=self._get_cwe_id(sink.sink_type),
references=self._get_references(sink.sink_type),
owasp_category=self._get_owasp_category(sink.sink_type)
)
vulnerabilities.append(vuln)
return vulnerabilities
def _determine_severity(self, source: TaintSource, sink: TaintSink) -> SeverityLevel:
"""Determine vulnerability severity based on source and sink types"""
critical_sinks = ['cmd', 'code']
high_sinks = ['sql', 'xpath', 'ldap']
medium_sinks = ['xss', 'file']
if sink.sink_type in critical_sinks:
return SeverityLevel.CRITICAL
elif sink.sink_type in high_sinks:
return SeverityLevel.HIGH
elif sink.sink_type in medium_sinks:
return SeverityLevel.MEDIUM
return SeverityLevel.LOW
def _generate_description(self, source: TaintSource, sink: TaintSink) -> str:
"""Generate detailed vulnerability description"""
return f"Unsanitized data from {source.source_type} ({source.match}) " \
f"flows into {sink.sink_type} sink ({sink.match}) at line {sink.line_number}"
def _generate_remediation(self, sink_type: str) -> str:
"""Generate specific remediation advice"""
remediation_advice = {
'xss': "Use htmlspecialchars() or htmlentities() to encode output",
'sql': "Use prepared statements or mysqli_real_escape_string()",
'cmd': "Use escapeshellarg() or escapeshellcmd() for command arguments",
'file': "Validate file paths and use basename() to prevent directory traversal",
'code': "Avoid using eval() or other dynamic code execution functions",
}
return remediation_advice.get(sink_type, "Implement proper input validation and sanitization")
def _track_all_variables(self, content: str) -> Dict[str, List[str]]:
"""Track all variable assignments in the code"""
variables = {}
assignment_pattern = r'\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*=\s*([^;]+)'
matches = re.finditer(assignment_pattern, content)
for match in matches:
var_name = match.group(1)
assignment = match.group(2)
if var_name not in variables:
variables[var_name] = []
variables[var_name].append(assignment.strip())
return variables
class PHPParser:
"""Simple PHP parser using regex patterns."""
def __init__(self):
self.patterns = {
'function_call': r'(\w+)\s*\((.*?)\)',
'variable': r'\$\w+',
'class': r'class\s+(\w+)',
'method': r'function\s+(\w+)\s*\((.*?)\)',
}
def parse(self, content: str):
"""
Parse PHP content into a simple AST-like structure.
Args:
content: PHP code as string
Returns:
PHPNode: Root node of the parsed structure
"""
lines = content.split('\n')
root = PHPNode('root', 'root', 0, content)
children = []
for line_num, line in enumerate(lines, 1):
for match in re.finditer(self.patterns['function_call'], line):
func_name = match.group(1)
children.append(PHPNode(
type='function_call',
name=func_name,
line=line_num,
content=line.strip()
))
for match in re.finditer(self.patterns['variable'], line):
children.append(PHPNode(
type='variable',
name=match.group(0),
line=line_num,
content=line.strip()
))
for match in re.finditer(self.patterns['class'], line):
children.append(PHPNode(
type='class',
name=match.group(1),
line=line_num,
content=match.group(0)
))
for match in re.finditer(self.patterns['method'], line):
children.append(PHPNode(
type='method',
name=match.group(1),
line=line_num,
content=match.group(0)
))
root.children = children
return root
def _extract_params(self, param_str: str) -> List[str]:
"""Extract parameters from a parameter string."""
return [p.strip() for p in param_str.split(',') if p.strip()]
class DependencyScanner(VulnerabilityScanner):
"""Advanced dependency analysis."""
def __init__(self):
self.advisories_api = SecurityAdvisoriesAPI()
def scan(self, content: str) -> List[VulnerabilityResult]:
results = []
if 'composer.json' in content or 'composer.lock' in content:
dependencies = self._parse_dependencies(content)
for dep in dependencies:
vulnerabilities = self.advisories_api.check_package(dep)
results.extend(self._convert_to_results(vulnerabilities))
return results
class ConfigurationScanner(VulnerabilityScanner):
"""Configuration and environment analysis."""
def __init__(self):
self.dangerous_settings = {
'display_errors': 'On',
'allow_url_include': 'On',
'expose_php': 'On',
'session.use_strict_mode': 'Off',
}
def _check_php_config(self, content: str) -> List[VulnerabilityResult]:
"""Check for dangerous PHP configuration settings."""
results = []
for setting, dangerous_value in self.dangerous_settings.items():
pattern = rf'ini_set\([\'"]?{setting}[\'"]?,\s*[\'"]?{dangerous_value}[\'"]?\)'
matches = re.finditer(pattern, content)
for match in matches:
results.append(VulnerabilityResult(
type='Dangerous Configuration',
severity=SeverityLevel.HIGH,
line_number=content.count('\n', 0, match.start()) + 1,
line_content=match.group(),
function='N/A',
description=f'Dangerous PHP configuration: {setting} = {dangerous_value}',
remediation=f'Remove or secure the {setting} configuration',
cwe_id='CWE-756',
references=['https://www.php.net/manual/en/security.php']
))
return results
def scan(self, content: str) -> List[VulnerabilityResult]:
"""Scan for configuration vulnerabilities."""
results = []
results.extend(self._check_php_config(content))
return results
class ReportGenerator:
"""Generate detailed vulnerability reports."""
def __init__(self, results: Dict[str, List[VulnerabilityResult]]):
self.results = results
def generate_html(self, output_file: str):
"""Generate enhanced HTML report with visualizations."""
print(f"\nGenerating HTML report to: {output_file}")
print(f"Number of files with vulnerabilities: {len(self.results)}")
total_vulns = sum(len(vulns) for vulns in self.results.values())
severity_counts = {
'CRITICAL': 0,
'HIGH': 0,
'MEDIUM': 0,
'LOW': 0,
'INFO': 0
}
vulnerability_content = ""
for filepath, vulnerabilities in self.results.items():
if vulnerabilities:
print(f"\nProcessing file: {filepath}")
print(f"Number of vulnerabilities: {len(vulnerabilities)}")
vulnerability_content += f'<h2>{html.escape(filepath)}</h2><div class="vulnerability-list">'
for vuln in vulnerabilities:
severity_counts[vuln.severity.name] += 1
print(f"- {vuln.type} ({vuln.severity.name}) on line {vuln.line_number}")
vulnerability_content += f"""
<div class="vulnerability-card severity-{vuln.severity.name}">
<div class="vuln-header">
<span class="vuln-type">{html.escape(vuln.type)}</span>
<span class="severity-badge {vuln.severity.name}">{vuln.severity.name}</span>
</div>
<div class="line-info">
<div class="line-number">Line {vuln.line_number}</div>
<div class="line-content">
{html.escape(vuln.line_content)}
<button class="copy-button" onclick="copyToClipboard(`{html.escape(vuln.line_content)}`)">Copy</button>
</div>
</div>
<div class="vuln-details">
<div class="detail-item">
<span class="detail-label">Description:</span>
<p>{html.escape(vuln.description)}</p>
</div>
<div class="remediation">
<span class="detail-label">Remediation:</span>
<p>{html.escape(vuln.remediation)}</p>
</div>
<div class="detail-item">
<span class="detail-label">CWE:</span>
<span>{html.escape(vuln.cwe_id)}</span>
</div>
</div>
</div>
"""
vulnerability_content += '</div>'
html_content = f"""<!DOCTYPE html>
<html>
<head>
<title>Security Vulnerability Report</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
:root {{
--critical-color: #dc3545;
--high-color: #fd7e14;
--medium-color: #ffc107;
--low-color: #0dcaf0;
--info-color: #20c997;
}}
body {{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: #f8f9fa;
color: #212529;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}}
header {{
text-align: center;
margin-bottom: 3rem;
padding: 2rem;
background: white;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);