# 2. Network Protocols and Traffic Analysis ## โปรโตคอลเครือข่ายและการวิเคราะห์ทราฟฟิก **ผู้จัดทำ:** อรรถพล คงหวาน --- # Outline * 2.1 ทบทวน OSI Model และ TCP/IP Stack ในมุมมองด้านความมั่นคงปลอดภัย * 2.2 โปรโตคอลระดับ Network และ Transport Layer: IP, TCP, UDP, ICMP * 2.3 เครื่องมือวิเคราะห์เครือข่าย: Wireshark, tcpdump, nmap * 2.4 Packet Inspection และ Deep Packet Inspection (DPI) * 2.5 Network Traffic Analysis และการตรวจจับความผิดปกติ * 2.6 Multimedia Network Protocols: RTP, RTSP, VoIP Security * 2.7 โปรโตคอล DNS, DHCP และปัญหาความมั่นคงปลอดภัยที่เกี่ยวข้อง --- ## 2.1 ทบทวน OSI Model และ TCP/IP Stack ในมุมมองด้านความมั่นคงปลอดภัย --- ### 2.1.1 OSI Model — ภาพรวมและความสัมพันธ์กับความมั่นคงปลอดภัย **OSI Model (Open Systems Interconnection Model)** คือกรอบแนวคิดที่แบ่งกระบวนการสื่อสารเครือข่ายออกเป็น 7 ชั้น (Layer) แต่ละชั้นมีหน้าที่เฉพาะ และมีช่องโหว่ด้านความมั่นคงปลอดภัยที่แตกต่างกัน การเข้าใจ OSI Model จากมุมมองด้านความมั่นคงปลอดภัยช่วยให้เราสามารถ: * ระบุจุดโจมตี (Attack Surface) ในแต่ละชั้น * ออกแบบมาตรการป้องกันได้อย่างเป็นระบบ * เข้าใจว่าการโจมตีแต่ละประเภทเกิดขึ้นที่ชั้นใด --- ### 2.1.1 OSI Model — มุมมองด้านความมั่นคงปลอดภัย ```mermaid graph LR L7["ชั้นที่ 7: Application Layer
(ชั้นแอปพลิเคชัน)
HTTP, FTP, SMTP, DNS
⚠️ XSS, SQLi, Phishing"] L6["ชั้นที่ 6: Presentation Layer
(ชั้นการนำเสนอ)
SSL/TLS, MIME, Encoding
⚠️ Man-in-the-Middle, SSL Stripping"] L5["ชั้นที่ 5: Session Layer
(ชั้นการเชื่อมต่อ)
SMB, RPC, NetBIOS
⚠️ Session Hijacking, Replay Attack"] L4["ชั้นที่ 4: Transport Layer
(ชั้นการส่งผ่าน)
TCP, UDP, SCTP
⚠️ SYN Flood, Port Scanning"] L3["ชั้นที่ 3: Network Layer
(ชั้นเครือข่าย)
IP, ICMP, OSPF, BGP
⚠️ IP Spoofing, Route Hijacking"] L2["ชั้นที่ 2: Data Link Layer
(ชั้นข้อมูล)
Ethernet, ARP, MAC
⚠️ ARP Spoofing, MAC Flooding"] L1["ชั้นที่ 1: Physical Layer
(ชั้นกายภาพ)
สายเคเบิล, สัญญาณ
⚠️ Wiretapping, Jamming"] L7 --> L6 --> L5 --> L4 --> L3 --> L2 --> L1 style L7 fill:#cc241d,color:#ebdbb2 style L6 fill:#d65d0e,color:#ebdbb2 style L5 fill:#d79921,color:#282828 style L4 fill:#98971a,color:#ebdbb2 style L3 fill:#458588,color:#ebdbb2 style L2 fill:#b16286,color:#ebdbb2 style L1 fill:#689d6a,color:#ebdbb2 ``` --- ### 2.1.2 TCP/IP Stack เทียบกับ OSI Model **TCP/IP Stack** เป็นโมเดลที่ใช้งานจริงในอินเทอร์เน็ต ประกอบด้วย 4 ชั้น โดยรวมหลายชั้นของ OSI เข้าด้วยกัน | OSI Layer | TCP/IP Layer | โปรโตคอลหลัก | ภัยคุกคามที่พบบ่อย | |-----------|--------------|---------------|---------------------| | Application, Presentation, Session | Application (แอปพลิเคชัน) | HTTP, HTTPS, DNS, SMTP, SSH | XSS, SQLi, DNS Poisoning | | Transport | Transport (การส่งผ่าน) | TCP, UDP | SYN Flood, UDP Flood | | Network | Internet (อินเทอร์เน็ต) | IP, ICMP, ARP | IP Spoofing, Smurf Attack | | Data Link, Physical | Network Access (การเข้าถึงเครือข่าย) | Ethernet, Wi-Fi, ARP | ARP Spoofing, Sniffing | --- ### 2.1.3 เป้าหมายการโจมตีในแต่ละชั้น (Attack Targets per Layer) ในมุมมองด้านความมั่นคงปลอดภัย ผู้โจมตีมักเลือกโจมตีชั้นที่ให้ผลได้สูงสุด: * **ชั้น Application (L7):** เป้าหมายยอดนิยมเพราะมีความซับซ้อนสูงและมักมีช่องโหว่ในโค้ด * **ชั้น Transport (L4):** เป้าหมายสำหรับการโจมตีแบบ DoS/DDoS เพื่อทำให้บริการล่ม * **ชั้น Network (L3):** เป้าหมายสำหรับการปลอมแปลงที่อยู่ IP และเปลี่ยนเส้นทางการรับส่งข้อมูล * **ชั้น Data Link (L2):** เป้าหมายสำหรับการดักข้อมูลในเครือข่ายท้องถิ่น (LAN) --- ### 2.1.3 ตัวอย่างการตรวจสอบ OSI Stack ด้วย Linux (1/2) ```bash # ดูข้อมูล Network Interface (Layer 1-2) ip link show # ดูตาราง ARP (Layer 2-3) arp -n ip neigh show # ดูตาราง Routing (Layer 3) ip route show netstat -rn ``` --- ### 2.1.3 ตัวอย่างการตรวจสอบ OSI Stack ด้วย Linux (2/2) ```bash # ดูการเชื่อมต่อ TCP/UDP ที่ใช้งานอยู่ (Layer 4) ss -tulpn netstat -tulpn # ดูการเชื่อมต่อแอปพลิเคชัน (Layer 7) ss -tulpn | grep LISTEN ``` **ตัวอย่างผลลัพธ์ที่คาดหวัง:** ``` # ip route show default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.100 metric 100 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 # ss -tulpn tcp LISTEN 0 128 0.0.0.0:22 users:(("sshd",pid=1234,fd=3)) tcp LISTEN 0 511 0.0.0.0:80 users:(("nginx",pid=5678,fd=6)) ``` --- ## 2.2 โปรโตคอลระดับ Network และ Transport Layer: IP, TCP, UDP, ICMP --- ### 2.2.1 Internet Protocol (IP) — ความมั่นคงปลอดภัยและช่องโหว่ **IP (Internet Protocol)** คือโปรโตคอลหลักของชั้น Network ทำหน้าที่ระบุที่อยู่และนำส่งแพ็กเก็ต (Packet) จากต้นทางไปยังปลายทาง **ฟิลด์สำคัญด้านความมั่นคงปลอดภัย:** * **TTL (Time to Live):** ค่าที่ลดลงทีละ 1 ทุกครั้งที่ผ่าน Router หากเป็น 0 แพ็กเก็ตจะถูกทิ้ง ใช้ตรวจจับ Traceroute ได้ * **Source IP:** สามารถถูกปลอมแปลง (Spoofing) ได้ — เป็นพื้นฐานของการโจมตีหลายรูปแบบ * **Protocol:** ระบุโปรโตคอลของชั้นถัดไป (6=TCP, 17=UDP, 1=ICMP) * **Fragment Offset:** ใช้ในการโจมตีแบบ IP Fragmentation Attack --- ### 2.2.1 โครงสร้าง IPv4 Header ```mermaid graph LR subgraph IPv4Header["IPv4 Header (20-60 bytes)"] direction LR A~~~B~~~C~~~D~~~E~~~F~~~G~~~H~~~I~~~J~~~K~~~L~~~M A["Version
4 bits"] B["IHL
4 bits"] C["DSCP/ECN
8 bits"] D["Total Length
16 bits"] E["Identification
16 bits"] F["Flags
3 bits"] G["Fragment Offset
13 bits"] H["TTL
8 bits"] I["Protocol
8 bits"] J["Header Checksum
16 bits"] K["Source IP
32 bits"] L["Destination IP
32 bits"] M["Options
(variable)"] end style A fill:#458588,color:#ebdbb2 style K fill:#cc241d,color:#ebdbb2 style L fill:#98971a,color:#ebdbb2 style H fill:#d79921,color:#282828 style I fill:#b16286,color:#ebdbb2 ``` --- ### 2.2.1 ตัวอย่างการตรวจสอบ IP Header ด้วย Linux ```bash # ส่ง ping พร้อมดู TTL ping -c 4 8.8.8.8 # ตรวจสอบค่า TTL เพื่อประมาณระบบปฏิบัติการปลายทาง # Linux: TTL=64, Windows: TTL=128, Cisco: TTL=255 ping -c 1 192.168.1.1 | grep ttl # ดู Packet ด้วย tcpdump พร้อม IP Header sudo tcpdump -i eth0 -v 'ip' | head -30 ``` --- ### 2.2.2 Transmission Control Protocol (TCP) **TCP (Transmission Control Protocol)** คือโปรโตคอลที่เน้นความน่าเชื่อถือ (Reliable) ในชั้น Transport มีกระบวนการ **Three-Way Handshake** เพื่อสร้างการเชื่อมต่อ **Three-Way Handshake:** * **SYN** — Client ส่งคำขอเชื่อมต่อ (seq=x) * **SYN-ACK** — Server ตอบรับและส่งคำขอยืนยัน (seq=y, ack=x+1) * **ACK** — Client ยืนยันการเชื่อมต่อ (ack=y+1) --- ### 2.2.2 Three-Way Handshake และ SYN Flood Attack ```mermaid sequenceDiagram participant C as Client (ไคลเอนต์) participant S as Server (เซิร์ฟเวอร์) participant A as Attacker (ผู้โจมตี) Note over C,S: การเชื่อมต่อปกติ (Normal Connection) C->>S: SYN (seq=x) — ขอเชื่อมต่อ S->>C: SYN-ACK (seq=y, ack=x+1) — ตอบรับ C->>S: ACK (ack=y+1) — ยืนยันการเชื่อมต่อ Note over C,S: ✅ เชื่อมต่อสำเร็จ (Connection Established) Note over A,S: SYN Flood Attack (การโจมตีแบบ SYN Flood) A->>S: SYN (src=1.2.3.4 ปลอม) A->>S: SYN (src=5.6.7.8 ปลอม) A->>S: SYN (src=9.10.11.12 ปลอม) Note over S: ❌ SYN Queue เต็ม — ปฏิเสธการเชื่อมต่อใหม่ ``` --- ### 2.2.2 โครงสร้าง TCP Flags | Flag | ค่า (Hex) | ความหมาย | การใช้ในการโจมตี | |------|-----------|-----------|-----------------| | SYN | 0x02 | เริ่มการเชื่อมต่อ | SYN Flood, SYN Scan | | ACK | 0x10 | ตอบรับการรับข้อมูล | ACK Scan (ผ่าน Stateless Firewall) | | FIN | 0x01 | ปิดการเชื่อมต่อ | FIN Scan | | RST | 0x04 | รีเซ็ตการเชื่อมต่อ | TCP RST Attack | | PSH | 0x08 | ส่งข้อมูลทันที | — | | URG | 0x20 | ข้อมูลเร่งด่วน | — | | NULL | 0x00 | ไม่มี Flag | NULL Scan | --- ### 2.2.2 การคำนวณ TCP Sequence Number **Sequence Number** ใช้ระบุลำดับข้อมูล และหากคาดเดาได้ อาจนำไปสู่การ Hijack Session
Sequence Number ถัดไป
=
Sequence Number ปัจจุบัน
+
ขนาดข้อมูลที่ส่ง (bytes)
**ตัวอย่างการคำนวณ:** * ส่งข้อมูล 1460 bytes (ขนาด MSS ทั่วไปบน Ethernet) เริ่มต้นจาก Seq=1000 * Seq ถัดไป = 1000 + 1460 = **2460** * หากส่งอีก 1460 bytes: Seq ถัดไป = 2460 + 1460 = **3920** --- ### 2.2.2 การวิเคราะห์ TCP ด้วย Linux (1/2) ```bash # ดู TCP Connection States ทั้งหมด ss -tan # ดูเฉพาะ SYN_RECV (อาจบ่งชี้ SYN Flood) ss -tan | grep SYN_RECV | wc -l # ดู TCP Statistics cat /proc/net/netstat | grep -i syn # ดู TCP Flags ด้วย tcpdump sudo tcpdump -i eth0 'tcp[13] & 2 != 0' -n # tcp[13] คือ byte ที่ 13 ของ TCP header = Flags byte # 2 = SYN flag (binary: 00000010) ``` --- ### 2.2.2 การวิเคราะห์ TCP ด้วย Linux (2/2) ```bash # นับจำนวน SYN ที่ไม่มี ACK (บ่งชี้ SYN Flood) sudo tcpdump -i eth0 'tcp[tcpflags] == tcp-syn' -n -c 100 2>/dev/null ``` --- ### 2.2.3 User Datagram Protocol (UDP) **UDP (User Datagram Protocol)** เป็นโปรโตคอลที่ไม่เน้นความน่าเชื่อถือ (Connectionless) ส่งข้อมูลได้เร็วกว่า TCP แต่ไม่มีการตรวจสอบว่าข้อมูลถึงปลายทางหรือไม่ เหมาะกับงานที่ต้องการความเร็ว เช่น VoIP, DNS, Video Streaming **จุดอ่อนของ UDP:** * **UDP Amplification Attack:** ผู้โจมตีส่ง Request ขนาดเล็กไปยังเซิร์ฟเวอร์ (เช่น DNS, NTP) โดยปลอม Source IP เป็น IP ของเหยื่อ เซิร์ฟเวอร์จะตอบกลับด้วยข้อมูลขนาดใหญ่ไปยังเหยื่อ * **UDP Flood:** ส่ง UDP Packet จำนวนมากไปยังพอร์ตสุ่ม ทำให้เซิร์ฟเวอร์เสียเวลาตอบกลับ --- ### 2.2.3 การคำนวณ Amplification Factor
Amplification Factor
=
ขนาด Response (bytes)
ขนาด Request (bytes)
**ตัวอย่างการคำนวณ DNS Amplification:** * DNS Request (ANY record): 28 bytes * DNS Response (ALL records): 3,000 bytes * Amplification Factor = 3,000 ÷ 28 ≈ **107 เท่า** * ผู้โจมตีใช้แบนด์วิดท์เพียง 1 Mbps สามารถสร้างทราฟฟิกโจมตีได้ถึง 107 Mbps --- ### 2.2.3 ตรวจสอบ UDP ด้วย Linux ```bash # ทดสอบ UDP port scanning ด้วย nmap sudo nmap -sU -p 53,67,123,161 192.168.1.0/24 # ดู UDP connections ss -uanp # ตรวจสอบ UDP traffic ด้วย tcpdump sudo tcpdump -i eth0 udp -n ``` --- ### 2.2.4 Internet Control Message Protocol (ICMP) **ICMP (Internet Control Message Protocol)** ใช้ส่งข้อความควบคุมและรายงานข้อผิดพลาดในชั้น Network เช่น ข้อความ "Destination Unreachable" หรือการตอบสนองต่อ `ping` | Type | Code | ความหมาย | ความเสี่ยง | |------|------|-----------|-----------| | 0 | 0 | Echo Reply (ping ตอบกลับ) | ใช้ใน Ping Sweep | | 3 | 0-15 | Destination Unreachable | เปิดเผยโครงสร้างเครือข่าย | | 5 | 0-3 | Redirect | ICMP Redirect Attack | | 8 | 0 | Echo Request (ping) | ใช้ใน Ping Sweep, Ping Flood | | 11 | 0 | Time Exceeded (TTL=0) | ใช้ใน Traceroute | | 30 | 0 | Traceroute | เปิดเผยเส้นทางเครือข่าย | --- ### 2.2.4 การโจมตีผ่าน ICMP * **Ping of Death (PoD):** ส่ง ICMP Packet ขนาดใหญ่เกิน 65,535 bytes ทำให้ระบบล่ม (ปัจจุบันได้รับการแก้ไขแล้ว) * **Smurf Attack:** ส่ง ICMP Echo Request ไปยัง Broadcast Address โดยปลอม Source IP เป็น IP เหยื่อ * **ICMP Tunneling:** ซ่อนข้อมูลไว้ใน ICMP Payload เพื่อหลบเลี่ยง Firewall --- ### 2.2.4 ตรวจสอบ ICMP ด้วย Linux ```bash # ทดสอบ ICMP พื้นฐาน ping -c 4 -s 1400 192.168.1.1 # ระบุขนาด packet # Traceroute ผ่าน ICMP traceroute -I 8.8.8.8 # -I ใช้ ICMP traceroute -T -p 80 8.8.8.8 # -T ใช้ TCP # ตรวจสอบ ICMP ด้วย tcpdump sudo tcpdump -i eth0 icmp -n # บล็อก ICMP ด้วย iptables (ตัวอย่าง) sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP # ดูสถิติ ICMP cat /proc/net/snmp | grep Icmp ``` --- ## 2.3 เครื่องมือวิเคราะห์เครือข่าย: Wireshark, tcpdump, nmap --- ### 2.3.1 tcpdump — เครื่องมือ Packet Capture บรรทัดคำสั่ง **tcpdump** คือเครื่องมือ Command-line สำหรับดักจับและวิเคราะห์ Packet บน Linux/Unix ใช้ Library ชื่อ **libpcap** ในการจับข้อมูล ```bash # ติดตั้ง tcpdump sudo apt install tcpdump # Ubuntu/Debian sudo pacman -S tcpdump # Arch Linux / CachyOS # ดูรายการ Network Interface sudo tcpdump -D # จับ Packet บน Interface eth0 แบบ Verbose sudo tcpdump -i eth0 -v ``` --- ### 2.3.1 tcpdump — คำสั่งพื้นฐาน (ต่อ) ```bash # จับ Packet และบันทึกเป็นไฟล์ .pcap sudo tcpdump -i eth0 -w capture.pcap # อ่านไฟล์ .pcap ที่บันทึกไว้ sudo tcpdump -r capture.pcap -n # จำกัดจำนวน Packet ที่จับ sudo tcpdump -i eth0 -c 100 -w capture.pcap ``` --- ### 2.3.1 BPF Filter (Berkeley Packet Filter) — การกรอง Packet (1/2) ```bash # กรองเฉพาะ Traffic จาก/ไปยัง Host เฉพาะ sudo tcpdump -i eth0 host 192.168.1.100 # กรองเฉพาะ Traffic บน Port 80 (HTTP) sudo tcpdump -i eth0 port 80 # กรอง TCP Traffic บน Port 443 (HTTPS) sudo tcpdump -i eth0 tcp port 443 # กรอง Traffic จาก Source เฉพาะ sudo tcpdump -i eth0 src host 10.0.0.1 # กรอง Traffic ไปยัง Destination เฉพาะ sudo tcpdump -i eth0 dst host 8.8.8.8 ``` --- ### 2.3.1 BPF Filter — การกรอง Packet (2/2) ```bash # รวม Filter หลายเงื่อนไข sudo tcpdump -i eth0 '(src host 192.168.1.100) and (dst port 80 or dst port 443)' # กรอง DNS Traffic (UDP Port 53) sudo tcpdump -i eth0 udp port 53 # ดู HTTP Request Headers sudo tcpdump -i eth0 -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' # ตรวจจับ SYN Scan (nmap -sS) sudo tcpdump -i eth0 'tcp[tcpflags] == tcp-syn' -n ``` --- ### 2.3.1 ตัวอย่างการวิเคราะห์ — ตรวจจับ nmap Scan ```bash # สถานการณ์: สงสัยมีการ Scan ด้วย nmap # ตรวจสอบ SYN ที่ไม่มี ACK ตอบกลับ (Half-Open Scan) sudo tcpdump -i eth0 -n 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn' 2>/dev/null | head -20 ``` **ตัวอย่างผลลัพธ์:** ``` 14:23:15.123456 IP 203.0.113.10.45678 > 192.168.1.100.22: Flags [S] 14:23:15.123789 IP 203.0.113.10.45679 > 192.168.1.100.23: Flags [S] 14:23:15.124012 IP 203.0.113.10.45680 > 192.168.1.100.25: Flags [S] ``` > **สังเกต:** IP เดียวกัน Port ปลายทางเพิ่มทีละ 1 = สัญญาณของ Port Scan! --- ### 2.3.2 Wireshark — GUI Packet Analyzer **Wireshark** คือเครื่องมือวิเคราะห์ Packet แบบ GUI ที่ทรงพลังที่สุด สามารถถอดรหัส (Decode) โปรโตคอลได้หลายร้อยชนิด ```bash # ติดตั้ง sudo apt install wireshark # Ubuntu/Debian sudo pacman -S wireshark-qt # Arch Linux / CachyOS # อนุญาตให้ User ที่ไม่ใช่ root ใช้ Wireshark sudo usermod -aG wireshark $USER newgrp wireshark # รัน Wireshark แบบ CLI (TShark) sudo apt install tshark ``` --- ### 2.3.2 TShark — Command-line Version ของ Wireshark (1/2) ```bash # จับ Packet ด้วย TShark sudo tshark -i eth0 -c 100 # Filter HTTP Traffic และแสดง URL sudo tshark -i eth0 -Y 'http.request' -T fields \ -e ip.src -e http.host -e http.request.uri ``` **ตัวอย่างผลลัพธ์:** ``` 192.168.1.50 www.example.com /index.html 192.168.1.50 api.service.com /v1/users ``` --- ### 2.3.2 TShark — Command-line Version ของ Wireshark (2/2) ```bash # วิเคราะห์ไฟล์ .pcap sudo tshark -r capture.pcap -Y 'tcp.flags.syn==1 && tcp.flags.ack==0' \ -T fields -e ip.src -e tcp.dstport | sort | uniq -c | sort -rn | head -20 # หา DNS Queries ที่ผิดปกติ sudo tshark -r capture.pcap -Y 'dns.qry.type==1' \ -T fields -e ip.src -e dns.qry.name | sort | uniq -c | sort -rn ``` --- ### 2.3.2 Display Filters ที่ใช้บ่อยใน Wireshark | Filter | ความหมาย | |--------|-----------| | `ip.addr == 192.168.1.1` | ทุก Packet ที่เกี่ยวกับ IP นี้ | | `tcp.port == 80` | TCP บน Port 80 | | `http.request.method == "POST"` | HTTP POST Request | | `dns.qry.name contains "evil"` | DNS Query ที่มีคำว่า "evil" | | `tcp.flags.syn == 1 && tcp.flags.ack == 0` | SYN Packet เท่านั้น | | `!(arp or icmp or dns)` | ไม่แสดง ARP, ICMP, DNS | | `frame.time_delta > 5` | Packet ที่ใช้เวลา > 5 วินาที | | `tcp.analysis.retransmission` | TCP Retransmission (อาจบ่งชี้ปัญหาเครือข่าย) | --- ### 2.3.3 nmap — Network Mapper **nmap** เป็นเครื่องมือ Network Scanning ที่ทรงพลัง ใช้ทั้งในการ Penetration Testing และการตรวจสอบความมั่นคงปลอดภัย ```mermaid graph TD subgraph nmap["nmap Scan Types — ประเภทการสแกน"] A["nmap Target"] B["Ping Scan
-sn
ตรวจว่า Host ออนไลน์"] C["TCP SYN Scan
-sS (Default)
Stealth Scan"] D["TCP Connect
-sT
Full Connection"] E["UDP Scan
-sU
ตรวจ UDP Ports"] F["Version Detection
-sV
หาเวอร์ชันบริการ"] G["OS Detection
-O
หาระบบปฏิบัติการ"] H["Script Scan
-sC / --script
NSE Scripts"] end A --> B A --> C A --> D A --> E C --> F F --> G G --> H style A fill:#458588,color:#ebdbb2 style C fill:#98971a,color:#ebdbb2 style H fill:#cc241d,color:#ebdbb2 ``` --- ### 2.3.3 คำสั่ง nmap ที่ใช้บ่อย (1/2) ```bash # ติดตั้ง nmap sudo apt install nmap # สแกนหา Host ที่ออนไลน์ใน subnet sudo nmap -sn 192.168.1.0/24 # สแกน Port พื้นฐาน (SYN Scan) — ต้องใช้ root sudo nmap -sS 192.168.1.100 # สแกน Port เฉพาะ sudo nmap -p 22,80,443,3306,8080 192.168.1.100 # สแกนทุก Port (1-65535) sudo nmap -p- 192.168.1.100 ``` --- ### 2.3.3 คำสั่ง nmap ที่ใช้บ่อย (2/2) ```bash # ตรวจหาเวอร์ชันบริการและ OS sudo nmap -sV -O 192.168.1.100 # สแกนแบบครบถ้วน (Aggressive) sudo nmap -A 192.168.1.100 # ใช้ NSE Script ตรวจหาช่องโหว่ sudo nmap --script=vuln 192.168.1.100 # ตรวจหา SMB ที่มีช่องโหว่ (EternalBlue) sudo nmap --script=smb-vuln-ms17-010 192.168.1.0/24 # สแกนแบบเงียบ (Slow/Stealth) เพื่อหลีกเลี่ยง IDS sudo nmap -sS -T1 --max-retries 1 192.168.1.100 # บันทึกผลลัพธ์เป็น XML sudo nmap -sV -oX scan_result.xml 192.168.1.0/24 ``` --- ### 2.3.3 ตัวอย่างผลลัพธ์ nmap ``` Starting Nmap 7.94 ( https://nmap.org ) Nmap scan report for 192.168.1.100 Host is up (0.00045s latency). PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 8.9p1 Ubuntu 80/tcp open http nginx 1.22.1 443/tcp open https nginx 1.22.1 3306/tcp open mysql MySQL 8.0.33 8080/tcp closed http-proxy OS details: Linux 5.15 - 6.1 ``` --- ## 2.4 Packet Inspection และ Deep Packet Inspection (DPI) --- ### 2.4.1 ระดับของ Packet Inspection **Packet Inspection** คือกระบวนการตรวจสอบเนื้อหาของ Network Packet แบ่งระดับตามความลึกของการตรวจสอบ ```mermaid graph LR subgraph levels["ระดับการตรวจสอบ Packet"] direction LR A["Shallow Packet Inspection
(การตรวจสอบระดับตื้น)
• IP Header
• TCP/UDP Header
• Port Number
• เร็ว, ใช้ทรัพยากรน้อย"] B["Stateful Inspection
(การตรวจสอบสถานะ)
• ติดตาม Connection State
• TCP Flags
• Session Table
• ปานกลาง"] C["Deep Packet Inspection (DPI)
(การตรวจสอบระดับลึก)
• Application Payload
• Content/Keywords
• Protocol Analysis
• ช้า, ใช้ทรัพยากรมาก"] end A --> B --> C style A fill:#98971a,color:#ebdbb2 style B fill:#d79921,color:#282828 style C fill:#cc241d,color:#ebdbb2 ``` --- ### 2.4.2 Deep Packet Inspection (DPI) **DPI (Deep Packet Inspection)** คือเทคนิคการตรวจสอบ Packet ที่วิเคราะห์ได้ถึงระดับ Application Layer Payload (เนื้อหาจริง) ไม่ใช่แค่ Header **การทำงานของ DPI:** 1. จับ Packet จาก Network Interface 2. Reassemble TCP Segments ให้เป็นข้อมูลที่สมบูรณ์ 3. วิเคราะห์ Protocol ของ Application Layer 4. ตรวจสอบเนื้อหาเทียบกับ Pattern/Signature 5. ตัดสินใจว่าจะ Allow, Block, หรือ Log --- ### 2.4.2 กรณีการใช้งาน DPI **กรณีการใช้งาน DPI:** * **ISP:** ตรวจจับและจำกัด BitTorrent, VoIP บางประเภท * **Enterprise Firewall (NGFW):** ตรวจจับ Malware, Data Exfiltration * **IDS/IPS:** ตรวจจับ Intrusion ตาม Signature * **Content Filtering:** บล็อก Website หรือ Content บางประเภท --- ### 2.4.2 เครื่องมือ DPI บน Linux ```bash # ndpi — Open-source DPI Library git clone https://github.com/ntop/nDPI cd nDPI && ./autogen.sh && make sudo make install # ใช้ Suricata (IDS ที่มี DPI ในตัว) สำหรับตรวจสอบ Traffic sudo apt install suricata sudo suricata -c /etc/suricata/suricata.yaml -i eth0 # ใช้ ntopng สำหรับ Network Traffic Monitoring พร้อม DPI sudo apt install ntopng sudo ntopng -i eth0 -w 3000 # เข้าใช้ผ่าน Browser: http://localhost:3000 ``` --- ### 2.4.3 ข้อพิจารณาด้านความเป็นส่วนตัว (Privacy) | ประเด็น | รายละเอียด | |---------|-----------| | **ความเป็นส่วนตัว** | DPI สามารถอ่านเนื้อหาของ Packet ได้ หากไม่มีการเข้ารหัส | | **TLS/HTTPS** | DPI ไม่สามารถอ่านเนื้อหาได้หากใช้การเข้ารหัส แต่สามารถวิเคราะห์ Metadata ได้ | | **SSL Inspection** | องค์กรบางแห่งทำ SSL Bump (MITM) เพื่อให้ DPI ทำงานกับ HTTPS ได้ | | **กฎหมาย** | การใช้ DPI อาจต้องได้รับอนุญาตตามกฎหมาย พ.ร.บ. คอมพิวเตอร์ของไทย | --- ## 2.5 Network Traffic Analysis และการตรวจจับความผิดปกติ --- ### 2.5.1 หลักการวิเคราะห์ Traffic เครือข่าย **Network Traffic Analysis (NTA)** คือกระบวนการรวบรวม, วิเคราะห์ และตีความข้อมูล Traffic เครือข่าย เพื่อระบุพฤติกรรมปกติ (Baseline) และตรวจจับความผิดปกติ (Anomaly) --- ### 2.5.1 กระบวนการ NTA ```mermaid flowchart TB subgraph collect["1\. รวบรวมข้อมูล - (Data Collection)"] direction LR C1~~~C2~~~C3~~~C4 C1["SPAN/Mirror Port"] C2["NetFlow / sFlow"] C3["Packet Capture"] C4["Log Files"] end subgraph process["2\. ประมวลผล - (Processing)"] direction LR P1~~~P2~~~P3 P1["Flow Aggregation"] P2["Protocol Parsing"] P3["Metadata Extraction"] end subgraph analyze["3\. วิเคราะห์ - (Analysis)"] direction LR A1~~~A2~~~A3~~~A4 A1["Baseline Comparison
เปรียบเทียบกับค่าปกติ"] A2["Statistical Analysis
วิเคราะห์สถิติ"] A3["Signature Matching
จับคู่ Signature"] A4["ML/AI Detection
ตรวจจับด้วย AI"] end subgraph respond["4\. ตอบสนอง - (Response)"] direction LR R1~~~R2~~~R3 R1["Alert / แจ้งเตือน"] R2["Block / บล็อก"] R3["Log / บันทึก"] end collect --> process --> analyze --> respond style collect fill:#458588,color:#ebdbb2 style process fill:#98971a,color:#ebdbb2 style analyze fill:#d79921,color:#282828 style respond fill:#cc241d,color:#ebdbb2 ``` --- ### 2.5.2 การสร้าง Baseline Traffic **Baseline** คือค่าปกติของ Traffic ที่ใช้เป็นเกณฑ์เปรียบเทียบ เพื่อตรวจจับความผิดปกติ **พารามิเตอร์สำคัญในการสร้าง Baseline:** * **จำนวน Packets per Second (PPS):** อัตราการส่ง Packet ต่อวินาที * **Bandwidth Utilization:** การใช้แบนด์วิดท์เฉลี่ย * **Protocol Distribution:** สัดส่วนของแต่ละโปรโตคอล (TCP:UDP:ICMP) * **Top Talkers:** Host ที่ส่ง Traffic มากที่สุด * **Port Distribution:** พอร์ตที่ใช้งานบ่อย * **Connection Duration:** ระยะเวลาเฉลี่ยของการเชื่อมต่อ --- ### 2.5.2 การคำนวณ Bandwidth Utilization
Bandwidth Utilization (%)
=
Traffic จริง (Mbps)
Bandwidth สูงสุด (Mbps)
×
100
**ตัวอย่างการคำนวณ:** * Bandwidth สูงสุด: 1 Gbps = 1,000 Mbps * Traffic ในชั่วโมงเร่งด่วน: 650 Mbps * Bandwidth Utilization = (650 ÷ 1,000) × 100 = **65%** * หาก Utilization เกิน 80% อย่างต่อเนื่อง อาจเป็นสัญญาณของ DDoS หรือต้องอัปเกรด Bandwidth --- ### 2.5.2 การคำนวณค่าเบี่ยงเบนมาตรฐาน (Standard Deviation)
ค่าเบี่ยงเบนมาตรฐาน (σ)
=
∑
i
=
1
n
(
x
i
-
x
¯
)
2
n
**ตัวอย่างการคำนวณ:** * เก็บข้อมูล PPS ใน 5 นาที: [1000, 1100, 950, 1050, 5000] * ค่าเฉลี่ย x̄ = (1000+1100+950+1050+5000) ÷ 5 = **1,820 PPS** * ค่าที่ 5 (5000 PPS) ห่างจากค่าเฉลี่ยมาก = **ผิดปกติ** ควรตรวจสอบ > **หมายเหตุ:** ค่า σ คือค่าเบี่ยงเบนมาตรฐาน, x̄ คือค่าเฉลี่ย, n คือจำนวนตัวอย่าง ใช้ตรวจจับ Traffic ที่ผิดปกติเกิน 2-3 σ --- ### 2.5.3 NetFlow — การวิเคราะห์ Traffic ระดับ Flow **NetFlow** คือโปรโตคอลจาก Cisco สำหรับรวบรวมข้อมูล IP Traffic เป็นข้อมูล Flow (กลุ่มของ Packet ที่มี 5-tuple เหมือนกัน: Src IP, Dst IP, Src Port, Dst Port, Protocol) ```bash # ติดตั้ง nfdump (NetFlow Analyzer) sudo apt install nfdump fprobe # เริ่มต้นเก็บ NetFlow จาก Interface sudo fprobe -i eth0 localhost:2055 # ส่ง Flow ไปยัง Collector ที่ Port 2055 # เริ่ม NetFlow Collector sudo nfcapd -b 127.0.0.1 -p 2055 -l /var/cache/nfdump/ ``` --- ### 2.5.3 NetFlow — การวิเคราะห์ข้อมูล Flow ```bash # วิเคราะห์ข้อมูล Flow nfdump -r /var/cache/nfdump/nfcapd.current -s ip/bytes | head -20 # ดู Top 10 IP ที่ใช้ Bandwidth มากที่สุด nfdump -r /var/cache/nfdump/nfcapd.current -s ip/bytes -n 10 -o line # ดูเฉพาะ Traffic บน Port 80 nfdump -r /var/cache/nfdump/nfcapd.current 'dst port 80' # สร้างกราฟด้วย nfexpire nfexpire -e /var/cache/nfdump/ -s 1G # จำกัดขนาด Storage ที่ 1GB ``` --- ### 2.5.4 รูปแบบ Traffic ที่ผิดปกติ (Anomaly Detection) | รูปแบบผิดปกติ | สัญญาณที่ตรวจพบ | ภัยคุกคามที่น่าสงสัย | |---------------|-----------------|---------------------| | PPS สูงผิดปกติ | > เฉลี่ย + 3σ | DDoS Attack | | Port Scan | SYN จาก 1 IP ไปหลาย Port | Reconnaissance | | Beaconing | เชื่อมต่อออกนอกทุก N วินาที | Malware C2 Communication | | Large Upload | Traffic ออกนอกสูงผิดปกติ | Data Exfiltration | | DNS Tunneling | DNS Query ขนาดใหญ่/บ่อยผิดปกติ | DNS Tunneling | | Lateral Movement | เชื่อมต่อระหว่าง Internal Host จำนวนมาก | APT / Worm | --- ### 2.5.4 การตรวจจับความผิดปกติด้วย Linux ```bash # ตรวจจับ Port Scan ด้วย iptables log sudo iptables -A INPUT -p tcp --tcp-flags ALL SYN -m recent --name PORTSCAN \ --set -j LOG --log-prefix "PortScan: " sudo iptables -A INPUT -p tcp --tcp-flags ALL SYN -m recent --name PORTSCAN \ --rcheck --seconds 60 --hitcount 20 -j DROP # ตรวจจับ Beaconing ด้วยการวิเคราะห์ Log awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20 # ใช้ ss เพื่อดู Connection ที่ผิดปกติ ss -tan | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -10 ``` --- ## 2.6 Multimedia Network Protocols: RTP, RTSP, VoIP Security --- ### 2.6.1 Real-time Transport Protocol (RTP) **RTP (Real-time Transport Protocol)** เป็นโปรโตคอลมาตรฐาน (RFC 3550) สำหรับการส่ง Audio และ Video แบบ Real-time บนเครือข่าย IP ทำงานร่วมกับ **RTCP (RTP Control Protocol)** เพื่อตรวจสอบคุณภาพการส่ง **พารามิเตอร์ RTP สำคัญ:** * **Sequence Number (16-bit):** หมายเลขลำดับแพ็กเก็ต ใช้ตรวจจับ Packet Loss และการเรียงลำดับ * **Timestamp (32-bit):** เวลาของ Sample แรกใน Packet ใช้สำหรับ Synchronization * **SSRC (Synchronization Source, 32-bit):** ระบุแหล่งที่มาของ Stream --- ### 2.6.1 RTP Protocol Stack ```mermaid graph TB subgraph rtp_stack["RTP Protocol Stack"] APP["Application
VoIP / Video Conferencing"] RTP["RTP (Real-time Transport Protocol)
เลขลำดับ, Timestamp, Payload Type"] RTCP["RTCP (Control Protocol)
สถิติคุณภาพ, Jitter, Packet Loss"] UDP["UDP (Transport Layer)
Port 5004-5005 (RTP/RTCP)"] IP["IP Layer"] end APP --> RTP APP --> RTCP RTP --> UDP RTCP --> UDP UDP --> IP style RTP fill:#98971a,color:#ebdbb2 style RTCP fill:#458588,color:#ebdbb2 style UDP fill:#b16286,color:#ebdbb2 ``` --- ### 2.6.1 การคำนวณ Jitter (ความผันแปรของ Delay)
Jitter
=
Jitter เดิม
+
|
Transit Time ปัจจุบัน
-
Transit Time ก่อนหน้า
|
-
Jitter เดิม
16
**ตัวอย่างการคำนวณ Jitter:** * Transit Time Packet N-1: 50 ms, Packet N: 80 ms * |80 - 50| = 30 ms (ผลต่าง), Jitter ปัจจุบัน = 0 (เริ่มต้น) * Jitter ใหม่ = 0 + (|30 - 0|) ÷ 16 = **1.875 ms** * ค่า Jitter ที่ยอมรับได้สำหรับ VoIP: **< 30 ms** --- ### 2.6.2 Session Initiation Protocol (SIP) และ VoIP Security **SIP (Session Initiation Protocol)** เป็นโปรโตคอลหลักสำหรับการสร้าง, แก้ไข และยกเลิก Multimedia Session (VoIP Calls, Video Calls) * ทำงานที่ Port **5060** (UDP/TCP) * ทำงานที่ Port **5061** (TLS) --- ### 2.6.2 SIP Call Flow ```mermaid sequenceDiagram participant UA1 as User Agent A
(ผู้โทร) participant P1 as SIP Proxy
(เซิร์ฟเวอร์) participant UA2 as User Agent B
(ผู้รับสาย) Note over UA1,UA2: SIP Call Flow — การโทร VoIP UA1->>P1: INVITE sip:bob@example.com P1->>UA2: INVITE sip:bob@example.com UA2->>P1: 100 Trying P1->>UA1: 100 Trying UA2->>P1: 180 Ringing (กำลังดัง) P1->>UA1: 180 Ringing UA2->>P1: 200 OK (รับสาย) P1->>UA1: 200 OK UA1->>UA2: ACK (ยืนยัน) Note over UA1,UA2: 📞 RTP Stream เริ่มต้น (เสียง/วิดีโอ) UA1->>UA2: BYE (วางสาย) UA2->>UA1: 200 OK ``` --- ### 2.6.2 การโจมตี VoIP ที่พบบ่อย | การโจมตี | คำอธิบาย | วิธีป้องกัน | |----------|-----------|------------| | SIP Scanning | สแกนหา SIP Server | ซ่อน SIP Port, ใช้ Fail2ban | | SIP Brute Force | เดา Password Extension | จำกัดจำนวนครั้งที่ Login | | RTP Eavesdropping | ดักฟัง Audio Stream | ใช้ SRTP (Secure RTP) | | SIP Injection | แทรกคำสั่ง SIP ปลอม | Validate และ Sanitize Input | | Toll Fraud | โทรออกผ่าน PBX โดยไม่ได้รับอนุญาต | ACL, Auth ที่แข็งแกร่ง | | VoIP Spam (SPIT) | ส่ง INVITE จำนวนมาก | Rate Limiting, CAPTCHA | --- ### 2.6.3 SRTP (Secure RTP) — การเข้ารหัส VoIP **SRTP (Secure Real-time Transport Protocol)** เพิ่มความมั่นคงปลอดภัยให้กับ RTP ด้วย: * **การเข้ารหัส (Encryption):** AES-128-CM (Counter Mode) หรือ AES-256-GCM * **การพิสูจน์ตัวตน (Authentication):** HMAC-SHA1 หรือ HMAC-SHA256 * **การป้องกัน Replay:** ด้วย Sequence Number --- ### 2.6.3 คำสั่งทดสอบ VoIP ```bash # ทดสอบ SIP Server ด้วย sipsak sudo apt install sipsak sipsak -s sip:192.168.1.100 -v # ตรวจสอบ SIP Server # ดัก VoIP Traffic ด้วย tcpdump sudo tcpdump -i eth0 -w voip_capture.pcap 'udp port 5060 or udp portrange 10000-20000' # วิเคราะห์ VoIP ด้วย sngrep (SIP Message Viewer) sudo apt install sngrep sudo sngrep -i eth0 -L voip.pcap # Capture และบันทึก sudo sngrep -I voip.pcap # อ่านจากไฟล์ # ตรวจสอบ SRTP ว่าใช้อยู่หรือไม่ sudo tcpdump -i eth0 -n 'udp portrange 10000-20000' | head -20 ``` --- ### 2.6.4 RTSP (Real-Time Streaming Protocol) **RTSP (Real-Time Streaming Protocol)** ใช้ควบคุม Streaming Media Server เช่น กล้อง IP, Streaming Platform ทำงานที่ Port **554** (RTSP) คล้ายกับ HTTP แต่สำหรับ Media Streaming ```bash # ทดสอบ RTSP Stream (เช่น IP Camera) sudo apt install ffmpeg # ดู RTSP Stream จาก IP Camera (ตัวอย่าง) ffmpeg -i rtsp://admin:password@192.168.1.200:554/stream1 -vcodec copy output.mp4 # สแกนหา RTSP Server ด้วย nmap sudo nmap -sU -p 554 --script rtsp-url-brute 192.168.1.0/24 # ดัก RTSP Traffic sudo tcpdump -i eth0 -w rtsp.pcap 'tcp port 554 or udp port 554' ``` --- ## 2.7 โปรโตคอล DNS, DHCP และปัญหาความมั่นคงปลอดภัยที่เกี่ยวข้อง --- ### 2.7.1 DNS — หลักการทำงานและโครงสร้าง **DNS (Domain Name System)** คือระบบแปลงชื่อโดเมน (Domain Name) เป็นหมายเลข IP ทำงานที่ Port **53** * **UDP** — สำหรับ Query ปกติ * **TCP** — สำหรับ Zone Transfer และ Response ขนาดใหญ่ --- ### 2.7.1 DNS Resolution Process ```mermaid sequenceDiagram participant Client as Client
(192.168.1.100) participant LocalDNS as Local DNS Resolver
(192.168.1.1) participant Root as Root Name Server
(.) participant TLD as TLD Name Server
(.com) participant Auth as Authoritative NS
(example.com) Client->>LocalDNS: ❓ www.example.com คือ IP อะไร? Note over LocalDNS: ตรวจสอบ Cache — ไม่พบ LocalDNS->>Root: ❓ www.example.com? Root->>LocalDNS: 💡 ไปถาม .com TLD Server ที่ 192.5.6.30 LocalDNS->>TLD: ❓ www.example.com? TLD->>LocalDNS: 💡 ไปถาม NS ของ example.com ที่ 205.251.196.1 LocalDNS->>Auth: ❓ www.example.com? Auth->>LocalDNS: ✅ www.example.com = 93.184.216.34 (TTL=3600) LocalDNS->>Client: ✅ 93.184.216.34 Note over LocalDNS: บันทึก Cache TTL=3600 วินาที ``` --- ### 2.7.1 ประเภทของ DNS Record | Record Type | ความหมาย | ตัวอย่าง | |------------|-----------|---------| | A | IPv4 Address | example.com → 93.184.216.34 | | AAAA | IPv6 Address | example.com → 2606:2800:220:1:248:1893:25c8:1946 | | MX | Mail Server | example.com → mail.example.com (priority 10) | | CNAME | Canonical Name (Alias) | www.example.com → example.com | | NS | Name Server | example.com → ns1.example.com | | TXT | Text Record | SPF, DKIM, DMARC records | | PTR | Reverse DNS (IP → Domain) | 34.216.184.93.in-addr.arpa → example.com | | SOA | Start of Authority | ข้อมูลหลักของ Zone | --- ### 2.7.2 การโจมตี DNS ```mermaid graph TD subgraph attacks["การโจมตี DNS — ประเภทหลัก"] A["DNS Cache Poisoning
(การวางยาพิษ Cache)
แทรก Record ปลอมใน Cache
เพื่อเปลี่ยนเส้นทาง"] B["DNS Hijacking
(การจี้ DNS)
เปลี่ยน DNS Server
ของเหยื่อเป็น Server ปลอม"] C["DNS Amplification DDoS
(การขยายการโจมตี)
ใช้ DNS เป็นอาวุธขยาย Traffic
สูงสุด 100x"] D["DNS Tunneling
(การสร้างอุโมงค์)
ซ่อน C2 Traffic ใน DNS Query
หลีกเลี่ยง Firewall"] E["NXDOMAIN Attack
(การโจมตีแบบ NXDOMAIN)
ส่ง Query Domain ที่ไม่มีจริง
ทำให้ Resolver ล้มเหลว"] F["DNS Zone Transfer
(การดึงข้อมูล Zone)
ดึงข้อมูล DNS ทั้งหมด
ด้วย AXFR Request"] end style A fill:#cc241d,color:#ebdbb2 style B fill:#d65d0e,color:#ebdbb2 style C fill:#d79921,color:#282828 style D fill:#98971a,color:#ebdbb2 style E fill:#458588,color:#ebdbb2 style F fill:#b16286,color:#ebdbb2 ``` --- ### 2.7.2 การทดลอง DNS ด้วย Linux Tools — Query พื้นฐาน ```bash # ค้นหา A Record dig www.example.com A nslookup www.example.com # ค้นหา MX Record dig example.com MX # ค้นหา TXT Record (สำหรับ SPF, DKIM) dig example.com TXT # ค้นหา Reverse DNS dig -x 8.8.8.8 host 8.8.8.8 # ทดสอบ DNS Zone Transfer (เฉพาะ Domain ที่ตัวเองดูแล!) dig @ns1.example.com example.com AXFR # หากสำเร็จ แสดงว่า DNS Server ตั้งค่าไม่ถูกต้อง! ``` --- ### 2.7.2 ตรวจสอบ DNS Cache และ Traffic ```bash # ดู DNS Cache บน systemd-resolved resolvectl statistics resolvectl flush-caches # ล้าง Cache # ตรวจสอบ DNS Traffic sudo tcpdump -i eth0 -n udp port 53 # ดู DNS Query ทั้งหมดพร้อม Domain Name sudo tcpdump -i eth0 -n udp port 53 | grep -oP 'A\? \K[^\s]+' # ทดสอบ DNS over TLS (DoT) kdig -d @1.1.1.1 +tls-ca example.com A # ต้องติดตั้ง knot-dnsutils ``` --- ### 2.7.2 ตรวจจับ DNS Tunneling **DNS Tunneling** ซ่อนข้อมูลใน Subdomain ที่ยาวมาก: * ปกติ: `www.example.com` * DNS Tunnel: `aGVsbG8gd29ybGQ=.evil-c2.com` (Base64 encoded data) ```bash # ตรวจจับ DNS Query ที่มี Subdomain ยาวผิดปกติ (> 50 chars) sudo tcpdump -i eth0 -n udp port 53 -l 2>/dev/null | \ awk '{for(i=1;i<=NF;i++){if($i ~ /\?$/){query=$(i-1);if(length(query) > 50){print "⚠️ Long DNS Query: " query}}}}' # วิเคราะห์จำนวน DNS Query จาก Host เดียวกัน (> 1000/นาที = ผิดปกติ) sudo tshark -i eth0 -Y 'dns.qry.type==1' -T fields -e ip.src \ 2>/dev/null | sort | uniq -c | sort -rn | head -10 ``` --- ### 2.7.3 DNSSEC — การเพิ่มความมั่นคงปลอดภัยให้ DNS **DNSSEC (DNS Security Extensions)** เพิ่มการรับรองความถูกต้อง (Integrity) ให้กับ DNS Response ด้วยการใช้ Digital Signature **องค์ประกอบของ DNSSEC:** * **RRSIG:** Resource Record Signature — ลายเซ็นดิจิทัลของ DNS Record * **DNSKEY:** กุญแจสาธารณะของ Zone สำหรับตรวจสอบลายเซ็น * **DS:** Delegation Signer — ใช้สร้าง Chain of Trust * **NSEC/NSEC3:** ยืนยันการไม่มีอยู่ของ Domain (Authenticated Denial of Existence) --- ### 2.7.3 คำสั่งตรวจสอบ DNSSEC ```bash # ตรวจสอบ DNSSEC ของ Domain dig +dnssec www.cloudflare.com dig +dnssec example.com DNSKEY # ตรวจสอบ DNSSEC Validation dig +sigchase www.iana.org A # ต้องการ dig ที่รองรับ +sigchase # ใช้ delv (DNS Lookup and Validation) สำหรับตรวจสอบ DNSSEC delv @8.8.8.8 www.example.com A +rtrace # ตรวจสอบว่า Domain ใช้ DNSSEC หรือไม่ dig example.com DS +short # มี Output = ใช้ DNSSEC ``` --- ### 2.7.4 DHCP — หลักการและการโจมตี **DHCP (Dynamic Host Configuration Protocol)** แจกจ่ายค่าการกำหนดเครือข่าย (IP Address, Subnet Mask, Gateway, DNS) ให้กับ Client โดยอัตโนมัติ * ทำงานที่ Port **67** (Server) และ Port **68** (Client) --- ### 2.7.4 กระบวนการ DHCP (DORA) และ Rogue DHCP Attack ```mermaid sequenceDiagram participant C as DHCP Client
(ไคลเอนต์ใหม่) participant S as DHCP Server
(เซิร์ฟเวอร์) participant R as Rogue DHCP Server
(เซิร์ฟเวอร์ปลอม) Note over C,S: DHCP DORA Process (ปกติ) C->>+S: 1. DISCOVER (Broadcast) — ขอ IP S-->>-C: 2. OFFER (IP: 192.168.1.100, GW: 192.168.1.1) C->>+S: 3. REQUEST (ขอ IP ที่ Server เสนอ) S-->>-C: 4. ACK (ยืนยัน IP พร้อม Lease Time) Note over C,S: ✅ Client ได้รับ IP 192.168.1.100 Note over C,R: Rogue DHCP Attack (การโจมตี) C->>R: 1. DISCOVER (Broadcast) R-->>C: 2. OFFER เร็วกว่า (IP: 192.168.1.200, GW: 10.0.0.1 ปลอม) C->>R: 3. REQUEST R-->>C: 4. ACK (Client ใช้ Gateway ปลอม = MITM!) ``` --- ### 2.7.4 การโจมตี DHCP | การโจมตี | หลักการ | ผลกระทบ | |---------|---------|---------| | **DHCP Starvation** | ส่ง DISCOVER พร้อม MAC ปลอมจำนวนมาก จนหมด IP Pool | Client ใหม่ไม่ได้รับ IP | | **Rogue DHCP Server** | ตั้ง DHCP Server ปลอม ตอบสนองเร็วกว่า Server จริง | Man-in-the-Middle | | **DHCP Snooping Bypass** | หลีกเลี่ยง DHCP Snooping บน Switch | ได้รับ IP จาก Rogue Server | --- ### 2.7.4 คำสั่งทดสอบ DHCP (1/2) ```bash # ดู DHCP Lease ปัจจุบัน cat /var/lib/dhcp/dhclient.leases # Ubuntu/Debian cat /var/lib/dhclient/dhclient.leases # ร้องขอ IP ใหม่จาก DHCP sudo dhclient -r eth0 # คืน IP เดิม sudo dhclient eth0 # ขอ IP ใหม่ # ดู DHCP Traffic ด้วย tcpdump sudo tcpdump -i eth0 -n udp port 67 or udp port 68 -e ``` --- ### 2.7.4 คำสั่งทดสอบ DHCP (2/2) ```bash # ตรวจจับ Rogue DHCP Server sudo nmap --script broadcast-dhcp-discover -e eth0 # ตัวอย่างผลลัพธ์: # DHCP Server: 192.168.1.1 (Gateway จริง) # DHCP Server: 192.168.1.200 (⚠️ Rogue Server!) # ใช้ dhtest เพื่อทดสอบ DHCP Server sudo apt install dhtest sudo dhtest -i eth0 -m 00:11:22:33:44:55 # ทดสอบด้วย MAC ระบุ ``` --- ### 2.7.5 DHCP Snooping — การป้องกันการโจมตี DHCP **DHCP Snooping** เป็นฟีเจอร์ความมั่นคงปลอดภัยบน Switch ที่กรอง DHCP Traffic โดยแยก Port เป็น **Trusted** (เชื่อถือได้) และ **Untrusted** (ไม่เชื่อถือ) ```bash # จำลอง DHCP Snooping ด้วย iptables บน Linux Router # ยกเว้น DHCP Server จริง (192.168.1.1) บน eth0 sudo iptables -I FORWARD -p udp --dport 68 ! -s 192.168.1.1 -j DROP # Log Rogue DHCP OFFER sudo iptables -I FORWARD -p udp --dport 68 ! -s 192.168.1.1 \ -j LOG --log-prefix "Rogue DHCP: " ``` --- ### สรุปบทที่ 2 (Chapter Summary) ```mermaid mindmap root((บทที่ 2
Network Protocols
and Traffic Analysis)) OSI and TCP/IP 7 Layers of OSI Attack Surface ในแต่ละชั้น Encapsulation / Decapsulation Core Protocols IP — Spoofing, Fragmentation TCP — SYN Flood, Handshake UDP — Amplification Attack ICMP — Ping of Death, Tunneling Analysis Tools tcpdump — BPF Filter Wireshark / TShark nmap — Scanning Types Traffic Analysis Baseline Measurement Anomaly Detection NetFlow Analysis DPI Shallow vs Deep Inspection Privacy Concerns nDPI / Suricata Multimedia Protocols RTP / RTCP — Jitter SIP / VoIP — Toll Fraud SRTP — Encryption DNS and DHCP DNS Cache Poisoning DNS Tunneling DNSSEC Validation DHCP Starvation Rogue DHCP Server ``` --- ### ประเด็นสำคัญที่ควรจำ (1/2) 1. **OSI Model** ไม่ใช่แค่ทฤษฎี แต่ช่วยระบุว่าการโจมตีเกิดที่ชั้นใด และควรป้องกันอย่างไร 2. **TCP Three-Way Handshake** เป็นเป้าหมายของ SYN Flood ควรตรวจสอบจำนวน `SYN_RECV` ที่ผิดปกติ 3. **UDP** ไม่มี State ทำให้เสี่ยงต่อ Amplification Attack ควรระวัง DNS/NTP/SSDP บน UDP 4. **tcpdump และ Wireshark** เป็นเครื่องมือหลักในการวิเคราะห์ ควรฝึกการเขียน BPF Filter --- ### ประเด็นสำคัญที่ควรจำ (2/2) 5. **DPI** ช่วยตรวจจับ Threat ได้ลึกกว่า แต่ต้องคำนึงถึงความเป็นส่วนตัวและประสิทธิภาพ 6. **Baseline Traffic** เป็นพื้นฐานของ Anomaly Detection ควรสร้างและอัปเดตสม่ำเสมอ 7. **DNS** เป็นโปรโตคอลที่ถูกใช้ในการโจมตีหลายรูปแบบ ทั้ง Poisoning, Tunneling และ Amplification 8. **DHCP Snooping** เป็นมาตรการพื้นฐานที่ควรเปิดใช้บน Switch ทุกตัว --- ### เอกสารอ้างอิง (References) (1/2) 1. **RFC 791** — Internet Protocol (IP), IETF, 1981. https://www.rfc-editor.org/rfc/rfc791 2. **RFC 793** — Transmission Control Protocol (TCP), IETF, 1981. https://www.rfc-editor.org/rfc/rfc793 3. **RFC 768** — User Datagram Protocol (UDP), IETF, 1980. https://www.rfc-editor.org/rfc/rfc768 4. **RFC 792** — Internet Control Message Protocol (ICMP), IETF, 1981. https://www.rfc-editor.org/rfc/rfc792 5. **RFC 3550** — RTP: A Transport Protocol for Real-Time Applications, IETF, 2003. https://www.rfc-editor.org/rfc/rfc3550 6. **RFC 3261** — SIP: Session Initiation Protocol, IETF, 2002. https://www.rfc-editor.org/rfc/rfc3261 7. **RFC 4033** — DNS Security Introduction and Requirements (DNSSEC), IETF, 2005. https://www.rfc-editor.org/rfc/rfc4033 8. **RFC 2131** — Dynamic Host Configuration Protocol (DHCP), IETF, 1997. https://www.rfc-editor.org/rfc/rfc2131 --- ### เอกสารอ้างอิง (References) (2/2) 9. **Wireshark User's Guide** — https://www.wireshark.org/docs/wsug_html_chunked/ 10. **nmap Reference Guide** — https://nmap.org/book/man.html 11. **tcpdump Manual** — https://www.tcpdump.org/manpages/tcpdump.1.html 12. **Sanders, C. & Smith, J.** (2017). *Applied Network Security Monitoring: Collection, Detection, and Analysis*. Syngress. 13. **McNab, C.** (2017). *Network Security Assessment, 3rd Edition*. O'Reilly Media. 14. **Stallings, W.** (2022). *Network Security Essentials: Applications and Standards, 6th Edition*. Pearson. 15. **NIST SP 800-81-2** — Secure Domain Name System (DNS) Deployment Guide. https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-81-2.pdf --- # คำถาม - ข้อสงสัย