ngrep

Network grep. Applique la correspondance de motifs regex façon grep au trafic réseau : recherche des chaînes dans les payloads de paquets en temps réel ou depuis des fichiers pcap sans analyse complète des paquets. Network grep. Applies grep-style regex pattern matching to network traffic — searches packet payloads for strings in real-time or from pcap files without full packet analysis.

↗ https://github.com/jpr5/ngrep

Overview

ngrep works like grep but on network traffic. It captures packets and searches payloads for regex patterns — ideal for quick credential hunting, protocol debugging, and finding specific strings in network traffic without setting up full traffic analysis.

Basic Usage

Search for a pattern in live traffic

sudo ngrep "password" -i eth0

Case-insensitive search

sudo ngrep -i "password" -d eth0

Search in pcap file

ngrep "password" -I capture.pcap

Search with BPF filter

sudo ngrep "User-Agent" -d eth0 'port 80'

Pattern Matching

Find credentials in HTTP

sudo ngrep -i "password|passwd|login|credential" -d eth0 'port 80'

Find basic auth headers

sudo ngrep "Authorization: Basic" -d eth0

Find FTP credentials

sudo ngrep -i "user\|pass" -d eth0 'port 21'

Find SMTP auth

sudo ngrep "AUTH\|password" -d eth0 'port 25'

Catch POST data

sudo ngrep "POST" -d eth0 'port 80'

BPF Filters

Specific port

sudo ngrep "pattern" -d eth0 'port 80'

Multiple ports

sudo ngrep "pattern" -d eth0 'port 80 or port 443'

Specific host

sudo ngrep "pattern" -d eth0 'host 192.168.1.100'

Protocol

sudo ngrep "pattern" -d eth0 'udp port 53'

Output Control

Print only matching lines (no context)

sudo ngrep -q "password" -d eth0

Show timestamps

sudo ngrep -t "password" -d eth0

XML output

sudo ngrep -O output.pcap "password" -d eth0

Save matched packets to pcap

sudo ngrep -O matches.pcap "login" -d eth0

Read from pcap

ngrep "GET /admin" -I capture.pcap

Practical Examples

Monitor for SQL injection attempts

sudo ngrep -i "union\|select\|drop table" -d eth0 'port 80'
# Capture HTTP Basic Auth
sudo ngrep "Authorization: Basic" -d eth0 | \
  grep -oP 'Basic [A-Za-z0-9+/=]+' | \
  while read line; do
    echo "$line" | awk '{print $2}' | base64 -d
  done

Watch for suspicious DNS

sudo ngrep -i "evil\|malware\|c2" -d eth0 'port 53'

Monitor web app traffic

sudo ngrep -i "error\|exception\|debug" -d eth0 'port 8080'

Tips

  • Faster to set up than Wireshark for specific string searches
  • Use BPF filters to narrow scope and improve performance
  • -q (quiet) removes the dots for non-matching packets — cleaner output
  • For credential hunting, pair with base64 -d to decode HTTP Basic Auth
  • ngrep doesn’t reassemble TCP streams — use tcpflow for full session data
Help / Man page
ngrep [options] [match expression] [bpf filter]

-d IFACE    Interface (default: first non-loopback)
-I FILE     Input pcap file
-O FILE     Output pcap file (matched packets)
-i          Case-insensitive matching
-q          Quiet (no non-matching output)
-t          Show timestamps
-x          Print in hex
-A N        After-match context (N packets)
-B N        Before-match context (N packets)
-W byline   Print each packet on one line

Vue d’ensemble

ngrep fonctionne comme grep mais sur le trafic réseau. Il capture les paquets et recherche des motifs regex dans les payloads : idéal pour la chasse rapide aux credentials, le débogage de protocole, et la recherche de chaînes spécifiques dans le trafic réseau sans mettre en place une analyse de trafic complète.

Utilisation de base

# Rechercher un motif dans le trafic en direct
sudo ngrep "password" -i eth0

# Recherche insensible à la casse
sudo ngrep -i "password" -d eth0

# Rechercher dans un fichier pcap
ngrep "password" -I capture.pcap

# Recherche avec un filtre BPF
sudo ngrep "User-Agent" -d eth0 'port 80'

Correspondance de motifs

# Trouver des credentials dans HTTP
sudo ngrep -i "password|passwd|login|credential" -d eth0 'port 80'

# Trouver les en-têtes d'authentification basique
sudo ngrep "Authorization: Basic" -d eth0

# Trouver des credentials FTP
sudo ngrep -i "user\|pass" -d eth0 'port 21'

# Trouver l'authentification SMTP
sudo ngrep "AUTH\|password" -d eth0 'port 25'

# Capturer les données POST
sudo ngrep "POST" -d eth0 'port 80'

Filtres BPF

# Port spécifique
sudo ngrep "pattern" -d eth0 'port 80'

# Plusieurs ports
sudo ngrep "pattern" -d eth0 'port 80 or port 443'

# Hôte spécifique
sudo ngrep "pattern" -d eth0 'host 192.168.1.100'

# Protocole
sudo ngrep "pattern" -d eth0 'udp port 53'

Contrôle de la sortie

# N'afficher que les lignes correspondantes (sans contexte)
sudo ngrep -q "password" -d eth0

# Afficher les timestamps
sudo ngrep -t "password" -d eth0

# Sortie XML
sudo ngrep -O output.pcap "password" -d eth0

# Sauvegarder les paquets correspondants dans un pcap
sudo ngrep -O matches.pcap "login" -d eth0

# Lire depuis un pcap
ngrep "GET /admin" -I capture.pcap

Exemples pratiques

# Surveiller les tentatives d'injection SQL
sudo ngrep -i "union\|select\|drop table" -d eth0 'port 80'

# Capturer l'authentification HTTP Basic
sudo ngrep "Authorization: Basic" -d eth0 | \
  grep -oP 'Basic [A-Za-z0-9+/=]+' | \
  while read line; do
    echo "$line" | awk '{print $2}' | base64 -d
  done

# Surveiller le DNS suspect
sudo ngrep -i "evil\|malware\|c2" -d eth0 'port 53'

# Surveiller le trafic d'une application web
sudo ngrep -i "error\|exception\|debug" -d eth0 'port 8080'

Conseils

  • Plus rapide à mettre en place que Wireshark pour des recherches de chaînes spécifiques
  • Utiliser des filtres BPF pour réduire la portée et améliorer les performances
  • -q (quiet) supprime les points pour les paquets non correspondants : sortie plus propre
  • Pour la chasse aux credentials, associer avec base64 -d pour décoder l’authentification HTTP Basic
  • ngrep ne réassemble pas les flux TCP : utiliser tcpflow pour les données de session complètes
Aide / Page de manuel
ngrep [options] [match expression] [bpf filter]

-d IFACE    Interface (default: first non-loopback)
-I FILE     Input pcap file
-O FILE     Output pcap file (matched packets)
-i          Case-insensitive matching
-q          Quiet (no non-matching output)
-t          Show timestamps
-x          Print in hex
-A N        After-match context (N packets)
-B N        Before-match context (N packets)
-W byline   Print each packet on one line