10.10 Hunting with Notebooks
Hunting with Notebooks
Introduction
KQL is powerful for structured queries against tabular data. But some hunting analysis requires capabilities beyond KQL: statistical modelling, machine learning, data visualisation beyond workbook charts, integration with external APIs, and complex multi-step analysis with intermediate results. Jupyter notebooks in Sentinel provide these capabilities — running Python code in a notebook environment that connects to your Sentinel workspace data.
When notebooks add value
Use notebooks when: Your analysis requires Python libraries (pandas, scikit-learn, NetworkX, matplotlib) that KQL does not provide. You need iterative, exploratory analysis where each step builds on the previous result. You want to apply machine learning models (clustering, classification, anomaly detection) to hunting data. You need to integrate with external APIs during the analysis (threat intelligence enrichment, WHOIS lookups, DNS resolution). You want publication-quality visualisations for hunt reports.
Use standard KQL when: The analysis can be expressed as a single or small set of KQL queries. The hunting pattern maps to one of the six patterns from subsection 10.3. Performance is important (KQL executes directly on the workspace; notebooks add an intermediary hop). The analyst team is KQL-proficient but not Python-proficient.
For most SOC operations, KQL is sufficient. Notebooks are a specialist tool for advanced hunting scenarios — not a replacement for day-to-day KQL hunting.
The Sentinel notebook environment
Navigate to Sentinel → Threat management → Notebooks. Sentinel provides a built-in notebook experience powered by Azure Machine Learning (AML) or direct Jupyter integration.
Setup: Create or connect an Azure Machine Learning workspace. Launch a notebook from the Sentinel portal or upload a custom notebook. The notebook connects to your Sentinel workspace using the azure-sentinel Python package and authenticates with your Azure credentials.
MSTICPy (Microsoft Threat Intelligence Python library) is the primary library for Sentinel notebook hunting. It provides: data connectors (query Sentinel tables from Python), threat intelligence lookups (VirusTotal, OTX, AbuseIPDB, Shodan), visualisations (process trees, timeline plots, network graphs), and analysis functions (anomaly detection, geo-clustering, domain analysis).
Notebook hunting example: network graph analysis
KQL can identify individual connections. A notebook can visualise the entire network of connections as a graph — revealing patterns invisible in tabular data.
| |
In the notebook, load this data into a pandas DataFrame, then build a network graph with NetworkX:
| |
The network graph reveals: which external IPs are connected to the most internal devices (high degree centrality = potential C2 infrastructure), clusters of devices connecting to the same external IPs (potential botnet or shared C2), and isolated connections that stand out from the normal pattern.
Notebook hunting example: time series anomaly detection
KQL can compare today’s count to a 30-day average. A notebook can apply proper statistical time series decomposition — separating trend, seasonality, and anomalies.
| |
This finds hours where sign-in volume was anomalously high or low — accounting for daily and weekly seasonality that a simple average comparison misses.
Notebook hunting example: geographic clustering
Identify geographic clusters of sign-in activity that may indicate attacker infrastructure.
| |
DBSCAN clustering groups sign-in locations into geographic clusters. The user’s regular locations (home, office) form dense clusters. Outlier sign-ins (attacker locations) are classified as noise (cluster = -1). This is more sophisticated than simple “first-time country” detection — it identifies outlier locations even within known countries.
Notebook hunting example: process tree visualisation
Visualise the complete process execution chain on a compromised endpoint — revealing the attacker’s toolchain from initial execution to final payload.
| |
The visual process tree shows: the root process (explorer.exe), the user-launched application (outlook.exe), the malicious child process (powershell.exe spawned by the macro), and all subsequent processes the attacker launched. In KQL, this requires multiple self-joins and is difficult to visualise. In a notebook, the process tree renders as an interactive visual — click any node to see the full command line and timestamps.
MSTICPy data providers and queries
MSTICPy abstracts the Sentinel query interface, making it easy to run common hunting queries without writing raw KQL.
| |
MSTICPy provides hundreds of pre-built query templates covering: Azure AD/Entra ID sign-in analysis, endpoint process investigation, network connection analysis, email analysis, and Azure resource activity. These templates accelerate notebook-based hunting — you do not need to write every query from scratch.
Notebook deployment guide
Option 1: Azure Machine Learning workspace. The production-grade approach. Create an AML workspace in the same subscription as your Sentinel workspace. Launch notebooks from the Sentinel portal → they execute on AML compute. Supports: persistent storage, scheduled execution, collaboration, and GPU compute for ML models.
Option 2: Local Jupyter. For testing and personal hunting. Install Jupyter locally (pip install notebook msticpy). Connect to Sentinel using MSTICPy’s QueryProvider. Simpler setup but no collaboration or persistence.
Option 3: Azure Synapse / Databricks. For organisations with existing data analytics platforms. Connect to the Sentinel Log Analytics workspace via the Azure Monitor API. Use Spark for large-scale analysis.
Recommended path: Start with Content Hub notebook templates on AML (Option 1). Run pre-built templates to learn the workflow. Customise templates for your environment. Build custom notebooks only when the pre-built templates do not cover your specific hunting need.
MSTICPy threat intelligence enrichment
MSTICPy integrates with multiple TI providers for in-notebook enrichment during hunts.
| |
In-notebook TI enrichment eliminates the need to manually check each IP in VirusTotal. The hunter runs a query, extracts suspicious IPs, enriches them all at once, and uses the enrichment results to prioritise which findings warrant full investigation.
Notebook-to-analytics-rule conversion
When a notebook-based hunt discovers a pattern that should be detected automatically, convert the notebook analysis into a KQL analytics rule.
Conversion workflow:
Step 1: Identify the detection logic in the notebook. Which step produced the “this is suspicious” result? What were the criteria?
Step 2: Express the logic in pure KQL. If the notebook used Python libraries (clustering, ML models), simplify: can the detection be approximated with KQL operators? Often, a complex ML model in a notebook can be approximated with a KQL threshold or statistical deviation check that captures 80% of the detection value.
Step 3: If pure KQL cannot express the detection, consider: running the notebook on a schedule (via Azure Machine Learning pipelines) and writing results to a custom table that an analytics rule monitors.
Example: The geographic clustering notebook found that sign-ins from cluster -1 (outliers) correlate with compromises. In KQL, approximate with: “sign-in from a country the user has not visited in 90 days AND the IP is not in a known VPN range” — this captures most of the same outliers without requiring ML clustering. An hour with 500 sign-ins on a Tuesday at 10am may be normal (peak business hours), while 500 sign-ins on a Sunday at 3am is anomalous (same volume, different context).
Content Hub notebook templates
Microsoft provides notebook templates through Content Hub: “Guided Investigation - Process Alerts,” “Entity Explorer - Account,” “Guided Hunting - Anomalous Sign-In,” and others. These templates provide pre-built analysis workflows that you can execute against your workspace data without writing Python from scratch.
Using templates: Install the template from Content Hub. Open in your notebook environment. Configure the workspace connection. Run the cells sequentially. Review the visualisations and analysis output.
Customising templates: Modify the KQL queries to match your environment’s table names and column formats. Adjust thresholds and parameters. Add additional analysis cells for your specific hunting needs.
Notebook limitations
Skill requirement: Notebooks require Python proficiency. Most SOC analysts are KQL-proficient, not Python-proficient. Training investment is needed before notebooks become productive.
Performance: Notebooks query Sentinel data via the Azure Monitor API, which is slower than direct KQL execution in the Logs blade. Large datasets require longer load times.
Not real-time. Notebooks are batch analysis tools — not real-time monitoring. Use Livestream (subsection 10.6) for real-time hunting.
Reproducibility. Notebooks are interactive — each execution may produce different results depending on the data state. Document the parameters, time ranges, and data sources used in each notebook execution to ensure reproducibility.
Try it yourself
Navigate to Sentinel → Notebooks. If an AML workspace is available, launch a Content Hub notebook template (e.g., "Guided Hunting - Anomalous Sign-In"). Configure the workspace connection and run the template cells. Review the output visualisations. If no AML workspace is available, review the notebook templates in the Sentinel GitHub repository to understand the structure and analysis patterns — you can set up the environment later when the skill requirement is met.
What you should observe
The notebook connects to your workspace, queries data via MSTICPy, and produces analysis outputs: tables, charts, and anomaly highlights. The template handles the Python complexity — you provide the workspace credentials and review the results. For most organisations, notebooks are a Phase 2 capability — deployed after KQL-based hunting is well-established.
Knowledge check
Check your understanding
1. When should you use a notebook instead of standard KQL for hunting?