Access Control Lists (ACLs)

คู่มือฉบับสมบูรณ์สำหรับการกำหนดค่าและใช้งาน ACLs บน Cisco Router

🔰 บทนำเกี่ยวกับ Access Control Lists

ACLs คืออะไร?

Access Control Lists (ACLs) เป็นเครื่องมือที่ให้ Network Engineers สามารถตั้งโปรแกรม Filter ใน Router เพื่อควบคุมการไหลของ Packets ทั้งในทิศทาง Inbound และ Outbound

หลักการทำงาน: ACL จะตรวจสอบ Headers ของ Packets (IP, TCP, UDP) และตัดสินใจว่าจะอนุญาต (permit) หรือปฏิเสธ (deny) Packet นั้น

คุณสมบัติหลักของ ACLs:

  • ควบคุมการเข้าถึงโดยใช้ First-Match Logic
  • สามารถ Filter ได้หลายเงื่อนไข เช่น Source/Destination IP, Port Numbers
  • มี Implicit Deny All ที่ท้าย ACL เสมอ
  • ทำงานแบบ Sequential (เรียงลำดับ)

📍 ตำแหน่งและทิศทางของ ACL

การกำหนดตำแหน่ง ACL

ACL ต้องถูกผูกกับ Interface และกำหนดทิศทางการไหลของ Packet:

ตัวอย่างการวาง ACL:
[PC] ← → [R1 F0/0] ← → [WAN] ← → [R2 S0/0/1] ← → [R2 F0/0] ← → [Server]
ตำแหน่ง ทิศทาง การทำงาน
R1's F0/0 Inbound Filter Packets ที่เข้ามาจาก PC
R1's S0/0/0 Outbound Filter Packets ที่ส่งออกไป WAN
R2's S0/0/1 Inbound Filter Packets ที่เข้ามาจาก WAN
R2's F0/0 Outbound Filter Packets ที่ส่งไป Server

🔢 ประเภทของ IP ACLs

ประเภท ACL หมายเลข ความสามารถ
Standard Numbered ACLs 1-99, 1300-1999 Filter เฉพาะ Source IP Address
Extended Numbered ACLs 100-199, 2000-2699 Filter หลายเงื่อนไข (Protocol, Source/Dest IP, Port)
Named ACLs ใช้ชื่อแทนหมายเลข จัดการและแก้ไขได้ง่าย มี Sequence Numbers

🏷️ Standard Numbered ACLs (1-99)

หลักการทำงาน

Standard ACL สามารถ Match ได้เฉพาะ Source IP Address เท่านั้น และใช้ First-Match Logic

ข้อจำกัด: ไม่สามารถระบุ Destination IP, Protocol, หรือ Port Numbers ได้

รูปแบบคำสั่งพื้นฐาน

Router(config)# access-list [ACL-number] [permit|deny] [source-IP] [wildcard-mask] Router(config)# interface [interface-type number] Router(config-if)# ip access-group [ACL-number] [in|out]

ตัวอย่างที่ 1: ปฏิเสธ Host เดียว

! ปฏิเสธ IP 192.168.1.100 และอนุญาตที่เหลือทั้งหมด Router(config)# access-list 10 deny 192.168.1.100 0.0.0.0 Router(config)# access-list 10 permit any ! Apply ACL เข้า Interface Router(config)# interface fastethernet0/0 Router(config-if)# ip access-group 10 in

ตัวอย่างที่ 2: อนุญาตเฉพาะ Subnet

! อนุญาตเฉพาะ subnet 10.1.1.0/24 Router(config)# access-list 20 permit 10.1.1.0 0.0.0.255 ! Implicit deny all อยู่ที่ท้ายอัตโนมัติ Router(config)# interface serial0/0/0 Router(config-if)# ip access-group 20 out

ตัวอย่างที่ 3: Multiple Rules

! ACL ที่มีหลายเงื่อนไข Router(config)# access-list 30 deny 192.168.10.5 0.0.0.0 Router(config)# access-list 30 deny 192.168.10.0 0.0.0.7 Router(config)# access-list 30 permit 192.168.10.0 0.0.0.255 Router(config)# access-list 30 deny 192.168.0.0 0.0.255.255 Router(config)# access-list 30 permit any

อธิบาย:

  • ปฏิเสธ 192.168.10.5
  • ปฏิเสธ 192.168.10.0-192.168.10.7 (8 addresses)
  • อนุญาต subnet 192.168.10.0/24 ที่เหลือ
  • ปฏิเสธ network 192.168.0.0/16 ทั้งหมด
  • อนุญาตที่เหลือทั้งหมด

🔧 Extended Numbered ACLs (100-199)

ความสามารถของ Extended ACLs

Extended ACL สามารถตรวจสอบได้หลายเงื่อนไขพร้อมกัน:

  • Protocol: IP, TCP, UDP, ICMP, etc.
  • Source IP Address และ Wildcard Mask
  • Destination IP Address และ Wildcard Mask
  • Source และ Destination Port Numbers
  • TCP Flags, ICMP Types และอื่นๆ

รูปแบบคำสั่งสำหรับ TCP/UDP

access-list [100-199] [permit|deny] [protocol] [source-IP wildcard] [dest-IP wildcard] [port-options]
Port Options:
eq [port] - เท่ากับ port ที่ระบุ
ne [port] - ไม่เท่ากับ port ที่ระบุ
lt [port] - น้อยกว่า port ที่ระบุ
gt [port] - มากกว่า port ที่ระบุ
range [port1] [port2] - ช่วง port

ตัวอย่างที่ 1: Block Web Traffic

! ปฏิเสธการเข้าถึง Web (HTTP และ HTTPS) จาก subnet 192.168.1.0/24 Router(config)# access-list 100 deny tcp 192.168.1.0 0.0.0.255 any eq 80 Router(config)# access-list 100 deny tcp 192.168.1.0 0.0.0.255 any eq 443 Router(config)# access-list 100 permit ip any any Router(config)# interface fastethernet0/1 Router(config-if)# ip access-group 100 in

ตัวอย่างที่ 2: Allow Specific Services

! อนุญาตเฉพาะ HTTP, HTTPS, DNS และ ICMP Router(config)# access-list 110 permit tcp any any eq 80 Router(config)# access-list 110 permit tcp any any eq 443 Router(config)# access-list 110 permit tcp any any eq 53 Router(config)# access-list 110 permit udp any any eq 53 Router(config)# access-list 110 permit icmp any any ! Implicit deny all ที่เหลือ

ตัวอย่างที่ 3: Complex Network Security

! Security Policy สำหรับ DMZ Router(config)# access-list 120 permit tcp any 10.1.1.10 0.0.0.0 eq 80 Router(config)# access-list 120 permit tcp any 10.1.1.10 0.0.0.0 eq 443 Router(config)# access-list 120 permit tcp any 10.1.1.11 0.0.0.0 eq 25 Router(config)# access-list 120 deny tcp any 10.1.1.0 0.0.0.255 eq 22 Router(config)# access-list 120 permit tcp 10.1.2.0 0.0.0.255 10.1.1.0 0.0.0.255 eq 22 Router(config)# access-list 120 deny ip any any

อธิบาย:

  • อนุญาต HTTP/HTTPS ไป Web Server (10.1.1.10)
  • อนุญาต SMTP ไป Mail Server (10.1.1.11)
  • ปฏิเสธ SSH จาก Internet ไป DMZ
  • อนุญาต SSH เฉพาะจาก Management Network
  • ปฏิเสธที่เหลือทั้งหมด

ตัวอย่างที่ 4: Port Range และ Well-Known Ports

! ใช้ Well-Known Port Names Router(config)# access-list 130 permit tcp any any eq www Router(config)# access-list 130 permit tcp any any eq ftp Router(config)# access-list 130 permit tcp any any eq telnet Router(config)# access-list 130 permit tcp any any eq smtp ! ใช้ Port Range Router(config)# access-list 130 permit tcp any any range 1024 65535 Router(config)# access-list 130 deny tcp any any lt 1024

📝 Named ACLs

ข้อดีของ Named ACLs

  • ชื่อที่มีความหมาย: ใช้ชื่อแทนหมายเลข ทำให้จำและเข้าใจได้ง่าย
  • ACL Subcommands: ใช้ Configuration Mode แยกต่างหาก
  • Sequence Numbers: สามารถแก้ไข เพิ่ม ลบ Entry ได้
  • การจัดการที่ดีกว่า: แก้ไขได้โดยไม่ต้องลบ ACL ทั้งหมด

รูปแบบคำสั่ง Named ACL

! สร้าง Named ACL Router(config)# ip access-list [standard|extended] [ACL-name] ! เข้าสู่ ACL Configuration Mode Router(config-std-nacl)# [sequence-number] [permit|deny] [conditions]

ตัวอย่างที่ 1: Named Standard ACL

! สร้าง Named Standard ACL Router(config)# ip access-list standard BLOCK_MARKETING Router(config-std-nacl)# 10 deny 192.168.100.0 0.0.0.255 Router(config-std-nacl)# 20 deny 192.168.101.0 0.0.0.255 Router(config-std-nacl)# 30 permit any Router(config-std-nacl)# exit ! Apply ACL Router(config)# interface gigabitethernet0/0 Router(config-if)# ip access-group BLOCK_MARKETING out

ตัวอย่างที่ 2: Named Extended ACL

! สร้าง Named Extended ACL สำหรับ Security Policy Router(config)# ip access-list extended DMZ_SECURITY Router(config-ext-nacl)# 10 permit tcp any host 10.1.1.10 eq 80 Router(config-ext-nacl)# 20 permit tcp any host 10.1.1.10 eq 443 Router(config-ext-nacl)# 30 permit tcp any host 10.1.1.11 eq 25 Router(config-ext-nacl)# 40 permit tcp 10.1.2.0 0.0.0.255 10.1.1.0 0.0.0.255 eq 22 Router(config-ext-nacl)# 50 permit icmp any any echo-reply Router(config-ext-nacl)# 60 deny ip any any Router(config-ext-nacl)# exit Router(config)# interface fastethernet0/0 Router(config-if)# ip access-group DMZ_SECURITY in

การแก้ไข Named ACLs ด้วย Sequence Numbers

ตัวอย่างการแก้ไข ACL

! ดู ACL ปัจจุบัน Router# show ip access-lists DMZ_SECURITY ! เพิ่ม Entry ใหม่ระหว่าง line 20 และ 30 Router(config)# ip access-list extended DMZ_SECURITY Router(config-ext-nacl)# 25 permit tcp any host 10.1.1.12 eq 21 ! ลบ Entry ที่ sequence 40 Router(config-ext-nacl)# no 40 ! แก้ไข Entry โดยลบแล้วเพิ่มใหม่ Router(config-ext-nacl)# no 50 Router(config-ext-nacl)# 50 permit icmp any any

🎯 First-Match Logic และ Implicit Deny

หลักการ First-Match Logic

ACL ใช้หลักการ First-Match Logic หมายความว่า:

  1. Router จะตรวจสอบ Packet กับ ACL Entry ตามลำดับจากบนลงล่าง
  2. เมื่อ Packet ตรงกับ Entry ใดก็ตาม Router จะดำเนินการ (permit/deny) ทันที
  3. Router จะหยุดการตรวจสอบ Entry ที่เหลือ
  4. หาก Packet ไม่ตรงกับ Entry ใดเลย จะถูก Deny โดย Implicit Deny All
สำคัญ: ลำดับของ ACL Entry มีความสำคัญมาก! Entry ที่เฉพาะเจาะจงควรอยู่ด้านบน และ Entry ที่กว้างๆ ควรอยู่ด้านล่าง

ตัวอย่างผลกระทบของลำดับ Entry

! ❌ ลำดับที่ผิด - จะไม่ทำงานตามต้องการ Router(config)# access-list 50 permit any Router(config)# access-list 50 deny 192.168.1.100 0.0.0.0 ! 192.168.1.100 จะไม่ถูก deny เพราะ permit any ก่อน ! ✅ ลำดับที่ถูกต้อง Router(config)# access-list 60 deny 192.168.1.100 0.0.0.0 Router(config)# access-list 60 permit any ! 192.168.1.100 จะถูก deny, ที่เหลือจะถูก permit

🔌 Well-Known Port Numbers

Port Numbers ที่ใช้บ่อยใน ACLs

Protocol Port Number Keyword Description
HTTP 80 www Web browsing
HTTPS 443 - Secure web browsing
FTP Data 20 ftp-data File transfer data
FTP Control 21 ftp File transfer control
SSH 22 - Secure Shell
Telnet 23 telnet Remote terminal
SMTP 25 smtp Email sending
DNS 53 domain Domain name resolution
DHCP Server 67 bootps DHCP server
DHCP Client 68 bootpc DHCP client
POP3 110 pop3 Email receiving
SNMP 161 snmp Network management

🎭 Wildcard Masks

การทำความเข้าใจ Wildcard Masks

Wildcard Mask เป็นส่วนกลับของ Subnet Mask:

  • 0 = ต้องตรงกันทุก bit (must match)
  • 1 = ไม่สนใจ bit นี้ (don't care)
Subnet Mask Wildcard Mask CIDR Meaning
255.255.255.255 0.0.0.0 /32 Host เดียว
255.255.255.0 0.0.0.255 /24 Subnet ทั้งหมด (254 hosts)
255.255.0.0 0.0.255.255 /16 Class B network
255.0.0.0 0.255.255.255 /8 Class A network
0.0.0.0 255.255.255.255 /0 Any/All networks

ตัวอย่างการใช้ Wildcard Masks

! Host เดียว 192.168.1.100 access-list 10 deny 192.168.1.100 0.0.0.0 ! หรือใช้ keyword "host" access-list 10 deny host 192.168.1.100 ! Subnet 192.168.1.0/24 ทั้งหมด access-list 10 permit 192.168.1.0 0.0.0.255 ! ทุก IP Address access-list 10 permit 0.0.0.0 255.255.255.255 ! หรือใช้ keyword "any" access-list 10 permit any ! Range ของ IP (192.168.1.64 - 192.168.1.95) access-list 10 permit 192.168.1.64 0.0.0.31

💼 ตัวอย่างการใช้งานจริง

Scenario 1: Branch Office Security

เงื่อนไข: Branch Office ต้องการควบคุมการเข้าถึง Internet

  • อนุญาต HTTP, HTTPS, DNS สำหรับทุกคน
  • ปฏิเสธ Social Media (Facebook, Twitter)
  • อนุญาต FTP เฉพาะ Management VLAN
  • ปฏิเสธ P2P Traffic
! สร้าง Named Extended ACL Router(config)# ip access-list extended BRANCH_INTERNET ! อนุญาต DNS (UDP และ TCP) Router(config-ext-nacl)# 10 permit udp any any eq 53 Router(config-ext-nacl)# 20 permit tcp any any eq 53 ! อนุญาต HTTP และ HTTPS Router(config-ext-nacl)# 30 permit tcp any any eq 80 Router(config-ext-nacl)# 40 permit tcp any any eq 443 ! ปฏิเสธ Social Media (Facebook IPs example) Router(config-ext-nacl)# 50 deny tcp any 31.13.64.0 0.0.31.255 Router(config-ext-nacl)# 60 deny tcp any 173.252.64.0 0.0.31.255 ! อนุญาต FTP เฉพาะ Management VLAN (192.168.10.0/24) Router(config-ext-nacl)# 70 permit tcp 192.168.10.0 0.0.0.255 any eq 21 Router(config-ext-nacl)# 80 permit tcp 192.168.10.0 0.0.0.255 any eq 20 ! ปฏิเสธ P2P Ports Router(config-ext-nacl)# 90 deny tcp any any range 6881 6889 Router(config-ext-nacl)# 100 deny udp any any range 6881 6889 ! อนุญาต ICMP สำหรับ Network Troubleshooting Router(config-ext-nacl)# 110 permit icmp any any ! Deny All ที่เหลือ (Implicit) Router(config-ext-nacl)# exit ! Apply ACL to WAN Interface Router(config)# interface serial0/0/0 Router(config-if)# ip access-group BRANCH_INTERNET out

Scenario 2: DMZ Server Protection

เงื่อนไข: ป้องกัน DMZ Servers และควบคุมการเข้าถึง

  • Web Server (10.1.1.10): รับ HTTP/HTTPS จาก Internet
  • Mail Server (10.1.1.11): รับ SMTP/POP3 จาก Internet
  • Management จาก Internal Network เท่านั้น
  • ไม่อนุญาตให้ DMZ เข้าถึง Internal Network
! ACL สำหรับ Traffic จาก Internet เข้า DMZ Router(config)# ip access-list extended INTERNET_TO_DMZ ! Web Server Services Router(config-ext-nacl)# 10 permit tcp any host 10.1.1.10 eq 80 Router(config-ext-nacl)# 20 permit tcp any host 10.1.1.10 eq 443 ! Mail Server Services Router(config-ext-nacl)# 30 permit tcp any host 10.1.1.11 eq 25 Router(config-ext-nacl)# 40 permit tcp any host 10.1.1.11 eq 110 ! อนุญาต ICMP Echo Reply (Response to Ping) Router(config-ext-nacl)# 50 permit icmp any 10.1.1.0 0.0.0.255 echo-reply ! Deny All อื่นๆ Router(config-ext-nacl)# 60 deny ip any any Router(config-ext-nacl)# exit ! ACL สำหรับ Management จาก Internal Network Router(config)# ip access-list extended INTERNAL_TO_DMZ ! SSH Management เฉพาะจาก Management Subnet Router(config-ext-nacl)# 10 permit tcp 192.168.100.0 0.0.0.255 10.1.1.0 0.0.0.255 eq 22 ! SNMP Monitoring Router(config-ext-nacl)# 20 permit udp 192.168.100.10 0.0.0.0 10.1.1.0 0.0.0.255 eq 161 ! ICMP สำหรับ Network Monitoring Router(config-ext-nacl)# 30 permit icmp 192.168.100.0 0.0.0.255 10.1.1.0 0.0.0.255 ! Deny DMZ Access to Internal Networks Router(config-ext-nacl)# 40 deny ip 10.1.1.0 0.0.0.255 192.168.0.0 0.0.255.255 ! อนุญาต DMZ เข้า Internet Router(config-ext-nacl)# 50 permit ip 10.1.1.0 0.0.0.255 any Router(config-ext-nacl)# exit ! Apply ACLs Router(config)# interface fastethernet0/0 Router(config-if)# description "Internet Interface" Router(config-if)# ip access-group INTERNET_TO_DMZ in Router(config)# interface fastethernet0/1 Router(config-if)# description "Internal Network Interface" Router(config-if)# ip access-group INTERNAL_TO_DMZ in

🔧 การแก้ไขปัญหา ACLs

คำสั่งสำหรับตรวจสอบ ACLs

! ดู ACL ทั้งหมด Router# show ip access-lists ! ดู ACL เฉพาะ Router# show ip access-lists [ACL-name/number] ! ดูการ Apply ACL บน Interface Router# show ip interface [interface-name] ! ดู Hit Count ของ ACL Router# show access-lists ! Clear Hit Counters Router# clear access-list counters [ACL-name/number]

ปัญหาที่พบบ่อยและวิธีแก้ไข

🚫 ปัญหา: ACL ไม่ทำงาน

สาเหตุที่เป็นไปได้:
  • ลืม Apply ACL บน Interface
  • Apply ผิดทิศทาง (in/out)
  • ลำดับ Entry ผิด
  • Wildcard Mask ผิด

✅ วิธีแก้ไข:

  1. ตรวจสอบว่า ACL ถูก Apply บน Interface หรือไม่
  2. ตรวจสอบทิศทางการ Apply (in/out)
  3. ตรวจสอบลำดับ Entry ใน ACL
  4. ใช้ Debug เพื่อติดตาม Packet Flow

ตัวอย่างการ Debug ACL

! เปิด Debug ACL Router# debug ip access-list [ACL-number] ! ดู Log Messages Router# show logging ! ปิด Debug Router# no debug ip access-list [ACL-number] Router# undebug all

🌟 Best Practices สำหรับ ACLs

หลักการสำคัญ

  1. วางแผนก่อนสร้าง ACL: วาดแผนภาพ Network และกำหนด Security Policy
  2. ใช้ Named ACLs: ง่ายต่อการจัดการและเข้าใจ
  3. เรียงลำดับอย่างระมัดระวัง: Entry เฉพาะเจาะจงก่อน, กว้างๆ ทีหลัง
  4. ใช้ Comments: อธิบายจุดประสงค์ของแต่ละ Entry
  5. Test อย่างละเอียด: ทดสอบก่อน Deploy จริง

การจัดวาง ACL

ประเภท ACL ตำแหน่งที่แนะนำ เหตุผล
Standard ACLs ใกล้ Destination Filter เฉพาะ Source IP เท่านั้น
Extended ACLs ใกล้ Source Filter ได้หลายเงื่อนไข, ประหยัด Bandwidth

Template สำหรับ Security ACL

! Standard Security ACL Template ip access-list extended SECURITY_TEMPLATE ! 1. อนุญาต Traffic ที่จำเป็น 10 permit tcp any any eq 80 20 permit tcp any any eq 443 30 permit udp any any eq 53 ! 2. ปฏิเสธ Traffic ที่เป็นอันตราย 40 deny tcp any any eq 135 50 deny tcp any any eq 139 60 deny tcp any any eq 445 ! 3. Log Suspicious Activity 70 deny ip any any log

💡 เคล็ดลับสำหรับมือใหม่:

  • เริ่มจาก ACL ง่ายๆ ก่อน แล้วค่อยซับซ้อนขึ้น
  • เก็บ Backup Configuration ก่อนแก้ไข ACL
  • ใช้ Lab Environment สำหรับทดสอบ
  • ศึกษา Network Traffic Pattern ก่อนสร้าง ACL
  • Monitor Hit Counters เป็นประจำ