iptables 執行個體雜錦(連接埠封閉或轉寄,ip,mac)

來源:互聯網
上載者:User
#/sbin/modprobe iptable_filter
#/sbin/modprobe ip_tables
#/sbin/modprobe iptable_nat
#/sbin/iptables -F -t nat
adsl撥號的轉寄
/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -o ppp0 -j MASQUERADE
 
網卡轉寄
/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -o eth0 -j MASQUERADE
 
封堵 tcp udp 連接埠, 並開啟特定的連接埠(包括vpn轉寄,進入的連接埠)
/sbin/iptables -I INPUT -p tcp -s 172.16.0.0/24 -m multiport --dports 110,80,25,22,443,5432,3306,21,445,10101,5252,8080,8383,139,1723 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s 172.16.0.0/24 --dport 5432 -j ACCEPT
/sbin/iptables -A INPUT -p udp -s 172.16.0.0/24 -m multiport --dports 53,111 -j ACCEPT
/sbin/iptables -A INPUT -p gre -s 172.16.0.0/24 -j ACCEPT
#/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A INPUT -s 172.16.0.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT 

/sbin/iptables -A FORWARD -p tcp -s 172.16.0.0/24 -m multiport --dports 80,110,21,25,1723,22,5252,10101,3306,443,5432,8080,2000,1935,8383 -j ACCEPT
/sbin/iptables -A FORWARD -p udp -s 172.16.0.0/24 --dport 53 -j ACCEPT
/sbin/iptables -A FORWARD -p udp -s 172.16.0.0/24 --dport 8000 -j ACCEPT
某遊戲連接埠
#/sbin/iptables -A FORWARD -p udp -s 172.16.0.0/24 --dport 7000 -j ACCEPT
vpn 所需協議
/sbin/iptables -A FORWARD -p gre -s 172.16.0.0/24 -j ACCEPT
運行ping
/sbin/iptables -A FORWARD -p icmp -s 172.16.0.0/24 -j ACCEPT
 
#加這一句, ICMP應答、FTP傳輸、DCC等才能穿過防火牆正常工作。大部分還有一些UDP協議都依賴這個機制。不開啟, bt們就不能變態了,bt軟體不能穿透防火牆了, 需要進行ftp時候再開啟, 限制只讓 tcp 的包穿透防火牆
#/sbin/iptables -A FORWARD -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

/sbin/iptables -A FORWARD -s 172.16.0.0/24 -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
 
#封住 tcp, udp 所有連接埠 (應該有其他方法)
/sbin/iptables -A FORWARD -p tcp -s 172.16.0.0/24 --dport 0:65535 -j DROP
/sbin/iptables -A FORWARD -p udp -s 172.16.0.0/24 --dport 0:65535 -j DROP
/sbin/iptables -A INPUT -p tcp -s 172.16.0.0/24 --dport 0:65535 -j DROP
/sbin/iptables -A INPUT -p udp -s 172.16.0.0/24 --dport 0:65535 -j DROP
 
#我的ip, 不受規則限制
/sbin/iptables -I INPUT -s 172.16.0.108 -j ACCEPT
/sbin/iptables -I FORWARD -s 172.16.0.108 -j ACCEPT
 
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  172.16.0.108         anywhere           
ACCEPT     tcp  --  172.16.0.0/24        anywhere            multiport dports pop3,http,smtp,ssh,https,postgres,mysql,ftp,microsoft-ds,10101,5252,webcache,8383,netbios-ssn,1723
ACCEPT     tcp  --  172.16.0.0/24        anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  172.16.0.0/24        anywhere            tcp dpt:postgres
ACCEPT     udp  --  172.16.0.0/24        anywhere            multiport dports domain,sunrpc
ACCEPT     gre  --  172.16.0.0/24        anywhere           
DROP       tcp  --  172.16.0.0/24        anywhere            tcp
DROP       udp  --  172.16.0.0/24        anywhere            udp
 
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     all  --  anywhere             anywhere            state NEW,RELATED,ESTABLISHED 
ACCEPT     all  --  172.16.0.108         anywhere           
ACCEPT     tcp  --  172.16.0.0/24        anywhere            multiport dports http,pop3,ftp,smtp,1723,ssh,5252,10101,mysql,https,postgres,webcache,sieve,1935,8383
ACCEPT     udp  --  172.16.0.0/24        anywhere            udp dpt:domain
ACCEPT     udp  --  172.16.0.0/24        anywhere            udp dpt:8000
ACCEPT     udp  --  172.16.0.0/24        anywhere            udp dpt:afs3-fileserver
ACCEPT     gre  --  172.16.0.0/24        anywhere           
ACCEPT     icmp --  172.16.0.0/24        anywhere           
DROP       tcp  --  172.16.0.0/24        anywhere            tcp
DROP       udp  --  172.16.0.0/24        anywhere            udp
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   



=======================================================
封ip
/sbin/iptables -A FORWARD -s 172.16.0.*** -j DROP
取消ip封閉
/sbin/iptables -D FORWARD -s 172.16.0.*** -j DROP
=======================================================
路由連接埠轉寄, 電驢為高id使用者, 但是試過後,好像不行
/sbin/iptables -t nat -A PREROUTING -p udp -m udp --dport 19145 -j DNAT --to-destination 172.16.0.108:19145
/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -d 172.16.0.108 -p udp -m udp --dport 19145 -j SNAT --to-source 172.16.0.2
 
/sbin/iptables -t nat -A PREROUTING -p tcp -m tcp --dport 19145 -j DNAT --to-destination 172.16.0.108:19145
/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -d 172.16.0.108 -p tcp -m tcp --dport 19145 -j SNAT --to-source 172.16.0.2
 
#/sbin/iptables -t nat -A PREROUTING -p udp -m udp --dport 4662 -j DNAT --to-destination 172.16.0.108:4662
#/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -d 172.16.0.108 -p udp -m udp --dport 4662 -j SNAT --to-source 172.16.0.2
 
#/sbin/iptables -t nat -A PREROUTING -p tcp -m tcp --dport 4662 -j DNAT --to-destination 172.16.0.108:4662
#/sbin/iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -d 172.16.0.108 -p tcp -m tcp --dport 4662 -j SNAT --to-source 172.16.0.2
=============================================================
###### 用mac地址封閉上網
######注意順序問題
 
#/sbin/iptables -A FORWARD -m mac --mac-source 00:00:BA:A5:7D:12 -p tcp -m multiport --dports 80,110,21,25,1723,22,5252,10101,3306,443,5432,8080,2000,1935,8383 -j  ACCEPT
 
#/sbin/iptables -A FORWARD -m mac --mac-source 00:00:BA:A5:7D:12 -p udp --dport 53 -j ACCEPT
 
#/sbin/iptables -A FORWARD -p icmp -j ACCEPT
 
######這一行保持在最後. 用iptables -L 查看
 
#/sbin/iptables -A FORWARD -m mac --mac-source 00:00:BA:A5:7D:12 -j DROP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.