tcpdump
CLI packet capture. Built-in di semua Linux. Capture di server, analisis di Wireshark.
1. Kenapa tcpdump?
tcpdump sudah pre-installed di hampir semua distribusi Linux. Tidak perlu install apa-apa ā langsung bisa capture. Ini menjadikannya tool pertama yang kamu pakai saat perlu capture traffic di server production, VM, atau container. Karakteristik tcpdump: ⢠Lightweight ā CLI only, minimal resource ⢠Pre-installed ā ada di semua Linux ⢠Capture di server, analisis di workstation ⢠BPF filter ā sama seperti Wireshark capture filter tcpdump dan Wireshark saling melengkapi: tcpdump untuk capture di server (SSH), Wireshark untuk analisis mendalam di workstation (GUI).
2. Basic Capture
# Capture di interface eth0
sudo tcpdump -i eth0
# Capture dan simpan ke file
sudo tcpdump -i eth0 -w capture.pcap
# Disable DNS resolution (lebih cepat)
sudo tcpdump -i eth0 -n
# Limit jumlah paket
sudo tcpdump -i eth0 -c 1000 -w capture.pcap
# Verbose output
sudo tcpdump -i eth0 -v
sudo tcpdump -i eth0 -vv
# Print ASCII content
sudo tcpdump -i eth0 -A
# Print hex + ASCII
sudo tcpdump -i eth0 -XFlag penting: -i (interface), -w (write to file), -n (no DNS), -c (count limit), -v (verbose), -A (ASCII), -X (hex+ASCII).
3. Selalu pakai -n dan -w
-n (disable DNS resolution) mencegah tcpdump melakukan reverse DNS lookup untuk setiap IP ā ini mempercepat capture dan menghindari DNS traffic tambahan. -w (write to file) menyimpan raw packet ke file PCAP. Selalu simpan ke file ā kamu bisa analisis nanti di Wireshark dengan semua fitur GUI-nya.
4. BPF Filters
# Filter by host
sudo tcpdump -i eth0 host 192.168.1.100
# Filter by port
sudo tcpdump -i eth0 port 80
sudo tcpdump -i eth0 port 443
# Filter by network
sudo tcpdump -i eth0 net 192.168.1.0/24
# Filter by protocol
sudo tcpdump -i eth0 tcp
sudo tcpdump -i eth0 udp
sudo tcpdump -i eth0 icmp
# Logical operators
sudo tcpdump -i eth0 host 10.0.0.5 and port 80
sudo tcpdump -i eth0 not port 22
sudo tcpdump -i eth0 src host 10.0.0.5 and dst port 443
# Practical: capture semua kecuali SSH
sudo tcpdump -i eth0 -n -w evidence.pcap 'not port 22'
# Practical: capture DNS traffic saja
sudo tcpdump -i eth0 -n -w dns.pcap 'udp port 53'
# Practical: capture traffic ke/dari subnet tertentu
sudo tcpdump -i eth0 -n -w lateral.pcap 'net 10.0.0.0/8 and not host 10.0.0.1'BPF filter syntax sama dengan Wireshark capture filter. Bisa kombinasi dengan and, or, not. Selalu quote filter expression dengan single quotes.
5. Advanced: Rotate & Timestamp
# File rotation by size (100MB per file, max 10 files)
sudo tcpdump -i eth0 -n -w capture.pcap -C 100 -W 10
# File rotation by time (new file every 3600 seconds/1 hour)
sudo tcpdump -i eth0 -n -w capture_%Y%m%d_%H%M%S.pcap -G 3600
# Human-readable timestamps
sudo tcpdump -i eth0 -n -tttt
# Snap length (capture first 96 bytes only ā headers)
sudo tcpdump -i eth0 -n -s 96 -w headers_only.pcap
# Full packet capture (default di modern tcpdump)
sudo tcpdump -i eth0 -n -s 0 -w full_capture.pcap-C (size rotation) dan -G (time rotation) sangat berguna untuk long-running capture. -tttt menampilkan timestamp human-readable. -s mengatur snap length (berapa byte per paket yang di-capture).
6. Read & Quick Analysis
# Read pcap file
sudo tcpdump -r capture.pcap
# Read dengan filter
sudo tcpdump -r capture.pcap 'host 192.168.1.100 and port 80'
# Top talkers (IP paling banyak traffic)
sudo tcpdump -r capture.pcap -n | awk '{print $3}' | cut -d. -f1-4 | sort | uniq -c | sort -rn | head -10
# Top destination ports
sudo tcpdump -r capture.pcap -n 'tcp' | awk '{print $5}' | cut -d. -f5 | sort | uniq -c | sort -rn | head -10
# Grep untuk pattern tertentu
sudo tcpdump -r capture.pcap -A | grep -i 'password\|user\|login'
sudo tcpdump -r capture.pcap -A | grep -i 'cmd\.exe\|powershell'tcpdump -r membaca file PCAP. Kombinasi dengan awk, sort, uniq untuk quick analysis. Untuk analisis mendalam, transfer ke Wireshark.
7. Capture ā Analyze Workflow
- 1
SSH ke Server
SSH ke server yang ingin di-capture traffic-nya. Pastikan kamu punya akses root atau sudo.
- 2
Identifikasi Interface
Jalankan 'ip a' atau 'ifconfig' untuk melihat interface yang tersedia. Biasanya eth0, ens33, atau docker0.
- 3
Start Capture (exclude SSH)
sudo tcpdump -i eth0 -n -w /tmp/evidence.pcap 'not port 22' ā exclude port 22 agar SSH session kamu tidak ikut ter-capture.
- 4
Reproduce / Tunggu
Reproduce aktivitas yang ingin di-capture, atau tunggu sampai traffic mencurigakan muncul. Biarkan tcpdump berjalan.
- 5
Stop Capture
Tekan Ctrl+C untuk stop. tcpdump akan menampilkan jumlah paket yang di-capture.
- 6
Transfer & Analisis
Transfer file PCAP ke workstation: scp user@server:/tmp/evidence.pcap . ā lalu buka di Wireshark untuk analisis mendalam.
8. Jangan Capture ke Evidence Disk
JANGAN pernah capture traffic ke disk yang sama dengan evidence. Ini bisa mengubah evidence dan merusak chain of custody. Gunakan USB external, network share, atau disk terpisah untuk menyimpan file capture. Pastikan destination disk punya cukup space ā PCAP bisa sangat besar.