詳述iptables五鏈
防火墻程序是工作在內(nèi)核的TCP/IP的網(wǎng)絡(luò)協(xié)議棧的框架之上送浊,通過(guò)網(wǎng)絡(luò)過(guò)濾可以實(shí)現(xiàn)入侵檢測(cè)及入侵防御的功能竞慢。
凡是到達(dá)本機(jī)內(nèi)部來(lái)的數(shù)據(jù)包我們都將其按照某種規(guī)則進(jìn)行轉(zhuǎn)發(fā)岩调,凡是要從本機(jī)內(nèi)部出去的將其按照另外一種規(guī)則進(jìn)行轉(zhuǎn)發(fā)秃症,另外凡是經(jīng)過(guò)本機(jī)轉(zhuǎn)發(fā)的我們按照對(duì)應(yīng)的轉(zhuǎn)發(fā)規(guī)則轉(zhuǎn)發(fā)的機(jī)制就是防火墻框架骂倘。
防火墻的框架就是在內(nèi)核的TCP/IP協(xié)議棧中精心設(shè)置了幾個(gè)卡點(diǎn)掺涛,對(duì)于數(shù)據(jù)包必流經(jīng)的位置設(shè)置了幾個(gè)鉤子函數(shù):這些卡點(diǎn)的位置如進(jìn)口或出口處,如有一個(gè)人要進(jìn)入我們的領(lǐng)域匿情,我們?cè)诖箝T(mén)口放一臺(tái)鉤機(jī)兰迫,進(jìn)來(lái)之前先用鉤機(jī)將其鉤起,然后一條一條的進(jìn)行規(guī)制匹配炬称,匹配通過(guò)了就放進(jìn)來(lái)汁果,匹配不通過(guò)的就直接扔掉。
Linux系統(tǒng)上的防火墻是由iptables/netfilter組成玲躯,其中iptables是規(guī)則的制定工具据德,netfilter在內(nèi)核協(xié)議框架中定義了5個(gè)卡點(diǎn)位置,并在這5個(gè)位置通過(guò)鉤子函數(shù)對(duì)進(jìn)出的數(shù)據(jù)包進(jìn)行過(guò)濾跷车,從而達(dá)到防火墻的功能棘利。iptables工具工作在用戶(hù)控件,他可以制定一些規(guī)則然后送到內(nèi)核空間朽缴,然后結(jié)合netfilter的鉤子函數(shù)及處理方法對(duì)數(shù)據(jù)包進(jìn)行放行或者拒絕處理善玫。
-
5個(gè)鉤子(hook function)的位置如下圖:
-
hook function :
- prerouting: 流入的數(shù)據(jù)包進(jìn)入路由表之前
- input:通過(guò) 路由表判斷后目的是本機(jī),然后進(jìn)入本機(jī)內(nèi)部資源
- forward:通過(guò)路由表判斷后目的地不是本機(jī)密强,然后通過(guò)路由轉(zhuǎn)發(fā)到其他地方
- output:由本機(jī)產(chǎn)生的數(shù)據(jù)向外部轉(zhuǎn)發(fā)
- postrouting:傳出的數(shù)據(jù)包到達(dá)網(wǎng)卡出口之前
- 在每個(gè)鉤子上對(duì)應(yīng)的有五個(gè)鏈蝌焚,用于添加匹配規(guī)則裹唆,名稱(chēng)和鉤子相同,但是都必須用大寫(xiě)來(lái)標(biāo)識(shí):
- PREROUTING
- INPUT
- FORWARD
- OUTPUT
- POSTROUTING
- 防火墻里面有多個(gè)table只洒,每個(gè)表格里面都定義許多規(guī)則许帐,并且每個(gè)表的用途不同,iptables按用途個(gè)功能將其分為四個(gè)表毕谴,這四個(gè)表又由五個(gè)鏈組成成畦,這五個(gè)鏈對(duì)應(yīng)五個(gè)鉤子函數(shù)。
- 這四個(gè)表分別是filter涝开、nat循帐、mangle、raw舀武,默認(rèn)為filter拄养;表的處理優(yōu)先級(jí)一次是:raw、mangle银舱、nat瘪匿、filter;每個(gè)表的功能:
- filter:一般用于過(guò)濾功能寻馏,防火墻
- nat:用于修改源IP或目標(biāo)IP棋弥,也可以修改端口
- mangle:拆解報(bào)文,做出修改并重新封裝起來(lái)诚欠,對(duì)特定數(shù)據(jù)包修改(使用情況很少)
- raw:關(guān)閉natbiao上啟用的連接追蹤機(jī)制
- 四個(gè)表中每個(gè)表多對(duì)應(yīng)的鏈分別為:
- FILTER: INPUT顽染、OUTPUT、FORWARD
- NAT: PREROUTING轰绵、 POSTROUTING 粉寞、 FORWARD
- MANGLE: INPUT、 OUTPUT左腔、 FORWARD唧垦、 PREROUTING、 POSTROUTING
- RAW: PREROUTING翔悠、 OUTPUT
- 報(bào)文流向
- 流入本機(jī):PREROUTING --> INPUT
- 由本機(jī)流出:OUTPUT --> POSTROUTING
- 轉(zhuǎn)發(fā):PREROUTING --> FORWARD --> POSTROUTING
iptables /netfilter規(guī)則:
- 組成部分:根據(jù)規(guī)則的匹配條件來(lái)嘗試匹配報(bào)文,一旦匹配成功野芒,就由規(guī)則定義的處理動(dòng)作做出處理蓄愁;
- 匹配條件:
- 基本匹配條件:內(nèi)建
- 擴(kuò)展匹配條件:由擴(kuò)展模塊定義;
- 處理動(dòng)作:
- 基本處理動(dòng)作:內(nèi)建
- 擴(kuò)展處理動(dòng)作:由擴(kuò)展模塊定義狞悲;
- 自定義處理機(jī)制:自定義鏈
- 匹配條件:
- iptables的鏈:內(nèi)置鏈和自定義鏈
- 內(nèi)置鏈:對(duì)應(yīng)于hook function
- 自定義鏈接:用于內(nèi)置鏈的擴(kuò)展和補(bǔ)充撮抓,可實(shí)現(xiàn)更靈活的規(guī)則管理機(jī)制;
- 添加規(guī)則時(shí)的考量點(diǎn):
<1> 要實(shí)現(xiàn)那種功能:判斷添加在哪個(gè)表上摇锋;
<2> 報(bào)文流經(jīng)的路徑:判斷添加在哪個(gè)鏈上丹拯; - 鏈上的規(guī)則在匹配的過(guò)程中是有次序的站超,即為檢查次序,因此在制定時(shí)要遵循以下法則:
<1> 同類(lèi)規(guī)則(訪(fǎng)問(wèn)同一應(yīng)用)乖酬,匹配范圍小的放上面死相;
<2> 不同類(lèi)的規(guī)則(訪(fǎng)問(wèn)不同應(yīng)用),匹配到報(bào)文頻率較大的放在上面咬像;
<3> 將那些可由一條規(guī)則描述的多個(gè)規(guī)則合并起來(lái)算撮;
<4> 設(shè)置默認(rèn)策略;
iptables命令:
高度模塊化县昂,由諸多擴(kuò)展模塊實(shí)現(xiàn)其檢查條件或處理動(dòng)作的定義肮柜;其擴(kuò)展模塊路徑:/usr/lib64/xtables; IPv4的模塊為libipt_,libxt_;
IPv6的模塊為libip6t_;
規(guī)則格式:
iptables [-t table] COMMAND chain [-m matchname [per-match-options]] -j targetname [per-target-options]
- -t table:
> raw, mangle, nat, [filter]默認(rèn)
-
COMMAND:
- 鏈管理:
- -N:new, 自定義一條新的規(guī)則鏈倒彰;
- -X: delete审洞,刪除自定義的規(guī)則鏈;
注意:僅能刪除 用戶(hù)自定義的 引用計(jì)數(shù)為0的 空的 鏈待讳; - -P:Policy芒澜,設(shè)置默認(rèn)策略;對(duì)filter表中的鏈而言耙箍,其默認(rèn)策略有:
ACCEPT:接受
DROP:丟棄
REJECT:拒絕 - -E:重命名自定義鏈撰糠;引用計(jì)數(shù)不為0的自定義鏈不能夠被重命名,也不能被刪除辩昆;
- 規(guī)則管理:
- -A:append阅酪,追加;
- -I:insert, 插入汁针,要指明位置术辐,省略時(shí)表示第一條;
- -D:delete施无,刪除辉词;
(1) 指明規(guī)則序號(hào);
(2) 指明規(guī)則本身猾骡; - -R:replace瑞躺,替換指定鏈上的指定規(guī)則;
- -F:flush兴想,清空指定的規(guī)則鏈幢哨;
- -Z:zero,置零;
iptables的每條規(guī)則都有兩個(gè)計(jì)數(shù)器:
(1) 匹配到的報(bào)文的個(gè)數(shù);
(2) 匹配到的所有報(bào)文的大小之和怖竭;
- 查看:
- -L:list, 列出指定鏈上的所有規(guī)則可岂;
- -n:numberic岸售,以數(shù)字格式顯示地址和端口號(hào)践樱;
- -v:verbose,詳細(xì)信息凸丸;
-vv, -vvv - -x:exactly拷邢,顯示計(jì)數(shù)器結(jié)果的精確值;
- --line-numbers:顯示規(guī)則的序號(hào)甲雅;
- 鏈管理:
-
chain:
- PREROUTING解孙,INPUT,F(xiàn)ORWARD抛人,OUTPUT弛姜,POSTROUTING
-
匹配條件:
- 基本匹配條件:無(wú)需加載任何模塊,由iptables/netfilter自行提供妖枚;
- [!] -s, --source address[/mask][,...]:檢查報(bào)文中的源IP地址是否符合此處指定的地址或范圍廷臼;
- [!] -d, --destination address[/mask][,...]:檢查報(bào)文中的目標(biāo)IP地址是否符合此處指定的地址或范圍;
所有地址:0.0.0.0/0 - [!] -p, --protocol protocol
protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or "all"
{tcp|udp|icmp} - [!] -i, --in-interface name:數(shù)據(jù)報(bào)文流入的接口绝页;只能應(yīng)用于數(shù)據(jù)報(bào)文流入的環(huán)節(jié)荠商,只能應(yīng)用于PREROUTING,INPUT和FORWARD鏈续誉;
- [!] -o, --out-interface name:數(shù)據(jù)報(bào)文流出的接口莱没;只能應(yīng)用于數(shù)據(jù)報(bào)文流出的環(huán)節(jié),只能應(yīng)用于FORWARD酷鸦、OUTPUT和POSTROUTING鏈饰躲;
- 基本匹配條件:無(wú)需加載任何模塊,由iptables/netfilter自行提供妖枚;
- 擴(kuò)展匹配條件:
- 隱式擴(kuò)展:在使用-p選項(xiàng)指明了特定的協(xié)議時(shí),無(wú)需再同時(shí)使用-m選項(xiàng)指明擴(kuò)展模塊的擴(kuò)展機(jī)制臼隔;
- 隱式擴(kuò)展:不需要手動(dòng)加載擴(kuò)展模塊嘹裂;因?yàn)樗鼈兪菍?duì)協(xié)議的擴(kuò)展,所以摔握,但凡使用-p指明了協(xié)議寄狼,就表示已經(jīng)指明了要擴(kuò)展的模塊;
- tcp:
[!] --source-port, --sport port[:port]:匹配報(bào)文的源端口氨淌;可以是端口范圍泊愧;
[!] --destination-port,--dport port[:port]:匹配報(bào)文的目標(biāo)端口;可以是端口范圍盛正;
[!] --tcp-flags mask comp
mask is the flags which we should examine, written as a comma-separated list删咱,例如 SYN,ACK,FIN,RST
comp is a comma-separated list of flags which must be set,例如SYN
例如:“--tcp-flags SYN,ACK,FIN,RST SYN”表示蛮艰,要檢查的標(biāo)志位為SYN,ACK,FIN,RST四個(gè)腋腮,其中SYN必須為1,余下的必須為0壤蚜;
[!] --syn:用于匹配第一次握手即寡,相當(dāng)于”--tcp-flags SYN,ACK,FIN,RST SYN“; - udp
[!] --source-port, --sport port[:port]:匹配報(bào)文的源端口袜刷;可以是端口范圍聪富;
[!] --destination-port,--dport port[:port]:匹配報(bào)文的目標(biāo)端口;可以是端口范圍著蟹; - icmp
[!] --icmp-type {type[/code]|typename}
echo-request:8
echo-reply:0
- tcp:
- 隱式擴(kuò)展:不需要手動(dòng)加載擴(kuò)展模塊嘹裂;因?yàn)樗鼈兪菍?duì)協(xié)議的擴(kuò)展,所以摔握,但凡使用-p指明了協(xié)議寄狼,就表示已經(jīng)指明了要擴(kuò)展的模塊;
- 顯式擴(kuò)展:必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制墩蔓;
- 顯式擴(kuò)展:必須要手動(dòng)加載擴(kuò)展模塊, [-m matchname [per-match-options]]萧豆;必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制奸披;
- 1、multiport
This module matches a set of source or destination ports. Up to 15 ports can be specified. A port range (port:port) counts as two ports. It can only be used in conjunction with one of the following protocols: tcp, udp, udplite, dccp and sctp.
以離散或連續(xù)的 方式定義多端口匹配條件涮雷,最多15個(gè)阵面;- [!] --source-ports,--sports port[,port|,port:port]...:指定多個(gè)源端口;
- [!] --destination-ports,--dports port[,port|,port:port]...:指定多個(gè)目標(biāo)端口洪鸭;
- 2样刷、iprange
以連續(xù)地址塊的方式來(lái)指明多IP地址匹配條件;- [!] --src-range from[-to]
- [!] --dst-range from[-to]
- 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:使用內(nèi)核配置的時(shí)區(qū)而非默認(rèn)的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
- 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
限制本機(jī)某tcp服務(wù)接收新請(qǐng)求的速率:--syn, -m limit
- 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: 新連接請(qǐng)求梅肤;
ESTABLISHED:已建立的連接司蔬;
INVALID:無(wú)法識(shí)別的連接;
RELATED:相關(guān)聯(lián)的連接姨蝴,當(dāng)前連接是一個(gè)新請(qǐng)求俊啼,但附屬于某個(gè)已存在的連接;
UNTRACKED:未追蹤的連接左医;
- INVALID, ESTABLISHED, NEW, RELATED or UNTRACKED.
- state擴(kuò)展:
- 內(nèi)核模塊裝載:
nf_conntrack
nf_conntrack_ipv4 - 手動(dòng)裝載:
nf_conntrack_ftp
- 內(nèi)核模塊裝載:
- 追蹤到的連接:
/proc/net/nf_conntrack - 調(diào)整可記錄的連接數(shù)量最大值:
/proc/sys/net/nf_conntrack_max - 超時(shí)時(shí)長(zhǎng):
/proc/sys/net/netfilter/timeout
- [!] --state state
- 1、multiport
- 顯式擴(kuò)展:必須要手動(dòng)加載擴(kuò)展模塊, [-m matchname [per-match-options]]萧豆;必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制奸披;
- 隱式擴(kuò)展:在使用-p選項(xiàng)指明了特定的協(xié)議時(shí),無(wú)需再同時(shí)使用-m選項(xiàng)指明擴(kuò)展模塊的擴(kuò)展機(jī)制臼隔;
- 處理動(dòng)作(跳轉(zhuǎn)目標(biāo)):
- -j targetname [per-target-options]
- 簡(jiǎn)單target:
ACCEPT授帕, DROP - 擴(kuò)展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).
- --reject-with type
- LOG
Turn on kernel logging of matching packets.- --log-level
- --log-prefix
默認(rèn)日志保存于/var/log/messages
- 簡(jiǎn)單target:
- RETURN:
返回調(diào)用者;
自定義鏈做為target:
- -j targetname [per-target-options]
#多端口匹配mulitport示例:
#放行ssh服務(wù)端口22浮梢,以便xshell可以連接到服務(wù)器
[root@localhost ~]# iptables -A INPUT -s 192.168.1.0/24 -d 192.168.1.106 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.106 -d 192.168.1.0/24 -p tcp --sport 22 -j ACCEPT
#設(shè)置默認(rèn)規(guī)則為拒絕
[root@localhost ~]# iptables -A INPUT -d 192.168.1.106 -j REJECT
[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.106 -j REJECT
#多端口匹配示例
[root@localhost ~]# iptables -I INPUT 1 -d 192.168.1.106 -p tcp -m multiport --dports 21,53,80,139,445 -j ACCEPT
[root@localhost ~]# iptables -I OUTPUT 1 -s 192.168.1.106 -p tcp -m multiport --sports 21,53,80,139,445 -j ACCEPT
#在另外一臺(tái)192.168.1.107主機(jī)上測(cè)試本機(jī)開(kāi)放的端口80
[root@localhost ~]# curl http://192.168.1.106
<h1>192.168.1.106</h1>
hello gay
...
#iprange使用示例:
[root@localhost ~]#iptables -I INPUT 3 -d 192.168.1.106 -p tcp --dport 23 -m iprange --src-range 192.168.1.107-192.168.1.110 -j ACCEP
[root@localhost ~]#iptables -I OUTPUT 3 -s 192.168.1.106 -p tcp --sport 23 -m iprange --dst-range 192.168.1.107-192.168.1.110 -j ACCEP
#使用192.168.1.107的主機(jī)telnet測(cè)試服務(wù)器
[root@localhost ~]# telnet 192.168.1.106 23
Trying 192.168.1.106...
Connected to 192.168.1.106.
Escape character is '^]'.
Kernel 3.10.0-693.el7.x86_64 on an x86_64
localhost login:
...
#time示例:
[root@localhost ~]# iptables -R INPUT 3 -d 192.168.1.106 -p tcp -m iprange --src-range 192.168.1.107-192.168.1.110 -m time --timestart 10:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 --kerneltz -j ACCEPT
[root@localhost ~]# iptables -R OUTPUT 3 -s 192.168.1.106 -p tcp -m iprange --dst-range 192.168.1.107-192.168.1.110 -m time --timestart 10:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 --kerneltz -j ACCEPT
#使用192.168.1.107telnet測(cè)試
[root@localhost ~]# telnet 192.168.1.106 23
Trying 192.168.1.106...
...
#string示例:(只對(duì)明文編碼的生效)
[root@localhost ~]# curl http://192.168.1.106
<h1>192.168.1.106</h1>
hello gay
#添加規(guī)則過(guò)濾gay
[root@localhost ~]# iptables -I OUTPUT -s 192.168.1.106 -m string --algo kmp --string "gay" -j REJECT
#添加規(guī)則后重新訪(fǎng)問(wèn)被拒絕
[root@localhost ~]# curl http://192.168.1.106
|
...
#connlimit并發(fā)連接限制:
[root@localhost ~]# iptables -I INPUT 2 -s 192.168.0.0/16 -d 192.168.1.106 -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]# iptables -I OUTPUT 2 -d 192.168.0.0/16 -s 192.168.1.106 -p tcp --sport 3306 -j ACCEPT
#先放行3306端口后修改此條規(guī)則跛十,限制并發(fā)訪(fǎng)問(wèn)數(shù)量為2
[root@localhost ~]# iptables -R INPUT 2 -s 192.168.0.0/16 -d 192.168.1.106 -p tcp --dport 3306 -m connlimit --connlimit-upto 2 -j ACCEPT
[root@localhost ~]# iptables -R OUTPUT 2 -d 192.168.0.0/16 -s 192.168.1.106 -p tcp --sport 3306 -m connlimit --connlimit-upto 2 -j ACCEPT
#分別使用192.168.1.107-192.168.1.109三臺(tái)主機(jī)測(cè)試,前2臺(tái)連接mysql數(shù)據(jù)庫(kù)正常
[root@localhost ~]# mysql -utest -h192.168.1.106 -pmagedu
Welcome to the MariaDB monitor. Commands end with ; or \g.
...
MariaDB [(none)]>
#使用192.168.1.109主機(jī)已經(jīng)連接不上
[root@localhost ~]# mysql -utest -h192.168.1.106 -pmagedu
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.106' (110)
...
#limit 速率限制示例:
[root@localhost ~]# iptables -I INPUT 3 -d 192.168.1.106 -s 192.168.1.0/24 -p icmp --icmp-type 8 -m limit --limit-burst 5 --limit 20/minute -j ACCEPT
[root@localhost ~]# iptables -R OUTPUT 3 -s 192.168.1.106 -d 192.168.1.0/24 -p icmp -m state --state ESTABLISHED -j ACCEPT
#使用107的主機(jī)ping測(cè)試
[root@localhost ~]# ping 192.168.1.106
PING 192.168.1.106 (192.168.1.106) 56(84) bytes of data.
64 bytes from 192.168.1.106: icmp_seq=1 ttl=64 time=31.1 ms
64 bytes from 192.168.1.106: icmp_seq=2 ttl=64 time=1.56 ms
64 bytes from 192.168.1.106: icmp_seq=3 ttl=64 time=0.903 ms
64 bytes from 192.168.1.106: icmp_seq=4 ttl=64 time=0.837 ms
64 bytes from 192.168.1.106: icmp_seq=5 ttl=64 time=1.88 ms
64 bytes from 192.168.1.106: icmp_seq=8 ttl=64 time=0.751 ms
64 bytes from 192.168.1.106: icmp_seq=11 ttl=64 time=0.869 ms
64 bytes from 192.168.1.106: icmp_seq=14 ttl=64 time=2.81 ms
...
#state報(bào)文狀態(tài)匹配示例:
#放行22秕硝,23,80,139,445的tcp端口芥映,和123,323 ,137,138的udp端口
[root@localhost ~]# iptables -A INPUT -d 192.168.1.106 -p tcp -m multiport --dports 22:23,80,139,445,3306 -m state --state NEW -j ACCEPT
[root@localhost ~]# iptables -I INPUT -d 192.168.1.106 -m state --state ESTABLISHED -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.106 -m state --state ESTABLISHED -j ACCEPT
[root@localhost ~]# iptables -A INPUT -d 192.168.1.106 -j REJECT
[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.106 -j REJECT
[root@localhost ~]# iptables -I INPUT 2 -d 192.168.1.106 -p udp --dport 137:138 -m state --state NEW -j ACCEPT
[root@localhost ~]# iptables -I OUTPUT 2 -s 192.168.1.106 -p udp -m multiport --dports 123,323 -m state --state NEW -j ACCEPT
#放行FTP奈偏,裝載連接追蹤
[root@localhost ~]# iptables -R INPUT 3 -d 192.168.1.106 -p tcp -m multiport --dports 21:23,80,139,445,3306 -m state --state NEW -j ACCEPT
[root@localhost ~]# iptables -R INPUT 1 -d 192.168.1.106 -m state --state ESTABLISHED,RELATED -j ACCEPT
#訪(fǎng)問(wèn)ftp服務(wù)測(cè)試
[root@localhost ~]# lftp 192.168.1.106
lftp 192.168.1.106:~> ls
drwxr-xr-x 2 0 0 6 Aug 03 2017 pub
lftp 192.168.1.106:/>
iptables/netfilter網(wǎng)絡(luò)防火墻:
-
iptables/netfilter網(wǎng)絡(luò)防火墻
- (1) 網(wǎng)關(guān)坞嘀;
- (2) filter表的FORWARD鏈;
要注意的問(wèn)題:
(1) 請(qǐng)求-響應(yīng)報(bào)文均會(huì)經(jīng)由FORWARD鏈惊来,要注意規(guī)則的方向性丽涩;
(2) 如果要啟用conntrack機(jī)制,建議將雙方向的狀態(tài)為ESTABLISHED的報(bào)文直接放行裁蚁;-
NAT: Network Address Translation
請(qǐng)求報(bào)文:由管理員定義矢渊;
響應(yīng)報(bào)文:由NAT的conntrack機(jī)制自動(dòng)實(shí)現(xiàn);- 請(qǐng)求報(bào)文:
- 改源地址:SNAT枉证,MASQUERADE
- 改目標(biāo)地址:DNAT
- iptables/netfilter:
- NAT定義在nat表矮男;
PREROUTING,INPUT室谚,OUTPUT昂灵,POSTROUTING
SNAT:POSTROUTING
DNAT:PREROUTING
PAT:
- NAT定義在nat表矮男;
- target:
- SNAT:隱藏內(nèi)網(wǎng)主機(jī)去訪(fǎng)問(wèn)外網(wǎng)
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]] - DNAT:隱藏服務(wù)器,只開(kāi)放有限協(xié)議有限服務(wù)的端口
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]] - 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.
SNAT場(chǎng)景中應(yīng)用于POSTROUTING鏈上的規(guī)則實(shí)現(xiàn)源地址轉(zhuǎn)換舞萄,但外網(wǎng)地址不固定時(shí)眨补,使用此target; - 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]
- SNAT:隱藏內(nèi)網(wǎng)主機(jī)去訪(fǎng)問(wèn)外網(wǎng)
- 請(qǐng)求報(bào)文:
實(shí)驗(yàn):按下圖配置網(wǎng)關(guān)主機(jī)實(shí)現(xiàn)客戶(hù)端訪(fǎng)問(wèn)服務(wù)器,演示網(wǎng)絡(luò)防火墻功能
-
按上圖配置好三臺(tái)虛擬機(jī)倒脓,并按圖中ip配置好撑螺,開(kāi)啟網(wǎng)關(guān)主機(jī)的核心轉(zhuǎn)發(fā)功能
[root@localhost ~]# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1 #在客戶(hù)端添加默認(rèn)路由條目 [root@localhost ~]# route add default gw 192.168.10.254 #在服務(wù)端添加路由 [root@localhost ~]# route add -net 192.168.10.0/24 gw 192.168.1.107 #至此客戶(hù)端和服務(wù)端之間連接建立,在客戶(hù)端192.168.10.2上訪(fǎng)問(wèn)服務(wù)器192.168.1.106的web服務(wù) [root@localhost ~]# curl http://192.168.1.106 <h1>192.168.1.106</h1> hello gay
-
在網(wǎng)關(guān)主機(jī)192.168.106上的FORWARD鏈上添加規(guī)則
#添加默認(rèn)規(guī)則崎弃,拒絕訪(fǎng)問(wèn) [root@localhost ~]# iptables -A FORWARD -j REJECT #使用客戶(hù)端測(cè)試web服務(wù) [root@localhost ~]# curl http://192.168.1.106 curl: (7) couldn't connect to host #是用ping命令測(cè)試客戶(hù)端和服務(wù)端之間連接被拒絕 [root@localhost ~]# ping 192.168.10.2 PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data. From 192.168.1.107 icmp_seq=1 Destination Port Unreachable From 192.168.1.107 icmp_seq=2 Destination Port Unreachable #放行服務(wù)器的80端口甘晤,使客戶(hù)端可以訪(fǎng)問(wèn)web服務(wù) [root@localhost ~]# iptables -I FORWARD -s 192.168.10.0/24 -d 192.168.1.106 -p tcp --dport 80 -j ACCEPT [root@localhost ~]# iptables -I FORWARD -m state --state ESTABLISHED -j ACCEPT #使用客戶(hù)機(jī)訪(fǎng)問(wèn)測(cè)試成功 [root@localhost ~]# curl http://192.168.1.106 <h1>192.168.1.106</h1> hello gay #單方向放行服務(wù)端到客戶(hù)端的所有訪(fǎng)問(wèn) [root@localhost ~]# iptables -D FORWARD 2 [root@localhost ~]# iptables -I FORWARD 2 -d 192.168.10.0/24 -m state --state NEW -j ACCEPT
...
-
實(shí)現(xiàn)SNAT,DNAT
SNAT: #清空FORWARD鏈上的所有規(guī)則 [root@localhost ~]# iptables -F FORWARD #隱藏服務(wù)器IP地址去訪(fǎng)問(wèn)外網(wǎng)(SNAT)在網(wǎng)關(guān)主機(jī)192.168.1.107主機(jī)上添加規(guī)則 [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 192.168.10.254 #使用192.168.1.106訪(fǎng)問(wèn)192.168.10.2的http服務(wù)正常 [root@localhost ~]# curl http://192.168.10.2 <h1>192.168.10.2</h1> #在192.168.10.2主機(jī)上抓包分析可以看到服務(wù)器IP地址已經(jīng)被隱藏,顯示的是192.168.10.254的IP地址 [root@localhost ~]# tcpdump -i ens33 -nn tcp port 80 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 17:30:16.081283 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [S], seq 854561726, win 29200, options [mss 1460,sackOK,TS val 11762377 ecr 0,nop,wscale 7], length 0 17:30:16.081410 IP 192.168.10.2.80 > 192.168.10.254.40290: Flags [S.], seq 2274859692, ack 854561727, win 28960, options [mss 1460,sackOK,TS val 4762953 ecr 11762377,nop,wscale 7], length 0 17:30:16.085611 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 11762382 ecr 4762953], length 0 17:30:16.085658 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [P.], seq 1:77, ack 1, win 229, options [nop,nop,TS val 11762382 ecr 4762953], length 76: HTTP: GET / HTTP/1.1 17:30:16.085735 IP 192.168.10.2.80 > 192.168.10.254.40290: Flags [.], ack 77, win 227, options [nop,nop,TS val 4762957 ecr 11762382], length 0 17:30:16.090704 IP 192.168.10.2.80 > 192.168.10.254.40290: Flags [P.], seq 1:264, ack 77, win 227, options [nop,nop,TS val 4762962 ecr 11762382], length 263: HTTP: HTTP/1.1 200 OK 17:30:16.110303 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [.], ack 264, win 237, options [nop,nop,TS val 11762391 ecr 4762962], length 0 17:30:16.112405 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [F.], seq 77, ack 264, win 237, options [nop,nop,TS val 11762392 ecr 4762962], length 0 17:30:16.113445 IP 192.168.10.2.80 > 192.168.10.254.40290: Flags [F.], seq 264, ack 78, win 227, options [nop,nop,TS val 4762985 ecr 11762392], length 0 17:30:16.117007 IP 192.168.10.254.40290 > 192.168.10.2.80: Flags [.], ack 265, win 237, options [nop,nop,TS val 11762413 ecr 4762985], length 0
...
DNAT:
#在192.168.1.107的網(wǎng)關(guān)主機(jī)上清空nat表上所有規(guī)則
[root@localhost ~]# iptables -t nat -F
#使用DNAT目標(biāo)地址轉(zhuǎn)換實(shí)現(xiàn):隱藏服務(wù)器ip饲做,僅開(kāi)放有限協(xié)議有限服務(wù)的端口
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.1.106 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.254
#使用192.168.10.2主機(jī)訪(fǎng)問(wèn)192.168.1.106的http服務(wù)
[root@localhost ~]# curl http://192.168.1.106
<h1>192.168.1.106</h1>
hello gay
#在192.168.1.106主機(jī)上抓包測(cè)試
[root@localhost ~]# tcpdump -i ens33 -nn tcp port 80
....