Skip to content

ngrep

100 commands

“Network grep” — matches regular expressions against packet payloads, like grep for network traffic.

  • packet capture
  • pattern matching

Install ngrep

sudo apt install ngrep

Basic Pattern Matching2

ngrep .

Captures and displays all traffic on the default interface by matching any character.

ngrep error

Searches for the string 'error' in all network packets on the default interface.

Interface Selection3

ngrep -d eth0 .

Listens for traffic specifically on the 'eth0' network interface.

ngrep -d any .

Listens for traffic on all available network interfaces simultaneously.

ngrep -d lo0 .

Listens for localhost loopback traffic specifically.

Quiet Mode1

ngrep -q GET

Runs in quiet mode, suppressing the '#' hash marks for non-matching packets.

Case Insensitive Matching1

ngrep -i pass

Searches for 'pass' regardless of case (e.g., matching PASS, Pass, or pass).

Invert Matching1

ngrep -v HTTP

Displays only packets that do NOT contain the pattern 'HTTP'.

Word Boundary Matching1

ngrep -w user

Matches 'user' as a whole word, ignoring substrings like 'username'.

Port Filtering2

ngrep . port 80

Captures all traffic destined for or originating from port 80.

Link
ngrep . port 443

Captures all traffic associated with HTTPS port 443.

Link

Host Filtering3

ngrep . host 192.168.1.5

Filters traffic to show only packets sent to or from host 192.168.1.5.

Link
ngrep . dst host 10.0.0.1

Captures only packets where the destination IP is 10.0.0.1.

Link
ngrep . src host 10.0.0.1

Captures only packets where the source IP is 10.0.0.1.

Link

Network Filtering1

ngrep . net 192.168.1.0/24

Captures traffic for the entire 192.168.1.0/24 subnet.

Link

Output Formatting3

ngrep -W byline GET port 80

Formats the payload output with line breaks, making HTTP headers readable.

Link
ngrep -W single GET port 80

Forces the output of the matching packet payload onto a single line.

Link
ngrep -W none GET

Displays payload without any special formatting (raw wrapping).

Link

Timestamping2

ngrep -t .

Prints a timestamp in YYYY/MM/DD HH:MM:SS.UUUUUU format for every match.

Link
ngrep -T .

Prints the time delta (elapsed time) between matched packets.

Link

Packet Count Control2

ngrep -n 5 .

Captures exactly 5 matching packets and then exits.

Link
ngrep -n 1 GET

Finds the first packet containing 'GET' and immediately exits.

Link

Protocol Filtering3

ngrep . tcp port 22

Restricts inspection to TCP traffic on port 22 (SSH).

Link
ngrep . udp port 53

Restricts inspection to UDP traffic on port 53 (DNS).

Link
ngrep . icmp

Captures only ICMP traffic (ping, traceroute).

Link

Hex Output2

ngrep -x .

Displays the packet payload in both hexadecimal and ASCII format.

Link
ngrep -x GET port 80

Matches 'GET' and shows the full packet in hex dump format.

Link

Hex Pattern Matching2

ngrep -X 0x474554

Searches traffic for the hex signature 474554 (which is 'GET' in ASCII).

Link
ngrep -X 0xDEADBEEF

Searches for the specific binary magic bytes 0xDEADBEEF in the payload.

Link

Saving to File2

ngrep -O capture.pcap .

Saves all matched packets to a file named 'capture.pcap' in pcap format.

Link
ngrep -O suspicious.pcap -q evil

Saves only packets containing 'evil' to a pcap file without printing hash marks.

Link

Reading from File2

ngrep -I capture.pcap .

Reads and analyzes packets from an existing 'capture.pcap' file instead of live traffic.

Link
ngrep -I capture.pcap -W byline HTTP

Searches for 'HTTP' within a pcap file and displays output in readable line format.

Link

Complex Filters3

ngrep . 'src host 1.1.1.1 and dst port 80'

Combines host and port filters to see web traffic originating from 1.1.1.1.

Link
ngrep . 'tcp and not port 22'

Captures all TCP traffic excluding traffic on port 22 (SSH).

Link
ngrep . 'host 10.0.0.1 or host 10.0.0.2'

Captures traffic involving either 10.0.0.1 or 10.0.0.2.

Link

HTTP Analysis4

ngrep -W byline "^GET" port 80

Captures HTTP GET requests, formatting headers cleanly.

Link
ngrep -W byline "^POST" port 80

Captures HTTP POST requests to analyze submitted form data.

Link
ngrep -W byline "User-Agent:" port 80

Inspects HTTP traffic specifically to see User-Agent strings.

Link
ngrep -W byline "Cookie:" port 80

Extracts Cookie headers from HTTP traffic for session analysis.

Link

DNS Analysis2

ngrep -d any . port 53

Monitors all DNS queries and responses on all interfaces.

Link
ngrep -i "google" udp port 53

Filters DNS traffic specifically for queries related to 'google'.

Link

FTP Analysis1

ngrep -W byline "USER|PASS" port 21

Watches FTP control traffic for login attempts (username and password).

Link

SMTP Analysis2

ngrep -W byline "MAIL FROM" port 25

Monitors outgoing emails by filtering for the SMTP MAIL FROM command.

Link
ngrep -W byline "RCPT TO" port 25

Monitors incoming email destinations via the SMTP RCPT TO command.

Link

Telnet Analysis1

ngrep -q . port 23

Captures cleartext Telnet traffic.

Link

Snapshot Length2

ngrep -s 1500 .

Sets the snaplen (capture size) to 1500 bytes to capture full Ethernet frames.

Link
ngrep -s 60 .

Sets a small snaplen to capture only packet headers, saving performance.

Link

Regular Expressions3

ngrep "^G.+HTTP" port 80

Uses regex to match packets starting with G, followed by chars, ending with HTTP.

Link
ngrep "[0-9]{3}-[0-9]{2}-[0-9]{4}" .

Searches traffic for patterns resembling Social Security Numbers (regex).

Link
ngrep "admin|root" port 22

Looks for 'admin' OR 'root' in SSH traffic (note: SSH is encrypted, usually yields no match unless re-keying).

Link

SQL Injection Monitoring2

ngrep -i "UNION SELECT" port 80

Detects potential SQL Injection attempts containing 'UNION SELECT'.

Link
ngrep -i "' OR '1'='1" port 80

Detects classic tautology based SQL Injection patterns.

Link

XSS Monitoring1

ngrep -i "<script>" port 80

Detects Cross-Site Scripting (XSS) payloads in cleartext HTTP.

Link

Shellshock Detection1

ngrep -d any "() { :;};" port 80

Monitors for the specific signature of the Shellshock vulnerability.

Link

Credit Card Hunting1

ngrep "[0-9]{4}.[0-9]{4}.[0-9]{4}.[0-9]{4}" port 80

Uses regex to hunt for cleartext credit card number patterns.

Link

SSH Version Scan1

ngrep -P "SSH-" port 22

Identifies the SSH version string during the initial handshake.

Link

Empty Pattern Filter1

ngrep "" port 80

Matches ALL packets on port 80 without searching for a specific string.

Link

IPv6 Analysis2

ngrep . ip6

Filters and captures only IPv6 protocol traffic.

Link
ngrep . ip6 and port 80

Captures web traffic specifically over IPv6 networks.

Link

VLAN Tagged Traffic1

ngrep . vlan

Captures traffic that contains 802.1Q VLAN tags.

Link

Broadcast Traffic1

ngrep . broadcast

Monitors network broadcast traffic.

Link

Multicast Traffic1

ngrep . multicast

Monitors network multicast traffic.

Link

TCP Flags3

ngrep . 'tcp[tcpflags] & tcp-syn != 0'

Captures only TCP packets with the SYN flag set (connection initiation).

Link
ngrep . 'tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)'

Captures TCP SYN-ACK packets (handshake response).

Link
ngrep . 'tcp[tcpflags] & tcp-rst != 0'

Captures TCP Reset (RST) packets, useful for debugging connection drops.

Link

Hex with Formatting1

ngrep -x -W byline GET port 80

Combines hex dump and byline formatting for detailed HTTP inspection.

Link

Combined Filters1

ngrep -d eth0 -q -W byline "POST" port 80 and host 10.10.10.5

Quietly monitors POST requests on eth0 specifically involving host 10.10.10.5.

Link

User/Pass Pattern1

ngrep -i "user|pass|login" port 80

Generic search for login-related terms in cleartext web traffic.

Link

Range of Ports1

ngrep . portrange 8000-8080

Captures traffic spanning a range of ports from 8000 to 8080.

Link

Exclude Source1

ngrep . not src host 192.168.1.1

Captures everything EXCEPT traffic coming from 192.168.1.1.

Link

Timestamp Delta Debug1

ngrep -T -q . icmp

Quietly pings with delta timestamps to measure network latency jitter.

Link

Non-Printable Char1

ngrep -X "00" port 80

Searches for null bytes (0x00) which might indicate binary data or overflow attempts.

Link

IRC Traffic2

ngrep -i "JOIN" port 6667

Monitors IRC channel joins on the standard IRC port.

Link
ngrep -i "PRIVMSG" port 6667

Monitors private messages in IRC traffic.

Link

Database Traffic2

ngrep . port 3306

Monitors generic MySQL database traffic.

Link
ngrep . port 5432

Monitors generic PostgreSQL database traffic.

Link

Kill TCP Connection1

ngrep -K 1 pattern

Attempts to send TCP RST to kill connections matching the pattern (works on some systems).

Link

Limit Context1

ngrep -n 3 -W byline . host 1.2.3.4

Debugging a specific host with formatting, stopping after 3 packets.

Link

ARP Traffic1

ngrep . arp

Monitors Address Resolution Protocol packets for ARP spoofing detection.

Link

RARP Traffic1

ngrep . rarp

Monitors Reverse ARP traffic.

Link

Packet Size Filter2

ngrep . 'greater 100'

Captures only packets larger than 100 bytes.

Link
ngrep . 'less 64'

Captures only packets smaller than 64 bytes (runts/control frames).

Link

DHCP Discovery1

ngrep . port 67 or port 68

Monitors DHCP requests and responses.

Link

NTP Analysis1

ngrep . port 123

Monitors Network Time Protocol traffic.

Link

SNMP Analysis1

ngrep . port 161

Monitors Simple Network Management Protocol traffic.

Link

Wildcard Interface1

ngrep -d any -q .

Quietly captures everything on every interface.

Link

High Speed Capture1

ngrep -d eth0 -n 1000 -q .

Rapidly grabs a 1000 packet sample from eth0 without screen output overhead.

Link

SIP/VoIP Analysis2

ngrep -W byline "INVITE" port 5060

Detects SIP VoIP call initiation packets.

Link
ngrep -W byline "REGISTER" port 5060

Detects SIP phone registration attempts.

Link

JSON API Debugging1

ngrep -W byline "application/json" port 80

Filters for HTTP packets indicating JSON content type.

Link

Basic Auth Discovery1

ngrep -W byline "Authorization: Basic" port 80

Finds HTTP headers using Basic Authentication (Base64 encoded creds).

Link

PCAP Replay Analysis1

ngrep -I traffic.pcap -O filtered.pcap "GET"

Reads traffic.pcap, finds packets with 'GET', and saves them to filtered.pcap.

Link

Configuration Files1

ngrep -F patterns.txt .

Reads search patterns from a file named 'patterns.txt' instead of command line.

Link

Ignore Case Regex1

ngrep -i "^error|^fail" .

Case-insensitive regex search for error or failure at line start.

Link

IMAP Traffic1

ngrep . port 143

Monitors unencrypted IMAP email traffic.

Link

POP3 Traffic1

ngrep . port 110

Monitors unencrypted POP3 email traffic.

Link

LDAP Traffic1

ngrep . port 389

Monitors Lightweight Directory Access Protocol queries.

Link

Advanced Troubleshooting1

ngrep -d any -t -T -W byline "Error" port 80

Combines interface, timestamp, delta, formatting, and pattern for deep analysis.

Link