Mastering Tcpdump
Dive deep into the world of network packet analysis with our expert guide on tcpdump. This comprehensive tutorial covers everything from capturing and saving packets to advanced filtering techniques. Ideal for IT professionals, network administrators, and cybersecurity enthusiasts, learn how to leverage tcpdump to troubleshoot network issues, monitor traffic in real-time, and secure your network. Equip yourself with the knowledge to use tcpdump effectively, with practical examples and tips.
Traffic that’s from 192.168.1.1 AND destined for ports 3389 or 22
Capture any packets with dst ip x.x.x.x and dst ports x, z
From One Network to Another
Find HTTP User Agents
By using egrep and multiple matches we can get the User Agent and the Host (or any other header) from the request
Capture only HTTP GET and POST packets only packets that match GET
Extract HTTP Passwords in POST Requests
Capture Cookies from Server and from Client
Show ICMP Packets that are not ECHO/REPLY (standard ping)
IPv6 with UDP and reading from a previously saved capture file
Usage Examples
```bash
Example Filter Showing Nmap NSE Script Testing
-
On Target:
bash nmap -p 80 --script=http-enum.nse targetip -
On Server:
bash tcpdump -nn port 80 | grep "GET /" GET /w3perl/ HTTP/1.1 GET /w-agora/ HTTP/1.1 GET /way-board/ HTTP/1.1 GET /web800fo/ HTTP/1.1 GET /webaccess/ HTTP/1.1 GET /webadmin/ HTTP/1.1 GET /webAdmin/ HTTP/1.1
Capture Start and End Packets of every non-local host
bash
tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'
Capture DNS Request and Response
bash
tcpdump -i wlp58s0 -s0 port 53
Capture HTTP data packets
bash
tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
Capture with tcpdump and view in Wireshark
bash
ssh wuseman@localhost 'tcpdump -s0 -c 1000 -nn -w - not port 22' | wireshark -k -i -
Top Hosts by Packets
bash
tcpdump -nnn -t -c 200 | cut -f 1,2,3,4 -d '.' | sort | uniq -c | sort -nr | head -n 20
Capture all the plaintext passwords
bash
tcpdump port http or port ftp or port smtp or port imap or port pop3 or port telnet -l -A | egrep -i -B5 'pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd=|password=|pass:|user:|username:|password:|login:|pass |user '
DHCP Example
bash
tcpdump -v -n port 67 or 68
Cleartext GET Requests
bash
tcpdump -vvAls0 | grep 'GET'
Find HTTP Host Headers
bash
tcpdump -vvAls0 | grep 'Host:'
Find HTTP Cookies
bash
tcpdump -vvAls0 | grep 'Set-Cookie|Host:|Cookie:'
Find SSH Connections
bash
tcpdump 'tcp[(tcp[12]>>2):4] = 0x5353482D'
Find DNS Traffic
bash
tcpdump -vvAs0 port 53
Find FTP Traffic
bash
tcpdump -vvAs0 port ftp or ftp-data
Find NTP Traffic
bash
tcpdump -vvAs0 port 123
Capture SMTP / POP3 Email
bash
tcpdump -nn -l port 25 \
| grep -i 'MAIL FROM\|RCPT TO'
Line Buffered Mode
bash
tcpdump -i eth0 -s0 -l port 80 | grep 'Server:'
Find traffic with evil bit
bash
tcpdump 'ip[6] & 128 != 0'
Filter on protocol (ICMP) and protocol-specific fields (ICMP type)
bash
tcpdump -n icmp and 'icmp[0] != 8 and icmp[0] != 0'
Same command can be used with predefined header field offset (icmptype) and ICMP type field values (icmp-echo and icmp-echoreply)
bash
tcpdump -n icmp and icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply
Filter on TOS field
bash
tcpdump -v -n ip and ip[1]!=0
Filter on TTL field
bash
tcpdump -v ip and 'ip[8]<2'
Filter on TCP flags (SYN/ACK)
bash
tcpdump -n tcp and port 80 and 'tcp[tcpflags] & tcp-syn == tcp-syn'
In the example above, all packets with TCP SYN flag set are captured. Other flags (ACK, for example) might be set also. Packets which have only TCP SYN flags set, can be captured
bash
tcpdump tcp and port 80 and 'tcp[tcpflags] == tcp-syn'
Catch TCP SYN/ACK packets (typically, responses from servers)
bash
tcpdump -n tcp and 'tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)'
tcpdump -n tcp and 'tcp[tcpflags] & tcp-syn == tcp-syn' and 'tcp[tcpflags] & tcp-ack == tcp-ack'
Catch ARP packets
bash
tcpdump -vv -e -nn ether proto 0x0806
Filter on IP packet length
bash
tcpdump -l icmp and '(ip[2:2]>50)' -w - \
|tcpdump -r - -v ip and '(ip[2:2]<60)'
Remark: due to some bug in tcpdump, the following command doesn't catch packets as expected
bash
tcpdump -v -n icmp and '(ip[2:2]>50)' and '(ip[2:2]<60)'
Filter on encapsulated content (ICMP within PPPoE)
bash
tcpdump -v -n icmp
Quieter output
bash
tcpdump -q -i eth0
tcpdump -t -i eth0
tcpdump -A -n -q -i eth0 'port 80'
tcpdump -A -n -q -t -i eth0 'port 80'
Print only useful packets from the HTTP traffic
bash
tcpdump -A -s 0 -q -t -i eth0 \
'port 80 and ( ((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12:2]&0xf0)>>2)) != 0)'
Dump SIP Traffic
bash
tcpdump -nq -s 0 -A -vvv port 5060 and host 1.2.3.4
Checking packet content
bash
tcpdump -i any -c10 -nn -A port 80
Checking packet content
bash
tcpdump -i any -c10 -nn -A port 80
Using tcpdump with port ranges and file count/size
bash
/usr/sbin/tcpdump -i any -s 0 -n -Z <user_name> -C 500 -W 100 -w /home/<user_name>/$(hostname).pcap -f '(port (# or # or # or # or # or # or ...) or portrange <start>-<end>)' &>/dev/null
Resource(s)"