#!/usr/bin/env python3 """ Security scanner for ClawHub skills Detects common malicious patterns and security risks """ import os import re import sys import json import base64 from pathlib import Path from typing import List, Dict, Tuple class SkillScanner: """Scan skill files for security issues""" # Dangerous patterns to detect (pattern, description, severity) # Severity: CRITICAL, HIGH, MEDIUM, LOW, INFO PATTERNS = { 'code_execution': [ (r'\beval\s*\(', 'eval() execution', 'CRITICAL'), (r'\bexec\s*\(', 'exec() execution', 'CRITICAL'), (r'__import__\s*\(', 'dynamic imports', 'HIGH'), (r'importlib\.import_module\s*\(', 'importlib dynamic import', 'HIGH'), (r'compile\s*\(', 'code compilation', 'HIGH'), (r'getattr\s*\(.*,.*[\'"]system[\'"]', 'getattr obfuscation', 'CRITICAL'), ], 'subprocess': [ (r'subprocess\.(call|run|Popen).*shell\s*=\s*True', 'shell=True', 'CRITICAL'), (r'os\.system\s*\(', 'os.system()', 'CRITICAL'), (r'os\.popen\s*\(', 'os.popen()', 'HIGH'), (r'commands\.(getoutput|getstatusoutput)', 'commands module', 'HIGH'), ], 'obfuscation': [ (r'base64\.b64decode', 'base64 decoding', 'MEDIUM'), (r'codecs\.decode.*[\'"]hex[\'"]', 'hex decoding', 'MEDIUM'), (r'\\x[0-9a-fA-F]{2}', 'hex escapes', 'LOW'), (r'\\u[0-9a-fA-F]{4}', 'unicode escapes', 'LOW'), (r'chr\s*\(\s*\d+\s*\)', 'chr() obfuscation', 'MEDIUM'), ], 'network': [ (r'requests\.(get|post|put|delete)\s*\(', 'HTTP requests', 'MEDIUM'), (r'urllib\.request\.urlopen', 'urllib requests', 'MEDIUM'), (r'socket\.socket\s*\(', 'raw sockets', 'HIGH'), (r'http\.client\.(HTTPConnection|HTTPSConnection)', 'http.client', 'MEDIUM'), ], 'file_operations': [ (r'open\s*\(.*[\'"]w[\'"]', 'file writing', 'MEDIUM'), (r'os\.remove\s*\(', 'file deletion', 'HIGH'), (r'shutil\.(rmtree|move|copy)', 'bulk file ops', 'HIGH'), (r'pathlib\.Path.*\.unlink\s*\(', 'path deletion', 'HIGH'), ], 'env_access': [ (r'os\.environ\[', 'env variable access', 'MEDIUM'), (r'os\.getenv\s*\(', 'env variable reading', 'LOW'), (r'subprocess.*env\s*=', 'env manipulation', 'HIGH'), ], 'prompt_injection': [ (r'