1. 程式人生 > 其它 >Java基礎學習:方法05

Java基礎學習:方法05

一、名字解釋

ACCEPT:允許資料包通過。

DROP:直接丟棄資料包,不給任何迴應資訊,這時候客戶端會感覺自己的請求泥牛入海了,過了超時時間才會有反應。

REJECT:拒絕資料包通過,必要時會給資料傳送端一個響應的資訊,客戶端剛請求就會收到拒絕的資訊。

SNAT:源地址轉換,解決內網使用者用同一個公網地址上網的問題。

MASQUERADE:是SNAT的一種特殊形式,適用於動態的、臨時會變的ip上。

DNAT:目標地址轉換。

REDIRECT:在本機做埠對映。

LOG:在/var/log/messages檔案中記錄日誌資訊,然後將資料包傳遞給下一條規則,也就是說除了記錄以外不對資料包做任何其他操作,仍然讓下一條規則去匹配。

二、查詢

iptables -t 表名 -L          檢視對應表的所有規則,-t選項指定要操作的表,省略”-t 表名”時,預設表示操作filter表,-L表示列出規則,即檢視規則。
iptables -t 表名 -L 鏈名       檢視指定表的指定鏈中的規則。
iptables -t 表名 -v -L 檢視指定表的所有規則,並且顯示更詳細的資訊(更多欄位),一般簡寫為iptables -t 表名 -vL。
iptables -t 表名 -n -L 查看錶的所有規則,並且在顯示規則時,不對規則中的IP或者埠進行名稱反解,-n選項表示不解析IP地址。
iptables --line-numbers -t 表名 -L 查看錶的所有規則,並且顯示規則的序號
iptables -t 表名 -v -x -L     查看錶中的所有規則,-x選項表示顯示計數器的精確值。為了方便可以一起使用iptables --line -t filter -nvxL

三、設定指定表的指定鏈的預設策略(預設動作)

將filter表中FORWARD鏈的預設策略設定為ACCEPT
命令語法:iptables -t 表名 -P 鏈名 動作
示例:iptables -t filter -P FORWARD ACCEPT

四、新增規則

在指定表的指定鏈的尾部新增一條規則,-A選項表示在對應鏈的末尾新增規則,省略-t選項時,表示預設操作filter表中的規則。
iptables -t 表名 -A 鏈名 匹配條件 -j 動作 示例:iptables -t filter -A INPUT -s 192.168.1.146 -j DROP

在指定表的指定鏈的首部新增一條規則,-I選型表示在對應鏈的開頭新增規則。
命令語法:iptables -t 表名 -I 鏈名 匹配條件 -j 動作
示例:iptables -t filter -I INPUT -s 192.168.1.146 -j ACCEPT

在指定表的指定鏈的指定位置新增一條規則
命令語法:iptables -t 表名 -I 鏈名 規則序號 匹配條件 -j 動作
示例:iptables -t filter -I INPUT 5 -s 192.168.1.146 -j REJECT

五、刪除規則

按照規則序號刪除規則,刪除指定表的指定鏈的指定規則,-D選項表示刪除對應鏈中的規則。
命令語法:iptables -t 表名 -D 鏈名 規則序號
示例:iptables -t filter -D INPUT 3

按照具體的匹配條件與動作刪除規則,刪除指定表的指定鏈的指定規則。
命令語法:iptables -t 表名 -D 鏈名 匹配條件 -j 動作
示例:iptables -t filter -D INPUT -s 192.168.1.146 -j DROP

刪除指定表的指定鏈中的所有規則,-F選項表示清空對應鏈中的規則,執行時需三思。
命令語法:iptables -t 表名 -F 鏈名
示例:iptables -t filter -F INPUT

刪除指定表中的所有規則,執行時需三思。
命令語法:iptables -t 表名 -F
示例:iptables -t filter -F

六、修改規則

修改指定表中指定鏈的指定規則,-R選項表示修改對應鏈中的規則,使用-R選項時要同時指定對應的鏈以及規則對應的序號,並且規則中原本的匹配條件不可省略。
命令語法:iptables -t 表名 -R 鏈名 規則序號 規則原本的匹配條件 -j 動作
示例:iptables -t filter -R INPUT 3 -s 192.168.1.146 -j ACCEPT
上述示例表示修改filter表中INPUT鏈的第3條規則,將這條規則的動作修改為ACCEPT, -s 192.168.1.146為這條規則中原本的匹配條件,如果省略此匹配條件,修改後的規則中的源地址可能會變為0.0.0.0/0。

修改指定表的指定鏈的預設策略(預設動作),並非修改規則
命令語法:iptables -t 表名 -P 鏈名 動作
示例:iptables -t filter -P FORWARD ACCEPT

七、儲存規則

service iptables save
iptables-save > /etc/sysconfig/iptables

八、載入規則

iptables-restore < /etc/sysconfig/iptables

九、匹配條件

  • 基本匹配條件
-s用於匹配報文的源地址,可以同時指定多個源地址,每個IP之間用逗號隔開,也可以指定為一個網段。
#示例如下
iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT

-d用於匹配報文的目標地址,可以同時指定多個目標地址,每個IP之間用逗號隔開,也可以指定為一個網段。
#示例如下
iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT

-p用於匹配報文的協議型別,可以匹配的協議型別tcp、udp、udplite、icmp、esp、ah、sctp等(centos7中還支援icmpv6、mh)。
#示例如下
iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT
iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

-i用於匹配報文是從哪個網絡卡介面流入本機的,由於匹配條件只是用於匹配報文流入的網絡卡,所以在OUTPUT鏈與POSTROUTING鏈中不能使用此選項。
#示例如下
iptables -t filter -I INPUT -p icmp -i eth4 -j DROP
iptables -t filter -I INPUT -p icmp ! -i eth4 -j DROP

-o用於匹配報文將要從哪個網絡卡介面流出本機,於匹配條件只是用於匹配報文流出的網絡卡,所以在INPUT鏈與PREROUTING鏈中不能使用此選項。
#示例如下
iptables -t filter -I OUTPUT -p icmp -o eth4 -j DROP
iptables -t filter -I OUTPUT -p icmp ! -o eth4 -j DROP

  • 擴充套件匹配條件
tcp擴充套件模組
常用的擴充套件匹配條件如下:
-p tcp -m tcp –sport 用於匹配tcp協議報文的源埠,可以使用冒號指定一個連續的埠範圍
-p tcp -m tcp –dport 用於匹配tcp協議報文的目標埠,可以使用冒號指定一個連續的埠範圍
#示例如下
iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp --sport 22 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 22:25 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport :22 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 80: -j REJECT
iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp ! --sport 22 -j ACCEPT

multiport擴充套件模組
常用的擴充套件匹配條件如下:
-p tcp -m multiport –sports 用於匹配報文的源埠,可以指定離散的多個埠號,埠之間用”逗號”隔開
-p udp -m multiport –dports 用於匹配報文的目標埠,可以指定離散的多個埠號,埠之間用”逗號”隔開
#示例如下
iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j REJECT

–tcp-flags
用於匹配報文的tcp頭的標誌位
#示例
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags ALL SYN -j REJECT
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags ALL SYN,ACK -j REJECT

–syn
用於匹配tcp新建連線的請求報文,相當於使用”–tcp-flags SYN,RST,ACK,FIN SYN”
#示例
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT

十、常用擴充套件模板

  • iprange擴充套件模組
包含的擴充套件匹配條件如下
–src-range:指定連續的源地址範圍
–dst-range:指定連續的目標地址範圍

#示例
iptables -t filter -I INPUT -m iprange --src-range 192.168.1.127-192.168.1.146 -j DROP
iptables -t filter -I OUTPUT -m iprange --dst-range 192.168.1.127-192.168.1.146 -j DROP
iptables -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.146 -j DR
  • string擴充套件模組
常用擴充套件匹配條件如下
–algo:指定對應的匹配演算法,可用演算法為bm、kmp,此選項為必需選項。
–string:指定需要匹配的字串

#示例
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT
iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT
  • time擴充套件模組
常用擴充套件匹配條件如下
–timestart:用於指定時間範圍的開始時間,不可取反
–timestop:用於指定時間範圍的結束時間,不可取反
–weekdays:用於指定”星期幾”,可取反
–monthdays:用於指定”幾號”,可取反
–datestart:用於指定日期範圍的開始日期,不可取反
–datestop:用於指定日期範圍的結束時間,不可取反

#示例
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --monthdays 22,23 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time ! --monthdays 22,23 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --weekdays 5 --monthdays 22,23,24,25,26,27,28 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --datestart 2017-12-24 --datestop 2017-12-27 -j REJECT
  • connlimit擴充套件模組
常用的擴充套件匹配條件如下
–connlimit-above:單獨使用此選項時,表示限制每個IP的連結數量。
–connlimit-mask:此選項不能單獨使用,在使用–connlimit-above選項時,配合此選項,則可以針對”某類IP段內的一定數量的IP”進行連線數量的限制。

#示例
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 27 -j REJECT
  • limit擴充套件模組
常用的擴充套件匹配條件如下
–limit-burst:類比”令牌桶”演算法,此選項用於指定令牌桶中令牌的最大數量,
–limit:類比”令牌桶”演算法,此選項用於指定令牌桶中生成新令牌的頻率,可用時間單位有second、minute 、hour、day。

#示例 #注意,如下兩條規則需配合使用
iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
iptables -t filter -A INPUT -p icmp -j REJECT
  • udp和icmo擴充套件模組
udp擴充套件
常用的擴充套件匹配條件
–sport:匹配udp報文的源地址
–dport:匹配udp報文的目標地址
#示例
iptables -t filter -I INPUT -p udp -m udp --dport 137 -j ACCEPT
iptables -t filter -I INPUT -p udp -m udp --dport 137:157 -j ACCEPT
#可以結合multiport模組指定多個離散的埠

icmp擴充套件
常用的擴充套件匹配條件
–icmp-type:匹配icmp報文的具體型別
#示例
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j REJECT
iptables -t filter -I INPUT -p icmp --icmp-type 8 -j REJECT
iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0/0 -j REJECT
iptables -t filter -I OUTPUT -p icmp --icmp-type 0 -j REJECT
iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j REJECT
  • state擴充套件模組
待新增