Friday, March 22, 2013

How to for UFW script configures firewall iptables

ทำไมผมใช้ sudo ufw allow
How to for UFW script configures firewall iptables
(important note: UFW is not the firewall. UFW just configures your iptables)

in most cases I recommend doing the following immediately: ในกรณีส่วนมากแนะนำให้ตั้งค่า เริ่มต้นของ UFW เป็น deny (default ในที่นี้จะเป็น incoming package)
sudo ufw default deny
sudo ufw default deny incoming

Turn on the firewall : : เปิดการทำงาน UFW
sudo ufw enable

Turn off the firewall : ปิดการทำงาน UFW
sudo ufw disable

You can also delete all the rules with a single command : ล้างกฏทั้งหมด
sudo ufw reset

To get the current status of your UFW rules : แสดงกฏที่ตั้งไว้
sudo ufw status
sudo ufw status verbose
sudo ufw status numbered

To add deny rules : เพิ่มกฏปฏิเสธ
blocking aport
sudo ufw deny port
sudo ufw deny port 22

blocking an ip address : กั้นการเข้าถึงโดยระบุหมายเลขไอพี
sudo ufw deny from
sudo ufw deny from 10.10.10.10

blocking a specific ip address and port : กั้นการเข้าถึงโดยระบุหมายเลขไอพีและพอร์ต
sudo ufw deny from to port
sudo ufw deny from 10.10.10.10 to port 22

advanced deny example fro denying access from an ip address range 10.120.0.1 - 10.120.0.255 for SSH port 22 : ตัวอย่างการปฏิเสธการเข้าถึงพอร์ต 22 ด้วยช่วงไอพี 10.120.0.1 - 10.120.0. 255
sudo ufw deny from 10.0.0.1/24 to any port 22

To remove a deny or allow rule : ถอนกฏการปฏิเสธและการอนุญาต
sudo ufw delete from to any port

Example add and remove rule : ตัวอย่างการถอนกฏ แค่เพิ่ม delete ก่อนคำสั่ง deny หรือ allow
sudo ufw deny from 10.10.10.10
sudo ufw delete deny from 10.10.10.10

sudo ufw allow to any port 5900
sudo ufw delete allow to any port 5900

sudo ufw allow from 10.0.0.1/24 to any port 22
sudo ufw delete allow from 10.0.0.1/24 any port 22

SECNARIO : ลำดับขึ้นตอนการตั้งกฏ
เมื่อเราต้องการ block port 22 จาก ip 192.168.0.1 และ 192.168.0.7 แต่อนุญาตสำหรับ ip อื่นๆ ที่อยู่ในช่วง ip นั้น
192.168.0.x ips ให้สามารถ access port 22 ได้
sudo ufw deny from 192.168.0.1 to any port 22
sudo ufw deny from 192.168.0.7 to any port 22
sudo ufw allow from 192.168.0.0/24 to any port 22

เมื่อเราลองแสดง status rules จะได้ผลดังนี้
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     DENY    192.168.0.7
22:udp                     DENY    192.168.0.7
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

กฏของ UFW
คำสั่ง deny ต้องอยู่เหนือ allow เสมอ ถ้าเมื่อใดคำสั่ง allow อยู่เหนือ deny แล้ว คำสั่ง deny จะไม่ประมวลผลและใช้งานคำสั่งได้

In exist allow rule and want add deny rule : ในกรณีที่เรามีคำสั่ง allow อยู่แล้วต้องการเพิ่ม deny ในภายหลัง
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

Use key insert : ใช้ insert ในการแทรกกฏ
Example 1
block this ip to any port & block this ip to any port
sudo ufw insert 1 deny from 192.168.0.1
sudo ufw insert 1 deny from 192.168.0.1 to any port 22

block range ip to any port & block range ip to this port
sudo ufw insert 1 deny from 192.168.0.0/16
sudo ufw insert 1 deny from 192.168.0.0/16 to any port 80

sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

Example 2
sudo ufw insert 3 deny from 192.168.0.7 to any port 22
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     DENY    192.168.0.7
22:udp                     DENY    192.168.0.7
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24
-----------------------------------------------------
การตั้งค่าเหล่านี้ (ด้านบนที่เขียนมา) จะเป็น default คือ ขาเข้า (inbound)
ถ้าต้องการจัดการขาออก (outbound) ด้วยก็ตัวอย่างประมาณนี้ (ค่าเริ่มต้นของ outbound คิดว่าน่าจะเป็น allow อยู่แล้วนะ) ฉนั้นเมื่อต้องการปิด ไม่ให้ package ที่ request จาก server วิ่งออกได้ให้ใช้คำสั่งนี้
sudo ufw default deny outgoing

ค่อยๆ ไล่เปิด outbound ที่คิดว่าจำเป็น
ufw allow out to 8.8.8.8 port 53
ufw allow out port 53
ufw allow out port 80

ส่วนตัวผู้เขียนเลือกตั้งค่าเป็น ปิดทุกพอร์ตของ inbound และเปิดทุกพอร์ตของ outbound แล้วจึงค่อยๆ ไล่เปิดพอร์ต inbound ที่จำเป็นและปลอดภัย (ในระดับหนึ่ง) เท่านั้น
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow out to any port 22  # SSH
sudo ufw allow out to any port 123 # NTP
sudo ufw allow out to any port 80  # HTTP
sudo ufw allow out to any port 22  # HTTPS
sudo ufw allow out to any port 20  # FTP Data Message
sudo ufw allow out to any port 21  # FTP Control Message
sudo ufw allow out to any port 161 # SNMP

การถอนกฏก็ทำเหมือนตอนเพิ่มแค่ delete ก่อน deny หรือ allow หลังนั้นก็พิมพ์เหมือนเดิม
sudo ufw delete allow out to any port 161 # SNMP
----------------------------------------------------
อ้างอิง
man ufw
http://ubuntuforums.org/showthread.php?t=823741
http://forum.linode.com/viewtopic.php?p=35542
http://ubuntuforums.org/showthread.php?t=1893751
http://mondotech.blogspot.com/2010/11/ufw-block-outgoing-traffic.html
http://1000umbrellas.com/2010/04/29/how-to-set-up-the-firewall-using-ufw-on-ubuntu-lucid-lynx-server
http://www2.thaiadmin.org/board/index.php?topic=121572.0

No comments:

Post a Comment

Popular Posts