linux防火墙扩展模块实战(二)
iptables擴展模塊
擴展匹配條件:需要加載擴展模塊(/usr/lib64/xtables/*.so),方可生效
查看幫助 man iptables-extensions
(1)隱式擴展:在使用-p選項指明了特定的協(xié)議時,無需再用-m選項指明擴展模塊的擴展機制,不需要手動加載擴展模塊
tcp協(xié)議的擴展選項
--source-port, --sport port[:port]:匹配報文源端口,可為端口范圍 --destination-port,--dport port[:port]:匹配報文目標(biāo)端口,可為范圍 --tcp-flags mask comp
mask 需檢查的標(biāo)志位列表,用,分隔
例如 SYN,ACK,FIN,RST
comp 在mask列表中必須為1的標(biāo)志位列表,無指定則必須為0,用,分隔
演示:TCP協(xié)議的擴展選項
A主機:192.168.34.101
B主機:192.168.34.102
(1)在B主機上先新建一個網(wǎ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)此時在B主機進行控制其他機器的訪問
[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 拒絕其他所有主機訪問本機 [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)此時A主機無法訪問B主機
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
(4)此時只允許A主機訪問本機的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)查看此時A主機通過tcp協(xié)議就可以訪問B主機的httpd服務(wù)內(nèi)容
[root@centos7~]#curl 192.168.34.102 welcome to beijing
(6)在B主機將mysql數(shù)據(jù)庫允許A主機訪問
[root@centos777~]#iptables -I INPUT 3 -s 192.168.34.101 -p tcp --dport 3306 -j ACCEPT
(7)在B主機創(chuàng)建一個mysql賬號,驗證效果
[root@centos777~]#mysql -e "grant all on *.* to test@'192.168.34.%' identified by 'centos'"
(8)此時在A主機啟動自身的mysql數(shù)據(jù)庫,并能連接對方的mysql數(shù)據(jù)庫
[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é)議的擴展選項
示例:
--tcp-flags SYN,ACK,FIN,RST SYN 表示要檢查的標(biāo)志位為SYN,ACK,FIN,RST四個,其中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
示例:
只允許此時有tcp規(guī)則(握手)進行拒絕,但可以允許其他方式訪問
[root@centos777~]#iptables -I INPUT 4 -s 192.168.34.100 -p tcp --syn -j REJECT 拒絕C主機進行握手訪問 [root@centos777~]#iptables -I INPUT 5 -s 192.168.34.100 -j ACCEPT 允許C主機能訪問 [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
此時在C主機(192.168.34.100)進行訪問,此時通過握手協(xié)議訪問被拒絕
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
此時在C主機可以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擴展選項
[!] --source-port, --sport port[:port]:匹配報文的源端口或端口范圍 [!] --destination-port,--dport port[:port]:匹配報文的目標(biāo)端口或端口范圍
icmp擴展協(xié)議
[!] --icmp-type {type[/code]|typename}
type/code
0/0 echo-reply icmp應(yīng)答
8/0 echo-request icmp請求
實戰(zhàn)演練:實現(xiàn)本機ping對方可以通,對方不能ping通本機,或指定對方能ping通本機
(1)在本機進行修改防火墻策略
[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意思是本機ping對方是經(jīng)過INPUT,此時是應(yīng)答結(jié)果
(2)驗證效果,在本機進行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)在對方進行ping本機IP地址,此時無法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)將本機的icmp協(xié)議改為8,此時對方就可以ping通本機
[root@centos777~]#iptables -I INPUT 3 -p icmp --icmp-type 8 -j ACCEPT
(5)在對方機器進行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
顯式擴展:必須使用-m選項指明要調(diào)用的擴展模塊的擴展機制,要手動加載擴展模塊
[-m matchname [per-match-options]]
顯式擴展:必須顯式地指明使用的擴展模塊進行的擴展
使用幫助:
CentOS 6: man iptables
CentOS 7: man iptables-extensions
1、multiport擴展
以離散方式定義多端口匹配,最多指定15個端口
[!] --source-ports,--sports port[,port|,port:port]... 指定多個源端口 [!] --destination-ports,--dports port[,port|,port:port]... 指定多個目標(biāo)端口 [!] --ports port[,port|,port:port]...多個源或目標(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ù)并啟動
[root@centos777~]#yum install samba -y 安裝samba服務(wù) [root@centos777~]#systemctl start smb
(2)創(chuàng)建一個系統(tǒng)賬號并加入到samba服務(wù)中,成為samba賬號
[root@centos777~]#useradd -s /sbin/nologin smb1 ; smbpasswd -a smb1 New SMB password: Retype new SMB password: Added user smb1.
(3)創(chuàng)建防火墻規(guī)則,此時可以一次性指定兩個不連續(xù)的端口號,且都在一行顯示,方便管理
[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
此時在另外一臺主機就可以登錄samba服務(wù)
[root@centos7~]#smbclient //192.168.34.102/smb1 -U smb1%centos Try "help" to get a list of possible commands. smb: >
也可以在本機加入samba的UDP協(xié)議端口,由于兩個端口號連續(xù),不需要加multiport模塊
[root@centos777~]#iptables -I INPUT 4 -p udp --dport 137:138 -j ACCEPT
2、iprange擴展
指明連續(xù)的(但一般不是整個網(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擴展
指明源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
實戰(zhàn)演示:允許B主機通過MAC地址進行ping本機
A主機:192.168.34.102
B主機:192.168.34.101
(1)在A主機上設(shè)置B主機的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主機開始ping主機A,此時就可以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擴展
對報文中的應(yīng)用層數(shù)據(jù)做字符串模式匹配檢測
--algo {bm|kmp} 字符串匹配檢測算法
bm:Boyer-Moore
kmp:Knuth-Pratt-Morris
--from offset 開始偏移
--to offset 結(jié)束偏移
[!] --string pattern 要檢測的字符串模式
[!] --hex-string pattern要檢測字符串模式,16進制格式
示例:
iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT
實戰(zhàn)演練:不允許對方主機訪問google網(wǎng)頁
(1)在本機先新建幾個網(wǎng)頁
[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)然后對所有主機設(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)此時在對方主機進行訪問本機的網(wǎng)頁,此時就無法訪問google網(wǎng)頁
5、time擴展
根據(jù)將報文到達的時間與指定的時間范圍進行匹配
--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...] 星期幾,1 – 7 分別表示星期一到星期日 --kerneltz:內(nèi)核時區(qū),不建議使用,CentOS7系統(tǒng)默認為UTC,UTC時間與本地時間相差8小時。如:UTC時間是1點,實際時間是9點,centos6默認就是當(dāng)?shù)貢r間,不需要加8.
注意: centos6 不支持kerneltz ,--localtz指定本地時區(qū)(默認)
示例:
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
實戰(zhàn)演練:
(1)設(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個小時,才是本地的時間。 [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è)置的文件范圍內(nèi) Thu Dec 5 22:49:46 CST 2019
(2)其他主機訪問此主機的網(wǎng)頁是就會被拒絕
[root@centos7~]#curl 192.168.34.102 curl: (7) Failed connect to 192.168.34.102:80; Connection refused
6、connlimit擴展
根據(jù)每客戶端IP做并發(fā)連接數(shù)數(shù)量匹配
缺點:就是黑客用不同的IP地址進行訪問,就無法針對連接數(shù)進行阻擋。
可防止Dos(Denial of Service,拒絕服務(wù))攻擊 --connlimit-upto #:連接的數(shù)量小于等于#時匹配
--connlimit-above #:連接的數(shù)量大于#時匹配
通常分別與默認的拒絕或允許策略配合使用
示例:
iptables -A INPUT -d 172.16.100.10 -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
實戰(zhàn)演練:防止DOS攻擊,制定防火墻策略
(1)在本機設(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)此時在對方主機訪問本機小于100次連接的都可以訪問網(wǎng)頁
[root@centos7~]#curl 192.168.34.102 welcome to beijing
7、limit擴展
基于收發(fā)報文的速率做匹配
令牌桶過濾器
--limit #[/second|/minute|/hour|/day] --limit-burst number
實戰(zhàn)演練:
[root@centos777~]#iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 10 -j ACCEPT 接收規(guī)則,并允許前10個訪問網(wǎng)頁 [root@centos777~]#iptables -A INPUT -j REJECT 剩下的全部拒絕
8、state擴展
根據(jù)”連接追蹤機制“去檢查連接的狀態(tài),較耗資源
conntrack機制:追蹤本機上的請求和響應(yīng)之間的關(guān)系
狀態(tài)有如下幾種:
NEW:新發(fā)出請求;連接追蹤信息庫中不存在此連接的相關(guān)信息條目,因此,將其識別為第一次發(fā)出的請求 ESTABLISHED:NEW狀態(tài)之后,連接追蹤信息庫中為其建立的條目失效之前期間內(nèi)所進行的通信狀態(tài) RELATED:新發(fā)起的但與已有連接相關(guān)聯(lián)的連接,如:ftp協(xié)議中的數(shù)據(jù)連接與命令連接之間的關(guān)系 INVALID:無效的連接,如flag標(biāo)記不正確 UNTRACKED:未進行追蹤的連接,如raw表中關(guān)閉追蹤
示例:
老用戶通過ssh可以連接遠程主機
設(shè)置老用戶連接不被拒絕,但是老用戶通過ssh連接的主機退出后就無法連接,新用戶連接就被拒絕防火墻
[root@centos7~]#iptables -I INPUT 3 -p tcp --dport 22 -m state --state ESTABLISHED,RELATED -j ACCEPT
由于設(shè)置了防火墻,新用戶無法連接
[!] --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)追蹤到的并記錄下來的連接信息庫
/proc/net/nf_conntrack
調(diào)整連接追蹤功能所能夠容納的最大連接數(shù)量
/proc/sys/net/nf_conntrack_max
不同的協(xié)議的連接追蹤時長
/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 臨時修改到88888
修改完配置文件之后,使配置文件生效:
[root@centos7~]#sysctl -p net.nf_conntrack_max = 88888
iptables的鏈接跟蹤表最大容量為/proc/sys/net/nf_conntrack_max,各種狀態(tài)的超時鏈接會從表中刪除;當(dāng)模板滿載時,后續(xù)連接可能會超時
解決方法兩個:
(1) 加大nf_conntrack_max 值
vim /etc/sysctl.conf net.nf_conntrack_max = 393216 net.netfilter.nf_conntrack_max = 393216
(2) 降低 nf_conntrack timeout時間
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
開放被動模式的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) 放行請求報文:
命令連接: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)報文:
iptables -I OUTPUT -s LocalIP -p tcp -m state --state ESTABLISHED -j ACCEPT
實戰(zhàn)演示:開放被動模式的ftp服務(wù)
A主機:192.168.34.101
B主機:192.168.34.102
(1)在A主機先添加一個允許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主機安裝vsftpd服務(wù)并啟動服務(wù)
[root@centos7~]#yum install vsftpd -y [root@centos7~]#systemctl start vsftpd
(3)此時在B主機只能連接A主機的ftp服務(wù)器,被動模式的端口號是隨機的,A主機不能添加指定的tcp協(xié)議端口號,因此B主機不能執(zhí)行其他操作。
(4)在A主機加載ftp相關(guān)模塊,能識別FTP協(xié)議,能分析ftp21端口號的數(shù)據(jù)傳輸?shù)男畔ⅲ瑥亩軌虻弥麓瓮ㄓ嵾^程中被動模式使用的端口號是多少
[root@centos7~]#modprobe nf_conntrack_ftp
(5)在A主機添加一個iptables防火墻規(guī)則,注意:ESTABLISHED,RELATED和tcp 21協(xié)議的合理性,將tcp 21的防火墻規(guī)則放在后面較好,當(dāng)用戶訪問大量數(shù)據(jù)時,提高效率,優(yōu)化性能方面可以考慮。
[root@centos7~]#iptables -I INPUT 3 -m state --state ESTABLISHED,RELATED -j ACCEPT
(6)最后在B主機驗證連接ftp效果,此時就可以訪問文件
Target:
ACCEPT, DROP, REJECT, RETURN
LOG, SNAT, DNAT, REDIRECT, MASQUERADE,..
LOG:非中斷target,本身不拒絕和允許,放在拒絕和允許規(guī)則前
并將日志記錄在/var/log/messages系統(tǒng)日志中
--log-level level 級別: debug,info,notice, warning, error, crit, alert,emerg --log-prefix prefix 日志前綴,用于區(qū)別不同的日志,最多29個字符
收集指定的主機訪問本機的log日志
(1)在A主機配置一個防火墻規(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主機訪問當(dāng)前的信息,就會在系統(tǒng)日志中記錄來自于B主機的信息
(3)在A主機進行l(wèi)og日志跟蹤,可以看到跟蹤的日志信息
iptables防火墻規(guī)則總結(jié)
任何不允許的訪問,應(yīng)該在請求到達時給予拒絕
規(guī)則在鏈接上的次序即為其檢查時的生效次序
基于上述,規(guī)則優(yōu)化
1 安全放行所有入站和出站的狀態(tài)為ESTABLISHED狀態(tài)連接
2 謹慎放行入站的新請求
3 有特殊目的限制訪問功能,要在放行規(guī)則之前加以拒絕
4 同類規(guī)則(訪問同一應(yīng)用),匹配范圍小的放在前面,用于特殊處理
5 不同類的規(guī)則(訪問不同應(yīng)用),匹配范圍大的放在前面 例如:將一個網(wǎng)段的IP地址放在前面,包含在此網(wǎng)段的IP地址放在后面
6 應(yīng)該將那些可由一條規(guī)則能夠描述的多個規(guī)則合并為一條
7 設(shè)置默認策略,建議白名單(只放行特定連接)
1) iptables -P,不建議
2) 建議在規(guī)則的最后定義規(guī)則做為默認策略
規(guī)則有效期限:
使用iptables命令定義的規(guī)則,手動刪除之前,其生效期限為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)將本地開機啟動加執(zhí)行權(quán)限,并將執(zhí)行的文件存在此配置文件中,開機啟動即可
[root@centos7~]#chmod +x /etc/rc.d/rc.local 給開機啟動的本地服務(wù)加上執(zhí)行權(quán)限 [root@centos7~]#vim /etc/rc.d/rc.local 修改本地開機配置文件信息
CentOS 6防火墻保存規(guī)則
service iptables save 將規(guī)則覆蓋保存至/etc/sysconfig/iptables文件中
然后設(shè)置為開機啟動
chkconfig iptables on
?
總結(jié)
以上是生活随笔為你收集整理的linux防火墙扩展模块实战(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看到关于java资料比较全的,自己收藏
- 下一篇: Android 中基本图像绘制