Lab 18: AI in Cybersecurity — Threat Detection and AI Attacks
Objective
AI for Defence
Anomaly Detection at Scale
from sklearn.ensemble import IsolationForest
import pandas as pd
import numpy as np
# Load network flow data
flows = pd.read_csv("network_flows.csv")
features = flows[["bytes_in", "bytes_out", "packets", "duration_s", "unique_dests", "port_entropy"]]
# Isolation Forest: anomalies are statistically "isolated" (short paths in trees)
detector = IsolationForest(
contamination=0.001, # expect 0.1% of traffic to be anomalous
n_estimators=200,
random_state=42
)
detector.fit(features)
# Score each flow (-1 = anomaly, 1 = normal)
flows["anomaly_score"] = detector.decision_function(features)
flows["is_anomaly"] = detector.predict(features) == -1
# Investigate flagged flows
anomalies = flows[flows["is_anomaly"]].sort_values("anomaly_score")
print(f"Flagged {len(anomalies)} anomalous flows out of {len(flows)}")
print(anomalies[["src_ip", "dst_ip", "bytes_out", "anomaly_score"]].head(10))AI-Powered Vulnerability Discovery
Phishing Detection
AI Attack Techniques
1. Adversarial Examples
2. Data Poisoning
3. Model Extraction / Stealing
Prompt Injection and Jailbreaking
Prompt Injection
Jailbreaking
Measuring Jailbreak Resistance
AI Security Best Practices for Developers
Further Reading
Last updated
