Spaces:
Build error
Build error
File size: 11,867 Bytes
52d8286 | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | # app.py
"""
UEBA Risk Scoring demo (Gradio + Hugging Face Spaces)
- Train an unsupervised anomaly detector (IsolationForest) on historical logs
- Build user baseline profiles (devices, IPs, common country, frequent actions)
- Score new events with a blended risk score (model anomaly + rule signals)
Expected CSV schema for both training and scoring:
user,timestamp,action,success,country,device,ip
Where:
- user: string identifier
- timestamp: ISO8601 or any pandas-parsable datetime
- action: free-form string (e.g., 'login', 'file_download', 'admin_change')
- success: 1 or 0 (e.g., login success flag; use 1 for non-login actions)
- country: two-letter or name, free-form string
- device: string identifier
- ip: string identifier
This is a simplified educational demo -- not production security tooling.
"""
import os
import pickle
import json
from datetime import datetime
import numpy as np
import pandas as pd
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import MinMaxScaler
import gradio as gr
ARTIFACT_DIR = "artifacts"
MODEL_PATH = os.path.join(ARTIFACT_DIR, "isolation_forest.pkl")
PROFILE_PATH = os.path.join(ARTIFACT_DIR, "baseline_profiles.json")
SCALER_PATH = os.path.join(ARTIFACT_DIR, "feature_scaler.pkl")
ANOMALY_RANGE_PATH = os.path.join(ARTIFACT_DIR, "anomaly_range.json")
FEATURES_JSON = os.path.join(ARTIFACT_DIR, "features.json")
os.makedirs(ARTIFACT_DIR, exist_ok=True)
FEATURE_COLUMNS = [
"hour",
"time_since_last_minutes",
"failed_login",
"is_night",
"location_change",
"new_device",
"new_ip",
"rare_action",
"impossible_travel"
]
RULE_WEIGHTS = {
"failed_login": 25,
"is_night": 10,
"location_change": 20,
"new_device": 15,
"new_ip": 10,
"rare_action": 10,
"impossible_travel": 25,
}
# -----------------
# Feature Engineering
# -----------------
def _parse_time(ts):
try:
return pd.to_datetime(ts, errors='coerce')
except Exception:
return pd.NaT
def build_baseline_profiles(df: pd.DataFrame):
"""Create per-user baseline: common_country, known_devices, known_ips, action_counts."""
profiles = {}
for user, g in df.groupby("user"):
# common country = mode
common_country = g["country"].mode().iloc[0] if not g["country"].mode().empty else None
devices = sorted(list(set(g["device"].dropna().astype(str))))
ips = sorted(list(set(g["ip"].dropna().astype(str))))
action_counts = g["action"].value_counts().to_dict()
profiles[user] = {
"common_country": common_country,
"devices": devices,
"ips": ips,
"action_counts": action_counts,
"total_actions": int(g.shape[0])
}
return profiles
def extract_features(df: pd.DataFrame, profiles: dict):
df = df.copy()
df["timestamp"] = df["timestamp"].apply(_parse_time)
df.sort_values(["user", "timestamp"], inplace=True)
# Basic fields
df["hour"] = df["timestamp"].dt.hour.fillna(0)
df["is_night"] = df["hour"].apply(lambda h: 1 if (h <= 5 or h >= 22) else 0)
df["failed_login"] = df["success"].apply(lambda x: 1 if str(x) in ["0", 0, False, "False"] else 0)
# Time since last per user
df["time_since_last_minutes"] = 0.0
last_time = {}
for idx, row in df.iterrows():
u = row["user"]
t = row["timestamp"]
if pd.isna(t):
df.at[idx, "time_since_last_minutes"] = 0.0
else:
if u in last_time and not pd.isna(last_time[u]):
delta = (t - last_time[u]).total_seconds() / 60.0
df.at[idx, "time_since_last_minutes"] = max(0.0, min(delta, 1440.0)) # clip 0..1 day
else:
df.at[idx, "time_since_last_minutes"] = 1440.0
last_time[u] = t
# Profile-derived flags
df["location_change"] = 0
df["new_device"] = 0
df["new_ip"] = 0
df["rare_action"] = 0
for idx, row in df.iterrows():
u = row["user"]
country = str(row.get("country", ""))
device = str(row.get("device", ""))
ip = str(row.get("ip", ""))
action = str(row.get("action", ""))
prof = profiles.get(u, {
"common_country": None,
"devices": [],
"ips": [],
"action_counts": {},
"total_actions": 0,
})
if prof.get("common_country") and country and country != prof.get("common_country"):
df.at[idx, "location_change"] = 1
if device and device not in set(prof.get("devices", [])):
df.at[idx, "new_device"] = 1
if ip and ip not in set(prof.get("ips", [])):
df.at[idx, "new_ip"] = 1
total = max(1, prof.get("total_actions", 0))
count = prof.get("action_counts", {}).get(action, 0)
rarity = count / total
if rarity <= 0.05:
df.at[idx, "rare_action"] = 1
# Impossible travel (simplified): location change with very short time gap
df["impossible_travel"] = df.apply(lambda r: 1 if (r["location_change"] == 1 and r["time_since_last_minutes"] < 120) else 0, axis=1)
# Keep only expected columns; fill NaNs
feature_df = df[["user", "timestamp"] + FEATURE_COLUMNS].fillna(0)
return feature_df
# -----------------
# Training & Scoring
# -----------------
def train_baseline(csv_file):
try:
df = pd.read_csv(csv_file)
except Exception:
# try excel
df = pd.read_excel(csv_file, engine="openpyxl")
# Validate schema
required_cols = {"user", "timestamp", "action", "success", "country", "device", "ip"}
missing = required_cols - set(df.columns)
if missing:
raise ValueError(f"Missing columns: {sorted(list(missing))}")
# Build profiles
profiles = build_baseline_profiles(df)
feature_df = extract_features(df, profiles)
# Fit scaler and model
X = feature_df[FEATURE_COLUMNS].astype(float).values
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
iso = IsolationForest(
n_estimators=200,
contamination=0.02, # assume ~2% anomalies in baseline
random_state=42,
n_jobs=-1
)
iso.fit(X_scaled)
# For scaling anomaly scores later
decision_scores = iso.decision_function(X_scaled)
# Lower decision_function -> more anomalous; we'll invert
anomaly_raw = -decision_scores
anom_min = float(np.min(anomaly_raw))
anom_max = float(np.max(anomaly_raw))
# Persist artifacts
with open(MODEL_PATH, "wb") as f:
pickle.dump(iso, f)
with open(SCALER_PATH, "wb") as f:
pickle.dump(scaler, f)
with open(PROFILE_PATH, "w") as f:
json.dump(profiles, f)
with open(ANOMALY_RANGE_PATH, "w") as f:
json.dump({"min": anom_min, "max": anom_max}, f)
with open(FEATURES_JSON, "w") as f:
json.dump(FEATURE_COLUMNS, f)
summary = {
"users": len(profiles),
"events": int(df.shape[0]),
"features_shape": list(X.shape),
"anomaly_range": {"min": anom_min, "max": anom_max},
}
return "Baseline trained ✅", pd.DataFrame(feature_df.head(10)), json.dumps(summary, indent=2)
def _load_artifacts():
if not (os.path.exists(MODEL_PATH) and os.path.exists(SCALER_PATH) and os.path.exists(PROFILE_PATH) and os.path.exists(ANOMALY_RANGE_PATH)):
raise RuntimeError("Artifacts not found. Please train the baseline first.")
with open(MODEL_PATH, "rb") as f:
iso = pickle.load(f)
with open(SCALER_PATH, "rb") as f:
scaler = pickle.load(f)
with open(PROFILE_PATH, "r") as f:
profiles = json.load(f)
with open(ANOMALY_RANGE_PATH, "r") as f:
anomaly_range = json.load(f)
return iso, scaler, profiles, anomaly_range
def _blend_risk(anomaly_raw, rule_risk):
# Normalize anomaly_raw to 0..100 using training range
with open(ANOMALY_RANGE_PATH, "r") as f:
rng = json.load(f)
mn, mx = rng["min"], rng["max"]
if mx <= mn:
anom_norm = 50.0
else:
anom_norm = 100.0 * (anomaly_raw - mn) / (mx - mn)
anom_norm = float(np.clip(anom_norm, 0, 100))
# Blend: 60% model, 40% rules
final = 0.6 * anom_norm + 0.4 * rule_risk
return float(np.clip(final, 0, 100)), float(anom_norm)
def score_events(csv_file):
iso, scaler, profiles, _ = _load_artifacts()
try:
df = pd.read_csv(csv_file)
except Exception:
df = pd.read_excel(csv_file, engine="openpyxl")
required_cols = {"user", "timestamp", "action", "success", "country", "device", "ip"}
missing = required_cols - set(df.columns)
if missing:
raise ValueError(f"Missing columns: {sorted(list(missing))}")
feats = extract_features(df, profiles)
X = feats[FEATURE_COLUMNS].astype(float).values
X_scaled = scaler.transform(X)
decision_scores = iso.decision_function(X_scaled)
anomaly_raw = -decision_scores
# Compute rule risk and reasons
rule_risks = []
reasons = []
for idx, row in feats.iterrows():
rr = 0.0
rs = []
for k, w in RULE_WEIGHTS.items():
if row[k] == 1:
rr += w
rs.append(f"{k.replace('_', ' ').title()} (+{w})")
rr = float(np.clip(rr, 0, 100))
rule_risks.append(rr)
reasons.append("; ".join(rs) if rs else "None")
final_scores = []
anom_norms = []
for a, rr in zip(anomaly_raw, rule_risks):
final, anorm = _blend_risk(a, rr)
final_scores.append(final)
anom_norms.append(anorm)
out = pd.DataFrame({
"user": feats["user"],
"timestamp": feats["timestamp"],
"risk_score": final_scores,
"model_anomaly": anom_norms,
"rule_risk": rule_risks,
"reasons": reasons,
"failed_login": feats["failed_login"],
"is_night": feats["is_night"],
"location_change": feats["location_change"],
"new_device": feats["new_device"],
"new_ip": feats["new_ip"],
"rare_action": feats["rare_action"],
"impossible_travel": feats["impossible_travel"],
})
# Sort by highest risk first
out.sort_values("risk_score", ascending=False, inplace=True)
return out
# -----------------
# Gradio UI
# -----------------
def ui_train(file):
if file is None:
return "Please upload a CSV.", None, None
status, head_df, summary = train_baseline(file.name)
return status, head_df, summary
def ui_score(file):
if file is None:
return None
out_df = score_events(file.name)
return out_df
with gr.Blocks(title="UEBA Risk Scoring (Demo)") as demo:
gr.Markdown("""
# UEBA Risk Scoring (Demo)
Train an unsupervised anomaly detector on historical logs and score new events with a blended risk score.
**Note:** This demo is simplified for illustration; tailor features, weights, and thresholds to your environment.
""")
with gr.Tab("1) Train Baseline"):
gr.Markdown("Upload historical logs (CSV) to learn normal behavior.")
train_file = gr.File(file_types=[".csv", ".xlsx"], label="Training data")
train_btn = gr.Button("Train Baseline")
train_status = gr.Markdown()
train_head = gr.Dataframe(headers=None, interactive=False)
train_summary = gr.JSON()
train_btn.click(ui_train, inputs=[train_file], outputs=[train_status, train_head, train_summary])
with gr.Tab("2) Score Events"):
gr.Markdown("Upload new events (CSV) to get risk scores.")
score_file = gr.File(file_types=[".csv", ".xlsx"], label="Events to score")
score_btn = gr.Button("Score")
score_df = gr.Dataframe(interactive=False)
score_btn.click(ui_score, inputs=[score_file], outputs=[score_df])
if __name__ == "__main__":
demo.launch()
|