linux防火墙扩展模块实战(二)
iptables擴(kuò)展模塊
擴(kuò)展匹配條件:需要加載擴(kuò)展模塊(/usr/lib64/xtables/*.so),方可生效
查看幫助 man iptables-extensions
(1)隱式擴(kuò)展:在使用-p選項(xiàng)指明了特定的協(xié)議時(shí),無(wú)需再用-m選項(xiàng)指明擴(kuò)展模塊的擴(kuò)展機(jī)制,不需要手動(dòng)加載擴(kuò)展模塊
tcp協(xié)議的擴(kuò)展選項(xiàng)
--source-port, --sport port[:port]:匹配報(bào)文源端口,可為端口范圍 --destination-port,--dport port[:port]:匹配報(bào)文目標(biāo)端口,可為范圍 --tcp-flags mask comp
mask 需檢查的標(biāo)志位列表,用,分隔
例如 SYN,ACK,FIN,RST
comp 在mask列表中必須為1的標(biāo)志位列表,無(wú)指定則必須為0,用,分隔
演示:TCP協(xié)議的擴(kuò)展選項(xiàng)
A主機(jī):192.168.34.101
B主機(jī):192.168.34.102
(1)在B主機(jī)上先新建一個(gè)網(wǎng)頁(yè),并啟動(dòng)httpd和mariadb服務(wù)
[root@centos777~]#yum install mariadb-server httpd -y [root@centos777~]#systemctl start httpd [root@centos777~]#systemctl start mariadb [root@centos777~]#echo welcome to beijing > /var/www/html/index.html
(2)此時(shí)在B主機(jī)進(jìn)行控制其他機(jī)器的訪問
[root@centos777~]#iptables -A INPUT -s 192.168.34.1,127.0.0.1 -j ACCEPT 允許本地windows系統(tǒng)訪問 [root@centos777~]#iptables -A INPUT -j REJECT 拒絕其他所有主機(jī)訪問本機(jī) [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 68 4836 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 4 packets, 432 bytes) num pkts bytes target prot opt in out source destination
(3)此時(shí)A主機(jī)無(wú)法訪問B主機(jī)
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
(4)此時(shí)只允許A主機(jī)訪問本機(jī)的HTTPD服務(wù)
[root@centos777~]#iptables -I INPUT 3 -s 192.168.34.101 -p tcp --dport 80 -j ACCEPT [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 217 15779 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 0 0 ACCEPT tcp -- * * 192.168.34.101 0.0.0.0/0 tcp dpt:80 4 1 60 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 4 packets, 544 bytes) num pkts bytes target prot opt in out source destination
(5)查看此時(shí)A主機(jī)通過tcp協(xié)議就可以訪問B主機(jī)的httpd服務(wù)內(nèi)容
[root@centos7~]#curl 192.168.34.102 welcome to beijing
(6)在B主機(jī)將mysql數(shù)據(jù)庫(kù)允許A主機(jī)訪問
[root@centos777~]#iptables -I INPUT 3 -s 192.168.34.101 -p tcp --dport 3306 -j ACCEPT
(7)在B主機(jī)創(chuàng)建一個(gè)mysql賬號(hào),驗(yàn)證效果
[root@centos777~]#mysql -e "grant all on *.* to test@'192.168.34.%' identified by 'centos'"
(8)此時(shí)在A主機(jī)啟動(dòng)自身的mysql數(shù)據(jù)庫(kù),并能連接對(duì)方的mysql數(shù)據(jù)庫(kù)
[root@centos7~]#systemctl start mariadb [root@centos7~]#mysql -utest -pcentos -h192.168.34.102 Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 4 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]>
tcp協(xié)議的擴(kuò)展選項(xiàng)
示例:
--tcp-flags SYN,ACK,FIN,RST SYN 表示要檢查的標(biāo)志位為SYN,ACK,FIN,RST四個(gè),其中SYN必須為1,余下的必須為0 --tcp-flags SYN,ACK,FIN,RST SYN,ACK --tcp-flags ALL ALL --tcp_flags ALL NONE
--syn:用于匹配第一次握手
相當(dāng)于:--tcp-flags SYN,ACK,FIN,RST SYN
示例:
只允許此時(shí)有tcp規(guī)則(握手)進(jìn)行拒絕,但可以允許其他方式訪問
[root@centos777~]#iptables -I INPUT 4 -s 192.168.34.100 -p tcp --syn -j REJECT 拒絕C主機(jī)進(jìn)行握手訪問 [root@centos777~]#iptables -I INPUT 5 -s 192.168.34.100 -j ACCEPT 允許C主機(jī)能訪問 [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 634 46456 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 11 685 ACCEPT tcp -- * * 192.168.34.101 0.0.0.0/0 tcp dpt:3306 4 0 0 REJECT tcp -- * * 192.168.34.100 0.0.0.0/0 tcp flags:0x17/0x02 5 6 398 ACCEPT all -- * * 192.168.34.100 0.0.0.0/0 6 34 4423 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 4 packets, 528 bytes) num pkts bytes target prot opt in out source destination
此時(shí)在C主機(jī)(192.168.34.100)進(jìn)行訪問,此時(shí)通過握手協(xié)議訪問被拒絕
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
此時(shí)在C主機(jī)可以ping通
[root@centos7~]#ping 192.168.34.102 PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data. 64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=1.21 ms 64 bytes from 192.168.34.102: icmp_seq=2 ttl=64 time=0.383 ms 64 bytes from 192.168.34.102: icmp_seq=3 ttl=64 time=0.379 ms
udp擴(kuò)展選項(xiàng)
[!] --source-port, --sport port[:port]:匹配報(bào)文的源端口或端口范圍 [!] --destination-port,--dport port[:port]:匹配報(bào)文的目標(biāo)端口或端口范圍
icmp擴(kuò)展協(xié)議
[!] --icmp-type {type[/code]|typename}
type/code
0/0 echo-reply icmp應(yīng)答
8/0 echo-request icmp請(qǐng)求
實(shí)戰(zhàn)演練:實(shí)現(xiàn)本機(jī)ping對(duì)方可以通,對(duì)方不能ping通本機(jī),或指定對(duì)方能ping通本機(jī)
(1)在本機(jī)進(jìn)行修改防火墻策略
[root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 935 69248 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 39 4843 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 4 packets, 416 bytes) num pkts bytes target prot opt in out source destination [root@centos777~]#iptables -I INPUT 3 -p icmp --icmp-type 0 -j ACCEPT 其中--icmp-type 0意思是本機(jī)ping對(duì)方是經(jīng)過INPUT,此時(shí)是應(yīng)答結(jié)果
(2)驗(yàn)證效果,在本機(jī)進(jìn)行ping192.168.34.101,可以Ping通
[root@centos777~]#ping 192.168.34.101 PING 192.168.34.101 (192.168.34.101) 56(84) bytes of data. 64 bytes from 192.168.34.101: icmp_seq=1 ttl=64 time=0.745 ms
(3)在對(duì)方進(jìn)行ping本機(jī)IP地址,此時(shí)無(wú)法ping通
[root@centos7~]#ping 192.168.34.102 PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data. From 192.168.34.102 icmp_seq=1 Destination Port Unreachable From 192.168.34.102 icmp_seq=2 Destination Port Unreachable
(4)將本機(jī)的icmp協(xié)議改為8,此時(shí)對(duì)方就可以ping通本機(jī)
[root@centos777~]#iptables -I INPUT 3 -p icmp --icmp-type 8 -j ACCEPT
(5)在對(duì)方機(jī)器進(jìn)行ping結(jié)果
[root@centos7~]#ping 192.168.34.102 PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data. 64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=0.630 ms
顯式擴(kuò)展:必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制,要手動(dòng)加載擴(kuò)展模塊
[-m matchname [per-match-options]]
顯式擴(kuò)展:必須顯式地指明使用的擴(kuò)展模塊進(jìn)行的擴(kuò)展
使用幫助:
CentOS 6: man iptables
CentOS 7: man iptables-extensions
1、multiport擴(kuò)展
以離散方式定義多端口匹配,最多指定15個(gè)端口
[!] --source-ports,--sports port[,port|,port:port]... 指定多個(gè)源端口 [!] --destination-ports,--dports port[,port|,port:port]... 指定多個(gè)目標(biāo)端口 [!] --ports port[,port|,port:port]...多個(gè)源或目標(biāo)端口
示例:
iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.10 -p tcp -m multiport --dports 20:22,80 -j ACCEPT
演練:
(1)安裝samba服務(wù)并啟動(dòng)
[root@centos777~]#yum install samba -y 安裝samba服務(wù) [root@centos777~]#systemctl start smb
(2)創(chuàng)建一個(gè)系統(tǒng)賬號(hào)并加入到samba服務(wù)中,成為samba賬號(hào)
[root@centos777~]#useradd -s /sbin/nologin smb1 ; smbpasswd -a smb1 New SMB password: Retype new SMB password: Added user smb1.
(3)創(chuàng)建防火墻規(guī)則,此時(shí)可以一次性指定兩個(gè)不連續(xù)的端口號(hào),且都在一行顯示,方便管理
[root@centos777~]#iptables -I INPUT 4 -p tcp -m multiport --ports 139,445 -j ACCEPT [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 2044 151K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 2 168 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 4 14 2394 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport ports 139,445 5 1 84 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0 6 61 6969 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 26 packets, 3231 bytes) num pkts bytes target prot opt in out source destination
此時(shí)在另外一臺(tái)主機(jī)就可以登錄samba服務(wù)
[root@centos7~]#smbclient //192.168.34.102/smb1 -U smb1%centos Try "help" to get a list of possible commands. smb: >
也可以在本機(jī)加入samba的UDP協(xié)議端口,由于兩個(gè)端口號(hào)連續(xù),不需要加multiport模塊
[root@centos777~]#iptables -I INPUT 4 -p udp --dport 137:138 -j ACCEPT
2、iprange擴(kuò)展
指明連續(xù)的(但一般不是整個(gè)網(wǎng)絡(luò))ip地址范圍
[!] --src-range from[-to] 源IP地址范圍 [!] --dst-range from[-to] 目標(biāo)IP地址范圍
示例:
iptables -A INPUT -d 172.16.1.100 -p tcp --dport 80 -m iprange --src-range 172.16.1.5-172.16.1.10 -j DROP
3、mac擴(kuò)展
指明源MAC地址
適用于:PREROUTING, FORWARD,INPUT chains
[!] --mac-source XX:XX:XX:XX:XX:XX
示例:
iptables -A INPUT -s 172.16.0.100 -m mac --mac-source 00:50:56:12:34:56 -j ACCEPT iptables -A INPUT -s 172.16.0.100 -j REJECT
實(shí)戰(zhàn)演示:允許B主機(jī)通過MAC地址進(jìn)行ping本機(jī)
A主機(jī):192.168.34.102
B主機(jī):192.168.34.101
(1)在A主機(jī)上設(shè)置B主機(jī)的MAC地址防火墻規(guī)則
[root@centos777~]#iptables -I INPUT 3 -m mac --mac-source 00:0c:29:4e:31:b6 -j ACCEPT [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 2629 195K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 1 84 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 MAC 00:0C:29:4E:31:B6 4 81 9753 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 5 packets, 884 bytes) num pkts bytes target prot opt in out source destination
(2)在B主機(jī)開始ping主機(jī)A,此時(shí)就可以ping通
[root@centos7~]#ping 192.168.34.102 PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data. 64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=0.883 ms ^C --- 192.168.34.102 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.883/0.883/0.883/0.000 ms
4、string擴(kuò)展
對(duì)報(bào)文中的應(yīng)用層數(shù)據(jù)做字符串模式匹配檢測(cè)
--algo {bm|kmp} 字符串匹配檢測(cè)算法
bm:Boyer-Moore
kmp:Knuth-Pratt-Morris
--from offset 開始偏移
--to offset 結(jié)束偏移
[!] --string pattern 要檢測(cè)的字符串模式
[!] --hex-string pattern要檢測(cè)字符串模式,16進(jìn)制格式
示例:
iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT
實(shí)戰(zhàn)演練:不允許對(duì)方主機(jī)訪問google網(wǎng)頁(yè)
(1)在本機(jī)先新建幾個(gè)網(wǎng)頁(yè)
[root@centos777~]#echo www.google.com > /var/www/html/google.html [root@centos777~]#echo www.google.com > /var/www/html/test.html [root@centos777~]#echo welcom to beijing > /var/www/html/index.html [root@centos777~]#cd /var/www/html [root@centos777html]#ls google.html index.html test.html
(2)然后對(duì)所有主機(jī)設(shè)置google關(guān)鍵字樣拒絕訪問的防火墻規(guī)則
[root@centos777html]#iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT [root@centos777html]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 3010 229K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 25 2006 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 MAC 00:0C:29:4E:31:B6 4 99 13459 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 32 packets, 2872 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:80 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
(3)此時(shí)在對(duì)方主機(jī)進(jìn)行訪問本機(jī)的網(wǎng)頁(yè),此時(shí)就無(wú)法訪問google網(wǎng)頁(yè)
5、time擴(kuò)展
根據(jù)將報(bào)文到達(dá)的時(shí)間與指定的時(shí)間范圍進(jìn)行匹配
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 日期 --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] --timestart hh:mm[:ss] 時(shí)間 --timestop hh:mm[:ss] [!] --monthdays day[,day...] 每個(gè)月的幾號(hào) [!] --weekdays day[,day...] 星期幾,1 – 7 分別表示星期一到星期日 --kerneltz:內(nèi)核時(shí)區(qū),不建議使用,CentOS7系統(tǒng)默認(rèn)為UTC,UTC時(shí)間與本地時(shí)間相差8小時(shí)。如:UTC時(shí)間是1點(diǎn),實(shí)際時(shí)間是9點(diǎn),centos6默認(rèn)就是當(dāng)?shù)貢r(shí)間,不需要加8.
注意: centos6 不支持kerneltz ,--localtz指定本地時(shí)區(qū)(默認(rèn))
示例:
iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.10 -p tcp --dport 80 -m time --timestart 14:30 --timestop 18:30 --weekdays Sat,Sun -j DROP
實(shí)戰(zhàn)演練:
(1)設(shè)置時(shí)間模塊,指定具體時(shí)間段訪問網(wǎng)絡(luò)
[root@centos777~]#iptables -I INPUT 3 -m time --timestart 1:00 --timestop 10:00 -j ACCEPT # 只允許在9:00到18:00訪問,centos7系統(tǒng)需要加8個(gè)小時(shí),才是本地的時(shí)間。 [root@centos777~]#cd [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 3364 255K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 TIME from 01:00:00 to 10:00:00 UTC 4 202 48180 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 8 packets, 1024 bytes) num pkts bytes target prot opt in out source destination [root@centos777~]#date 此時(shí)的時(shí)間不在設(shè)置的文件范圍內(nèi) Thu Dec 5 22:49:46 CST 2019
(2)其他主機(jī)訪問此主機(jī)的網(wǎng)頁(yè)是就會(huì)被拒絕
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
6、connlimit擴(kuò)展
根據(jù)每客戶端IP做并發(fā)連接數(shù)數(shù)量匹配
缺點(diǎn):就是黑客用不同的IP地址進(jìn)行訪問,就無(wú)法針對(duì)連接數(shù)進(jìn)行阻擋。
可防止Dos(Denial of Service,拒絕服務(wù))攻擊 --connlimit-upto #:連接的數(shù)量小于等于#時(shí)匹配
--connlimit-above #:連接的數(shù)量大于#時(shí)匹配
通常分別與默認(rèn)的拒絕或允許策略配合使用
示例:
iptables -A INPUT -d 172.16.100.10 -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
實(shí)戰(zhàn)演練:防止DOS攻擊,制定防火墻策略
(1)在本機(jī)設(shè)置防火墻規(guī)則
[root@centos777~]#iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 100 -j REJECT 制定防火墻規(guī)則,訪問次數(shù)大于100的被拒絕 [root@centos777~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 4148 325K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 #conn src/32 > 100 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1900 bytes) num pkts bytes target prot opt in out source destination
(2)此時(shí)在對(duì)方主機(jī)訪問本機(jī)小于100次連接的都可以訪問網(wǎng)頁(yè)
[root@centos7~]#curl 192.168.34.102 welcome to beijing
7、limit擴(kuò)展
基于收發(fā)報(bào)文的速率做匹配
令牌桶過濾器
--limit #[/second|/minute|/hour|/day] --limit-burst number
實(shí)戰(zhàn)演練:
[root@centos777~]#iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 10 -j ACCEPT 接收規(guī)則,并允許前10個(gè)訪問網(wǎng)頁(yè) [root@centos777~]#iptables -A INPUT -j REJECT 剩下的全部拒絕
8、state擴(kuò)展
根據(jù)”連接追蹤機(jī)制“去檢查連接的狀態(tài),較耗資源
conntrack機(jī)制:追蹤本機(jī)上的請(qǐng)求和響應(yīng)之間的關(guān)系
狀態(tài)有如下幾種:
NEW:新發(fā)出請(qǐng)求;連接追蹤信息庫(kù)中不存在此連接的相關(guān)信息條目,因此,將其識(shí)別為第一次發(fā)出的請(qǐng)求 ESTABLISHED:NEW狀態(tài)之后,連接追蹤信息庫(kù)中為其建立的條目失效之前期間內(nèi)所進(jìn)行的通信狀態(tài) RELATED:新發(fā)起的但與已有連接相關(guān)聯(lián)的連接,如:ftp協(xié)議中的數(shù)據(jù)連接與命令連接之間的關(guān)系 INVALID:無(wú)效的連接,如flag標(biāo)記不正確 UNTRACKED:未進(jìn)行追蹤的連接,如raw表中關(guān)閉追蹤
示例:
老用戶通過ssh可以連接遠(yuǎn)程主機(jī)
設(shè)置老用戶連接不被拒絕,但是老用戶通過ssh連接的主機(jī)退出后就無(wú)法連接,新用戶連接就被拒絕防火墻
[root@centos7~]#iptables -I INPUT 3 -p tcp --dport 22 -m state --state ESTABLISHED,RELATED -j ACCEPT
由于設(shè)置了防火墻,新用戶無(wú)法連接
[!] --state state
示例:
iptables -A INPUT -d 172.16.1.10 -p tcp -m multiport --dports 22,80 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -s 172.16.1.10 -p tcp -m multiport --sports 22,80 -m state --state ESTABLISHED -j ACCEPT
已經(jīng)追蹤到的并記錄下來(lái)的連接信息庫(kù)
/proc/net/nf_conntrack
調(diào)整連接追蹤功能所能夠容納的最大連接數(shù)量
/proc/sys/net/nf_conntrack_max
不同的協(xié)議的連接追蹤時(shí)長(zhǎng)
/proc/sys/net/netfilter/
注意:CentOS7 需要加載模塊: modprobe nf_conntrack_ipv4
/proc/sys/net/nf_conntrack_max:連接跟蹤的最大連接數(shù)
可以將此參數(shù)寫在配置文件中,永久生效:
vim /etc/sysctl.conf
net.nf_conntrack_max=88888 臨時(shí)修改到88888
修改完配置文件之后,使配置文件生效:
[root@centos7~]#sysctl -p net.nf_conntrack_max = 88888
iptables的鏈接跟蹤表最大容量為/proc/sys/net/nf_conntrack_max,各種狀態(tài)的超時(shí)鏈接會(huì)從表中刪除;當(dāng)模板滿載時(shí),后續(xù)連接可能會(huì)超時(shí)
解決方法兩個(gè):
(1) 加大nf_conntrack_max 值
vim /etc/sysctl.conf net.nf_conntrack_max = 393216 net.netfilter.nf_conntrack_max = 393216
(2) 降低 nf_conntrack timeout時(shí)間
vim /etc/sysctl.conf net.netfilter.nf_conntrack_tcp_timeout_established = 300 net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120 net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120 iptables -t nat -L -n
開放被動(dòng)模式的ftp服務(wù)
(1) 裝載ftp連接追蹤的專用模塊:
跟蹤模塊路徑:/lib/modules/kernelversion/kernel/net/netfilter
vim /etc/sysconfig/iptables-config 配置文件 IPTABLES_MODULES=“nf_conntrack_ftp"
modproble nf_conntrack_ftp # 加載此模塊
(2) 放行請(qǐng)求報(bào)文:
命令連接:NEW, ESTABLISHED
數(shù)據(jù)連接:RELATED, ESTABLISHED
iptables –I INPUT -d LocalIP -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -d LocalIP -p tcp --dport 21 -m state --state NEW -j ACCEPT
? (3) 放行響應(yīng)報(bào)文:
iptables -I OUTPUT -s LocalIP -p tcp -m state --state ESTABLISHED -j ACCEPT
實(shí)戰(zhàn)演示:開放被動(dòng)模式的ftp服務(wù)
A主機(jī):192.168.34.101
B主機(jī):192.168.34.102
(1)在A主機(jī)先添加一個(gè)允許tcp協(xié)議,21端口連接的訪問
[root@centos7~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 445 34224 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 40 5213 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 state RELATED,ESTABLISHED 4 3 320 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 77 packets, 8175 bytes) num pkts bytes target prot opt in out source destination [root@centos7~]#iptables -I INPUT 3 -p tcp --dport 21 -j ACCEPT
(2)在A主機(jī)安裝vsftpd服務(wù)并啟動(dòng)服務(wù)
[root@centos7~]#yum install vsftpd -y [root@centos7~]#systemctl start vsftpd
(3)此時(shí)在B主機(jī)只能連接A主機(jī)的ftp服務(wù)器,被動(dòng)模式的端口號(hào)是隨機(jī)的,A主機(jī)不能添加指定的tcp協(xié)議端口號(hào),因此B主機(jī)不能執(zhí)行其他操作。
(4)在A主機(jī)加載ftp相關(guān)模塊,能識(shí)別FTP協(xié)議,能分析ftp21端口號(hào)的數(shù)據(jù)傳輸?shù)男畔ⅲ瑥亩軌虻弥麓瓮ㄓ嵾^程中被動(dòng)模式使用的端口號(hào)是多少
[root@centos7~]#modprobe nf_conntrack_ftp
(5)在A主機(jī)添加一個(gè)iptables防火墻規(guī)則,注意:ESTABLISHED,RELATED和tcp 21協(xié)議的合理性,將tcp 21的防火墻規(guī)則放在后面較好,當(dāng)用戶訪問大量數(shù)據(jù)時(shí),提高效率,優(yōu)化性能方面可以考慮。
[root@centos7~]#iptables -I INPUT 3 -m state --state ESTABLISHED,RELATED -j ACCEPT
(6)最后在B主機(jī)驗(yàn)證連接ftp效果,此時(shí)就可以訪問文件
Target:
ACCEPT, DROP, REJECT, RETURN
LOG, SNAT, DNAT, REDIRECT, MASQUERADE,..
LOG:非中斷target,本身不拒絕和允許,放在拒絕和允許規(guī)則前
并將日志記錄在/var/log/messages系統(tǒng)日志中
--log-level level 級(jí)別: debug,info,notice, warning, error, crit, alert,emerg --log-prefix prefix 日志前綴,用于區(qū)別不同的日志,最多29個(gè)字符
收集指定的主機(jī)訪問本機(jī)的log日志
(1)在A主機(jī)配置一個(gè)防火墻規(guī)則
[root@centos7~]#iptables -I INPUT 4 -s 192.168.34.102 -j LOG --log-prefix "from 34.102 access:" [root@centos7~]#iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1467 110K ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 3 17 939 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 4 0 0 LOG all -- * * 192.168.34.102 0.0.0.0/0 LOG flags 0 level 4 prefix "from 34.102 access:" 5 10 1226 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 32 packets, 2856 bytes) num pkts bytes target prot opt in out source destination
(2)在B主機(jī)訪問當(dāng)前的信息,就會(huì)在系統(tǒng)日志中記錄來(lái)自于B主機(jī)的信息
(3)在A主機(jī)進(jìn)行l(wèi)og日志跟蹤,可以看到跟蹤的日志信息
iptables防火墻規(guī)則總結(jié)
任何不允許的訪問,應(yīng)該在請(qǐng)求到達(dá)時(shí)給予拒絕
規(guī)則在鏈接上的次序即為其檢查時(shí)的生效次序
基于上述,規(guī)則優(yōu)化
1 安全放行所有入站和出站的狀態(tài)為ESTABLISHED狀態(tài)連接
2 謹(jǐn)慎放行入站的新請(qǐng)求
3 有特殊目的限制訪問功能,要在放行規(guī)則之前加以拒絕
4 同類規(guī)則(訪問同一應(yīng)用),匹配范圍小的放在前面,用于特殊處理
5 不同類的規(guī)則(訪問不同應(yīng)用),匹配范圍大的放在前面 例如:將一個(gè)網(wǎng)段的IP地址放在前面,包含在此網(wǎng)段的IP地址放在后面
6 應(yīng)該將那些可由一條規(guī)則能夠描述的多個(gè)規(guī)則合并為一條
7 設(shè)置默認(rèn)策略,建議白名單(只放行特定連接)
1) iptables -P,不建議
2) 建議在規(guī)則的最后定義規(guī)則做為默認(rèn)策略
規(guī)則有效期限:
使用iptables命令定義的規(guī)則,手動(dòng)刪除之前,其生效期限為kernel存活期限
保存規(guī)則:
保存規(guī)則至指定的文件
CentOS 7
(1)將防火墻規(guī)則保存到指定的文件中
[root@centos7~]#iptables-save > /data/iptables.rule 保存到data目錄下
[root@centos7~]#iptables -F 清空防火墻規(guī)則之后
[root@centos7~]#iptables-restore < /data/iptables.rule 從保存的文件中導(dǎo)出,即可恢復(fù)之前的防火墻策略
[root@centos7~]#iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
24 1792 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0
0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 LOG all -- * * 192.168.34.102 0.0.0.0/0 LOG flags 0 level 4 prefix "from 34.102 access:"
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes)
pkts bytes target prot opt in out source destination
(2)將本地開機(jī)啟動(dòng)加執(zhí)行權(quán)限,并將執(zhí)行的文件存在此配置文件中,開機(jī)啟動(dòng)即可
[root@centos7~]#chmod +x /etc/rc.d/rc.local 給開機(jī)啟動(dòng)的本地服務(wù)加上執(zhí)行權(quán)限 [root@centos7~]#vim /etc/rc.d/rc.local 修改本地開機(jī)配置文件信息
CentOS 6防火墻保存規(guī)則
service iptables save 將規(guī)則覆蓋保存至/etc/sysconfig/iptables文件中
然后設(shè)置為開機(jī)啟動(dòng)
chkconfig iptables on
?
總結(jié)
以上是生活随笔為你收集整理的linux防火墙扩展模块实战(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看到关于java资料比较全的,自己收藏
- 下一篇: 烟花转瞬即逝的唯美句子116个