iptables/netfilter:
Packets Filter Firewall:
包過濾型防火墻:
Firewall:隔離工具,工作于主機或網絡的邊緣處,對經由的報文根據預先定義的規則
(識別標準)進行檢測,對于能夠被規則匹配到的報文實行某種預定義的處理機制的一套組件
硬件防火墻:在硬件級別實現部分功能的;
軟件防火墻:應用軟件邏輯在通用硬件基礎上實現;
主機防火墻:
網絡防火墻:
iptables/netfilter:
iptables:規則管理工具;
netfilter:防火墻框架,承載并生效規則;
hook functions(netfilter):
prerouting
input
forward
output
postrouting
iptables:
PREROUTING
INPUT
FORWARD
OUTPUT
POSTROUTING
允許用戶自定義規則鏈;它們需要手動關聯至指定的”鉤子“;
netfilter:功能
filter(“防火”):包過濾
NAT:Network Address Translation 轉發
mangle:拆解報文,做出修改,而后重新封裝
raw:關閉nat表上啟用的連接追蹤機制
功能(表)<–>鉤子
filter:input, forward, output
nat:prerouting, input, output, postrouting
mangle:prerouting, input, forward, output, postrouting
raw:prerouting, output
優先級(由高而低):raw –> mangle –> nat –> filter
iptables規則的組成部分:
匹配條件:
網絡層首部:SourceIP, DestinationIP, …
傳輸層首部:SourtPort, DestinationPort, TCP Flags
(SYN,ACK,FIN,URG,RST,PSH), …
擴展模塊引入的輔助檢查機制:
跳轉目標:-j target
內建的處理機制:ACCEPT, DROP, REJECT, SNAT, DNAT, MASQUERADE,
MARK, LOG, …
用戶自定義鏈:
添加規則時需要考量的因素:
(1) 實現的功能:用于判定將規則添加至哪個表;
(2) 報文的流經位置:用于判斷將規則添加至哪個鏈;
(3) 報文的流向:判定規則中何為”源“,何為”目標“;
(4) 匹配條件:用于編寫正確的匹配規則;
(a) 專用于某種應用的同類規則,匹配范圍小的放前面;
(b) 專用于某些應用的不同類規則,匹配到的可能性較多的放前面
同一類別的規則可使用自定義鏈單獨存放;
(c) 用于通用目的的規則放前面;
filter表:過濾,”防火墻“意義的核心所在; INPUT,FORWARD,OUTPUT
安裝:
netfilter:位于內核中的tcp/ip協議棧報文處理框架;
iptables:
CentOS 5/6:iptables命令生成規則,可保存于文件中以反復裝載生效;
# iptables -t filter -F
# service iptables save
man iptables
CentOS 7:firewalld, firewall-cmd, firewall-config
# systemctl disable firewalld.service
man iptables
man iptables-extensions #擴展模塊手冊
iptables命令:
iptables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options…]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
rule-specification = [matches…] [target]
match = -m matchname [per-match-options]
target = -j targetname [per-target-options]
COMMANDs:
鏈管理:
-N, –new-chain chain:新建一個自定義的規則鏈;
-X, –delete-chain [chain]:刪除用戶自定義的引用計數為0的空鏈;
-F, –flush [chain]:清空指定的規則鏈上的規則;
-E, –rename-chain old-chain new-chain:重命名鏈;
-Z, –zero [chain [rulenum]]:置零計數器;
注意:每個規則都有兩個計數器
packets:被本規則所匹配到的所有報文的個數;
bytes:被本規則所匹配到的所有報文的大小之和;
-P, –policy chain target
規則管理:
-A, –append chain
rule-specification:追加新規則于指定鏈的尾部;
-I, –insert chain [rulenum]
rule-specification:插入新規則于指定鏈的指定位置,默認為首部
-R, –replace chain rulenum
rule-specification:替換指定的規則為新的規則;
-D, –delete chain rulenum:根據規則編號刪除規則;
-D, –delete chain rule-specification:根據規則本身刪除規則
規則顯示:
-L, –list [chain]:列出規則;
-v, –verbose:詳細信息;
-n, –numeric:數字格式顯示主機地址和端口號;
-x, –exact:顯示計數器的精確值,而非圓整后的數據;
–line-numbers:列出規則時,顯示其在鏈上的相應的編號;
-S, –list-rules [chain]:顯示指定鏈的所有規則;
rule-specification = [matches…] [target]
matches:匹配條件
target:跳轉目標
匹配條件:
通用匹配(PARAMETERS):
[!] -s, –source address[/mask][,…]:檢查報文的源IP地址是否符合此處指定的范圍,或是否等于此處給定的地址;
[!] -d, –destination address[/mask][,…]:檢查報文的目標IP
地址是否符合此處指定的范圍,或是否等于此處給定的地址;
[!] -p, –protocol protocol:匹配報文中的協議,可用值tcp,
udp, udplite, icmp, icmpv6,esp, ah, sctp, mh 或者 “all”
亦可以數字格式指明協議;
-m, –match
match:調用指定的擴展匹配模塊來擴展匹配條件檢查機制;
[!] -i, –in-interface
name:限定報文僅能夠從指定的接口流入;only for packets
entering the INPUT, FORWARD and PREROUTING chains.
[!] -o, –out-interface
name:限定報文僅能夠從指定的接口流出;for packets entering
the FORWARD, OUTPUT and POSTROUTING chains.
擴展匹配(MATCH EXTENSIONS)
-m tcp
–sport, –dport
跳轉目標:
-j targetname [per-target-options]
ACCEPT:接受
DROP:丟棄
REJECT:拒絕
練習:172.18.0.67
1、開放本機的所有tcp服務給所有主機;
# iptables -I INPUT -d 172.18.0.67 -p tcp -j ACCEPT
# iptables -I OUTPUT -s 172.18.0.67 -p tcp -j ACCEPT
2、開放本機的所有udp服務給172.16.0.0/16網絡中的主機,但不包含172.16.0.200;
# iptables -I INPUT 2 -d 172.18.0.67 -s 172.18.0.200 -p udp -j REJECT
# iptables -I INPUT 3 -d 172.18.0.67 -s 172.18.0.0/16 -p udp -j ACCEPT
# iptables -I OUTPUT 2 -s 172.18.0.67 -d 172.18.0.0/16 -p udp -j ACCEPT
3、默認策略為REJECT;
擴展:
1、僅開放本機的ssh服務給172.16.0.0/16中的主機,而且不包含172.16.0.200
回顧:
iptables/netfilter:
netfilter:框架
iptables:管理規則的工具
四表:filter, nat, mangle, raw
五鏈:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
流經位置:
PREROUTING –> INPUT
PREROUTING –> FORWARD –> POSTROUTING
OUTPUT –> POSTROUTING
iptables命令:
iptables [-t table] COMMAND [chain] [PARAMETERS] [-m matchname
[per-match-options]] [-j targetname [per-target-options]]
PARAMETERS:
-s, -d, -p {tcp|udp|icmp|sctp|udplite|…}, -i, -o, -m
COMMAND:
鏈管理:-P, -X, -N, -E, -F, -Z
規則管理:-A,-I,-R,-D
查看:
-L,-n, -v, -x, –line-numbers
-S
-j targetname #處理動作
ACCEPT, DROP, REJECT, RETURN
匹配條件:
基本匹配條件:PARAMETERS
擴展匹配條件:
隱式擴展:在使用-p選項指明了特定的協議時,無需再同時使用-m選項指
明擴展模塊的擴展機制;
顯式擴展:必須使用-m選項指明要調用的擴展模塊的擴展機制;
隱式擴展:
-p tcp:可直接使用tcp擴展模塊的專用選項;
[!] –sport
匹配報文源端口;可以給出多個端口,但只能是連續的端口范圍
[!] –dport
匹配報文目標端口;可以給出多個端口,但只能是連續的端口范圍
[!] –tcp-flags mask comp
匹配報文中的tcp協議的標志位;Flags are: SYN ACK FIN
RST URG PSH ALL NONE;
mask:要檢查的FLAGS list,以逗號分隔;
comp:在mask給定的諸多的FLAGS中,其值必須為1的FLAGS列表,
余下的其值必須為0;
–tcp-flags SYN,ACK,FIN,RST SYN
–tcp-flags ALL ALL
–tcp-flags ALL NONE
[!] –syn: –tcp-flags SYN,ACK,FIN,RST SYN
-p udp:可直接使用udp協議擴展模塊的專用選項:
[!] –sport
[!] –dport
[!] –icmp-type {type[/code]|typename} #互聯網控制消息協議
0/0: echo reply #回顯應答 用0標識
8/0: echo request #回顯請求
iptables -A INPUT -d 172.18.12.11 -p icmp –icmp-type 0 -j ACCEPT
顯式擴展:必須使用-m選項指明要調用的擴展模塊的擴展機制;
1、multiport
以離散或連續的 方式定義多端口匹配條件,最多15個;
# iptables -I INPUT -d 172.16.0.7 -p tcp -m multiport
–dports 22,80,139,445,3306 -j ACCEPT
2、iprange
以連續地址塊的方式來指明多IP地址匹配條件;
[!] –src-range from[-to]
[!] –dst-range from[-to]
# iptables -I INPUT -d 172.16.0.7 -p tcp -m multiport
–dports 22,80,139,445,3306 -m iprange –src-range
172.16.0.61-172.16.0.70 -j REJECT
3、time
This matches if the packet arrival time/date is within
a given range.
–timestart hh:mm[:ss]
–timestop hh:mm[:ss]
[!] –weekdays day[,day…]
[!] –monthdays day[,day…]
–datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–kerneltz:使用內核配置的時區而非默認的UTC;
4、string
This modules matches a given string by using some pattern
matching strategy.
–algo {bm|kmp}
[!] –string pattern
[!] –hex-string pattern
–from offset
–to offset
~]# iptables -I OUTPUT -m string –algo bm –string “gay” -j
REJECT
5、connlimit #限制客戶端的連接數
Allows you to restrict the number of parallel
connections to a server per client IP address
(or client address block).
–connlimit-upto n #達到
–connlimit-above n #超過 兩者只能使用一個
~]# iptables -I INPUT -d 172.16.0.7 -p tcp –syn –dport 22
-m connlimit –connlimit-above 2 -j REJECT
6、limit
This module matches at a limited rate using a token
bucket filter.
–limit rate[/second|/minute|/hour|/day]
–limit-burst number
~]# iptables -I OUTPUT -s 172.16.0.7 -p icmp –icmp-type 0
-j ACCEPT
7、state
The “state” extension is a subset of the “conntrack” module. “state” allows access to the connection tracking state for this packet.
[!] –state state
INVALID, ESTABLISHED, NEW, RELATED or UNTRACKED.
NEW: 新連接請求;
ESTABLISHED:已建立的連接;
INVALID:無法識別的連接;
RELATED:相關聯的連接,當前連接是一個新請求,但附屬于某個已存在的連接;
UNTRACKED:未追蹤的連接;
state擴展:
內核模塊裝載:
nf_conntrack
nf_conntrack_ipv4
手動裝載:
nf_conntrack_ftp
追蹤到的連接:
/proc/net/nf_conntrack
調整可記錄的連接數量最大值:
/proc/sys/net/nf_conntrack_max
超時時長:
/proc/sys/net/netfilter/timeout
處理動作(跳轉目標):
-j targetname [per-target-options]
簡單target:
ACCEPT, DROP
擴展target:
REJECT
This is used to send back an error packet in response to the matched packet: otherwise it is equivalent to DROP so it is a terminating TARGET, ending rule traversal.
–reject-with type
The type given can be icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreach‐ able, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited (*), which return the appropriate ICMP error message (icmp-port-unreachable is the default).
保存和載入規則:
保存:iptables-save > /PATH/TO/SOME_RULE_FILE
重載:iptabls-restore < /PATH/FROM/SOME_RULE_FILE
-n, –noflush:不清除原有規則
-t, –test:僅分析生成規則集,但不提交
CentOS 6:
保存規則:
service iptables save
保存規則于/etc/sysconfig/iptables文件,覆蓋保存;
重載規則:
service iptables restart
默認重載/etc/sysconfig/iptables文件中的規則
配置文件:/etc/sysconfig/iptables-config
CentOS 7:
(1) 自定義Unit File,進行iptables-restore;
(2) firewalld服務;
(3) 自定義腳本;
規則優化的思路:
使用自定義鏈管理特定應用的相關規則,模塊化管理規則;
(1) 優先放行雙方向狀態為ESTABLISHED的報文
(2) 服務于不同類別的功能的規則,匹配到報文可能性更大的放前面;
(3) 服務于同一類別的功能的規則,匹配條件較嚴格的放在前面;
(4) 設置默認策略:白名單機制
(a) iptables -P,不建議;
(b) 建議在規則的最后定義規則做為默認策略;
原創文章,作者:shewei,如若轉載,請注明出處:http://www.www58058.com/74420