File size: 2,216 Bytes
7270c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import datetime

class SecurityScanner:
    def __init__(self, target_dir):
        self.target_dir = target_dir
        self.results = {
            "scan_time": str(datetime.datetime.now()),
            "vulnerabilities": [],
            "summary": {"high": 0, "medium": 0, "low": 0}
        }

    def scan_files(self):
        print(f"Starting security scan on: {self.target_dir}")
        for root, dirs, files in os.walk(self.target_dir):
            for file in files:
                if file.endswith(('.py', '.js', '.html')):
                    self._check_file(os.path.join(root, file))
        
        self._save_results()

    def _check_file(self, file_path):
        # Simple pattern matching for demonstration
        patterns = {
            "hardcoded_api_key": {"regex": "api_key =", "severity": "high", "desc": "Potential hardcoded API key found"},
            "insecure_eval": {"regex": "eval\(", "severity": "high", "desc": "Use of insecure eval() function"},
            "todo_comment": {"regex": "TODO:", "severity": "low", "desc": "Unresolved TODO comment"}
        }
        
        try:
            with open(file_path, 'r', errors='ignore') as f:
                content = f.read()
                for key, data in patterns.items():
                    if data["regex"] in content:
                        self.results["vulnerabilities"].append({
                            "file": file_path,
                            "issue": key,
                            "severity": data["severity"],
                            "description": data["desc"]
                        })
                        self.results["summary"][data["severity"]] += 1
        except Exception as e:
            print(f"Error scanning {file_path}: {e}")

    def _save_results(self):
        output_path = os.path.join(self.target_dir, 'public/data/security_scan_latest.json')
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        with open(output_path, 'w') as f:
            json.dump(self.results, f, indent=4)
        print(f"Scan complete. Results saved to {output_path}")

if __name__ == "__main__":
    scanner = SecurityScanner('.')
    scanner.scan_files()