Skip to content

Latest commit

Β 

History

113 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ•΅οΈ NetGuard – Hybrid Python/C NIDS & Observability Engine

A full end-to-end real-time Network Intrusion Detection & Prevention System (NIDS/NIPS).
Combines a Multi-threaded Python (Scapy) capture and analysis engine with a dual-layer DPI pipeline β€” an Aho-Corasick automaton for keyword signatures and a Native C Extension (bounds-checked, called live via ctypes) for injection-pattern signatures β€” plus Sliding-Window anomaly detection, OS Firewall Active Defense, and a fully code-managed monitoring stack (Dashboard as Code) on Docker (Grafana + Loki + Promtail).

Python Badge DPI Badge Scapy Badge Docker Badge Grafana Badge Loki Badge NIDS Badge IaC Badge


πŸ”Ž Overview & Architecture

NetGuard provides a complete solution for monitoring, analyzing, and responding to network security events across OSI layers 3, 4, and 7. The architecture is built on a continuous Producer-Consumer pipeline that separates low-level packet capture, real-time threat analysis, and structured observability shipping.

Architecture Highlight: NetGuard runs a dual-layer DPI pipeline on non-encrypted traffic: an Aho-Corasick automaton for Layer-7 keyword signatures, and a native C Extension (ctypes) for injection-pattern detection. Encrypted payloads (e.g., port 443) are automatically bypassed to optimize CPU usage and eliminate false positives.

flowchart LR
    %% Traffic Input
    NIC[πŸ“‘ NIC] -->|Raw Packets| SnifferThread[🐍 Sniffer Thread<br>Scapy store=0]

    %% Core Engine
    subgraph Engine [Python NIDS Core Engine]
        SnifferThread -->|Non-blocking Put| Queue[πŸ“¦ Queue<br>maxsize=20000]
        Queue -->|Get Packet| Worker[βš™οΈ Worker Pool]

        subgraph Detection [Detection & Defense]
            Worker -->|L3/L4 Window| Anomaly[πŸ›‘οΈ DoS / Scan Detector]
            Worker -->|L7 Payload| DPI[⚑🐍 Aho-Corasick + Native C DPI]
            Anomaly -->|Threshold Breach| Firewall[🧱 OS Firewall<br>netsh / iptables]
            Anomaly & DPI -->|Update| State[πŸ”’ State & Blacklist]
        end

        GC[🧹 GC Thread] -->|Clean Every 30s| State
    end

    %% Logging & Observability
    Worker -->|JSON Log| LogFile[πŸ“„ logs/network_security.json]
    LogFile --> Promtail[πŸ”„ Promtail Container]
    Promtail -->|Push| Loki[πŸ—„οΈ Loki DB]
    Loki --> Grafana[πŸ“Š Grafana Dashboard]
Loading

πŸ“Š Dashboard Preview

Live view of the NetGuard Security Overview Grafana dashboard, provisioned automatically from code.

NetGuard Live Security Log Stream & Events Distribution
Live JSON log stream with DNS query events, alongside real-time Security Events Distribution and Threat Timeline panels.

NetGuard DoS Detection & DNS Tunneling Alert
Active Defense in action β€” a DoS/SYN Flood attack triggers automatic IP isolation, alongside a DNS Tunneling detection alert (Shannon entropy-based).

NetGuard Security Events Breakdown & Top Suspicious Source IPs
Full event-type breakdown (Port Scans, Stealth Scans, DPI Alerts, DoS Attacks) with Top Suspicious Source IPs and Total Security Alerts panels.


⚑ Performance & Resilience Analysis

  • Memory Backpressure & Drop Policy β€” The engine utilizes a bounded Queue(maxsize=20000) combined with store=0 in Scapy to ensure zero in-memory packet buffering by the sniffer thread. Under high-throughput conditions, excess packets are dropped safely rather than causing Out-Of-Memory (OOM) fatal crashes.
  • Concurrency & C-Level Unlocking β€” Low-level packet capture executes within native socket primitives (C-level libpcap/WinPcap), releasing Python's Global Interpreter Lock (GIL) and allowing the background worker thread and garbage collector thread to execute processing tasks concurrently.
  • Thread Safety via a Shared Lock β€” Multi-threaded access to volatile state structures (syn_history, port_history, blacklist) is protected by a single shared threading.Lock to guarantee atomic read/write state transitions without data races.
  • Active Defense & OS Firewall Integration β€” Automatically triggers dynamic OS firewall mitigation rules (netsh advfirewall on Windows, iptables on Linux) upon identifying DoS/SYN Flood attacks. Commands execute asynchronously in detached daemon threads to keep processing queues zero-latency.
  • Deterministic Resource Cleanup (Garbage Collector) β€” Dormant IP records and expired blacklist entries are purged every 30 seconds by a background garbage collection thread in bounded $O(N)$ time, ensuring steady memory utilization under sustained traffic.

πŸ”¬ DPI Engines: Live Detection vs. Throughput Benchmark

NetGuard runs two DPI engines together in the live pipeline (main.py), each covering a different signature class, plus a separate standalone script for measuring the C engine's raw throughput in isolation:

Live: Aho-Corasick (_check_dpi_keywords) Live: Native C (_check_dpi_native) Standalone: benchmark_dpi.py
Technology pyahocorasick automaton, pure-Python substring fallback if unavailable Native C via ctypes, loaded once in NetworkGuardian.__init__ Same libdpi shared library, loaded independently
Where it runs Inside NetworkGuardian._check_dpi, on every captured packet Inside NetworkGuardian._check_dpi, on every captured packet Standalone script only β€” not imported by main.py
Signature set admin, password, etc/passwd, select * from ' OR '1'='1, UNION SELECT, <script>, ../../, etc/passwd, cmd.exe, ; whoami Same as the live C engine (fixed in dpi.c)
Purpose Real-time keyword/credential-leak alerting Real-time injection-pattern alerting (SQLi, XSS, path traversal, cmd injection) Measuring native-C vs. pure-Python throughput on synthetic payloads
Required? Recommended (pip install pyahocorasick); falls back to pure Python if missing Optional β€” compile with make -C c_src; NetGuard runs on Aho-Corasick alone if the shared library isn't present N/A β€” dev/benchmarking tool only

In short: the ~8x acceleration number below is a controlled, isolated measurement of the C engine's throughput on synthetic payloads β€” it describes the engine's capability, not a claim about end-to-end NIDS throughput (which also includes Scapy capture, queueing, and the Aho-Corasick pass). Build libdpi (make -C c_src) before running main.py to get the C-accelerated injection-signature detection live; without it, NetGuard still runs correctly on Aho-Corasick alone.


🏎️ DPI Native C Engine Performance

  • Batch Processing & Bounds-Checked Safety β€” By eliminating Python FFI execution overhead and passing contiguous memory blocks directly to native C primitives, payload scanning avoids GIL bottlenecks. The engine uses bounds-checked memchr/memcmp scanning to prevent Out-Of-Bounds reads and Null-Byte truncation issues on raw binary network traffic.
  • Micro-Benchmark Results (100,000 Packets Evaluation):
Engine Implementation Execution Time Throughput Acceleration
🐍 Python Pure 0.3134 sec 319,038 Packets/sec Baseline (1.0x)
⚑ Native C Extension (Safe) 0.0395 sec 2,532,165 Packets/sec 7.94x Faster

Run Benchmark Locally:

# Linux / macOS
make -C c_src
# Windows (GCC) β€” requires MinGW/MSYS2 installed
gcc -shared -O3 -march=native -o libdpi.dll c_src/dpi.c

python benchmark_dpi.py

πŸ“‚ Project Structure

NetGuard/
β”œβ”€β”€ assets/                          # Static Documentation Assets (Dashboard Screenshots)
β”‚   β”œβ”€β”€ dashboard_overview.png
β”‚   β”œβ”€β”€ dashboard_threat_detection.png
β”‚   └── dashboard_analytics.png
β”œβ”€β”€ c_src/                          # Low-Level Native C Extensions
β”‚   β”œβ”€β”€ dpi.c                       # Native C DPI Engine (Batch Engine)
β”‚   └── Makefile                    # C Compilation Setup
β”œβ”€β”€ grafana/
β”‚   β”œβ”€β”€ dashboards/                 # Standard JSON Dashboard (Git Version-Controlled)
β”‚   β”‚   └── dashboard-NetGuard Security Overview.json
β”‚   └── provisioning/                # Grafana Automated Provisioning Configs
β”‚       β”œβ”€β”€ dashboards/
β”‚       β”‚   └── dashboards.yml
β”‚       └── datasources/
β”‚           └── datasources.yml
β”œβ”€β”€ logs/                           # Runtime Log Directory (Ignored by Git, not tracked)
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ benchmark_dpi.py                # Native C vs Python DPI Micro-Benchmark
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ main.py                         # NIDS Core Engine (Thread-Safe & GC Refactored)
β”œβ”€β”€ promtail-config.yml
β”œβ”€β”€ requirements.txt
└── test_attack.py                  # Traffic Simulator

πŸš€ Core Features

Domain Feature Status Description Performance Indicator
πŸ“‘ Network Real-time L3-L7 Sniffing βœ… Real-time capture and analysis of IP, TCP, UDP, and DNS traffic while preventing memory overflow (store=0). Bounded memory capture (no packet buffering), O(1) enqueue
πŸ›‘οΈ Cyber Security Sliding-Window & Stealth Detection βœ… Detects DoS (SYN Flood), standard port scans, and Stealth Scans (NULL, FIN, XMAS) via moving time windows. O(1) queue operations
🧬 DNS Security DNS Tunneling Detection βœ… Shannon Entropy calculation & query length evaluation to catch exfiltration over DNS. O(N) entropy check
⚑ Active Defense Dynamic IP Isolation & OS Firewall βœ… Active mitigation mechanism that dynamically injects OS firewall rules (netsh / iptables) to block malicious hosts upon threshold breach. Non-blocking async execution, O(1) blacklist check
πŸ” DPI Engine Deep Packet Inspection βœ… Dual-layer L7 payload scanning: an Aho-Corasick automaton for keyword signatures, plus a Native C Extension (loaded live via ctypes) for bounds-checked injection-pattern signatures. Falls back to Aho-Corasick alone if the C library isn't compiled. O(N+M) string matching; native C path measured at ~8x throughput vs. pure Python in isolation
βš™οΈ Architecture Producer-Consumer & Thread-Safety βœ… Bounded Queue, threading.Lock primitives, and a dedicated background Garbage Collector thread to prevent memory leaks. Bounded queue, backpressure-safe
πŸ“Š Observability & IaC Dashboard as Code (Grafana + Loki) βœ… A single all-in-one "NetGuard Security Overview" dashboard in standard JSON format, automatically loaded on container startup via Provisioning files. Instant provisioning on boot
πŸ“ Logging Structured JSON Dual-Stream βœ… Colorized console output alongside structured JSON log writes (logs/network_security.json), tailored for collection by Promtail. Low-overhead async writes
πŸ§ͺ Testing Traffic Attack Simulator βœ… Simulation script (test_attack.py) that generates synthetic attack traffic to validate detection mechanisms. Configurable synthetic load

πŸ› οΈ Technologies & Architectural Highlights

  • Python & Scapy β€” Raw-socket-level packet capture, protocol parsing, and deep payload-level inspection (DPI).
  • Aho-Corasick DPI (live) β€” Multi-pattern automaton (pyahocorasick) used inside main.py for real-time keyword/signature matching against packet payloads, with a pure-Python substring fallback when the library is unavailable.
  • Native C Extension (ctypes, live) β€” Batch-safe, memory-bounds-checked C DPI engine loaded via ctypes in NetworkGuardian.__init__ and called on every packet's payload in _check_dpi_native alongside the Aho-Corasick pass, delivering ~8x (7.94x measured) throughput over pure Python for its signature set. The same shared library is also loaded independently by benchmark_dpi.py for isolated throughput measurement. Requires make -C c_src first; NetGuard degrades gracefully to Aho-Corasick-only if the compiled library isn't present.
  • Producer-Consumer Architecture β€” Full separation between packet capture and analysis via queue.Queue(maxsize=20000), preventing packet loss under load.
  • Thread-Safety & Active Defense β€” Whitelist/Blacklist state management guarded by threading.Lock to prevent data races, integrated with background dynamic OS Firewall rule injection (netsh / iptables).
  • Background Garbage Collector β€” A dedicated background thread that cleans up stale data structures (Sliding Window History & Blacklist) from memory every 30 seconds, synchronously and thread-safely, ensuring zero memory leaks from dormant IP addresses.
  • Promtail & Grafana Loki β€” Shipping of structured JSON logs from the local logs directory and indexing them in Loki.
  • Dashboards as Code (IaC) β€” The "NetGuard Security Overview" dashboard is version-controlled in Git under grafana/dashboards/, automatically loaded into Grafana on container startup.
  • Docker Compose Stack β€” One-click deployment of the entire observability infrastructure.

🧬 Algorithmic Detection: Shannon Entropy for DNS Tunneling

NetGuard identifies covert communications and data exfiltration over DNS by analyzing the randomness (entropy) of domain query strings.

Standard domain names exhibit predictable natural-language patterns, whereas encrypted/encoded data streams (e.g., Base64/Hex DNS Tunneling) produce significantly higher entropy scores.

  • Mathematical Model: Calculates Shannon Entropy $H(X)$ over the unique byte/character distribution of each query string: $$H(X) = -\sum_{i=1}^{n} P(x_i) \log_2 P(x_i)$$
  • Detection Threshold: Queries exceeding the configured threshold ($H(X) &gt; 4.3$) alongside anomalous string lengths trigger an immediate DNS Tunneling Alert and log entry.

πŸ“‹ Prerequisites

  • Git β€” Required to clone the repository.
  • Docker & Docker Compose β€” For running Loki, Promtail, and Grafana.
  • Python 3.10+ β€” Required for running the NIDS engine and test suite.
  • GCC / Make β€” Required to compile the native C DPI engine shared object (libdpi.so on Linux, libdpi.dylib on macOS, libdpi.dll on Windows). Recommended before running main.py, since it's now loaded live for injection-signature detection; also used to run benchmark_dpi.py. NetGuard still runs correctly on Aho-Corasick alone if you skip this.
  • pyahocorasick β€” Optional but recommended for the live DPI engine's full performance; a pure-Python fallback is used automatically if it's not installed.
  • Administrator / Root Privileges β€” Required to capture raw socket traffic via Scapy and inject OS Firewall blocking rules (or use the least-privilege setcap option below on Linux).
  • Npcap (Windows only) β€” Required for Scapy to capture raw packets on Windows network adapters.

πŸ“ JSON Log Structure (Structured Logging)

{
  "timestamp": "2026-08-06T10:30:15.123456+03:00",
  "level": "WARNING",
  "message": "[PORT SCAN DETECTED] Host 10.0.0.4 scanned 18 unique ports",
  "logger": "NetworkGuardian",
  "src_ip": "10.0.0.4",
  "event_type": "PORT_SCAN",
  "details": "18 ports scanned"
}

βš™οΈ Installation & Quick Start

# 1. Clone the repository
git clone https://github.com/RazEini/NetGuard.git
cd NetGuard

# 2. Environment Setup
cp .env.example .env  # Set your Grafana password in .env

# 3. Start Observability Stack (Grafana, Loki, Promtail)
# Grafana will automatically provision all dashboards from grafana/dashboards/
docker compose up -d

# 4. Setup Python Environment
python -m venv .venv
.\.venv\Scripts\activate     # On Windows
source .venv/bin/activate    # On Linux/Mac
pip install -r requirements.txt

# 5. Compile the Native C DPI Engine (recommended)
# main.py auto-detects and loads this at startup for live injection-signature
# detection; if you skip this step NetGuard still runs fine on Aho-Corasick alone.
make -C c_src                                                # Linux / macOS
gcc -shared -O3 -march=native -o libdpi.dll c_src/dpi.c      # Windows β€” requires MinGW/MSYS2 installed

# Optional: run the standalone throughput benchmark (same shared library, isolated measurement)
python benchmark_dpi.py                                      # Verify ~8x memory-safe speedup

6a. Run NIDS Engine β€” Linux / macOS

# Principle of Least Privilege - grant raw socket capability without full sudo:
sudo setcap cap_net_raw,cap_net_admin=eip $(readlink -f .venv/bin/python)
.venv/bin/python main.py

# Or run directly with root:
sudo .venv/bin/python main.py

6b. Run NIDS Engine β€” Windows

# Run PowerShell / CMD as Administrator:
python main.py

7. Run Attack Simulator (in a separate terminal)

# Automatically targets local active IP:
python test_attack.py

# Or target a specific IP address explicitly:
python test_attack.py <TARGET_IP>

πŸ“Š Accessing Grafana: Open your browser to http://localhost:3000 (username: admin, password set in .env). All dashboards will already be loaded and ready to use!


πŸ“„ License

This project is distributed under the MIT license – free to use and modify for educational and research purposes.


πŸ‘¨β€πŸ’» Raz Eini (2026)

About

A hybrid Python/C Network Intrusion Detection & Prevention System (NIDS/NIPS). Features multi-threaded L2-L7 analysis, C-accelerated DPI, dynamic IP isolation, and an integrated Docker observability stack (Grafana, Loki, Promtail) with Dashboards as Code.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages