A network sensor written in Python that captures live packets, detects attacks with its own rule logic, adds a machine-learning layer on flow features, and sends alerts to Wazuh with MITRE ATT&CK mapping. Built and tested in an isolated VirtualBox lab (Kali attacker, Ubuntu victim and sensor, Wazuh server).
Lab only. Capture and attack only on a network you own and isolate. Never sniff or attack college, home or public networks.
- Reads packets from a live interface or a PCAP file (offline replay makes every test repeatable).
- Six rule detectors: port scan, stealth scan (NULL, FIN, Xmas), SYN flood, SSH brute force, ARP spoofing, DNS anomaly.
- An ML layer: builds flows with per-source window features and scores them with a Random Forest. It reports at least 30 s after an attack starts, so it is a second opinion, not the first line.
- One alert format (JSON lines) shared by every detector, with a cooldown against alert floods.
- Wazuh integration: the Wazuh agent reads the alert file, and nine custom rules map alerts to MITRE ATT&CK.
- A test harness that runs real attacks from Kali and measures detection and delay, plus a pytest suite.
flowchart LR
K["Kali: attacks (Nmap, Hydra, hping3)"] --> N["host-only lab network"]
N --> C["capture.py (Scapy)"]
C --> R["6 rule detectors"]
C --> M["ML scorer: flows + window features + Random Forest"]
R --> A["alerts.py: nids_alerts.json"]
M --> A
A --> W["Wazuh agent"]
W --> S["Wazuh manager: rules 100110-100122 + MITRE"]
S --> D["Wazuh dashboard"]
| Alert type | Idea | Wazuh rule | MITRE technique |
|---|---|---|---|
| port_scan | 15 or more distinct ports from one source in 10 s (SYN only) | 100110 | T1046 |
| stealth_scan | NULL, FIN or Xmas packets to 15 or more ports in 10 s | 100111 | T1046 |
| syn_flood | 100 or more SYNs in 5 s to few ports, almost no ACKs | 100112 | T1499.001 |
| ssh_bruteforce | 5 or more new connections to port 22 in 30 s | 100113 | T1110.001 |
| arp_spoof | an IP's MAC address changes between ARP replies | 100114 | T1557.002 |
| dns_anomaly | very long or high-entropy query names | 100115 | T1071.004 |
| ml_port_scan, ml_syn_flood, ml_ssh_bruteforce | Random Forest on flow features | 100120 to 100122 | same as above |
All thresholds are in nids/config.yaml. The window counters are updated incrementally, so a flood costs a constant amount of work per packet.
Live detection, real attacks from Kali (detection delay = seconds from attack start to the first alert; ranges are over repeated runs):
| Attack | Rule detector | ML layer |
|---|---|---|
Port scan (nmap -sS) |
detected, 0.4 to 7.5 s | detected, 36 to 42 s |
SYN flood (hping3 --flood) |
detected, 0.12 to 0.25 s | detected, 40 to 41 s |
| SSH brute force (Hydra) | detected, 0.36 to 0.62 s | detected, about 39 s |
| NULL scan | detected, 0.37 to 0.53 s | detected, about 36 s |
| FIN, Xmas scans | detected, 0.2 to 8.1 s | not detected (threshold effect) |
| ACK scan | no rule covers it | detected, 36 to 43 s |
| ARP spoofing, DNS anomaly | detected in live tests | not covered (not TCP flows) |
False alarms: 0 alerts in each 30-minute run of normal traffic (rules alone, and rules plus ML with the final model). Only one kind of normal traffic was generated (web requests, pings, DNS, short SSH logins).
Performance (Ubuntu VM, one CPU core):
| Test | Result |
|---|---|
| Replay of 101,900 packets, rules only | 18.05 s, about 5,600 packets/s, about 99 MB RAM, 99% of one core |
| Replay of a 102,580-packet flood, rules + ML | 24.9 s |
Scapy parses packets in Python, so one core is the limit. A C/libpcap capture core is future work.
Automated tests: 15 pytest tests (attack and clean case per detector, plus a speed regression test) pass in about 24 s.
- Features: duration, packet and byte counts, average packet size, mean inter-arrival time, SYN/ACK/FIN/RST counts, plus per-source window features (flows, distinct ports, flows per port in the last 10 s). Only flows addressed to the protected host are used.
- Training data: eight recorded rounds from the lab, balanced per scenario. Labels come from the scenario that was running.
- Validation: rounds were held out in pairs (each pair came from the same recording script). The decision threshold per attack type was chosen by a rule fixed in advance: the largest threshold that keeps recall at 98% or more.
- Result: 2 of 764 normal flows flagged (0.3%, 95% upper bound 0.9%), recall 0.986 to 1.000 per attack type. These numbers are optimistic because the thresholds were chosen on the same predictions, and 764 normal flows cannot show a rate below about 0.5%.
- Not reliable: Isolation Forest, Local Outlier Factor and One-Class SVM were tried on normal-only training and were not usable (the SVM flagged 20% of normal flows).
- Findings along the way: the first model scored 1.000, but only because every normal flow was larger than every attack flow; harder data cut that to 23 to 28% false alarms. The window features and a retrain with sparse sessions fixed that. A stateful filter (ignore replies on connections the victim opened) removed a test-setup artifact.
The agent (4.7.4, pinned to the manager's version) reads nids_alerts.json as JSON. The rules in integrations/nids_rules.xml match the type field and add the MITRE technique. They were checked with wazuh-analysisd -t and wazuh-logtest, and with live attacks.
- The ML layer alerts at least 30 s after an attack starts and covers only TCP/UDP flows.
- Attacks started within 10 s of each other can confuse the ML layer (observed once; a direct test has not been done). The rule detectors are not affected.
- Slow scans and scans from many source addresses can evade the per-source counters.
- ARP changes can be legitimate (for example a replaced router).
- The stateful filter misses connections that began before the sensor started.
- One cooldown rule: one alert per type and source every 30 s.
- All data comes from one small lab and my own scripts. The models may fail on other networks.
- Not tested against other tools such as Snort or Suricata, which are far faster and more complete.
nids/ capture engine, alert writer, config, flows, live ML scorer
nids/detectors/ the six rule detectors
ml/ feature generation, training and validation scripts, final model
tests/ pytest suite, attack harnesses, data recorders
integrations/ Wazuh rules and the agent snippet
results/ measured results (text and small CSV files)
docs/ notes
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# replay a capture
python -m nids.capture -r some.pcap
# live capture (needs sudo); add --ml for the flow model
sudo venv/bin/python -m nids.capture -i <interface> --ml
# tests
python -m pytest tests/test_detectors.py -v
- Edit the addresses in
nids/config.yaml(ml.victim_ip) and in thetests/scripts to match your lab. - The saved model is a scikit-learn pickle, so use a similar scikit-learn version.
- Never delete
nids_alerts.jsonwhile the Wazuh agent is reading it. Empty it with: > nids_alerts.json.
- C/libpcap capture core for speed.
- A fresh, independent test round for the ML layer, with more varied normal traffic.
- Window features that reset between attacks, to fix the mixing problem.
- Slow-scan and distributed-scan detection.
- Wazuh active response to block an attacker address.