Skip to content

simoabid/NIDS-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ NIDS β€” Network Intrusion Detection System

An intelligent, AI-powered Network Intrusion Detection System that analyzes network traffic in real time using Machine Learning/Deep Learning to distinguish legitimate connections from cyber attacks (DoS, Port Scanning, etc.).


πŸ“ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Network / Internet                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Raw packets / .pcap files
                      β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Zeek / Suricata     β”‚  ← NIDS Engine (packet inspection)
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Extracted metadata (IP, port, protocol, size)
                      β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚     Redis Streams     β”‚  ← Async message pipeline
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Feature vectors
                      β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   AI Service          β”‚  ← Python microservice (FastAPI)
          β”‚   (Scikit-Learn /     β”‚     Inference + confidence score
          β”‚    TensorFlow)        β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Prediction (Normal / DoS / Port Scan)
                      β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Backend API         β”‚  ← Node.js / Express (TypeScript)
          β”‚   REST + WebSockets   β”‚     Auth (JWT/RBAC), Audit layer
          β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚              β”‚
          REST (HTTPS)    Socket.io push
                 β”‚              β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   MongoDB   β”‚  β”‚  Frontend Dashboard    β”‚
          β”‚  (Alerts +  β”‚  β”‚  React.js / TS /       β”‚
          β”‚   History)  β”‚  β”‚  TailwindCSS           β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β–²
                          Nginx reverse proxy (port 80/443)

Data flow in one sentence: Zeek/Suricata captures packets β†’ metadata is pushed to Redis Streams β†’ AI Service runs inference β†’ Backend persists + pushes alerts via WebSockets β†’ React Dashboard displays real-time alerts in < 500 ms.


πŸ“ Folder Structure

nids-project/
β”‚
β”œβ”€β”€ ai-service/              # Python microservice β€” model training & inference
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ main.py          # FastAPI application entry point
β”‚   β”‚   β”œβ”€β”€ predictor.py     # Model loading + prediction logic
β”‚   β”‚   β”œβ”€β”€ preprocessor.py  # Feature extraction & normalization
β”‚   β”‚   └── schemas.py       # Pydantic request/response schemas
β”‚   β”œβ”€β”€ data/                # Training datasets (.csv) β€” gitignored if large
β”‚   β”œβ”€β”€ model/               # Serialized model files (.pkl, .h5) β€” gitignored
β”‚   β”œβ”€β”€ notebooks/           # Jupyter notebooks (EDA, training, evaluation)
β”‚   β”œβ”€β”€ tests/               # Unit tests for inference pipeline
β”‚   β”œβ”€β”€ requirements.txt     # Python dependencies
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ backend/                 # Node.js / Express API (TypeScript)
β”‚   └── src/
β”‚       β”œβ”€β”€ config/          # DB connection, environment config
β”‚       β”œβ”€β”€ controllers/     # Route handler logic
β”‚       β”œβ”€β”€ middleware/       # JWT auth, RBAC, error handling, audit logger
β”‚       β”œβ”€β”€ models/          # Mongoose schemas (Alert, User, AuditLog)
β”‚       β”œβ”€β”€ routes/          # Express route definitions
β”‚       └── services/
β”‚           β”œβ”€β”€ socketService.ts   # Socket.io β€” push alerts to frontend
β”‚           β”œβ”€β”€ redisService.ts    # Redis Streams consumer
β”‚           └── auditService.ts    # Audit trail for alerts + admin actions
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ frontend/                # React.js dashboard (TypeScript + TailwindCSS)
β”‚   └── src/
β”‚       β”œβ”€β”€ components/      # Reusable UI: AlertBanner, TrafficChart, etc.
β”‚       β”œβ”€β”€ hooks/           # Custom hooks: useSocket, useAlerts
β”‚       β”œβ”€β”€ pages/           # Login.tsx, Dashboard.tsx
β”‚       β”œβ”€β”€ services/        # Axios REST client + Socket.io setup
β”‚       └── types/           # Shared TypeScript interfaces
β”‚   β”œβ”€β”€ tailwind.config.ts
β”‚   β”œβ”€β”€ vite.config.ts
β”‚   β”œβ”€β”€ package.json
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ infra/                   # Infrastructure & DevOps configuration
β”‚   β”œβ”€β”€ nginx/
β”‚   β”‚   └── nginx.conf       # Reverse proxy routing rules
β”‚   β”œβ”€β”€ prometheus/
β”‚   β”‚   └── prometheus.yml   # Scrape configs for backend + ai-service metrics
β”‚   └── grafana/
β”‚       └── dashboards/      # Pre-built Grafana dashboard JSON exports
β”‚
β”œβ”€β”€ docs/                    # Project specifications (PDFs)
β”‚   β”œβ”€β”€ Cahier_des_charges_NIDS.pdf
β”‚   β”œβ”€β”€ Cahier_des_Charges_Dashboard_NIDS.pdf
β”‚   └── Cahier_des_charges_IA.pdf
β”‚
β”œβ”€β”€ docker-compose.yml       # Orchestrates all 6 services
β”œβ”€β”€ .env.example             # Environment variable template
β”œβ”€β”€ .gitignore
└── README.md                # ← you are here

🌐 Service Ports

Service URL Description
Frontend http://localhost:3000 React dashboard (Vite dev server)
Backend API http://localhost:5000 Express REST API + Socket.io
AI Service http://localhost:8000 FastAPI inference microservice
MongoDB localhost:27017 Alert history + user database
Redis localhost:6379 Streaming pipeline (Redis Streams)
Grafana http://localhost:3001 Monitoring dashboards
Prometheus http://localhost:9090 Metrics scraping & storage
Nginx http://localhost:80 Reverse proxy (production)

Note: In development, each service runs independently on its port. In production (docker-compose up), Nginx proxies all traffic through port 80/443.


πŸš€ Getting Started

Prerequisites


1. Clone & Configure

git clone https://github.com/simoabid/NIDS-Project.git
cd NIDS-Project

# Copy environment template and fill in your values
cp .env.example .env

Edit .env with your settings (JWT secret, MongoDB URI, etc.).


2. Run Everything with Docker Compose

# Build and start all services in detached mode
docker-compose up --build -d

# View aggregated logs
docker-compose logs -f

# Stop all services
docker-compose down

The dashboard will be available at http://localhost:3000.


3. Run Services Individually (Development)

Frontend

cd frontend
npm install
npm run dev          # http://localhost:3000

Backend API

cd backend
npm install
npm run dev          # http://localhost:5000 (ts-node-dev with hot reload)

AI Service

cd ai-service
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uvicorn src.main:app --reload --port 8000

Redis (local)

docker run -d -p 6379:6379 redis:alpine

MongoDB (local)

docker run -d -p 27017:27017 mongo:7

4. Train the AI Model

cd ai-service
source .venv/bin/activate

# Place your dataset (e.g., NSL-KDD, CICIDS2017) in ai-service/data/
# Then run the training notebook or script:
jupyter notebook notebooks/01_train_model.ipynb
# or
python src/train.py --dataset data/cicids2017.csv --output model/nids_model.pkl

5. Test with a .pcap File

# Using Zeek to process an offline capture:
zeek -r path/to/capture.pcap

# Using Suricata:
suricata -r path/to/capture.pcap -l /tmp/suricata-output/

πŸ” Authentication

The system uses JWT + RBAC (Role-Based Access Control).

Role Permissions
admin Full access: start/stop capture, view all alerts, manage users
viewer Read-only: view dashboard, alerts, and statistics

Tokens are issued on login and must be passed in the Authorization: Bearer <token> header for all protected API endpoints.


πŸ“‘ Key API Endpoints (Backend)

Method Endpoint Auth Description
POST /api/auth/login ❌ Authenticate and receive JWT
GET /api/alerts βœ… Paginated alert history
GET /api/alerts/:id βœ… Alert detail (IP, type, timestamp)
GET /api/stats βœ… Global traffic statistics
POST /api/capture/start βœ… admin Start real-time network capture
POST /api/capture/stop βœ… admin Stop capture
GET /api/audit βœ… admin Audit log of all admin actions

WebSocket events (Socket.io):

  • alert:new β€” emitted when the AI detects an attack; payload: { ip, type, confidence, timestamp }
  • stats:update β€” periodic traffic statistics update

πŸ§ͺ Testing

# Backend unit + integration tests
cd backend && npm test

# AI Service tests
cd ai-service && pytest tests/

# Frontend component tests
cd frontend && npm test

πŸ“Š Monitoring

Once the stack is running:

  • Grafana β†’ http://localhost:3001 β€” pre-built dashboards for detection rate, alert volume, and system health
  • Prometheus β†’ http://localhost:9090 β€” raw metrics from backend and AI service

πŸ—οΈ Tech Stack Summary

Layer Technology
Frontend React.js, TypeScript, TailwindCSS, Vite, Socket.io-client
Backend Node.js, Express.js, TypeScript, Socket.io, Mongoose
AI Service Python, FastAPI, Scikit-Learn / TensorFlow, Pydantic
Database MongoDB
Streaming Redis Streams
NIDS Engine Zeek or Suricata
Auth JWT, RBAC
Infrastructure Docker, Docker Compose, Nginx
Monitoring Prometheus, Grafana
Test Env VirtualBox / GNS3, Wireshark

πŸ“„ Specifications

Full project requirements are in docs/:

About

An intelligent, AI-powered Network Intrusion Detection System that analyzes network traffic in real time using Machine Learning/Deep Learning to distinguish legitimate connections from cyber attacks.

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors