iptables Firewall Rules


Iptables คือเครื่องมือของผู้ดูแลระบบที่ช่วยในการ packet filteringและการทำNAT ครับ ซึ่งตัวiptablesไ้รับการยอมรับว่ามีประสิทธิภาพที่สูงมาก ถูกใช้งานในการตรวจสอบตารางของ ip packet filter rule ใน Linux kernel
 

Iptables คืออะไร???

Iptables คือเครื่องมือของผู้ดูแลระบบที่ช่วยในการ packet filteringและการทำNAT ครับ ซึ่งตัวiptablesไ้รับการยอมรับว่ามีประสิทธิภาพที่สูงมาก ถูกใช้งานในการตรวจสอบตารางของ ip packet filter rule ใน Linux kernel

การใช้งานนั้นสามารถป้อนคำสั่งได้โดยตรงผ่านทาง command line หรือจะเซฟเป็น firewall script ใน dd-wrt administration panel ก็ได้ แต่ที่เรารู้ๆกันอยู่ว่าคำสั่งของ iptables นี้มีค่อนข้างมาก วันนี้ผมเลยรวบรวมเอามาฝากกันครับ

เริ่มที่การใช้งานทั่วๆไปครับ

iptables -[AD] chain rule-specification [options]
iptables -[RI] chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LFZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)

command ทั่วๆไป

–append -A chain Append to chain
–delete -D chain Delete matching rule from chain
–delete -D chain rulenum
Delete rule rulenum (1 = first) from chain
–insert -I chain [rulenum]
Insert in chain as rulenum (default 1=first)
–replace -R chain rulenum
Replace rule rulenum (1 = first) in chain
–list -L [chain] List the rules in a chain or all chains
–flush -F [chain] Delete all rules in chain or all chains
–zero -Z [chain] Zero counters in chain or all chains
–new -N chain Create a new user-defined chain
–delete-chain
-X [chain] Delete a user-defined chain
–policy -P chain target
Change policy on chain to target
–rename-chain
-E old-chain new-chain
Change chain name, (moving any references)

Options

–proto -p [!] proto
protocol: by number or name, eg. `tcp’
–source -s [!] address[/mask]
source specification
–destination -d [!] address[/mask]
destination specification
–sport [!] port[:endport]
source port (use `:’ when specifying range)
–dport [!] port[:endport]
destination port
–in-interface -i [!] input name[+]
network interface name ([+] for wildcard)
–jump -j target
target for rule (may load target extension)
–match -m match
extended match (may load extension)
–state state
connection states to match:
INVALID NEW ESTABLISHED RELATED
–tcp-flags [!] mask
match when the TCP flags are as specified:
SYN ACK FIN RST URG PSH ALL NONE
–numeric -n numeric output of addresses and ports
–out-interface -o [!] output name[+]
network interface name ([+] for wildcard)
–table -t table table to manipulate (default: `filter’)
–verbose -v verbose mode
–line-numbers print line numbers when listing
–exact -x expand numbers (display exact values)
–fragment -f match second or further fragments only
–modprobe= try to insert modules using this command
–set-counters PKTS BYTES set the counter during insert/append
–version -V print package version

MAC v1.3.7 options:
–mac-source [!] XX:XX:XX:XX:XX:XX
Match source MAC address

ทีนี้เรามาลองใช้งานจริงๆกันบ้าง อันแรกเลยคือ ทำ Port forwarding ไปที่ IP ที่เรากำหนด จะได้ command ดังนี้ครับ

iptables -t nat -I PREROUTING -p tcp -d $(nvram get wan_ipaddr) –dport 443 -j DNAT –to 192.168.1.2:443
iptables -I FORWARD -p tcp -d 192.168.1.2 –dport 443 -j ACCEPT

Block IP ที่กำหนดไม่ให้เราเข้าไป access

iptables -I FORWARD -d 123.123.123.123 -j DROP

อันนี้คือห้ามไม่ให้เครื่องของเราทำการติดต่อกับ IP ที่กำหนดหากมีการพยายามแหกกฎก็ให้ทำการเก็บ log ไว้ด้วย

iptables -I OUTPUT -d 239.255.255.250 -j logdrop

ทำการ block SMTP trafiic ขงทุกเครื่องยกเว้นเฉพาะเครื่องที่เรากำหนดเท่านั้น

/usr/sbin/iptables -I FORWARD 1 -p tcp -d safe.server1.com –dport 25 -j logaccept
/usr/sbin/iptables -I FORWARD 2 -p tcp -d safe.server2.com –dport 25 -j logaccept
/usr/sbin/iptables -I FORWARD 3 -p tcp –dport 25 -j logdrop

ิblock SMTP ฝั่งขาออก

iptables -I FORWARD 1 -p tcp -s 192.168.1.2 –dport 25 -j ACCEPT
iptables -I FORWARD 2 -p tcp -s 192.168.1.1/24 –dport 25 -j REJECT

อนุญาตเฉพาะโดเมนที่กำหนดเท่านั้นที่ให้ติดต่อทาง HTTP ได้

iptables -I FORWARD 1 -p tcp -d dd-wrt.com –dport 80 -j ACCEPT
iptables -I FORWARD 2 -p tcp –dport 80 -j DROP

ฺBlock ทุกๆ traffic ยกเว้น http, https และ ftp

iptables -I FORWARD 1 -p tcp -m multiport –dport 21,80,443 -j ACCEPT
iptables -I FORWARD 2 -m state –state ESTABLISHED,RELATED -j ACCEPT
iptables -I FORWARD 3 -j DROP

จำกัดการเข้าใช้งานโดย MAC Address

insmod ipt_mac
iptables -I INPUT -p tcp –dport 80 -m mac ! –mac-source 00:12:34:56:78:9A -j REJECT –reject-with tcp-reset

เปลี่ยนค่า Time To Live (TTL)

iptables -t mangle -I POSTROUTING -o vlan1 -j TTL –ttl-set 128

ยังไงก็ลองไปเล่นๆกันดูนะ iptables ยังทำอะไรได้อีกเยอะ
ใครมีคำสั่งอะไรน่าสนใจก็ลองมาโพสกันดูนะครับ

หมายเลขบันทึก: 225132เขียนเมื่อ 24 พฤศจิกายน 2008 14:00 น. ()แก้ไขเมื่อ 24 มิถุนายน 2012 00:42 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท