New FAMILUG

The PyMiers

Wednesday 18 June 2014

tcpdump - lưu giữ mọi khoảnh khắc của network

thanhnguyen@thanhnguyen:~$ whatis tcpdump
tcpdump (8)          - dump traffic on a network
Có thể nói, tcpdump là một trong những lệnh hữu dụng nhất trong quản trị mạng.

Tại sao?
Có những lúc bạn thắc mắc về các vấn đề kết nối mạng, hoặc thỉnh thoảng không biết tại sao một server không RESPONSE một ping request của bạn...
Trong những trường hợp như thế, tcpdump là một tool vô cùng hữu ích đối với bạn, nó có khả năng capturing packets khá mạnh mẽ. Nó hoạt động trên network layer và có thể capture tất cả các packets ra vào máy tính. Ngoài ra, có thể sử dụng tcpdump để capture và save các packets tới một file nào đó và phân tích sau.

Sau đây, là một số option phổ biến khi sử dụng tcpdump command.

-i option
Được sử dụng để xác định interface. tcpdump capture các packets gửi tới và truyền đi qua một interface cụ thể. => Chọn Interface để tcpdump capture các packet
Ví dụ, để capture các packet qua lo interface:
root@thanhnguyen:/home/thanhnguyen# tcpdump -i lo
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
16:59:28.368723 IP localhost.36167 > localhost.7768: Flags [P.], seq 2937870258:2937870487, ack 77294449, win 256, options [nop,nop,TS val 302793753 ecr 302793502], length 229
16:59:28.371315 IP localhost.36169 > localhost.7768: Flags [P.], seq
-n option
Sử dụng option -n, tất cả các sender and destination host address đều được thể hiện dưới dạng IP address (default là "name" format). Ví dụ sau sẽ hiện IP 127.0.0.1 thay vì localhost.

root@thanhnguyen:/home/thanhnguyen# tcpdump -n  -i lo
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
21:33:03.407573 IP 127.0.0.1.45154 > 127.0.0.1.53: 27183+ A? www.familug.org. (33)
21:33:03.434939 IP 127.0.0.1.53 > 127.0.0.1.45154: 27183 3/0/0 CNAME ghs.google.com., CNAME ghs.l.google.com., A 173.194.72.121 (97)
-c option
Giới hạn số lượng packets được captured
Ví dụ: -c 2: capture 2 packets
root@thanhnguyen:/home/thanhnguyen# tcpdump -n -c 2 -i lo
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
21:33:03.407573 IP 127.0.0.1.45154 > 127.0.0.1.53: 27183+ A? www.familug.org. (33)
21:33:03.434939 IP 127.0.0.1.53 > 127.0.0.1.45154: 27183 3/0/0 CNAME ghs.google.com., CNAME ghs.l.google.com., A 173.194.72.121 (97)
2 packets captured
4 packets received by filter
0 packets dropped by kernel
-e option
Hiển thị thêm MAC address của sender/ receiver.
root@thanhnguyen:/home/thanhnguyen# tcpdump -n -s0 -e  -c 2 -i lo
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
21:36:51.964606 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 75: 127.0.0.1.35984 > 127.0.0.1.53: 4250+ A? csi.gstatic.com. (33)
21:36:51.968088 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 75: 127.0.0.1.56955 > 127.0.0.1.53: 48450+ A? www.familug.org. (33)
2 packets captured
4 packets received by filter
0 packets dropped by kernel
 -vvv option
Các thông tin output của các packets sẽ được hiển thị một cách đầy chi tiết hơn, ví dụ như thông tin về flags, headers...

root@thanhnguyen:/home/thanhnguyen# tcpdump -n -s0 -e -vvv -c 2 -i lo
tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
21:38:06.872957 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 75: (tos 0x0, ttl 64, id 64062, offset 0, flags [DF], proto UDP (17), length 61)
    127.0.0.1.54078 > 127.0.0.1.53: [bad udp cksum 0xfe3c -> 0xe98f!] 4677+ A? csi.gstatic.com. (33)
21:38:06.877247 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 75: (tos 0x0, ttl 64, id 64063, offset 0, flags [DF], proto UDP (17), length 61)
    127.0.0.1.40215 > 127.0.0.1.53: [bad udp cksum 0xfe3c -> 0xc4ff!] 25568+ A? www.familug.org. (33)
2 packets captured
4 packets received by filter
0 packets dropped by kernel

-w option
Capture output và save output đó ra một file nào đó để phân tích kĩ hơn.
Chú ý: bạn nên sử dụng phần mở rộng ".pcap" cho file của bạn để giúp cho các tool phân tích packet khác có thể đọc nó một cách dễ dàng hơn.

Vd:
root@thanhnguyen:/home/thanhnguyen#  tcpdump -w sampletcpdump.pcap -s0 -vvv -e -n -c 2 -i eth0
-r option
Đọc các packet từ một file thay vì capture từ 1 interface, thường được sử dụng sau khi các gói dữ liệu đã được ghi vào một file với tùy chọn –w, bạn cần thêm option -r và passed tên file lưu trữ.
Ví dụ:
root@thanhnguyen:/home/thanhnguyen# tcpdump -r sampletcpdump.pcap 

Hiển thị thông tin packets qua một port xác định
Ví dụ bạn muốn hiển thị thông tin các packets qua port 80:
root@thanhnguyen:/home/thanhnguyen# tcpdump -s0 -n -c 2 -i eth0 port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:43:08.236129 IP 192.168.2.97.45839 > 10.2.20.14.80: Flags [S], seq 3753908323, win 14600, options [mss 1334,sackOK,TS val 326135 ecr 0,nop,wscale 7], length 0
01:43:08.236242 IP 10.2.20.14.80 > 192.168.2.97.45839: Flags [S.], seq 2422313268, ack 3753908324, win 14480, options [mss 1460,sackOK,TS val 625996691 ecr 326135,nop,wscale 5], length 0
2 packets captured
2 packets received by filter
0 packets dropped by kernel

Bỏ qua các packets không quan tâm
Ví dụ bạn muốn bỏ qua thông tin của các packets qua port 80, bạn vẫn dùng option "port" nhưng theo một cách khác:
root@thanhnguyen:/home/thanhnguyen# tcpdump -s0 -n -c 2 -i eth0 'port !80'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:44:29.195660 IP 10.2.20.14.22 > 192.168.2.97.53454: Flags [P.], seq 1823359192:1823359304, ack 3320258940, win 850, options [nop,nop,TS val 626016930 ecr 346367], length 112
01:44:29.195764 IP 10.2.20.14.22 > 192.168.2.97.53454: Flags [P.], seq 112:224, ack 1, win 850, options [nop,nop,TS val 626016930 ecr 346367], length 112
2 packets captured
4 packets received by filter
0 packets dropped by kernel

Notes: 'port !80'

Hiển thị thông tin của các packets qua một host xác định
Hiển thị các packets qua một host nào đó, bạn có thể thêm option "host":
root@thanhnguyen:/home/thanhnguyen# tcpdump -s0 -n -c 2 -i eth0 host 10.2.20.14
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:45:13.765910 IP 10.2.20.14.22 > 192.168.2.97.53454: Flags [P.], seq 1823361640:1823361832, ack 3320260236, win 850, options [nop,nop,TS val 626028073 ecr 357507], length 192
01:45:13.766491 IP 10.2.20.14.22 > 192.168.2.97.53454: Flags [P.], seq 192:416, ack 1, win 850, options [nop,nop,TS val 626028073 ecr 357507], length 224
2 packets captured
3 packets received by filter
0 packets dropped by kernel

Hiển thị thông tin của các packets từ một source xác định
Bạn có thể chỉ hiện thị thông tin của các packets từ một source (địa chỉ IP) nào đó với "src" option

root@thanhnguyen:/home/thanhnguyen#  tcpdump -s0 -vvv -e -n -c 2 -i eth0 src host 10.2.xx.xx

1 comment:

  1. Thích văn phong các bạn viết, mình có làm vài bài nhưng không làm tốt như các bạn. Đáng và rất muốn học hỏi

    Nhân tiện để link này cho ai tham khảo thêm nhé.
    https://github.com/vdcit/command-tcpdump

    ReplyDelete