Back to the courtyard

Cybersecurity / Python · Scikit-learn

ML-based intrusion detection

An Isolation Forest learns a baseline from synthetic network windows. Move the alert threshold and see what it catches or misses.

Isolation Forest / threshold explorerSynthetic holdout · 200 windows

Model output

Threshold explorer

The Scikit-learn model is already trained. Changing this cutoff changes the review queue, without retraining it.

More alertsFewer alerts
0.40.50.60.70.8

Higher scores mean less like the baseline. They are not attack probabilities.

40Attack scenarios flagged
18Benign windows flagged
20Attack scenarios missed
122Benign windows below cutoff

Example windows

window-0805Flagged for review

Synthetic port probe

Connections
170
Destination ports
62
Destination hosts
1
Mean bytes sent
51
Failed logins
1

Anomaly score 0.6596 > 0.6014 cutoff

This generated attack scenario enters the review queue. The scenario label was withheld from the model.

600 training windows · 200 calibration windows · 200 held-out windows. All records are generated. Scores come from the downloadable Python project.

Implementation notes

The baseline decides what looks unusual

Isolation Forest fits 600 benign training windows. A separate set of 200 windows sets the cutoff before the model sees the holdout. Scenario labels stay out of the features.

A benign backup can look unusual when Scikit-learn has never seen that traffic pattern. Low-rate probes can blend into routine traffic. Both cases are included in the fixture.

The Python example starts with aggregated flow data. Failed-login counts require an authentication-log join. Anomaly scores alone cannot establish zero-day detection.

Scikit-learn: IsolationForest

Run locally

Source and example data

The Python project includes the example inputs and tests. Open its README for prerequisites and the complete workflow.

Download project source
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python generate_fixture.py
python ids.py train
python ids.py evaluate
Read the setup notes
Back to the courtyardAll work