1. 概述
- 作用:
-m 指定
- 語法
iptables -m M_NAME
說明:如果命令行 -p 指明了協(xié)議,則不必手動添加
2. icmp 模塊
使用
-m icmp引用該模塊
2.1 示例(禁止本機(jī)ping其它主機(jī))
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-reply -j REJECT
查下邊--icmp-type可知 echo-reply對應(yīng)的TYPE只為0,因此可以寫做:
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 0 -j REJECT
2.2 示例(其他主機(jī)ping本機(jī))
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-request -j REJECT
查下邊--icmp-type可知 echo-reply對應(yīng)的TYPE只為8,因此可以寫做:
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8 -j REJECT
2.3 --icmp-type 說明
| TYPE | CODE | Description | Query | Error |
|---|---|---|---|---|
| 0 | 0 | Echo Reply——回顯應(yīng)答(Ping應(yīng)答) | x | |
| 3 | 0 | Network Unreachable——網(wǎng)絡(luò)不可達(dá) | x | |
| 3 | 1 | Host Unreachable——主機(jī)不可達(dá) | x | |
| 3 | 2 | Protocol Unreachable——協(xié)議不可達(dá) | x | |
| 3 | 3 | Port Unreachable——端口不可達(dá) | x | |
| 3 | 4 | Fragmentation needed but no frag. bit set——需要進(jìn)行分片但設(shè)置不分片比特 | x | |
| 3 | 5 | Source routing failed——源站選路失敗 | x | |
| 3 | 6 | Destination network unknown——目的網(wǎng)絡(luò)未知 | x | |
| 3 | 7 | Destination host unknown——目的主機(jī)未知 | x | |
| 3 | 8 | Source host isolated (obsolete)——源主機(jī)被隔離(作廢不用) | x | |
| 3 | 9 | Destination network administratively prohibited——目的網(wǎng)絡(luò)被強(qiáng)制禁止 | x | |
| 3 | 10 | Destination host administratively prohibited——目的主機(jī)被強(qiáng)制禁止 | x | |
| 3 | 11 | Network unreachable for TOS——由于服務(wù)類型TOS,網(wǎng)絡(luò)不可達(dá) | x | |
| 3 | 12 | Host unreachable for TOS——由于服務(wù)類型TOS,主機(jī)不可達(dá) | x | |
| 3 | 13 | Communication administratively prohibited by filtering——由于過濾,通信被強(qiáng)制禁止 | x | |
| 3 | 14 | Host precedence violation——主機(jī)越權(quán) | x | |
| 3 | 15 | Precedence cutoff in effect——優(yōu)先中止生效 | x | |
| 4 | 0 | Source quench——源端被關(guān)閉(基本流控制) | ||
| 5 | 0 | Redirect for network——對網(wǎng)絡(luò)重定向 | ||
| 5 | 1 | Redirect for host——對主機(jī)重定向 | ||
| 5 | 2 | Redirect for TOS and network——對服務(wù)類型和網(wǎng)絡(luò)重定向 | ||
| 5 | 3 | Redirect for TOS and host——對服務(wù)類型和主機(jī)重定向 | ||
| 8 | 0 | Echo request——回顯請求(Ping請求) | x | |
| 9 | 0 | Router advertisement——路由器通告 | ||
| 10 | 0 | Route solicitation——路由器請求 | ||
| 11 | 0 | TTL equals 0 during transit——傳輸期間生存時間為0 | x | |
| 11 | 1 | TTL equals 0 during reassembly——在數(shù)據(jù)報組裝期間生存時間為0 | x | |
| 12 | 0 | IP header bad (catchall error)——壞的IP首部(包括各種差錯) | x | |
| 12 | 1 | Required options missing——缺少必需的選項 | x | |
| 13 | 0 | Timestamp request (obsolete)——時間戳請求(作廢不用) | x | |
| 14 | Timestamp reply (obsolete)——時間戳應(yīng)答(作廢不用) | x | ||
| 15 | 0 | Information request (obsolete)——信息請求(作廢不用) | x | |
| 16 | 0 | Information reply (obsolete)——信息應(yīng)答(作廢不用) | x | |
| 17 | 0 | Address mask request——地址掩碼請求 | x | |
| 18 | 0 | Address mask reply——地址掩碼應(yīng)答 |
3. iprange模塊
作用
一次加入范圍IP地址示例
iptables -t filter -I INPUT -m iprange --src-range 192.168.2.20-192.168.2.100 -j REJECT
參數(shù)說明
- --src-range:指定源地址范圍。
- --dst-range:指定目標(biāo)地址范圍。
- ! --src-range:非指定的源地址。
- ! --dst-range:非指定的目標(biāo)地址。
4. multiport 模塊
- 作用
一次加入多個端口(連續(xù)或不連續(xù)的)
- 添加不連續(xù)端口
iptables -t filter -I INPUT -p tcp -m multiport --dports 20,21,22,25,80,110 -j ACCEPT
- 添加連續(xù)端口
iptables -t filter -I INPUT -p tcp -m multiport --dports 20,21,22,25,80,110 -j ACCEPT
5. state 模塊
5.1 語法
- 作用
按鏈接狀態(tài)過濾鏈接
- 示例
# iptables -t filter -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- 狀態(tài)說明
| 參數(shù) | 說明 |
|---|---|
| NEW | 新生態(tài) |
| ESTABLISHED | 連接態(tài) |
| RELATED | 衍生態(tài) |
| INVALID | 無效態(tài) |
5.2 完整示例
使用狀態(tài)防火墻,放行本機(jī)FTP服務(wù)[被動模式]
- 設(shè)置防火墻規(guī)則
iptables -t filter -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -I INPUT -p tcp --dport 21,20 -j ACCEPT
iptables -t filter -A INPUT -j REJECT
- 載入
nf_conntrack_ftp模塊
modprobe nf_conntrack_ftp
- 設(shè)置模塊自動添加
在/etc/sysconfig/iptables-config文件中修改如下項:
IPTABLES_MODULES="nf_conntrack_ftp"
如果要添加多個模塊,可寫為
IPTABLES_MODULES="m01 m02 ……"
6. limit模塊
6.1 語法
- 作用
通過令牌桶限制請求數(shù)
- 示例
iptables -t filter -A INPUT -p icmp -m limit --limit 10/minute --limit-burst 5 -j ACCEPT
- 說明
--limit 10/minute 每分中產(chǎn)生10個令牌
其他限制條件:x/second 、x/minute、x/hour、x/day
--limit-burst 5 令牌桶中放入5個令牌
6.2 完整示例
- 為方便演示,先清空防火墻策略,禁止對方ping本機(jī)
iptables -F
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8 -j REJECT
- 設(shè)置防火墻策略
每分鐘產(chǎn)生12個令牌(5秒一個),令牌桶中有5個令牌
iptables -t filter -I INPUT -p icmp -m limit --limit 12/minute --limit-burst 5 -j ACCEPT
- 測試
從另一臺服務(wù)器ping改服務(wù)器,結(jié)果如下
- 每5秒產(chǎn)生一個令牌,和我們設(shè)置的一樣
- 但是一開始并沒有連續(xù)取到5個令牌,對令牌桶的設(shè)置還是不太理解。
64 bytes from 10.10.239.32: icmp_seq=156 ttl=64 time=0.258 ms
64 bytes from 10.10.239.32: icmp_seq=157 ttl=64 time=0.238 ms
64 bytes from 10.10.239.32: icmp_seq=158 ttl=64 time=0.174 ms
From 10.10.239.32 icmp_seq=159 Destination Port Unreachable
From 10.10.239.32 icmp_seq=160 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=161 ttl=64 time=0.233 ms
From 10.10.239.32 icmp_seq=162 Destination Port Unreachable
From 10.10.239.32 icmp_seq=163 Destination Port Unreachable
From 10.10.239.32 icmp_seq=164 Destination Port Unreachable
From 10.10.239.32 icmp_seq=165 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=166 ttl=64 time=0.228 ms
From 10.10.239.32 icmp_seq=167 Destination Port Unreachable
From 10.10.239.32 icmp_seq=168 Destination Port Unreachable
From 10.10.239.32 icmp_seq=169 Destination Port Unreachable
From 10.10.239.32 icmp_seq=170 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=171 ttl=64 time=0.264 ms
From 10.10.239.32 icmp_seq=172 Destination Port Unreachable
From 10.10.239.32 icmp_seq=173 Destination Port Unreachable
From 10.10.239.32 icmp_seq=174 Destination Port Unreachable
From 10.10.239.32 icmp_seq=175 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=176 ttl=64 time=0.426 ms
From 10.10.239.32 icmp_seq=177 Destination Port Unreachable
From 10.10.239.32 icmp_seq=178 Destination Port Unreachable
From 10.10.239.32 icmp_seq=179 Destination Port Unreachable
From 10.10.239.32 icmp_seq=180 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=181 ttl=64 time=0.172 ms
From 10.10.239.32 icmp_seq=182 Destination Port Unreachable
From 10.10.239.32 icmp_seq=183 Destination Port Unreachable
From 10.10.239.32 icmp_seq=184 Destination Port Unreachable
From 10.10.239.32 icmp_seq=185 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=186 ttl=64 time=0.201 ms
From 10.10.239.32 icmp_seq=187 Destination Port Unreachable
From 10.10.239.32 icmp_seq=188 Destination Port Unreachable
From 10.10.239.32 icmp_seq=189 Destination Port Unreachable
From 10.10.239.32 icmp_seq=190 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=191 ttl=64 time=0.330 ms
From 10.10.239.32 icmp_seq=192 Destination Port Unreachable
From 10.10.239.32 icmp_seq=193 Destination Port Unreachable
From 10.10.239.32 icmp_seq=194 Destination Port Unreachable
From 10.10.239.32 icmp_seq=195 Destination Port Unreachable
64 bytes from 10.10.239.32: icmp_seq=196 ttl=64 time=0.266 ms
7. connlimit
7.1 語法
作用
限同一IP最大連接數(shù)語法示例
僅允許每個客戶端有兩個ssh連接
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit ! --connlimit-above 2 -j ACCEPT
7.2 完整示例( 限同一IP最大連接數(shù)為2)
- 允許連接態(tài)和衍生態(tài)(必要)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- 限同一IP最大連接數(shù)為2
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit ! --connlimit-above 2 -j ACCEPT
或
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT
- 測試
從客戶ssh到該服務(wù)器,只允許有兩個鏈接。
8. time模塊
作用
設(shè)置規(guī)則每天生效時間。示例
iptables -A INPUT -m time --timestart 8:00 --timestop 15:30 -j ACCEPT
9. comment模塊
作用
添加說明示例
iptables -A INPUT -s 10.10.237.32 -m comment --comment "禁止源訪問" -j REJECT
10. mark模塊
iptables -t filter -A INPUT -m mark --mark 2 -j REJECT
