tcpdump


https://opensource.com/article/18/10/introduction-tcpdump

https://danielmiessler.com/study/tcpdump/


Display filter reference: DNS

https://www.wireshark.org/docs/dfref/d/dns.html


IPV4


tcpdump default: 68 bytes (14 eth + 54 ip)
ethernet header = 14 bytes
max ip pkt in ethernet = 1500 (1480+20 ip hdr)
ip header = 20+ bytes
max ip4 pkt = 65,535 (0 invalid) 
tcp header = 20+ bytes

ip[0] &0x0F = ip hdr len * 4
ip[2:2] = total ip pkt len
ip[4:2] = ip id
ip[6] &0x40 = dont frag flag
ip[6] &0x20 = more frag flag
ip[6:2] & 0x1FFF = frag offset * 8
ip[9] = embedded proto
ip[12:4] = src ip addr
ip[16:4] = dst ip addr

TCP


tcp[0:2] = src port
tcp[2:2] = dst port
tcp[4:4] = seq nbr
tcp[8:4] = ack nbr
tcp[12] &0xF0 = tcp hdr len * 4
tcp[13] &03F = urg ack | psh rst syn fin

tcpdump cmd line


# Listen on all interfaces
sudo tcpdump -i any

# Capture Full Packet
sudo tcpdump -i any -s0

# List interfaces
sudo tcpdump -D  | sudo tcpdump --list-interfaces

# Capture on any interface
sudo tcpdump -i any  | sudo tcpdump --interface any

# Capture 5 packets
sudo tcpdump -i any -c 5

# Disable name resolution
sudo tcpdump -i any -c 5 -nn

Capture filter


# Filter by protocol
sudo tcpdump -i any -c 5 icmp
ping opensource.com

# Filter by source / destination
sudo tcpdump -i any -c 5 -nn src 10.0.2.15
sudo tcpdump -i any -c 5 -nn dst 10.2.0.15

# Filter by destination and port
sudo tcpdump -i any -c 5 -nn dst 192.168.1.1 and port 53

# More complex
sudo tcpdump -i any -c 5 -nn "dst 192.168.1.1 and (port 24 or port 53)"

# Save to file 
sudo tcpdump -i any -c 5 -nn -w ping_capture.cap port 53

# Absolute timestamp / date
sudo tcpdump -r ping_capture.cap -c 1 -v -tttt

# dest port dns/domain and ip proto = udp
sudo tcpdump port 53 and ip[9]==0x11 -r ping_capture.cap 
sudo tcpdump port 53 and ip[9]==17 -r ping_capture.cap   

# src port dns/domain
sudo tcpdump udp[0:2]==53 -r ping_capture.cap

# dest port dns/domain 
sudo tcpdump udp[2:2]==53 -r ping_capture.cap 

# icmp proto read from and write to pcap
sudo tcpdump ip[9]==1 -r traceroute.pcap -w icmp.pcap 

# pkts w ip header length of 20
sudo tcpdump -r udp_ping.cap 'ip[0]&0x0f=5'

# all ipv4 pkts - high order nibble = 4
sudo tcpdump -r udp_ping.cap 'ip[0]&0xf0=64'

# tcp pkt where tcp hdr len = 10 (high order hex a | 16*10)
tcpdump -r tcp_web.cap 'tcp[12]&0xf0=160' 

# tcp pkt where the syn bit is set
tcpdump -r tcp_web.cap -nn 'tcp[13] &0x02 != 0'

# tcp pkt where the ack bit is set
tcpdump -r tcp_web.cap -nn 'tcp[13] &0x10 != 0'

# tcp pkt where the psh bit is set
tcpdump -r tcp_web.cap -nn 'tcp[13] &0x08 != 0'

# tcp pkt where both the syn and ack bit are set 
#   (16^1 * 1 + 2)
tcpdump -r tcp_web.cap -nn 'tcp[13] &0x12 = 18'

# tcp pkt where either the syn or ack bit are set 
tcpdump -r tcp_web.cap -nn 'tcp[13] &0x12 != 0'

# tcp pkt where only the syn and ack bit are set 
tcpdump -r tcp_web.cap -nn 'tcp[13] = 0x12'

# ip pkt where MF flag set
tcpdump -r tcp_web.cap -nn 'ip[6] &0x20!=0'

# ip pkt where DF flag set
tcpdump -r tcp_web.cap -nn 'ip[6] &0x40!=0'

# tcp pkt w dst port 22
tcpdump -r ssh.cap -nn 'dst port 22'   

# ip6 pkt len > 0
tcpdump -r ssh.cap -nn 'src port 22 and ip6[4:2]>0' -tttt