DEV Community

SimonR
SimonR

Posted on

Building Your Own: A Practical Guide to Datasets, Pipelines and Evaluation for an AI Intrusion Detector

Search GitHub for intrusion detection projects and you will find hundreds of repositories, many of them student or research projects reporting accuracy above 99 percent. Impressive numbers, but they raise an obvious question: if it is that easy, why is detection still hard? The answer lies in what happens between a notebook and a live network. This guide walks through the practical stages of building an AI-based network intrusion detection system, with attention to the pitfalls that benchmark scores hide.

Step 1: Define the problem precisely

Before touching data, decide what you want the system to do.

  • Detect attacks in general (binary), or classify them by type (multi-class)?
  • Operate on flows, packets, or aggregated windows of activity?
  • Run in real time, or analyze captured traffic afterwards?
  • Which environment: enterprise LAN, cloud workload, IoT segment?

Clear answers prevent a common failure: building a model that scores well on a task nobody actually needs.

Step 2: Choose and understand your data

Public datasets are the usual starting point. Some widely used ones include:

  • NSL-KDD: An improved version of the older KDD Cup 1999 data. Still used in teaching, but dated and unrepresentative of modern traffic.
  • UNSW-NB15: Contains a mix of contemporary normal activity and synthetic attacks across several categories.
  • CIC-IDS2017 and CSE-CIC-IDS2018: Realistic flow data created at the Canadian Institute for Cybersecurity, covering attacks like brute force, DoS, web attacks, infiltration and botnets.
  • ToN-IoT, Bot-IoT and IoT-23: Datasets focused on IoT environments.

Known data issues worth checking

Researchers have documented flaws in popular datasets, including duplicate records, mislabeled flows, and artifacts that make classification easier than it should be. Always inspect for:

  • duplicates that leak between training and test sets
  • features that trivially reveal the label
  • severe class imbalance
  • unrealistic traffic mixes
  • attack data captured in a single short session, which encourages memorizing quirks

If possible, supplement public data with captures from your own environment, even if unlabeled, to test how the model behaves on traffic it has never seen.

Step 3: Collect and transform traffic into features

If you generate your own data, the pipeline usually runs like this:

  1. Capture. Use packet capture tools or mirrored traffic.
  2. Flow extraction. Tools such as CICFlowMeter, Zeek or NetFlow-style exporters aggregate packets into flows with statistics.
  3. Feature selection. Choose measures such as duration, packet and byte counts, inter-arrival times, flag counts and protocol indicators.
  4. Cleaning. Handle missing values, infinite values from division by zero, and inconsistent units.
  5. Encoding and scaling. Convert categorical fields, and normalize numeric ranges as required by the model.

A tip from experience: avoid features that identify a specific machine, such as raw IP addresses or timestamps, unless you have a deliberate reason. Models can latch onto them and fail when the network changes.

Step 4: Split data honestly

How you split data influences your results more than most algorithm choices.

  • Random splitting can leak near-identical records across training and test sets, inflating scores.
  • Time-based splitting, training on earlier traffic and testing on later, better simulates deployment.
  • Scenario- or attack-based splitting, holding out entire attack families for testing, shows how well the model generalizes to new behavior.

If a model drops from 99 percent to 70 percent when tested on a held-out attack family or a different dataset, that gap is the real story.

Step 5: Start with a baseline

Begin with a simple model such as a random forest or gradient-boosted trees. Record its performance and training time. Only then try deeper architectures like CNNs, LSTMs or autoencoders. Having a baseline protects against spending weeks on complex models that add nothing.

Step 6: Handle class imbalance

Real networks are overwhelmingly benign. Options include:

  • class weighting during training
  • oversampling minority classes (for example, SMOTE) or undersampling the majority
  • anomaly-detection formulations that train only on normal data
  • choosing metrics that reflect imbalance

Be careful with resampling: apply it only to the training set, never to validation or test data.

Step 7: Evaluate with the right metrics

Accuracy alone can be nearly meaningless. If 97 percent of records are benign, a model that always predicts "benign" scores 97 percent accuracy and detects nothing. Prefer:

  • Precision and recall per class
  • F1 score
  • False-positive rate, which determines analyst workload
  • ROC and precision-recall curves
  • Confusion matrices to see which attacks are mistaken for which
  • Detection latency and throughput for operational feasibility

A useful exercise is converting the false-positive rate into alerts per day at your expected traffic volume. A 1 percent false-positive rate sounds small until it generates thousands of daily alerts.

Step 8: Add explainability and testing

Apply SHAP or LIME to see what drives predictions and to catch shortcuts. Test robustness by adding noise, perturbing features, or simulating evasion attempts. Check behavior on edge cases: very short flows, unusual protocols, fragmented traffic.

Step 9: Plan deployment

Moving from notebook to system involves decisions beyond the model:

  • Inference path. Stream processing with tools such as Kafka or lightweight services, versus batch scoring.
  • Packaging. Containerize the model and feature pipeline so training and production use identical transformations.
  • Integration. Send alerts to a SIEM, ticketing system or messaging channel in a structured format.
  • Monitoring. Track input distribution, score distribution, alert volume and analyst feedback.
  • Retraining. Define triggers and a process for updating models, with versioning and rollback.
  • Safety. Start in alert-only mode before enabling any automated blocking.

Common mistakes seen in projects

  • Reporting results on a random split of a single dataset and claiming general effectiveness
  • Leaving duplicate or leaking records in the data
  • Ignoring the cost of false positives
  • Using features unavailable in real-time deployment, such as flow statistics that require the flow to finish
  • Forgetting that models degrade as traffic changes
  • Treating the model as the product rather than one part of a workflow

Build or buy?

Building teaches a team a great deal and allows tight customization, but maintenance, data curation and tuning are continuous efforts. Commercial platforms from providers like Futurism Security package much of this infrastructure, while open-source tooling suits teams with strong in-house expertise. Many organizations combine the two, experimenting with open components while relying on a supported product for production monitoring.

Key takeaways

  • Define the task and environment before selecting any model.
  • Audit public datasets for duplicates, leakage and unrealistic traffic.
  • Split by time or attack family to estimate real-world generalization.
  • Begin with strong baselines; use metrics suited to imbalanced data.
  • Production readiness depends on pipelines, monitoring and retraining as much as on accuracy.

Top comments (0)