Skip to content

PRATHAM777P/Deep-Packet-Inspection-DPI-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

12 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

DPI Engine Banner



A Deep Packet Inspection engine written in C++ that analyzes network traffic from PCAP files, identifies applications using packet metadata and TLS SNI inspection, and applies filtering rules to block specific applications, domains, or IP addresses.

The system parses multiple network protocol layers, tracks connections using the Five-Tuple, and processes packets using a multi-threaded architecture to improve performance and scalability.


๐Ÿ“Œ Overview โ€ข ๐Ÿš€ Features โ€ข ๐Ÿ— Architecture โ€ข ๐Ÿ“‚ Structure โ€ข ๐Ÿ›  Build โ€ข โ–ถ๏ธ Run โ€ข ๐Ÿ”ฎ Roadmap


๐Ÿ“Œ Project Overview

This project demonstrates how modern network monitoring and security systems analyze traffic beyond traditional packet filtering.

Captured network traffic is provided as a PCAP file, which is processed by the DPI engine. The engine analyzes each packet, applies filtering rules, and writes allowed traffic into a new filtered PCAP output file.

โš™๏ธ Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Input PCAP    โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚     DPI Engine       โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚  Filtered PCAP Out   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”„ Core Processing Steps

Step Description
๐Ÿ“ฆ Packet Parsing Parse raw bytes across protocol layers
๐Ÿ”— Flow Identification Group packets into network flows
๐Ÿง  Application Detection Identify apps via metadata & SNI
๐Ÿšซ Rule-based Filtering Apply block/allow rules
๐Ÿ“Š Statistics Generation Output traffic analysis report

๐Ÿš€ Key Features

๐Ÿ“ฆ Packet Parsing

The engine parses multiple network protocol layers:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Ethernet   โ”‚  โ† MAC addresses, EtherType
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚    IPv4      โ”‚  โ† Source/Destination IPs, TTL
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  TCP / UDP   โ”‚  โ† Ports, flags, sequence numbers
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   Payload    โ”‚  โ† Application-layer data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

This enables extraction of critical network metadata for deeper inspection.


๐Ÿ”— Flow Identification

Each connection is tracked using the Five-Tuple:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           Five-Tuple Flow ID            โ”‚
โ”‚                                         โ”‚
โ”‚  ๐ŸŒ Source IP        โ†’  192.168.x.x     โ”‚
โ”‚  ๐ŸŒ Destination IP   โ†’  142.250.x.x     โ”‚
โ”‚  ๐Ÿ”Œ Source Port      โ†’  54321           โ”‚
โ”‚  ๐Ÿ”Œ Destination Port โ†’  443             โ”‚
โ”‚  ๐Ÿ“ก Protocol         โ†’  TCP             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Packets sharing the same Five-Tuple belong to the same network flow, allowing stateful traffic analysis.


๐Ÿ” Deep Packet Inspection

The engine inspects packet payloads to detect applications.

For HTTPS traffic, it extracts the Server Name Indication (SNI) from the TLS handshake โ€” revealing the domain name before encryption begins.

TLS ClientHello
  โ””โ”€โ”€ Extension: server_name
        โ””โ”€โ”€ SNI: www.youtube.com
                  โ”‚
                  โ–ผ
        Detected App: YouTube โœ…

๐Ÿšซ Traffic Blocking

The system supports rule-based traffic filtering, allowing blocking by:

Rule Type Example
๐Ÿงพ IP Address --block-ip 203.0.113.5
๐Ÿ“ฑ Application --block-app YouTube
๐ŸŒ Domain Name --block-domain facebook

Packets matching blocking rules are dropped and not written to the output file.


๐Ÿ— Architecture

The project includes two implementations.


๐Ÿงฉ Single-Threaded Version

A simple implementation where packets are processed sequentially.

Useful for learning packet inspection and debugging packet processing.

PCAP Reader โ”€โ”€โ–ถ Packet Parser โ”€โ”€โ–ถ Classifier โ”€โ”€โ–ถ Rule Engine โ”€โ”€โ–ถ Output

โšก Multi-Threaded Version

A high-performance implementation that processes packets in parallel.

                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚          Parallel Processing Pool         โ”‚
                โ”‚                                           โ”‚
PCAP Reader โ”€โ”€โ–ถ โ”‚  Load Balancer โ”€โ”€โ–ถ โš™๏ธ Worker  โ”€โ”€โ–ถ ...    โ”‚โ”€โ”€โ–ถ Output Writer
                โ”‚                 โ”€โ”€โ–ถ โš™๏ธ Worker  โ”€โ”€โ–ถ ...    โ”‚
                โ”‚                 โ”€โ”€โ–ถ โš™๏ธ Worker  โ”€โ”€โ–ถ ...    โ”‚
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Thread Role
๐Ÿ“ฅ Reader Reads packets from PCAP file
โš–๏ธ Load Balancer Distributes packets across workers
โš™๏ธ Worker (Fast Path) Inspects and classifies packets
๐Ÿ’พ Output Writer Writes allowed packets to output

This architecture allows the system to scale with available CPU cores.


๐Ÿ“‚ Project Structure

deep-packet-inspector/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ include/
โ”‚   โ””โ”€โ”€ Header files for packet parsing, flow tracking, and DPI logic
โ”‚       โ”œโ”€โ”€ connection_tracker.h
โ”‚       โ”œโ”€โ”€ dpi_engine.h
โ”‚       โ”œโ”€โ”€ fast_path.h
โ”‚       โ”œโ”€โ”€ load_balancer.h
โ”‚       โ”œโ”€โ”€ packet_parser.h
โ”‚       โ”œโ”€โ”€ pcap_reader.h
โ”‚       โ”œโ”€โ”€ platform.h
โ”‚       โ”œโ”€โ”€ rule_manager.h
โ”‚       โ”œโ”€โ”€ sni_extractor.h
โ”‚       โ”œโ”€โ”€ thread_safe_queue.h
โ”‚       โ””โ”€โ”€ types.h
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ src/
โ”‚   โ””โ”€โ”€ Core implementation of the DPI engine
โ”‚       โ”œโ”€โ”€ main.cpp / main_dpi.cpp / main_working.cpp
โ”‚       โ”œโ”€โ”€ dpi_engine.cpp / dpi_mt.cpp
โ”‚       โ”œโ”€โ”€ pcap_reader.cpp
โ”‚       โ”œโ”€โ”€ packet_parser.cpp
โ”‚       โ”œโ”€โ”€ sni_extractor.cpp
โ”‚       โ”œโ”€โ”€ connection_tracker.cpp
โ”‚       โ”œโ”€โ”€ rule_manager.cpp
โ”‚       โ”œโ”€โ”€ fast_path.cpp
โ”‚       โ”œโ”€โ”€ load_balancer.cpp
โ”‚       โ””โ”€โ”€ types.cpp
โ”‚
โ”œโ”€โ”€ ๐Ÿ generate_test_pcap.py    โ† Generate sample network traffic
โ”œโ”€โ”€ ๐Ÿ“ฆ test_dpi.pcap            โ† Example PCAP file for testing
โ”œโ”€โ”€ โš™๏ธ  CMakeLists.txt           โ† Build configuration
โ”œโ”€โ”€ ๐ŸชŸ WINDOWS_SETUP.md         โ† Windows build instructions
โ””โ”€โ”€ ๐Ÿ“– README.md

๐Ÿ”„ Packet Processing Pipeline

  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚                                                          โ”‚
  โ”‚  1๏ธโƒฃ  READ        Read packet from PCAP file              โ”‚
  โ”‚         โ”‚                                                โ”‚
  โ”‚         โ–ผ                                                โ”‚
  โ”‚  2๏ธโƒฃ  PARSE       Extract MAC, IP, ports, protocol        โ”‚
  โ”‚         โ”‚                                                โ”‚
  โ”‚         โ–ผ                                                โ”‚
  โ”‚  3๏ธโƒฃ  IDENTIFY    Generate Five-Tuple flow ID             โ”‚
  โ”‚         โ”‚                                                โ”‚
  โ”‚         โ–ผ                                                โ”‚
  โ”‚  4๏ธโƒฃ  INSPECT     Extract SNI from TLS handshake          โ”‚
  โ”‚         โ”‚                                                โ”‚
  โ”‚         โ–ผ                                                โ”‚
  โ”‚  5๏ธโƒฃ  EVALUATE    Check packet against blocking rules     โ”‚
  โ”‚         โ”‚                                                โ”‚
  โ”‚        / \                                               โ”‚
  โ”‚       โ–ผ   โ–ผ                                              โ”‚
  โ”‚      โœ…   โŒ                                              โ”‚
  โ”‚   Forward  Drop                                          โ”‚
  โ”‚   to PCAP                                                โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ›  Building the Project

๐Ÿ“‹ Requirements

  • C++17 compatible compiler (g++, clang++, or MSVC)
  • Linux / macOS / Windows (MinGW)
  • Python 3 (optional โ€” for generating test PCAP files)

๐Ÿ”ง Compile

g++ -std=c++17 -O2 -I include \
    src/*.cpp \
    -o dpi_engine

๐ŸชŸ Windows users: See WINDOWS_SETUP.md for detailed Visual Studio, MinGW, and WSL instructions.


โ–ถ๏ธ Running the Engine

Basic Usage

./dpi_engine input.pcap output.pcap

Example with Blocking Rules

./dpi_engine input.pcap output.pcap \
  --block-app YouTube \
  --block-ip 192.168.1.50 \
  --block-domain facebook

๐Ÿ“Š Example Output

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘          DPI Engine โ€” Results            โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘  Total Packets   :   77                  โ•‘
โ•‘  Forwarded       :   69   โœ…             โ•‘
โ•‘  Dropped         :    8   โŒ             โ•‘
โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ
โ•‘  Detected Applications:                  โ•‘
โ•‘    โ€ข HTTPS                               โ•‘
โ•‘    โ€ข YouTube                             โ•‘
โ•‘    โ€ข Facebook                            โ•‘
โ•‘    โ€ข DNS                                 โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿ”ฎ Future Improvements

  • โž• Adding more application signatures
  • ๐Ÿ“ก Supporting live network packet capture
  • โฑ๏ธ Implementing bandwidth throttling
  • ๐ŸŒ Creating a web dashboard for monitoring
  • โšก Adding support for QUIC / HTTP3 traffic

๐ŸŽ“ Educational Purpose

This project demonstrates important network security and packet analysis concepts:

Concept What It Shows
๐Ÿ“ฆ Network packet structure Multi-layer protocol parsing
๐Ÿ”— Flow tracking Five-Tuple state machine
๐Ÿ”Ž Deep packet inspection Payload-level application detection
๐Ÿ” TLS handshake analysis SNI extraction before encryption
โšก Multi-threaded systems Thread-safe queues & load balancing

It serves as a practical learning project for:

Network Security โ€ข Packet Analysis โ€ข Traffic Monitoring Systems

Footer

About

A multi-threaded Deep Packet Inspection (DPI) engine in C++ that processes PCAP network traffic, identifies applications via packet metadata and SNI (Server Name Indication) inspection, and enforces filtering rules to block specific applications, domains, or IP addresses.

Topics

Resources

Stars

Watchers

Forks

Contributors