All posts

Python

Building a network anomaly detector with Scapy and scikit-learn

By Vivek Bhange2 min read

One of my projects was a small anomaly detection system for network traffic: capture live packets, turn them into features a model can use, flag what looks unusual, and show it on a dashboard. Here’s how the pipeline fits together.

1. Packets into flows

Single packets don’t say much. Grouping them into flows (source, destination, ports, protocol) gives you something with shape. Scapy keeps the capture side short:

from collections import defaultdict
import time
from scapy.all import sniff, IP, TCP

flows = defaultdict(lambda: {"pkts": 0, "bytes": 0, "syn": 0,
                             "fin": 0, "rst": 0, "first": None, "last": None})

def on_packet(p):
    if IP not in p:
        return
    key = (p[IP].src, p[IP].dst,
           getattr(p, "sport", 0), getattr(p, "dport", 0), p[IP].proto)
    f = flows[key]
    now = time.time()
    f["first"] = f["first"] or now
    f["last"] = now
    f["pkts"] += 1
    f["bytes"] += len(p)
    if TCP in p:
        flags = int(p[TCP].flags)
        f["syn"] += bool(flags & 0x02)
        f["fin"] += bool(flags & 0x01)
        f["rst"] += bool(flags & 0x04)

sniff(prn=on_packet, store=False, timeout=60)

2. Features that mean something

From each flow I derived duration, packet and byte counts, mean packet size, packets per second, and the share of SYN, FIN and RST flags. Those cover a lot of common oddities: a scan shows up as many short flows heavy on SYNs, a flood as a huge packet rate, a broken connection as a burst of resets.

Before training, the data needed cleaning: dropping zero-duration flows (they break the rate features), capping extreme outliers, and making sure the same flow wasn’t counted twice across capture windows.

3. The model

A tree-based classifier from scikit-learn worked well here: it copes with features on very different scales, and it’s easy to see which features drive a decision.

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42)

clf = RandomForestClassifier(n_estimators=200, class_weight="balanced",
                             random_state=42)
clf.fit(X_train, y_train)
print(classification_report(y_test, clf.predict(X_test)))

4. Keeping false positives down

Anomalies are rare, so a model can look accurate while being useless, and a detector that cries wolf gets ignored. Two things helped:

  • Look at precision and recall, not accuracy. Class weights make the model care about the rare class.
  • Tune the threshold. Instead of the default 0.5, pick the probability cut-off from the precision-recall curve that gives an alert rate people will actually read.

5. Making it visible

The last step was a live dashboard with logging, so flagged flows show up alongside the features that triggered them. A detector nobody looks at doesn’t detect anything.

What I’d do differently

Start with a simple rule-based baseline (thresholds on packet rate and flag ratios) before reaching for a model. It gives you something to beat and a quick way to label data.

Enjoyed this?

Written by Vivek Bhange, Full-Stack AI Engineer. See his work or work with me.

Keep reading

Easter egg hunt

This site hides 16 surprises. Found ones stay found on this device. On a phone, type secret words into the command bar (the >_ button).

  1. RickrolledNot found yet

    Show hint

    My photo has a secret. Double-click it (on a phone, press and hold).

  2. Impatient clickerNot found yet

    Show hint

    Click anywhere, fast, about 25 times in a row.

  3. Agent of changeNot found yet

    Show hint

    Type the word that means "make it different".

  4. Gravity checkNot found yet

    Show hint

    Type my first name.

  5. Secret wordNot found yet

    Show hint

    Type something friendly, like "chai" or "namaste".

  6. HackerNot found yet

    Show hint

    Type what hackers do (four letters).

  7. The snapNot found yet

    Show hint

    Type the name of a purple villain who snaps.

  8. Sharp eyesNot found yet

    Show hint

    Something almost invisible floats on the screen. Find it and click it (desktop).

  9. Root accessNot found yet

    Show hint

    Open the command bar (press /, or tap >_ on a phone), then try to become root.

  10. Duck hunterNot found yet

    Show hint

    A rubber duck is hiding on the 3D island.

  11. World travellerNot found yet

    Show hint

    Visit all four views.

  12. Night owlNot found yet

    Show hint

    Come back between midnight and 4 a.m.

  13. Arcade regularNot found yet

    Show hint

    Play every quick game in the Arcade (the ones that open on the Arcade page).

  14. Chain reactorNot found yet

    Show hint

    Set off 30 bursts with a single move in Critical Mass.

  15. Last one standingNot found yet

    Show hint

    Win a match of Drop Zone.

  16. Code whizNot found yet

    Show hint

    Get 10 answers right in a row in Code Clash.

zsh — visitor@vivek: ~

Ask Vivek

Ask me anything. My work, how I run projects, or whether I'm a fit.

AI answers from this site's content. It can make mistakes.