โดย : อดิศร ขาวสังข์
เขียนเมื่อ : 15/12/2006
ทดลองบน : Ubuntu 6.10
บทนำ
เนื่องจาก Gateway Server ที่ใช้งานอยู่ประจำหน่วยงานของผู้เขียนเกิดมีปัญหา เลยจัดการลงใหม่ซะเลย และก็เลือก Ubuntu 6.10 Server เป็น OS ก็ไม่แน่ใจว่าจะมีประโยชน์กับคนอื่นบ้างหรือเปล่า แต่ก็ได้แบ่งปันมาแล้วในที่นี้
ความต้องการของระบบ
- ต้องการให้ทำ NAT (Marquerade) ให้เครื่องข่ายภายใน (172.24.51.0/24) สามารถออกอินเตอร์เน็ตผ่าน IP จริงได้ โดย IP ที่ได้จัดสรรมาคือ
Public IP = 202.129.16.15
Private IP = 172.24.51.253
- เครือข่ายภายในดังกล่าวมีการชื่อต่อกับ สำนักงานใหญ่ส่วนกลาง และสำนักงานสาขาย่อยอื่น ๆ ที่ใช้ IP Address เป็น Private IP คือ 172.16.0.0/12 และ 10.0.0.0/8 ผ่าน Router ที่มีอยู่แล้ว ดังนั้นที่ Gateway Server เครื่องนี้จะต้องทำ Static Route ไปยัง Network ดังกล่าวโดยให้ผ่านไปที่ Router ซึ่งมี IP เป็น 172.24.51.1
- จัดทำ Firewall บนเครื่อง Gateway Server ให้มีความปลอดภัยเท่าที่จะทำได้
การติดตั้งและคอนฟิก
- ติดตั้ง Server ให้สามารถเืชื่อมต่อได้ทั้ง Public และ Private
- ในกรณีต้องการเซ็ตค่า IP ใหม่สามารถทำได้โดยแก้ที่ไฟล์ /etc/network/interfaces ซึ่งของผู้เขียนเป็นดังรูป
- restart การทำงานของ Network Interface ด้วยคำสั่ง :
sudo /etc/init.d/networking restart
การทำ Firewall และ Static Route
- สร้างไฟล์สำหรับ Firewall ไว้นที่ /etc/init.d/firewall.iptables (ตั้งชื่อเป็นไฟล์อื่นก็ได้นะครับ) ด้วยคำสั่ง:
sudo vim /etc/init.d/firewall.iptables
- สร้าง Static Route ด้วยคำสั่ง route add และสร้าง Firewall ไว้ในไฟล์ดังกล่าว ซึ่งข้อมูลในไฟล์เป็นดั้งนี้ :
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
#Adisorn firewall.
#Make Static route
route add -net 172.24.51.0 netmask 255.255.255.0 gw 172.24.51.1
route add -net 172.16.0.0 netmask 255.240.0.0 gw 172.24.51.1
route add -net 10.0.0.0 netmask 255.0.0.0 gw 172.24.51.1
# eth0 is connected to the internet.
# eth1 is connected to a private subnet.
PRIVATE=172.24.51.0/24
# Loopback address
LOOP=127.0.0.1
# Delete old iptables rules
# and temporarily block all traffic.
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -F
# Set default policies
iptables -P OUTPUT ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
# Prevent external packets from using loopback addr
iptables -A INPUT -i eth0 -s $LOOP -j DROP
iptables -A FORWARD -i eth0 -s $LOOP -j DROP
iptables -A INPUT -i eth0 -d $LOOP -j DROP
iptables -A FORWARD -i eth0 -d $LOOP -j DROP
# Anything coming from the Internet should have a real Internet address
iptables -A FORWARD -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A FORWARD -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
# Block outgoing NetBios (if you have windows machines running
# on the private subnet). This will stop local windows machines from
# broadcasting themselves to the internet.
iptables -A OUTPUT -p tcp --sport 135:139 -j DROP
iptables -A OUTPUT -p udp --sport 135:139 -j DROP
iptables -A FORWARD -p tcp --sport 135:139 -j DROP
iptables -A FORWARD -p udp --sport 135:139 -j DROP
#Block 445
iptables -A OUTPUT -p tcp --sport 445 -j DROP
iptables -A OUTPUT -p udp --sport 445 -j DROP
iptables -A FORWARD -p tcp --sport 445 -j DROP
iptables -A FORWARD -p udp --sport 445 -j DROP
#Check source address validity on packets going to internet
iptables -A FORWARD -s ! $PRIVATE -i eth1 -j DROP
# Allow local loopback
iptables -A INPUT -s $LOOP -j ACCEPT
iptables -A INPUT -d $LOOP -j ACCEPT
# Allow incoming pings (can be disabled)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# Allow services such as www , ssh and others(can be disabled)
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 23 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp --dport 161 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p udp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 554 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 10000 -j ACCEPT
# Allow packets from private subnets
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -j ACCEPT
# Keep state of connections from local machine and private subnets
iptables -A OUTPUT -m state --state NEW -o eth0 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state NEW -o eth0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
# Masquerade local subnet
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -F
iptables -t nat -A POSTROUTING -s $PRIVATE -o eth0 -j MASQUERADE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- จากนั้นให้บันทึก ruleset ดังกล่าว และก็กำหนดให้สามารถ execute ได้โดยใช้คำสั่ง chmod ดังนี้ :
sudo chmod 755 /etc/init.d/firewall.iptables
- การทำ initialize firewall ให้เป็น default ตอน startup คือจะมีการเรียกใช้งานไฟล์นี้ตอน startup โดยต้องมีการสร้าง link ไปยังไดเร็คทอรี่ /etc/rc2.d (โดย default แล้ว ubuntu จะรันที่ runlevel 2) ดังนี้ :
sudo ln -s /etc/init.d/firewall.iptables /etc/rc2.d/S13firewall
หรืออีกทางเลือกหนึ่งที่น่าจะถูกต้องกว่าคือการใช้คำสั่ง update-rc.d ในการสร้าง link แบบอัตโนมัติเพื่อเชื่อมโยงไปยัง firewall script ดังตัวอย่างดังนี้ :
sudo update-rc.d /etc/init.d/firewall.iptables defaults 13
ซึ่งคำสั่งข้างบนนี้เป็นการสร้าง symlinks ไปยัง firewall script ของคุณใน runlevel 2.5 (the "default")
จบครับ