Iptables規則:
Firewall:工作在主機或網絡邊緣,對進出的保溫按事先定義的規則進行檢查,并有比對到的規則進行處理的一組硬體或者軟體.
使用者請求進來,先到本地的route路由判斷是否通路的為本地ip,如果不是則檢視是否開啟了ip-forward功能,如果開起來,則繼續向後轉發.
iptables/netfilter:
netfilter:工作核心中,讓規則能夠生效的網絡架構.
iptables:工作于使用者空間,防火牆規則編寫工具,編寫規則并發送到netfilter
PREROUTING:主要做目标位址轉換
INPUT :從本機進來的
OUTPUT:從本機出去的
FORWARD:從本機轉發的
POSTROUTING:路由之後轉發,主要做源位址轉換
過濾:做封包篩選
NAT:Network Adress Translation 網絡位址轉換.
Dnat:目标位址轉換 公網響應,将公網位址自動Dnat到内網.
Snat:源位址轉換 内網位址通路公網資源,出去時将源位址轉換為公網位址.
表和鍊的對應關系:
filter:
INPUT FORWARD OUTPUT
nat:
PREROUTING OUTPUT POSTROUTING
mangle:
PREROUTING INPUT FORWARD OUTPUT POSTROUTING
檢查條件,處理機制:
通:預設為堵,隻對能識别的進行放行
堵:預設為通,隻對能識别的進行阻截
檢查條件:
IP:SIP DIP
TCP,UDP,ICMP協定首部
處理機制:
DROP:悄悄丢棄,不傳回任何結果
REJECT:拒絕,傳回拒絕資訊
ACCEPT:允許通過
SNAT:源位址轉換
DNAT:目标位址轉換
RETURN:未比對到重新傳回
REDIRECT:映射
LOG:日志
Iptables規則參數:
1
<code> </code><code># iptables -nL 檢視規則</code>
<code> </code><code>iptables [-t table] -N chain </code><code>#新增一條自定義的規則鍊(自定義的鍊隻能在被調用或轉發時生效)</code>
-I 插入,預設為第1條
<code> </code><code>iptables [-t table] -I chain [rulenum] rule-specification</code>
iptables [-t table] -X [chain] #删除一條自定義的空鍊
執行個體:
<code> </code><code># iptables -X Mew</code>
iptables [-t table] -E old-chain-name new-chain-name #修改自定義鍊名
2
3
4
5
6
7
8
9
10
<code> </code><code>[root@localhost ~]</code><code># iptables -E Mew Redhat</code>
<code> </code><code>[root@localhost ~]</code><code># iptables -nL</code>
<code> </code><code>Chain INPUT (policy ACCEPT)</code>
<code> </code><code>target prot opt </code><code>source</code> <code>destination </code>
<code> </code><code>Chain FORWARD (policy ACCEPT)</code>
<code> </code><code>Chain OUTPUT (policy ACCEPT)</code>
<code> </code><code>Chain Redhat (0 references)</code>
<code> </code><code>target prot opt </code><code>source</code> <code>destination</code>
iptables [-t table] -P chain target #修改鍊的預設政策,無法修改自定義鍊的預設規則.
<code># iptables -t filter -P FORWARD DROP</code>
<code>iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]</code>
-F :清空鍊中的規則
規則有編号,在鍊中自上而下,從1開始;
-L: list,列出表中的所有規則
-n:數字格式顯示IP和Port
-v:以詳細格式顯示
執行個體:
<code> </code><code>[root@localhost ~]</code><code># iptables -nL -v</code>
<code> </code><code>Chain INPUT (policy ACCEPT 10936 packets, 1068K bytes)</code>
<code> </code><code>pkts bytes target prot opt </code><code>in</code> <code>out </code><code>source</code> <code>destination </code>
<code> </code><code>Chain FORWARD (policy DROP 0 packets, 0 bytes)</code>
<code> </code><code>pkts bytes target prot opt </code><code>in</code> <code>out </code><code>source</code> <code>destination </code>
<code> </code><code>Chain OUTPUT (policy ACCEPT 8217 packets, 698K bytes)</code>
<code> </code><code>Chain Redhat (0 references)</code>
<code> </code><code>pkts bytes target prot opt </code><code>in</code> <code>out </code><code>source</code> <code>destination</code>
pkts:packets,被本規則所比對到的封包個數
bytes:被本規則所比對到的所有封包的大小之和,會執行機關換算.
target:目标,即處理機制
prot:協定,一般為{tcp|udp|ICMP}
opt:可選項
in:資料包流入接口.
out:資料包的流出接口
source:源位址
destination:目标位址:
-v
-vv
-x:exactly,精确值,不執行機關換算
--line-number:顯示各規則的行号
執行個體: --line-number 看到每條規則都有編号
11
12
<code> </code><code>[root@localhost ~]</code><code># iptables -nL --line-numbers</code>
<code> </code><code>Chain INPUT (policy ACCEPT)</code>
<code> </code><code>num target prot opt </code><code>source</code> <code>destination </code>
<code> </code><code>1 ACCEPT all -- 172.16.0.0</code><code>/16</code> <code>172.16.0.192 </code>
<code> </code><code>2 ACCEPT tcp -- 172.16.0.0</code><code>/16</code> <code>172.16.0.192 </code>
<code> </code><code>Chain FORWARD (policy DROP)</code>
<code> </code><code>Chain OUTPUT (policy ACCEPT)</code>
<code> </code><code>1 ACCEPT tcp -- 172.16.0.192 172.16.0.24 </code>
<code> </code><code>Chain Redhat (0 references)</code>
<code> </code><code>num target prot opt </code><code>source</code> <code>destination</code>
-Z:zero,規則計數器清零
iptables [-t table] {-A|-D} chain rule-specification
-A append,附加一條規則;rule-specification 條件比對 -j 處理機制,預設在指定鍊的最後一行添加
比對條件(通用比對)
-s:源ip,可以時ip,也可以是網絡位址,可以取反:!172.10.10.0/24,"!"取反
-s 相當于--src或者--source
-d:比對目标位址
-p:比對協定,通常隻使用{TCP|UDP|ICMP}三者之一
-i:資料封包流入的接口:通常隻用于INPUT/FORWARD/PREROUTING
-o:流出的接口:通常隻用于OUTPUT/FORWARD/POSTROUTING
執行個體:
<code># iptables -A INPUT -p tcp -s 172.16.0.0/16 -d 172.16.0.192 -j ACCEPT</code>
所有來自172.16.0.0這個網段的tcp協定通路本地172.16.0.192資源的都允許進來
<code> </code><code># iptables -A OUTPUT -p tcp -s 172.16.0.192 -d 172.16.0.24 -j ACCEPT</code>
所有從本機 172.16.0.192出去的協定為tcp的目标為172.16.0.24的店鋪允許出去.
-s:源位址不寫,預設為4個零
執行個體:
<code> </code><code>[root@localhost ~]</code><code># iptables -A INPUT -d 172.16.0.192 -j ACCEPT</code>
<code> </code><code>[root@localhost ~]</code><code># iptables -nL -v</code>
<code> </code><code>Chain INPUT (policy ACCEPT 163 packets, 15385 bytes)</code>
<code> </code><code>pkts bytes target prot opt </code><code>in</code> <code>out </code><code>source</code> <code>destination </code>
<code> </code><code>23 1752 ACCEPT all -- * * 0.0.0.0</code><code>/0</code> <code>172.16.0.192</code>
-i :指定流入網卡,不能在output上使用,否則會報錯.
<code> </code><code># iptables -A INPUT -i eth0 -d 172.16.0.192 -j ACCEPT</code>
隻要從eth0進來通路本機的就允許
從本機eth0出去通路公網的就允許.
<code> </code><code># iptables -A OUTPUT -o eth0 -s 172.16.0.192 -j ACCEPT</code>
<code>Chain OUTPUT (policy ACCEPT 66 packets, 5489 bytes)</code>
<code>pkts bytes target prot opt </code><code>in</code> <code>out </code><code>source</code> <code>destination </code>
<code> </code><code>4 592 ACCEPT all -- * eth0 172.16.0.192 0.0.0.0</code><code>/0</code>
儲存恢複規則:
service iptables save
規則會被儲存至/etc/sysconfig/iptables檔案中;
将火牆規則儲存到其他檔案中:iptables-save
# iptables-save > iptables.20170707
從某個檔案中恢複規則,iptables-restore
# iptables-restore < iptables.20170707
隐含擴充:使用-p{TCP|UDP|ICMP}指定某特定協定後,協定自身所能夠進行的自動擴充
-p:tcp
--dport :可以指定單個端口,可以使連續的多個端口.
--sport: 可以指定源端口---
--tcp-flags:标志位
RST,SYN,ACK,FIN
執行個體:
#允許172.16.0.0/16通過tcp協定的22端口通路本地資源
<code> </code><code>[root@localhost ~]</code><code># iptables -A INPUT -i eth0 -s 172.16.0.0/16 -d 172.16.0.192 -p tcp --dport 22 -j ACCEPT</code>
#允許本地通過tcp協定的22端口與172.16.0.0/16通訊.
<code>[root@localhost ~]</code><code># iptables -A OUTPUT -s 172.16.0.192 -d 172.16.0.0/16 -p tcp --sport 22 -j ACCEPT</code>
#允許本地lo端口進出.
<code># iptables -A INPUT -i lo -j ACCEPT</code>
<code># iptables -A OUTPUT -o lo -j ACCEPT</code>
-p: udp
--sport:
--dport:
udp協定沒有标志位。
-p:icmp
--icmp-type 8是ping請求 0是ping響應
例子:放行ping其他主機:
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p icmp --icmp-type 8 -j ACCEPT</code>
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p icmp --icmp-type 0 -j ACCEPT</code>
規則指令:
iptables -D chain rulenum [options] 删除規則
執行個體:删除OUTPUT鍊上的第1條規則
<code> </code><code># iptables -D OUTPUT 1</code>
iptables [-t table] -I chain [rulenum] rule-specification 在指定的地方插入規則
如果不指定預設為第一行.
執行個體:在第三行
<code># iptables -I INPUT 3 -i lo -j ACCEPT</code>
<code> </code><code># iptables -I INPUT 3 -i eth0 -d 172.16.0.192 -j ACCEPT</code>
iptables [-t table] -R chain rulenum rule-specification 替換指定規則
明确指定将第幾行替換為.
執行個體:
<code> </code><code># iptables -R INPUT 4 -i lo -p tcp -j ACCEPT</code>
顯示指定鍊的規則:-S 後面未指定具體的鍊,則顯示所有鍊的規則.
iptables [-t table] -S [chain [rulenum]]
<code> </code><code># iptables -S INPUT</code>
<code> </code><code>-P INPUT ACCEPT</code>
<code> </code><code>-A INPUT -s 172.16.0.0</code><code>/16</code> <code>-d 172.16.0.192</code><code>/32</code> <code>-i eth0 -p tcp -m tcp --dport 22 -j ACCEPT</code>
<code> </code><code>-A INPUT -i lo -j ACCEPT</code>
<code> </code><code>-A INPUT -d 172.16.0.192</code><code>/32</code> <code>-i eth0 -j ACCEPT</code>
<code> </code><code>-A INPUT -i lo -p tcp -j ACCEPT</code>
顯示擴充:必須明确指定擴充子產品
-m 擴充子產品名稱 --專用選項
multiport:多端口比對,一次指定多個(15個以内)零散端口
[!] --source-port,--sport port[:port]
[!] --destination-port,--dport port[:port]
--ports:
執行個體:
iptables -A INPUT -d 172.16.0.192 -p tcp -m multiport --dport 22,80 -j ACCEPT
# iptables -A OUTPUT -s 172.16.0.192 -p tcp -m multiport --sport 22,80 -j ACCEPT
iprange:ip位址範圍
time:指定時間範圍
--datestart:
--datestop:
--timestart:
--timestop:
執行個體:
# iptables -A INPUT -d 172.16.100.7 -p tcp --dport 901 -m time --weekdays Mon,Tus,Wed,Thu,Fri --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT
string:字元串
--algo {bm|kmp} :字元比對時使用到的算法
--string "string":要查找的字元串
--from offset
從本地80端口出去包含字元串"hello"的全丢掉.
<code># iptables -A OUTPUT -s 172.16.0.7 -p tcp --sport 80 -m string --algo kmp --string "hello" -j DROP</code>
<code># iptables -A INPUT -d 192.168.149.128 -p tcp -m multiport --dports 22,80 -j ACCEPT </code>
[!] --src-range from[-to]
[!] --dst-range from[-to]
ip-range:指定ip段通路:
隻允許這個網段通路本地的23端口telnet服務.
<code> </code><code># iptables -A INPUT -d 192.168.149.128 -p tcp --dport 23 -m iprange --src-range 172.16.0.9-172.16.0.110 -j ACCEPT</code>
<code># iptables -A OUTPUT -s 192.168.149.128 -p tcp --sport 23 -m iprange --dst-range 172.16.0.9-172.16.0.110 -j ACCEPT</code>
time:指定時間daunt
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--timestart hh:mm[:ss]
--timestop hh:mm[:ss]
[!] --weekdays day[,day...]
<code># iptables -A OUTPUT -s 192.168.149.128 -p tcp --sport 91 -m time --weekdays 1,7 --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT</code>
<code># iptables -A INPUT -d 192.168.149.128 -p tcp --dport 91 -m time --weekdays 1,7 --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT</code>
string:字元串通路控制
--algo {bm|kmp}:字元比對查找時使用的算法
--string:查找的字元串
[!] --hex-string pattern:要查找的字元串,字元編碼轉換
--from :
--to
<code># iptables -I OUTPUT -s 192.168.149.128 -p tcp --sport 80 -m string --algo kmp --string "hello" -j DROP</code>
connlimit :每個ip對指定服務的最大連接配接數
[!] --connlimit-above n
請求本地80端口,并發大于5的請求全部丢掉.
# iptables -I INPUT -d 192.168.149.128 -p tcp --dport 80 -m connlimit --connlimit-above 5 -j DROP
limit:封包的速率控制
--limit-burst number :峰值
--limit rate[/second|/minute|/hour|/day]每秒的速率
<code># iptables -A INPUT -d 192.168.149.128 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -j ACCEPT</code>
hping3 指令
ab -c 20 -n 2000 http://www.baidu.com/index.html 并發測試指令
網絡工具排名:
sectools.org
state:狀态比對
ip_conntrack,nf_conntrack Iptables根據這2個子產品完成連接配接追蹤
--state:
NEW 狀态;第一次連接配接狀态叫NEW
ESTABLISHED 連接配接狀态,後續的狀态都是ESTABLISHED
RELATED 相關聯的,指令請求和資料傳輸之間就是RELATED
INVALID 無效連接配接或者無法識别的連接配接
法則:
1、對于進入的狀态為ESTABLISHED的都應該放行
2、對于出去的狀态為ESTABLISHED的都應該放行
3、嚴格檢查進入的狀态為NEW的連接配接
4、所有狀态為INVIALIED的都應該拒絕
比對state 狀态為NEW,ESTABLISHED的放行.
調整連接配接追蹤功能所能容納的最大追蹤連接配接數:
/proc/sys/net/nf_conntrack_max 定義了onntrack 連接配接追蹤的最大值
/proc/net/nf_conntrack 定義了目前追蹤的所有連接配接
/proc/sys/net/netfilter/ 目錄定義個了逾時時間
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -m state --state NEW,ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p icmp -m state --state NEW,ESTABLISHED -j ACCEPT</code>
放行本地80端口,進來時state為NEW,ESTABLISHED,出去時狀态為 ESTABLISHED
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT</code>
-j target
<code> </code><code>RETURN:傳回調用連</code>
<code> </code><code># iptables -N clean_in</code>
<code> </code><code># iptables -t filter -I clean_in -d 172.16.0.255 -p tcp --tcp-flags ALL ALL -jDROP</code>
<code> </code><code># iptables -t filter -I clean_in -d 172.16.0.255 -p tcp --tcp-flags ALL NONE -jDROP</code>
<code> </code><code># iptables -A clean_in -d 172.16.255.255 -p ucmp -j DROP</code>
<code> </code><code># iptables -A clean_in -d 255.255.255.255 -p ucmp -j DROP</code>
<code> </code><code># iptables -A clean_in -d 172.16.0.177 -j RETURN</code>
<code> </code><code># iptables -I INPUT -d 172.16.0.177 -j RETURN</code>
<code> </code><code># iptables -I clean_in 4 -d 172.16.0.177 -m state --state INVALID -j DROP 無法識别的都應該要删除.</code>
被動伺服器放行例如ftp,伺服器被動模式下相應端口随機相應.
1、確定iptables加載ftp協定支援的子產品:ip_nat_ftp,ip_conntrack_ftp
編輯/rtc/sysconfig/iptables-config檔案,定義如下參數:
IPTABLES_MODULES="ip_nat_ftp,ip_conntrack_ftp"
手動安裝使用:modprobe ip_nat_ftp
2、方形請求封包的RELATED和ESTABLISHED
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p tcp --dport 22 -j ACCEPT</code>
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -p tcp --dport 21 -j ACCEPT</code>
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 22 -j ACCEPT</code>
<code> </code><code># iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 21 -j ACCEPT</code>
<code> </code><code># iptables -P INPUT DROP</code>
<code> </code><code># iptables -P OUTPUT DROP</code>
<code> </code><code># iptables -A INPUT -d 172.16.0.177 -m state --state RELATED,ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -A INPUT -s 172.16.0.177 -m state --state ESTABLISHED -j ACCEPT</code>
<code> </code><code># iptables -I INPUT 2 -d 172.16.0.177 -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT</code>
<code> </code><code># iptables -I OUTPUT 1 -s 172.16.0.177 -p tcp -m state --state ESTABLISHED -j ACCEPT</code>
本文轉自青衫解衣 51CTO部落格,原文連結:http://blog.51cto.com/215687833/1946477