新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
企業(yè)級iptables配置實例
iptables防火墻可以用于創(chuàng)建過濾(filter)與NAT規(guī)則。所有Linux發(fā)行版都能使用iptables,因此理解如何配置 iptables將會幫助你更有效地管理Linux防火墻。

目前創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、扶溝網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
iptables優(yōu)化案例
[root@ghzz ~]# vi /root/shell/iptables.sh
#!/bin/bash
#by qiuyuetao
#--------------------------------------------------------------------------
IPTAB=/sbin/iptables #定義環(huán)境變量
$IPTAB -X #清空自定義鏈
$IPTAB -Z #清空計數(shù)器
$IPTAB -F #清空當前表的所有規(guī)則
$IPTAB -t nat -F #清空nat表的所有規(guī)則
$IPTAB -t mangle -F #清空mangel 表規(guī)則
$IPTAB -t raw -F #清空raw表規(guī)則
$IPTAB -X -t nat #清空自定義鏈
$IPTAB -X -t raw
$IPTAB -X -t mangle
#--------------------------------------------------------------------------
$IPTAB -A INPUT -i lo -j ACCEPT #允許回環(huán)地址訪問
$IPTAB -A INPUT -s 127.0.0.1 -j ACCEPT #允許自己訪問
$IPTAB -A INPUT -s 192.168.0.0/22 -j ACCEPT# 允許內(nèi)網(wǎng)訪問
$IPTAB -A INPUT -s 119.161.189.1 -j ACCEPT #允許跳板機訪問
#--------------------------------------------------------------------------
$IPTAB -A INPUT -p icmp -j ACCEPT #允許icmp 也就是ping
$IPTAB -A INPUT -p tcp -m multiport --dport 80,22,3306,2188 -j ACCEPT #對外開放的端口
$IPTAB -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #對進來的包的狀態(tài)進行檢測。已經(jīng)建立tcp連接的包以及該連接相關(guān)的包允許通過!
$IPTAB -P INPUT DROP #除了上面的fiter表input鏈
$IPTAB -P FORWARD DROP
$IPTAB -P OUTPUT ACCEPT
#--------------------------------------------------------------------------
$IPTAB-save > /etc/sysconfig/iptables
##修改iptables -F -X -Z 都需要 /etc/init.d/iptables save
## -t:指定要保存的表的名稱。
# iptables-save -t filter > iptables.bak
恢復備份的iptables配置:
iptables-restor
iptables日常配置
防火墻是否開啟
[root@m01 ~] /etc/init.d/iptables status
開啟iptables
iptables -F #清空當前表規(guī)則
iptables -X #清空自定義鏈
iptables -Z #清空計數(shù)器
開啟內(nèi)核轉(zhuǎn)發(fā)
將net.ipv4.ip_forward = 1 由0改為1
sed -i 'snet.ipv4.ip_forward = 0net.ipv4.ip_forward = 1g'
sysctl -p 生效
添加iptables模塊
lsmod 顯示已經(jīng)加載到內(nèi)核中的模塊的狀態(tài)信息
lsmod|egrep "nat|filter" 查看nat 與filter表加載的模塊信息
modprobe ip_tables
modprobe ip_conntrack
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack_ftp 連接跟蹤
modprobe ip_nat_ftp nat模塊
modprobe ipt_state 狀態(tài)模塊
企業(yè)級的防火墻模式選擇
“兩種思想:”
逛公園:
1、默認規(guī)則默認是允許的狀態(tài)。
看電影:
2、默認規(guī)則默認是不允許的狀態(tài)。更安全。
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允許內(nèi)網(wǎng)IP地址段訪問
iptables -A INPUT -d 172.16.1.0/24 -j ACCEPT
#允許回環(huán)地址可以進出
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
修改默認規(guī)則
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
添加允許的服務及端口
#允許訪問http服務80 443
iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
#只允許內(nèi)網(wǎng)ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -s 172.16.1.0/24 -j ACCEPT
#允許ftp請求
#iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#允許某端口映射
iptables -A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --sport 53 -j ACCEPT
NAT 共享上網(wǎng)
/proc/sys/net/ipv4/ip_forward 開啟內(nèi)核轉(zhuǎn)發(fā)
iptables -t filter -A FORWARD -j ACCEPT
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.61
#所有內(nèi)網(wǎng)172.16.1.0網(wǎng)段地址,都通過10.0.0.61這個IP 轉(zhuǎn)發(fā)上外網(wǎng)
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE
#MASQUERADE 自動獲取服務端IP,做IP地址轉(zhuǎn)發(fā)(例如:openvpn)
其他應用服務規(guī)則案例
"1)http服務"
iptables -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 8080,8081,8082 -j ACCEPT
iptables -A INPUT -p tcp --dport 22000:22030 -j ACCEPT
#:冒號代表一段端口從22000-22030所有端口都允許
"2) db數(shù)據(jù)服務"
iptables -A INPUT -s 10.1.8.0/24 -p tcp --dport 3306 -j ACCEPT
"3) snmp 網(wǎng)絡管理"
iptables -A INPUT -s 10.1.8.0/24 -p UDP --dport 161 -j ACCEPT
"4) rsync 實時同步"
iptables -A INPUT -s 10.1.8.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
" 5) nfs 2049,rpc 111 (NFS網(wǎng)絡文件共享、rpc)"
iptables -A INPUT -s 10.1.8.0/24 -p udp -m multiport --dport 111,892,2049 -j ACCEPT
iptables -A INPUT -s 10.1.8.0/24 -p tcp -m multiport --dport 111,892,2049 -j ACCEPT
" 6) 允許所有ping"
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
" 7)允許內(nèi)網(wǎng)172.16.1.0 網(wǎng)段ping"
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -s 172.16.1.0/24 -j ACCEP
"8) keepalived vrr"
-A INPUT -d 172.16.1.0/24 -j ACCEPT
-A INPUT -p vrrp -j ACCEPT
"9) zabbix端口開放"
-A INPUT -p tcp -m tcp --dport 10050:10051 -j ACCEPT
-A INPUT -p tcp -m udp --dport 10050:10051 -j ACCEPT
iptable安全配置
1)限速
iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j ACCEPT
2)沒秒ping不超過10個
iptables -A FORWAD -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 10 -j ACCPET
3)將SYN及ACK SYN限制為每秒不超過200
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
4)防范 SYN-Flood 碎片***
iptables -N syn-flood
iptables -A INPUT -syn -j syn-flood
iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
iptables -A syn-flood -j DROP
prevent all Stealth Scans and TCP State Flags
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
All of the bits are cleared
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
SYN and RST are both set
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
SYN and FIN are both set
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
FIN and RST are both set
iptables -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
FIN is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
PSH is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
URG is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
當前標題:企業(yè)級iptables配置實例
當前地址:http://fisionsoft.com.cn/article/djscggp.html


咨詢
建站咨詢
