1. 程式人生 > 其它 >Linux中Iptables防火牆的使用

Linux中Iptables防火牆的使用

Linux中Iptables防火牆的使用

  • Iptables的使用
  • Iptables動作
  • Iptables基本的條件匹配
  • -s源地址、-d目標地址
  • --sport源埠、--dport目標埠
  • -i、-o、-m、-j動作
  • 模組

一、Iptables的使用

  1.安裝Iptables

[root@m01 ~]# yum install iptables*
可以使用 rpm -q iptables 命令檢視iptables是否安裝

  2.啟動Iptables

[root@m01 ~]# systemctl start iptables
可以使用 systemctl status iptables 命令檢視iptables狀態

  3.關閉firewalld

[root@m01 ~]# systemctl disable --now firewalld

  格式:

-t               指定操作的表
-L, --list       列出當前的規則
-v               顯示資料包和資料包大小
-n               不反解地址
-A, --append     追加一條規則到鏈中
-I, --insert     插入一條規則,插入到頂部
-F, --flush      清空
-Z, --zero       清空計數器(  包數量 、包大小)

-D, --delete     刪除鏈中的規則

-R, --replace    修改
-S, --list-rules 列出所有的規則


-N, --new-chain  建立一個自定義 鏈
-X, --delete-chain 刪除一個自定義鏈
-P, --policy     指定鏈的預設策略

二、Iptables動作

ACCEPT     將資料包放行,進行完此處理動作後,將不再比對其它規則,直接跳往下一個規則鏈。
REJECT     攔阻該資料包,並傳送資料包通知對方。
DROP       丟棄包不予處理,進行完此處理動作後,將不再比對其它規則,直接中斷過濾程式。
REDIRECT   將包重新導向到另一個埠,進行完此處理動作後,將會繼續比對其它規則。

三、Iptables基本的條件匹配

TCP(http)
UDP
ICMP(ping)
ALL

四、-s源地址、-d目標地址

  源地址:傳送請求的地址

  目標地址:訪問的地址

五、--sport源埠、--dport目標埠

  源埠:傳送請求的埠

  目標埠:訪問的埠

六、-i、-o、-m、-j動作

-i : 進來的網絡卡
-o : 出去的網絡卡
-m : 指定模組
-j : 轉發動作
-p :指定協議

七、案例

  案例1:只允許22埠可以訪問,其他埠全部無法訪問

iptables -t filter -A INPUT -p TCP --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例2:只允許22,80,443埠可以訪問,其他埠全部無法訪問

iptables -t filter -A INPUT -p TCP --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 443  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例3:只允許22,80,443埠可以訪問,其他埠全部無法訪問,但是本機可以訪問百度

  

  案例4:要求使用192.168.15.81能夠通過22埠連結,但是其他的不行

iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例5:只允許192.168.15.71能夠通過22埠連結,其他的不行

iptables -t filter -A INPUT -p  TCP -s 192.168.15.71  -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例6:要求192.168.15.71對外部不可見

iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP

  案例7:要求使用eth0網絡卡的所有請求全部拒絕

iptables -t filter -A INPUT -p TCP -i eth0 -j DROP

  案例8:要求訪問伺服器的8080埠轉發至80埠

iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80

  案例9:要求只允許windows通過ssh連線192.168.15.81,其他的拒絕

iptables -t filter -I INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 22 -j DROP

  知識儲備:

    檢視本機端口占用命令

netstat -nutlp

八、模組

  拓展Iptables的功能

-m : 指定模組

  1.連續匹配多個埠(multiport)

--dports  : 指定多個埠(不同埠之間以逗號分割,連續的埠使用冒號分割)

  2.指定一段連續的ip地址範圍(iprange)

--src-range from[-to]:源地址範圍
--dst-range from[-to]:目標地址範圍

  3.匹配指定字串(string)

--string pattern  # 指定要匹配的字串
--algo {bm|kmp}  # 匹配的查詢演算法

  4.根據時間段匹配報文(time)

--timestart hh:mm[:ss]    # 開始時間
--timestop hh:mm[:ss]    # 結束時間
--monthdays day[,day...]    # 指定一個月的某一天
--weekdays day[,day...]    # 指定周 還是  周天 

  5.禁ping, 預設本機無法ping別人 、別人無法ping自己

--icmp-type {type[/code]|typename}
echo-request  (8) 請求 
echo-reply    (0) 迴應

  6.限制連結數,併發連線數(connlimit)

--connlimit-upto n        #  如果現有連線數小於或等於  n  則 匹配
--connlimit-above n       #  如果現有連線數大於n 則匹配

  7.針對 報文速率 進行限制。 秒、分鐘、小時、天

--limit rate[/second|/minute|/hour|/day]  # 報文數量 
--limit-burst number  # 報文數量(預設:5

  

九、模組案例

  1.要求將22,80,443以及30000-50000之間所有的埠向外暴露,其他埠拒絕

iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  2.要求訪問資料包中包含HelloWorld的資料不允許通過

iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP

  3.要求192.168.15.1 - 192.168.15.10之間的所有IP能夠連線192.168.15.81,其他拒絕

iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT 
iptables -t filter -A INPUT -p TCP -j DROP

  4.要求每天的12到13之間,不允許訪問

iptables -t filter -A INPUT -p TCP -m time  --timestart 4:00   --timestop 5:00 -j DROP

    注意:時間必須使用UTC時間

  5.要求別人不能ping本機,但是本機可以ping別人

iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP

  6.要求主機連線最多有2個

iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP

  7.要求限制速率在500k/s左右

iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP