|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2005/2/19 20:18 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15773
|
- [分享]IPtables與防火牆
- iptables 跟 ipchains 是無法同時使用,如果您已經使用 ipchains ,要改用 iptables 時,要先將 ipchains 的 modules 移除,在載入 iptables 的 module , 才可以使用 iptables !!
1.先檢查是否載入 ipchains module lsmod | grep ipchains 2.如果有的話,要移除 ipchains /etc/rc.d/init.d/ipchains stop ( or ipchains -F ) rmmod ipchains 3.再檢查是否有成功移除 lsmod | grep ipchains 4.載入 iptable module modprobe ip_tables -------------------------------------------------------------------------------- 防止別人用ACK、SYN、FIN等等的封包來掃瞄 Linux iptables -A INPUT -i eth0 -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP -------------------------------------------------------------------------------- NAT iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE 不過此時 ftp 會無法正常運作,必須另外再加上 modprobe ip_conntrack_ftp modprobe ip_nat_ftp 另外可以配合 proxy server,強制每台 client 端均透過 proxy 連線 iptables -t nat -I PREROUTING -i eth0 -p tcp -s 192.168.0.0/255.255.255.0 -d 192.168.0.113 --dport 80 -j ACCEPT iptables -t nat -A PREROUTING -i eth0 -p tcp -s 192.168.0.0/24 --dport 80 -j REDIRECT --to-port 3128 -------------------------------------------------------------------------------- 限制連線條件 iptables -A FORWARD -p TCP -s 11.22.33.44 -d 44.33.22.11 -j DROP 從 11.22.33.44 的 port 1024-65535 連線到 44.33.22.11 的 port 80 ,一律檔掉!! iptables -A FORWARD -p TCP -s 11.22.33.44 --sport 1024:65535 -d 44.33.22.11 --dport www -j DROP -------------------------------------------------------------------------------- 防止 port scan # NMAP FIN/URG/PSH iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP # Xmas Tree iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL ALL -j DROP # Another Xmas Tree iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP # Null Scan(possibly) iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL NONE -j DROP # SYN/RST iptables -A INPUT -i eth0 -p tcp --tcp-flags SYN,RST SYN,RST -j DROP # SYN/FIN -- Scan(possibly) iptables -A INPUT -i eth0 -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP -------------------------------------------------------------------------------- 防止 sync flood 攻擊的設定: iptables -N synfoold iptables -A synfoold -p tcp --syn -m limit --limit 1/s -j RETURN iptables -A synfoold -p tcp -j REJECT --reject-with tcp-reset iptables -A INPUT -p tcp -m state --state NEW -j synfoold 不過流量一大就不太好了!! 所以可以調整時間與次數的觸發值 iptables -N ping iptables -A ping -p icmp --icmp-type echo-request -m limit --limit 1/second -j RETURN iptables -A ping -p icmp -j REJECT iptables -I INPUT -p icmp --icmp-type echo-request -m state --state NEW -j ping 關閉主機的 icmp echo request 或者是直接設定主機不回應 echo request /proc/sys/net/ipv4/icmp_echo_ignore_all -------------------------------------------------------------------------------- 其他 iptables 的 sample # 掛入相關 module modprobe ip_tables modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_conntrack_irc # 先清成空白 iptables -F iptables -X iptables -F -t nat iptables -X -t mangle # 把 FORWARD 關閉 iptables -P FORWARD DROP # 這是打開讓自己網域可以方便連結,也就是該網域不設防 iptables -A INPUT -p all -s 192.168.0.0/255.255.255.0 -j ACCEPT # 允許相關連結服務 iptables -A INPUT -i eth0 -p tcp --dport 20 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 21 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 23 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 25 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 53 -j ACCEPT iptables -A INPUT -i eth0 -p udp --dport 53 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 110 -j ACCEPT iptables -A INPUT -i eth0 -p tcp --dport 113 -j ACCEPT iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j DROP 上面是打開允許 port 20、21、22、23、25、53、110、113 等服務才能夠 被外面所連線。 port 20、21 : ftp 使用的。 port 22 : ssh 連線 port 23 : telnet 連線。方便使用,其實不開放比較安全。 port 25 : sendmail 使用。讓信件可以寄進來。 port 53 : dns 使用。dns 需要打開 udp 使用。 port 110 : pop3 使用 port 113 : auth 身份確認。打開是讓一些使用該 113 確認身份的主機 不至於反查時會卡住很久。 最後一行是對於主動連線或者是不合法連線,一律通通拒絕掉。 這個 script 內容,很適用只允許外面連結特定的 port 服務,剩下的其餘 port 就拒絕外面主動建立的連線。 (以上部分轉載自網路上的 news ,來自小州兄的大作,可惜小編一直抽不出時間完整的整理 iptables ...殘廿...) 以下是小編針對單一防護型的 server , 所撰寫的 iptables ,其目的只有防護自己,如果您的情形也是如此,可以直接採用,只需要修改哪些 port 要開放就可以了!! #!/bin/sh # Sep,30,2002 Mon Anderson add for testing # load modules if necessary modprobe ip_tables # modprobe ip_conntrack # modprobe ip_conntrack_ftp # modprobe ip_conntrack_irc # disable all chains iptables -F iptables -t nat -F iptables -t mangle -F # define default policy iptables -P FORWARD DROP iptables -P INPUT DROP iptables -P OUTPUT ACCEPT # allow all localhost iptables -A INPUT -i lo -j ACCEPT # allow ssh iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT # iptables -A INPUT -i eth0 -p tcp --dport 23 -j ACCEPT # iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT # iptables -A INPUT -p tcp -d 192.168.0.229 -j ACCEPT # log # iptables -A INPUT -p tcp -d 0.0.0.0/24 -j LOG --log-prefix "DROP_AAA__ " --log-level info # iptables -A INPUT -p tcp --dport 1:65535 -j LOG --log-prefix "DROP_BBB__ " --log-level info # allow old connection and deny new connection iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j DROP 如果用 iptables -L ,會得到以下結果 Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED DROP all -- anywhere anywhere state INVALID,NEW Chain FORWARD (policy DROP) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain RH-Lokkit-0-50-INPUT (0 references) target prot opt source destination 另外,使用 iptables -t nat -L ,會得到以下結果 Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 以上的 script , 用來保護單機型的 server ,應該是足夠了!! -------------------------------------------------------------------------------- 將 Server 藏在 LAN 中--DNAT 的實作 看完單機型的 firewall , 接著就來看看如何利用 iptables 實作 firewall , 將需要保護的 server 藏到 LAN , 透過 firewall 實作 DNAT 的方式來保護!! 架構圖如下 : 將 WWW Server 藏到 192.168.0.1 , 但是希望 Internet 的其他 user ,輸入 http://11.22.33.44/ 能夠看到 192.168.0.1 的網頁內容 #!/bin/sh # Dec,11,2002 Wed Anderson add for testing # load modules if necessary modprobe ip_tables modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_conntrack_irc # disable all chains iptables -F iptables -t nat -F iptables -t mangle -F # disable forward chain # iptables -P FORWARD DROPecho "1" > /proc/sys/net/ipv4/ip_forward iptables -P FORWARD ACCEPT iptables -P INPUT DROP iptables -P OUTPUT ACCEPT # alloe all localhost iptables -A INPUT -i lo -j ACCEPT # allow specical IP # iptables -A INPUT -p tcp -d 192.168.0.1 -j ACCEPT # allow ssh iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT iptables -A INPUT -i eth1 -p tcp --dport 22 -j ACCEPT # allow http iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT iptables -A INPUT -i eth1 -p tcp --dport 80 -j ACCEPT # allow old connection and deny new connection iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j DROP iptables -A INPUT -i eth1 -m state --state NEW,INVALID -j DROP # setup DMZ -- DNAT iptables -A PREROUTING -t nat -i eth0 -p tcp -d 11.22.33.44 --dport 80 -j DNAT --to-destination 192.168.0.1:80 # open DMZ goto Internet iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE 至於 WWW Server , 只要將 Default Gateway 指向 192.168.0.254 即可以,此時就可以從 Internet 上,輸入 http://11.22.33.44 連結到 LAN 上的 WWW Server 了!! 其他如果需要 FTP Server, Mail Server,則依照需要的 port 開放就可以了!!
|
|
討論串
-
[分享]簡易 NAT 伺服器 (冷日 (冷日), 2005/2/19 19:51)
-
[分享]PROXY.IP Masquerading.Transparent Proxy.工作原理 (冷日 (冷日), 2005/2/19 19:58)
-
[分享]iptables/chains + squid transparent proxy (冷日 (冷日), 2005/2/19 20:05)
-
[分享]使用 iptables 設定使用 NAT 分享網路頻寬 (冷日 (冷日), 2005/2/19 20:06)
-
[分享]使用 iptables 設定一些安全防護功能 (1) (冷日 (冷日), 2005/2/19 20:08)
-
[分享]使用 iptables 設定一些安全防護功能 (2) (冷日 (冷日), 2005/2/19 20:11)
-
[分享]使用 iptables 設定一些安全防護功能 (3) (冷日 (冷日), 2005/2/19 20:12)
- »
[分享]IPtables與防火牆 (冷日 (冷日), 2005/2/19 20:18)
-
[轉貼]How to bind 2 WAN 1 LAN (冷日 (冷日), 2005/2/19 20:23)
-
[分享]Linux 上多條對外連線(Multi-Path)實作 (冷日 (冷日), 2005/2/19 20:26)
-
[分享]iptables ─ 功能看齊 enterprise 防火牆 (冷日 (冷日), 2005/2/25 14:52)
-
[分享]iptables經典設置 (冷日 (冷日), 2005/5/18 19:42)
-
[分享]iptables動態NAT對應測試 (冷日 (冷日), 2005/11/4 6:44)
-
[分享]一張網卡 bind 兩個不同網段的 IP (冷日 (冷日), 2005/11/4 7:00)
-
[分享]Linux 指令簡單將攻擊IP列入iptables 限制範圍 (冷日 (冷日), 2008/4/24 6:21)
-
[轉貼]防 ssh 暴力攻擊 使用iptables & shell (冷日 (冷日), 2010/3/29 10:12)
-
[轉貼]自動擋掉嘗試用ssh入侵Linux的攻擊者IP (冷日 (冷日), 2010/3/29 10:16)
|