iptables:
Firewall:隔離工具;Packets Filter Firewall;工作于主機或網絡的邊緣,對經由的報文根據預先定義的規則(匹配條件)進行檢測,對于能夠被規則匹配到的報文實行某預定義的處理機制的一套組件;
硬件防火墻:在硬件級別實現部分功能的防火墻;另一個部分功能基于軟件實現;
軟件防火墻:應用軟件處理邏輯運行于通用硬件平臺之上的防火墻;
主機防火墻:服務范圍為當前主機;
網絡防火墻:服務范圍為防火墻被的局域網;
iptables/netfilter:
netfilter:防火墻框架,framework;位于內核空間;
iptables:命令行工具程序,位于用戶空間;規則管理工具;
netfilter:
hooks function
prerouting
input
forward
output
postrouting
iptables:
CHAINS:
PREROUTING
INPUT
FORWARD
OUTPUT
POSTROUTING
報文流向:
到本機某進程的報文:PREROUTING –> INPUT
由本機轉發的報文:PREROUTING –> FORWARD –> POSTROUTING
由本機的某進程發出報文:OUTPUT –> POSTROUTING
tables:
filter:過濾,防火墻;
nat:network address translation,網絡地址轉換;
mangle:拆解報文,做出修改,并重新封裝;
raw:關閉nat表上啟用的連接追蹤機制;
優先級次序(由高而低):
raw –> mangle –> nat –> filter
功能<–>鉤子:
raw:PREROUTING,OUTPUT
mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat:PREROUTING,INPUT,OUTPUT,POSTROUTING
filter:INPUT,FORWARD,OUTPUT
iptables規則的組成部分:
匹配條件:
網絡層首部:Source IP, Destination IP
傳輸層首部:Source Port, Destination Port
擴展檢查機制:
處理動作:target
ACCEPT,DROP,REJECT
安裝:
netfilter:位于內核中的tcp/ip協議棧報文處理框架;
iptables:
CentOS 5/6:iptables命令編寫規則;
# iptables -t filter -F
# service iptables save
CentOS 7:firewalld,firewall-cmd, firewall-config
# systemctl disable firewalld
程序包:iptables, iptstate
[root@centos7 ~]# yum list all ipt* 查看iptables的包
[root@centos7 ~]# yum info iptstate
iptables命令:
規則:根據指定的匹配條件來嘗試匹配每個流經此處的報文,一旦匹配成功,則由規則后面指定的處理動作進行處理;
匹配條件:
基本匹配條件:源地址,目標地址,傳輸層協議
擴展匹配條件:需要借助于擴展模塊進行指定的匹配條件
隱式擴展:已經在基本匹配條件中指明的協議相關的擴展;
顯式擴展:隱式擴展之外的其它擴展匹配條件;
處理動作:
基本動作:ACCEPT,DROP,…
擴展動作:需要借助于擴展模塊進行,但無須顯式指定,僅需指明動作;
添加規則時需要考量的問題:
(1) 報文流經的位置:用于判斷將規則添加至哪個鏈;
(2) 實現的功能:用于判斷將規則添加至哪個表;
(3) 報文的方向:用于判斷哪個為“源”,哪個為“目標”;
(4) 匹配條件:用于編寫能夠正確匹配目標報文的規則;
iptabls命令的使用格式:
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]
規則管理格式:
iptables [-t able] COMMAND chain cretieria [-m -m matchname [per-match-options]] [-j targetname [per-target-options]]
-t table:指明要管理的表; 默認為filter;
COMMANDS:
鏈管理:
-P:iptables [-t table] -P chain target,定義鏈的默認策略;其target一般可使用ACCEPT或DROP;
-N:iptables [-t table] -N chain,
自定義規則鏈;僅在默認鏈通過某規則進行調用方可生效;因此,每個自定義鏈都有其引用記數;
-X:iptables [-t table] -X [chain],刪除自定義的空的引用計數為0的鏈;
-F:iptables [-t table] -F [chain [rulenum]] [options…],清空指定的鏈,或刪除指定鏈上的規則 ;
-E:iptables [-t table] -E old-chain-name new-chain-name,重命名自定義的引用計數為0的鏈;
-Z:iptables [-t table] -Z [chain [rulenum]] [options…]
規則:
-A:append, iptables [-t table] -A chain rule-specification,追加規則到指定的鏈尾部;
-I:insert, iptables [-t table] -I chain [rulenum] rule-specification,
插入規則到指定的鏈中的指定位置,默認為鏈首;
-D:delete,iptables [-t table] -D chain rule-specification
iptables [-t table] -D chain rulenum,刪除指定的鏈上的指定規則;
-R:replace,iptables [-t table] -R chain rulenum rule-specification,將指定的鏈上的指定規則替換為新的規則;
查看:
-L:list, iptables [-t table] -L [chain [rulenum]] [options…]
-n:數字格式;
-v:verbose,詳細格式信息;
-vv, -vvv
–line-numbers:顯示鏈上的規則的編號;
-x:exactly,顯示計數器的精確值;
計數器:
每條規則以及鏈的默認策略分別有各自的兩個計數器:
(1) 匹配到的報文的個數:pkts
(2) 匹配到的所有報文的大小之積:bytes
iptables [-t able] COMMAND chain cretieria [-m -m matchname [per-match-options]] [-j targetname [per-target-options]]
匹配條件:
基本匹配條件
擴展匹配條件
隱式擴展
顯式擴展
注意:多重條件之間的隱含邏輯為“與”操作;
基本匹配條件:
[!] -s, –source address[/mask][,…]:檢查報文中的源IP地址是否符合此處指定的地址或地址范圍;
[!] -d, –destination address[/mask][,…]:檢查報文中的目標IP地址是否符合此處指定的地址或地址范圍;
[!] -p, –protocol protocol:檢查報文中傳輸層的協議類型,支持tcp,udp,udplite,icmp,icmpv6,esp,ah,sctp,mh,或者 "all";
[!] -i, –in-interface name:檢查報文進入本機時的接口是否符合本處指定的接口;INPUT, FORWARD and PREROUTING ;
[!] -o, –out-interface name:檢查報文即將離開本機時經由的接口是否符合本處指定的接口;FORWARD, OUTPUT and POSTROUTING;
-m, –match match:顯式指明要使用的擴展模塊;
-j, –jump target:跳轉目標;(可以是用戶自定義的鏈,或者是ACCEPT,DROP,…)
擴展匹配條件:
隱式擴展:不用-m選項明確給出要使用的擴展機制的擴展;此處主要指使用-p {tcp|udp|icmp}給定協議后可直接對給定的協議所進行的擴展;
-p tcp:可直接使用tcp協議對應的擴展選項;
[!] –source-port,–sport port[:port]:匹配報文中的傳輸層的源端口;可給出多個連接的端口;
[!] –destination-port,–dport port[:port]:匹配報文中的傳輸層的目標端口;可給出多個連接的端口;
[!] –tcp-flags mask comp
SYN,ACK,FIN,RST,URG,PSH;
mask:要檢查的標志位列表,以逗號分隔,例如SYN,ACK,FIN,RST
comp:mask給定的眾標志位中,其值必須為1的標志位列表,余下的必須為0;
–tcp-flags SYN,ACK,FIN,RST SYN
[!] –syn:相當于–tcp-flags SYN,ACK,FIN,RST SYN
-p udp:可直接使用udp協議對應的擴展選項;
[!] –source-port,–sport port[:port]:匹配報文中的傳輸層的源端口;可給出多個連接的端口;
[!] –destination-port,–dport port[:port]:匹配報文中的傳輸層的目標端口;可給出多個連接的端口;
-p icmp:可直接使用icmp協議對應的擴展選項;
[!] –icmp-type {type[/code]|typename}
–icmp-type 0/0:匹配對ping請求的響應報文
–icmp-type 8/0:匹配ping請求報文
顯式擴展:必須使用-m選項給出matchname的擴展,而且有些擴展都還存在專用選項;
1、multiport
以離散或連續的方式定義的多端口匹配條件; Up to 15 ports can be specified.
[!] –source-ports,–sports port[,port|,port:port]…:指定多個源端口;
[!] –destination-ports,–dports port[,port|,port:port]…:指定多個目標端口;
[!] –ports port[,port|,port:port]…:匹配此處指定的源或目標端口;
2、iprange
以連續的ip地址范圍指明多地址匹配條件;
[!] –src-range from[-to]
[!] –dst-range from[-to]
3、string
對報文中的應用層數據做字符串匹配檢測;
[!] –string pattern
[!] –hex-string pattern
–algo {bm|kmp}:字符串匹配檢查算法;
–from offset
–to offset
4、time
根據報文到達的時間與指定的時間范圍進行匹配度檢測;
–datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–timestart hh:mm[:ss]
–timestop hh:mm[:ss]
[!] –monthdays day[,day…]
[!] –weekdays day[,day…] Mon,Tue,Wed,Thu,Fri,Sat,Sun
5、connlimit
根據每客戶端IP做并發連接數限制,即限制單IP可同時發起連接請求;
–connlimit-upto n:連接數小于等于閾值;
–connlimit-above n:連接數超出閾值;
~]# iptables -I INPUT -d 10.1.0.6 -p tcp –dport 22 -m connlimit –connlimit-above 2 -j REJECT
6、limit
基于收發報文的速率進行匹配;
–limit rate[/second|/minute|/hour|/day] 速率
–limit-burst number 默認值是5
個人理解limit-burst是個初始值,匹配次數過了這個初始值,之后的就由limit xxx/s來控制了
~]# iptables -A INPUT -d 10.1.0.6 -p icmp –icmp-type 8 -m limit –limit-burst 3 –limit 20/minute -j ACCEPT
7、state
狀態檢測:連接追蹤機制(conntrack)
NEW:新連接
ESTABLISHED:已建立的連接
RELATED:相關聯的連接
INVALID:無法識別的連接
UNTRACKED:未被追蹤連接;
相關的內核模塊:
nf_conntrack
nf_conntrack_ipv4
nf_conntrack_ftp
追蹤到的鏈接,存放到/proc/net/nf_conntrack文件中;
能追蹤的最大連接數量定義在:/proc/sys/net/nf_conntrack_max
建議調整至足夠大;
不同的協議的連接追蹤時長:
/proc/sys/net/netfilter/
[!] –state state
如何開放被動模式的ftp服務:
(1) 裝載追蹤ftp協議的模塊;
# modprobe nf_conntrack_ftp
(2) 放行入站命令連接
# iptables -A INPUT -d SERVER_IP -p tcp –dport 21 -m state –state NEW,ESTABLISHED -j ACCEPT
(3) 放行入站數據連接
# iptables -A INPUT -d SERVER_IP -p tcp -m state –state RELATED,ESTABLISHED -j ACCEPT
(4) 放行出站的ESTABLISHED連接
# iptabls -A OUTPUT -s SERVER_IP -m state –state ESTABLISHED -j ACCEPT
處理動作(跳轉目標):
-j tagetname [per-target-options]
簡單target:
ACCEPT,DROP
擴展target:
REJECT:
–reject-with type
icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreach‐able, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited,默認為icmp-port-unreachable;
LOG:
Turn on kernel logging of matching packets.
–log-level level
–log-prefix prefix:日志信息的前導信息;
保存和載入規則:
保存:iptables-save > /PATH/TO/SOME_RULE_FILE
重載:iptables-restore < /PATH/FROM/SOME_RULE_FILE
-n, –noflush:不清除原有規則
-t, –test:僅分析生成規則集,但不予提交;
注意:重載文件中的規則,會清除已有規則;
CentOS 6:
保存規則:service iptables save
保存規則于/etc/sysconfig/iptables,保存操作會清除文件中原有的內容;
重載規則:server iptables restart
默認重載/etc/sysconfig/iptables文件中的規則
腳本配置文件:/etc/sysconfig/iptables-config
用于指明要裝載的模塊;
CentOS 7開機自動生效規則:
(1) firewalld服務;
(2) shell腳本,直接記錄iptables命令;
(3) 自定義unit file或init script;
規則優化的思路:
(1) 優先放行雙方向狀態為ESTABLISHED的報文;
(2) 服務于不同類別的功能的規則,匹配到報文可能性更大的放前面;
(3) 服務于同一類別的功能的規則,匹配條件較為嚴格的放前面;
(4) 設置默認策略:白名單機制
(a) 可使用iptables -P設定默認策略;
(b) 建議在規則鏈的最后定義規則做為默認策略;
iptables/netfilter網絡防火墻:
添加規則于FORWARD鏈,注意幾個問題:
(1) 請求和響應報文均會經由FORWARD鏈,要注意規則的方向性;
第一條:iptables -I FORWARD -m state –state ESTABLISHED,RELATED -j ACCEPT
(2) 如果可以啟用conntrack機制,注意網關主機所能夠追蹤的連接數的最大數量要符合需要
NAT:
源地址轉換:SNAT,POSTROUTING
靜態轉換:
動態轉換:
目標地址轉換:DNAT,PREROUTING
PAT:Port Address Translation
SNAT:
This target is only valid in the nat table, in the POSTROUTING and INPUT chains, and user-defined chains which are only called from those chains.
–to-source [ipaddr[-ipaddr]]
MASQUERADE:
This target is only valid in the nat table, in the POSTROUTING chain. It should only be used with dynamically assigned IP (dialup) connections: if you have a static IP address, you should use the SNAT target.
DNAT:
This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
–to-destination [ipaddr[-ipaddr]][:port[-port]]
REDIRECT:
This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
–to-ports port[-port]
RETURN:返回
原創文章,作者:songzizhe,如若轉載,請注明出處:http://www.www58058.com/55868