iptables基礎實戰練習,iptables基礎實戰

來源:互聯網
上載者:User

iptables基礎實戰練習,iptables基礎實戰

目錄:

  一、基本規則練習

  二、SNAT源地址轉移

  三、DNAT目標地址轉移

 

一、基礎規則練習

 

(1) 允許存取ssh (連接埠:22)

1 iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT2 iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT
 

(2)修改預設規則鏈(關閉所有連接埠)

1 iptables -P INPUT DROP2 iptables -P OUTPUT DROP3 iptables -P FORWARD DROP
 

(3)允許存取web(80)連接埠 httpd nginx

1 iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT2 iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT
 

(4)修改預設規則鏈後,我們發現ping不通自己,也ping不通別的主機

1 iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 2 iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT
 

(5)允許自己ping別的主機

1 iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT2 iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT
 

(6)允許任何人來ping本機

1 iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT2 iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT
 

(7)同時開發多個連接埠(多連接埠匹配)

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT2 iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT
 

(8)iptables -vnL –line-numbers #顯示數字

iptables  -vnL INPUT  --line-numbers Chain INPUT (policy DROP 1 packets, 229 bytes)num   pkts bytes target     prot opt in     out     source               destination         1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 82       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 03       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:805     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22
 (9) 源地址,目的位址範圍匹配
1 iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT2 iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT
 

(10)禁止包含”old”字元的頁面出來

1 iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP
 

(11)基於時間限定,9點到19點,禁止訪問80連接埠

1 iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP
 

(12)周一到周五9點到19點禁止訪問80連接埠

1 iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP
 

(13)連接埠大於2個並發串連(禁止)

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP
 

(14)連接埠同一個用戶端小於3個並發串連

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

 

(15)目標地址和連接埠轉換樣本(對22連接埠的轉換)

1 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 22022 -j DNAT --to-destination 192.168.2.4:22

 

  二、SNAT源地址轉移
 SNAT:源地址轉換。內網主機在訪問互連網的時候所有源地址都轉換為防火牆的外網地址,起到隱藏內網客戶機的目的。同時,也解決了IPV4公網地址不夠用的需求。
1 iptables -t nat -A POSTROUTING -s 10.1.249.158 -j SNAT --to-source 192.168.2.3 
  三、DNAT目標地址轉移

 

 

 

DNAT:目的地址轉換。當外網主機訪問內網的某台伺服器的時候,如果直接暴露伺服器的IP於公網,可能會遭受各種各樣的攻擊,而DNAT的主要作用就是在伺服器前面添加一台防火牆。將防火牆的地址公布出去,讓外網用戶端通過訪問防火牆的地址就可以訪問到本機伺服器。這樣就起到了保護伺服器的目的;
1 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.