tcpdump

tcpdump คือ อะไร ?

หลายคนอาจจะได้ยินคำว่า packet sniffer มาบ้าง แต่จะรู็ไหมว่า tcpdump คือ อะไร และเกี่ยวข้องอะไรกับ packet sniffer ? tcpdump เป็นเครื่องมือหนึ่งประเภทเดียวกับ wireshark คือใช้ในการดักจับ packet ใน network ในรูปแบบ command line และนิยมใช้งานกันบน linux และ unix สามารถแสดง packet TCP/IP และ protocol อื่นๆ ที่ส่งผ่าน network ได้ และที่สำคัญคือ freeware

 

คุณสมบัติของ tcpdump

สามารถแสดงข้อมูลภายใน network packet สามารถอ่าน packet จาก network interface card รวมถึงเขียนลง file ในรูปแบบมาตราฐาน (สามารถเอาไปเปิดใน wireshark ได้) มันสามารถเอาไปใช้ทำงานได้หลายแบบเช่น ดูข้อมูลที่สื่อสารกันระหว่าง computer หรือ ถ้าเอาไปใช้ทางที่ผิดก็ใช้ในการ hack ข้อมูลโดยหลักการ man in the middle แต่ user ที่จะใช้งานต้องมี privilege ระดับ root เพราะอย่างที่ทราบคือเป็นเครื่องมือที่อันตรายมาก เพราะสามารถเอามาแกะ packet ของ SSH, Telnet หรือ HTTP ได้ ทำให้สามารถเห็น user/password ที่มีการ login ผ่านตัว server ที่ใช้งานนี้ ถ้า packet เหล่านี้ไม่ทำการ encrypt ได้ดีพอ

 

วิธีการใช้งาน tcpdump

โดยปกติแล้ว linux จะมีการติดตั้ง tcpdump มาให้ตั้งแต่แรกแล้ว เช่น Ubuntu server ของ host นี้ แต่อย่างที่บอก ว่าการใช้งาน tcpdump ต้องการ privilage ระดับ root ผมเลยต้องใช้ sudo command ในการรัน tcpdump แต่เนื่องจาก tcpdump สามารถจับ packet network ได้เพียงแค่ 1 interface ต่อครั้งเท่านั้น สำรหรับ server ที่มีหลาย interface lan เราต้องรู้ด้วยว่า packet ที่เราสนใจวิ่งออกที่ interface ไหนด้วย (ใช ifconfig coomand ในการดูชื่อ interface) ซึ่งถ้าเราไม่ระบุ ก็จะจับที่ default ให้ ซึ่งในที่นี้คือ enp2s0

$ sudo tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes
11:56:03.854532 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: Flags [P.], seq 2340985208:2340985404, ack 1338209537, win 255, length 196
11:56:03.855072 IP saixiii.com.40291 > dns1.asianet.co.th.domain: 42671+ PTR? 62.90.196.134.in-addr.arpa. (44)
11:56:03.857250 IP cm-134-196-90-62.revip18.asianet.co.th.49321 > saixiii.com.ssh: Flags [.], ack 196, win 16140, length 0
11:56:03.873981 IP dns1.asianet.co.th.domain > saixiii.com.40291: 42671 1/0/0 PTR cm-134-196-90-62.revip18.asianet.co.th. (96)
11:56:03.874311 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: Flags [P.], seq 196:408, ack 1, win 255, length 212
11:56:03.874319 IP saixiii.com.57012 > dns1.asianet.co.th.domain: 3718+ PTR? 29.207.144.203.in-addr.arpa. (45)
11:56:03.883515 IP dns1.asianet.co.th.domain > saixiii.com.57012: 3718 1/0/0 PTR dns1.asianet.co.th. (77)

เราจะเห็นผลลัพธ์ออกมาจำนวนมาก นั้นก็เพราะใน network เรามีการสื่อสารกันตลอดเวลา ไม่ว่าจะรับ OS หรือ application ต่างๆที่ทำงานอยู่ รวมไปถึงการสื่อสารกับ application ภายนอก ทั้งตัว computer, hub, switch, router ทำให้เราต้องมีการเพิ่ม option เพื่อให้เราสามารถทำงานได้ง่ายขึ้น

1. การเลือก interface

ใช้ option -i ตามด้วยชื่อของ interface

# tcpdump -i <interface name>
$ sudo tcpdump -i enp2s0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes
12:53:08.810535 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: Flags [P.], seq 2342666736:2342666932, ack 1338217253, win 255, length 196
12:53:08.810647 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: Flags [P.], seq 196:248, ack 1, win 255, length 52
12:53:08.811113 IP saixiii.com.44205 > dns1.asianet.co.th.domain: 2673+ PTR? 62.90.196.134.in-addr.arpa. (44)

 

2. ตั้งค่า snaplen

บางครั้งข้อมูลที่ส่งกันก็จะมี content ที่ยาวมากทำให้ เปลืองพื่นที่ในการจัดเก็บ ซึ่งจริงๆแล้วเราอาจจะต้องการดูแค่ส่วนของ header เท่านั้น เราจึงควรจำกัด length data ที่แสดงผลก็พอ

ใช้ option -s ตามด้วยตัวเลขจำนวน bytes

# tcpdump -s <bytes>
$ sudo tcpdump -i enp2s0 -s 50
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0, link-type EN10MB (Ethernet), capture size 50 bytes
12:54:56.999032 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: [|tcp]
12:54:56.999628 IP saixiii.com.55252 > dns1.asianet.co.th.domain: [|domain]
12:54:57.001353 IP cm-134-196-90-62.revip18.asianet.co.th.49321 > saixiii.com.ssh: [|tcp]
12:54:57.015887 IP dns1.asianet.co.th.domain > saixiii.com.55252: [|domain]
12:54:57.016205 IP saixiii.com.ssh > cm-134-196-90-62.revip18.asianet.co.th.49321: [|tcp]
12:54:57.016215 IP saixiii.com.43351 > dns1.asianet.co.th.domain: [|domain]
12:54:57.029989 IP dns1.asianet.co.th.domain > saixiii.com.43351: [|domain]

 

3. เขียน packet ลง file

เราสามารถ save ลง file เพื่อเอาไปเปิดดูอย่างละเอียดด้วย wireshark หลังงจาก break แล้วจะพบว่า tcpdump จะแสดงข้อมูล จำนวน packet ที่เขียนลง file เพราะฉะนั้นระวังเรื่องการเปิดทิ้งไว้นานๆ หรือ ใช้งานบนอุปกรณ์ที่มีข้อมูลวิ่งผ่านจำนวนมาก เพราะจะทำให้ disk เต็มได้

ใช้ option -w ตามด้วยชื่อ file ที่ตั้งขึ้น

# tcpdump -w <filename>
$ sudo tcpdump -i enp2s0 -s 50 -w output.cap
tcpdump: listening on enp2s0, link-type EN10MB (Ethernet), capture size 50 bytes
15 packets captured
22 packets received by filter
0 packets dropped by kernel

 

4. การ filter packet

ส่วนสุดท้ายคือส่วนที่ยากที่สุด เพราะเป็นส่วน filter expression ที่เราจะต้องเขียนขึ้นเองตามความต้องการที่เราหวังไว้ ว่าอยากจับ packet ที่ port หรือ ip อะไร รวมถึง protocol ลักษณะไหน ซึ่งตรงนี้ค่อยข้างซับซ้อนมาก สามารถใช้ and or expression ร่วมด้วยได้ ซึ่งตัวอย่างผมลองจับ packet ที่ port 80 เพื่อดูการ display website http

# tcpdump <filter expression>
$ sudo tcpdump -i enp2s0 -s 50 port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0, link-type EN10MB (Ethernet), capture size 50 bytes
13:05:40.419302 IP ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010 > saixiii.com.http: [|tcp]
13:05:40.419383 IP saixiii.com.http > ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010: [|tcp]
13:05:40.460443 IP ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010 > saixiii.com.http: [|tcp]
13:05:40.460471 IP ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010 > saixiii.com.http: [|tcp]
13:05:40.460512 IP saixiii.com.http > ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010: [|tcp]
13:05:40.460804 IP saixiii.com.http > ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010: [|tcp]
13:05:40.460878 IP saixiii.com.http > ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010: [|tcp]
13:05:40.502052 IP ec2-52-74-97-174.ap-southeast-1.compute.amazonaws.com.55010 > saixiii.com.http: [|tcp]

ทั้งหมดนี้น่าจะพอให้คำตอบได้ว่า tcpdump คือ อะไร ใช้งานอย่างไร

 

Author: Suphakit Annoppornchai

Credit: https://saixiii.com, https://en.wikipedia.org

 

Leave a Reply