Secrets Scanner Implementation
import re
import os
class SecretsScanner:
def __init__(self):
self.patterns = [
(r'password\s*=\s*["\'][^"\']{8,}["\']', 'Hardcoded Password'),
(r'api[_-]?key\s*[=:]\s*["\'][^"\']{16,}["\']', 'API Key'),
(r'sk-[a-zA-Z0-9]{32,}', 'Secret Key'),
(r'pk_live_[a-zA-Z0-9]{24,}', 'Live API Key'),
(r'-----BEGIN [A-Z ]+-----', 'Private Key')
]
def scan_file(self, filepath):
violations = []
try:
with open(filepath, 'r') as f:
content = f.read()
for pattern, description in self.patterns:
matches = re.finditer(pattern, content, re.IGNORECASE)
for match in matches:
violations.append({
'file': filepath,
'line': content[:match.start()].count('\n') + 1,
'type': description,
'technique': 'T1552'
})
except Exception as e:
print(f"Error scanning {filepath}: {e}")
return violations