Copy import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
import warnings; warnings.filterwarnings('ignore')
class SecurityScreenshotAnalyzer:
"""
Full CV pipeline for security screenshot analysis:
1. Feature extraction (simulated ResNet)
2. Multi-task prediction: threat class + urgency + region of interest
"""
THREAT_CLASSES = ['benign', 'sql_injection', 'xss', 'ransomware', 'phishing']
URGENCY_LEVELS = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL']
def __init__(self):
np.random.seed(42)
self.scaler = StandardScaler()
self.clf_threat = LogisticRegression(max_iter=1000, multi_class='multinomial')
self.clf_urgency= LogisticRegression(max_iter=1000, multi_class='multinomial')
def extract_features(self, screenshot_desc: str) -> np.ndarray:
"""Extract visual + text features from screenshot"""
seed = sum(ord(c) for c in screenshot_desc) % 1000
np.random.seed(seed)
visual_feat = np.random.randn(256)
# Text features (keyword indicators)
keywords = {
'sql': [1,0,0,0,0], 'injection': [1,0,0,0,0],
'xss': [0,1,0,0,0], 'script': [0,1,0,0,0],
'ransomware': [0,0,1,0,0], 'encrypt': [0,0,1,0,0],
'phishing': [0,0,0,1,0], 'login': [0,0,0,0.5,0],
'normal': [0,0,0,0,1], 'ok': [0,0,0,0,1],
}
text_feat = np.zeros(5)
for kw, vec in keywords.items():
if kw in screenshot_desc.lower():
text_feat = np.array(vec)
break
return np.concatenate([visual_feat, text_feat])
def fit(self, training_examples: list):
"""Train on labelled screenshots"""
X = np.array([self.extract_features(d) for d, _, _ in training_examples])
y_threat = np.array([t for _, t, _ in training_examples])
y_urgency = np.array([u for _, _, u in training_examples])
X_s = self.scaler.fit_transform(X)
self.clf_threat.fit(X_s, y_threat)
self.clf_urgency.fit(X_s, y_urgency)
print(f"Trained on {len(training_examples)} screenshots")
def analyse(self, screenshot_desc: str) -> dict:
feat = self.extract_features(screenshot_desc)
X_s = self.scaler.transform(feat.reshape(1, -1))
threat_prob = self.clf_threat.predict_proba(X_s)[0]
urgency_prob = self.clf_urgency.predict_proba(X_s)[0]
top_threat = self.THREAT_CLASSES[threat_prob.argmax()]
top_urgency = self.URGENCY_LEVELS[urgency_prob.argmax()]
return {
'threat_class': top_threat,
'threat_confidence': round(float(threat_prob.max()), 3),
'urgency': top_urgency,
'urgency_conf': round(float(urgency_prob.max()), 3),
'action': {
'CRITICAL': 'Isolate immediately + page on-call',
'HIGH': 'Alert SOC + open P1 ticket',
'MEDIUM': 'Schedule investigation within 4h',
'LOW': 'Log and review in next shift',
}[top_urgency],
}
# Training data
training_data = [
("SQL injection attempt in access log with UNION SELECT", 1, 2),
("Normal user browsing dashboard activity", 0, 0),
("XSS script tags detected in form submission", 2, 2),
("Files being encrypted with ransomware extension", 3, 3),
("Phishing login page credential harvester", 4, 3),
("Normal login successful from known IP", 0, 0),
("SQL injection UNION based in URL parameter", 1, 2),
("Cross site scripting alert in WAF log", 2, 1),
("Multiple files renamed to locked extension", 3, 3),
("Suspicious phishing email with fake login", 4, 2),
("Routine system health check normal", 0, 0),
("Normal developer accessing staging environment", 0, 0),
]
analyzer = SecurityScreenshotAnalyzer()
analyzer.fit(training_data)
test_cases = [
"SQL injection attempt UNION SELECT password FROM users in Apache log",
"User opened dashboard normal working hours",
"XSS script tag found in contact form submission",
"Ransomware encrypting files in Documents folder",
"Phishing page mimicking Microsoft login stealing credentials",
]
print("\n=== Security Screenshot Analysis ===")
for desc in test_cases:
result = analyzer.analyse(desc)
flag = "🚨" if result['urgency'] in ('CRITICAL', 'HIGH') else "⚠️" if result['urgency'] == 'MEDIUM' else "✅"
print(f"\n{flag} Screenshot: {desc[:55]}...")
print(f" Threat: {result['threat_class']} ({result['threat_confidence']:.0%})")
print(f" Urgency: {result['urgency']} ({result['urgency_conf']:.0%})")
print(f" Action: {result['action']}")