{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CYB011 Baseline Classifier — Inference Example\n", "\n", "End-to-end demo: load the trained XGBoost and PyTorch MLP models from the Hugging Face repo and predict the **adversarial attack phase** for a per-timestep trajectory record.\n", "\n", "**Models predict one of 7 phases:** `reconnaissance`, `feature_space_probe`, `perturbation_craft`, `evasion_attempt`, `feedback_adaptation`, `campaign_consolidation`, `idle_dwell`.\n", "\n", "**This is a baseline reference model**, not a production phase classifier. See the model card and **`leakage_diagnostic.json`** for the structural-leakage findings (6 oracle paths documented across the dataset, 4 README-suggested targets unlearnable after honest leak removal)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Install dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install --quiet xgboost torch safetensors pandas numpy huggingface_hub" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Download model artifacts from Hugging Face" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import hf_hub_download\n", "\n", "REPO_ID = \"xpertsystems/cyb011-baseline-classifier\"\n", "\n", "files = {}\n", "for name in [\"model_xgb.json\", \"model_mlp.safetensors\",\n", " \"feature_engineering.py\", \"feature_meta.json\",\n", " \"feature_scaler.json\"]:\n", " files[name] = hf_hub_download(repo_id=REPO_ID, filename=name)\n", " print(f\" downloaded: {name}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys, os\n", "fe_dir = os.path.dirname(files[\"feature_engineering.py\"])\n", "if fe_dir not in sys.path:\n", " sys.path.insert(0, fe_dir)\n", "\n", "from feature_engineering import (\n", " transform_single, load_meta, build_segment_lookup, INT_TO_LABEL,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Load models and metadata" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import numpy as np\n", "import torch\n", "import torch.nn as nn\n", "import xgboost as xgb\n", "from safetensors.torch import load_file\n", "\n", "meta = load_meta(files[\"feature_meta.json\"])\n", "with open(files[\"feature_scaler.json\"]) as f:\n", " scaler = json.load(f)\n", "\n", "N_FEATURES = len(meta[\"feature_names\"])\n", "N_CLASSES = len(meta[\"int_to_label\"])\n", "print(f\"feature count: {N_FEATURES}\")\n", "print(f\"class count: {N_CLASSES}\")\n", "print(f\"label classes: {list(meta['int_to_label'].values())}\")\n", "print(f\"\\noracle columns excluded (do not pass these to the model):\")\n", "for c in meta.get(\"oracle_excluded\", []):\n", " print(f\" - {c}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xgb_model = xgb.XGBClassifier()\n", "xgb_model.load_model(files[\"model_xgb.json\"])\n", "\n", "# MLP architecture (must match training)\n", "class PhaseMLP(nn.Module):\n", " def __init__(self, n_features, n_classes=7, hidden1=128, hidden2=64, dropout=0.3):\n", " super().__init__()\n", " self.net = nn.Sequential(\n", " nn.Linear(n_features, hidden1),\n", " nn.BatchNorm1d(hidden1),\n", " nn.ReLU(),\n", " nn.Dropout(dropout),\n", " nn.Linear(hidden1, hidden2),\n", " nn.BatchNorm1d(hidden2),\n", " nn.ReLU(),\n", " nn.Dropout(dropout),\n", " nn.Linear(hidden2, n_classes),\n", " )\n", " def forward(self, x):\n", " return self.net(x)\n", "\n", "mlp_model = PhaseMLP(N_FEATURES, n_classes=N_CLASSES)\n", "mlp_model.load_state_dict(load_file(files[\"model_mlp.safetensors\"]))\n", "mlp_model.eval()\n", "print(\"models loaded\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Load segment topology for defender-feature lookup\n", "\n", "The model uses segment context (defender_architecture, detection_strength, ensemble_size, etc.) as features. To predict on a new trajectory, we look up its segment features from the network_topology." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import snapshot_download\n", "\n", "ds_path = snapshot_download(repo_id=\"xpertsystems/cyb011-sample\", repo_type=\"dataset\")\n", "segment_lookup = build_segment_lookup(f\"{ds_path}/network_topology.csv\")\n", "print(f\"loaded {len(segment_lookup)} segment records\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Prediction helper" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MU = np.array(scaler[\"mean\"], dtype=np.float32)\n", "SD = np.array(scaler[\"std\"], dtype=np.float32)\n", "\n", "def predict_attack_phase(record: dict) -> dict:\n", " \"\"\"Predict the adversarial attack phase for one trajectory record.\n", "\n", " Note: do NOT include detection_outcome, detector_confidence_score,\n", " or evasion_budget_consumed in the record. These were outcome leaks\n", " in the training data and are excluded from the feature set.\n", "\n", " Segment features (defender_architecture, detection_strength, etc.)\n", " are looked up from network_topology by target_segment_id.\n", " \"\"\"\n", " X = transform_single(record, meta, segment_lookup=segment_lookup)\n", "\n", " xgb_proba = xgb_model.predict_proba(X)[0]\n", " xgb_label = INT_TO_LABEL[int(np.argmax(xgb_proba))]\n", "\n", " Xs = ((X - MU) / SD).astype(np.float32)\n", " with torch.no_grad():\n", " logits = mlp_model(torch.tensor(Xs))\n", " mlp_proba = torch.softmax(logits, dim=1).numpy()[0]\n", " mlp_label = INT_TO_LABEL[int(np.argmax(mlp_proba))]\n", "\n", " return {\n", " \"xgboost\": {\n", " \"label\": xgb_label,\n", " \"probabilities\": {INT_TO_LABEL[i]: float(p) for i, p in enumerate(xgb_proba)},\n", " },\n", " \"mlp\": {\n", " \"label\": mlp_label,\n", " \"probabilities\": {INT_TO_LABEL[i]: float(p) for i, p in enumerate(mlp_proba)},\n", " },\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Run on an example record\n", "\n", "Real APT-tier trajectory at timestep 21 (mid-campaign). True phase is `evasion_attempt` — the attacker has built up 11 queries and is actively perturbing inputs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Real trajectory record from the sample dataset (true phase: evasion_attempt)\n", "# Note: target_segment_id is supplied so segment features are auto-looked-up\n", "example_record = {\n", " \"target_segment_id\": \"SEG00197\",\n", " \"timestep\": 21,\n", " \"perturbation_magnitude\": 0.14152,\n", " \"feature_delta_l2_norm\": 1.278436,\n", " \"feature_delta_linf_norm\": 0.14152,\n", " \"query_count_cumulative\": 11,\n", " \"attacker_capability_tier\": \"advanced_persistent_threat\",\n", "}\n", "\n", "result = predict_attack_phase(example_record)\n", "\n", "print(f\"XGBoost -> {result['xgboost']['label']}\")\n", "for lbl, p in sorted(result['xgboost']['probabilities'].items(), key=lambda x: -x[1]):\n", " print(f\" P({lbl:30s}) = {p:.4f}\")\n", "\n", "print(f\"\\nMLP -> {result['mlp']['label']}\")\n", "for lbl, p in sorted(result['mlp']['probabilities'].items(), key=lambda x: -x[1]):\n", " print(f\" P({lbl:30s}) = {p:.4f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Per-class confidence patterns\n", "\n", "The model has strong confidence on `evasion_attempt` (per-class F1 1.00), `reconnaissance` (F1 0.89), and `campaign_consolidation` (F1 0.81) — these phases have distinctive feature signatures (query usage, timestep position, perturbation activity).\n", "\n", "The middle phases overlap more in feature space. `perturbation_craft` is the hardest class (F1 0.49) because its trajectory features look similar to `feature_space_probe` at the per-timestep level. A sequence model considering event ordering within campaigns would likely do better than per-timestep classification." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Batch prediction on the sample dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "trajectories = pd.read_csv(f\"{ds_path}/attack_trajectories.csv\")\n", "\n", "# Score the first 500 events\n", "sample = trajectories.head(500).copy()\n", "preds = [predict_attack_phase(row.to_dict())[\"xgboost\"][\"label\"] for _, row in sample.iterrows()]\n", "sample[\"xgb_pred\"] = preds\n", "\n", "ct = pd.crosstab(sample[\"attack_phase\"], sample[\"xgb_pred\"],\n", " rownames=[\"true\"], colnames=[\"pred\"])\n", "print(\"Confusion on first 500 sample events (XGBoost):\")\n", "print(ct)\n", "acc = (sample[\"attack_phase\"] == sample[\"xgb_pred\"]).mean()\n", "print(f\"\\nbatch accuracy on first 500 events (in-distribution): {acc:.4f}\")\n", "print(\"\\nNote: this includes training-set events. See validation_results.json\\n\"\n", " \"for proper held-out test metrics (group-aware split by campaign_id).\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. Important reading: the leakage diagnostic\n", "\n", "Before using CYB011 sample data to train your own models, read **`leakage_diagnostic.json`** in this repo. It documents **6 oracle paths** across the sample's targets:\n", "\n", "**Phase target oracles (3 paths — dropped from features):**\n", "1. `detection_outcome` (`!= suppressed_alert` → 100% `evasion_attempt`)\n", "2. `detector_confidence_score` (threshold-derived from `detection_outcome`)\n", "3. `evasion_budget_consumed` (`== 0` → 100% one of 3 early phases)\n", "\n", "**Other documented leaks (for transparency, not features for this model):**\n", "4. `stealth_score` near-deterministic per `attacker_capability_tier` (campaign-level)\n", "5. Topology fingerprint (7 segment-level features uniquely identify `defender_architecture`)\n", "6. `timestep` partial oracle for 3 phases — **KEPT as legitimate campaign-progress observable**\n", "\n", "It also documents **4 README-suggested headline targets that are unlearnable on the sample** after honest leak removal: `campaign_success_flag`, `campaign_type` 8-class, `coordinated_attack_flag`, `defender_architecture` 8-class.\n", "\n", "And it documents the **missing `nation_state` attacker tier** — README claims 4 tiers, sample contains only 3 (script_kiddie, opportunistic, APT)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9. Next steps\n", "\n", "- See `validation_results.json` for held-out test metrics (2,100 events from ~30 test campaigns).\n", "- See `multi_seed_results.json` for the across-10-seeds picture (accuracy 0.867 ± 0.010, ROC-AUC 0.977 ± 0.002).\n", "- See `ablation_results.json` for per-feature-group contribution. Perturbation features carry the most signal (−20pp accuracy when removed); query features second (−4pp).\n", "- See **`leakage_diagnostic.json`** for the full 6-oracle-path audit and 4 unlearnable targets.\n", "- For the full ~383k-row CYB011 dataset and commercial licensing, contact **pradeep@xpertsystems.ai**." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.10" } }, "nbformat": 4, "nbformat_minor": 5 }