centos7防火墻如何設置只對部分端口號限源
centos7防火墻設置只對部分端口號限源
項目上線一段時候,安全測評整改的需要,需對特定一些端口進行限源。
其他端口不做限制
iptables與firewalld的區(qū)別
1),firewalld可以動態(tài)修改單條規(guī)則,動態(tài)管理規(guī)則集,允許更新規(guī)則而不破壞現(xiàn)有會話和連接。而iptables,在修改了規(guī)則后必須得全部刷新才可以生效;
2),firewalld使用區(qū)域和服務而不是鏈式規(guī)則;
3),firewalld默認是拒絕的,需要設置以后才能放行。而iptables默認是允許的,需要拒絕的才去限制;
4),firewalld自身并不具備防火墻的功能,而是和iptables一樣需要通過內(nèi)核的netfilter來實現(xiàn)。也就是說,firewalld和iptables一樣,它們的作用都用于維護規(guī)則,而真正使用規(guī)則干活的是內(nèi)核的netfilter。只不過
- firewalld和iptables的結(jié)果以及使用方法不一樣!
- firewalld是iptables的一個封裝,可以讓你更容易地管理iptables規(guī)則。它并不是iptables的替代品,雖然iptables命令仍可用于firewalld,但建議firewalld時僅使用firewalld命令。
一、安裝iptable
1.關閉默認的firewall防火墻
systemctl stop firewalld.service 關閉防火墻 systemctl disable firewalld.service 關閉開機啟動
2.開啟iptables
yum install iptables (根據(jù)centOS7的版本和內(nèi)核,有些版本已經(jīng)裝過,可以跳過此命令) yum install iptables-services
3.基本操作
查看防火墻狀態(tài)
查看防火墻狀態(tài) service iptables status 停止防火墻 service iptables stop 啟動防火墻 service iptables start 重啟防火墻 service iptables restart 永久關閉防火墻 chkconfig iptables off 永久關閉后重啟 chkconfig iptables on 開機自啟 systemctl enable iptables.service
二、設置規(guī)則
表示清空所有默認規(guī)則。
iptables -F
設置指定IP訪問指定端口8075
1、添加規(guī)則:禁止所有IP訪問8075
iptables -I INPUT -p tcp --dport 8075 -j DROP
查看規(guī)則
iptables --line -nvL INPUT
添加規(guī)則:允許127.0.0.1訪問8075
iptables -I INPUT -s 127.0.0.1 -p tcp --dport 8075 -j ACCEPT
規(guī)則已經(jīng)添加,測試
telnet 具體ip 8075
保存規(guī)則
service iptables save
三、特定url限源
示例添加swagger-相關限制
iptables -I INPUT -p tcp -m string --string "swagger-" --algo bm -j DROP iptables -I INPUT -s 10.0.120.13 -p tcp -m string --string "swagger-" --algo bm -j ACCEPT
查詢數(shù)據(jù)庫中的數(shù)據(jù)也可能包含"swagger-" 也會直接攔截,對數(shù)據(jù)庫等存儲也需要添加放行規(guī)則
開放源
iptables -I INPUT -s 某某ip -j ACCEPT
iptables 導入導出
導出 iptables-save > iptables_bak 導入 iptables-restore < iptables_bak
iptables 設置特定IP訪問指定端口
一、添加規(guī)則
設置禁止所有IP訪問指定端口8075
[root@zabbix_server ~]# iptables -I INPUT -p tcp --dport 8075 -j DROP
二、測試telnet
[root@zabbix_server ~]# telnet 127.0.0.1 8075 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection timed out
三、刪除規(guī)則
1、查詢規(guī)則編號
[root@zabbix_server ~]# iptables --line -nvL INPUT Chain INPUT (policy DROP 83 packets, 4016 bytes) num pkts bytes target prot opt in out source destination 1 8 408 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8075 2 144M 15G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 4037 214K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 4 3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25601 5 4085 218K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 6 22638 1169K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 7 264K 14M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9000 8 443K 23M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10050 9 76134 4093K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10051
可以看到禁止訪問8075的規(guī)則編號為1
2、刪除指定規(guī)則編號的規(guī)則
[root@zabbix_server ~]# iptables -D INPUT 1
再查詢
[root@zabbix_server ~]# iptables --line -nvL INPUT Chain INPUT (policy DROP 20 packets, 961 bytes) num pkts bytes target prot opt in out source destination 1 144M 15G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 4038 214K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25601 4 4087 218K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 5 22644 1169K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 6 264K 14M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9000 7 443K 23M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10050 8 76156 4094K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10051 9 44 2208 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dp
已經(jīng)刪除了,測試telnet
[root@zabbix_server ~]# telnet 127.0.0.1 8075 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.
四、設置指定IP訪問指定端口8075
1、添加規(guī)則:禁止所有IP訪問8075
[root@zabbix_server ~]# iptables -I INPUT -p tcp --dport 8075 -j DROP [root@zabbix_server ~]# iptables --line -nvL INPUT Chain INPUT (policy DROP 3 packets, 156 bytes) num pkts bytes target prot opt in out source destination 1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8075 2 145M 15G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 4038 214K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 4 3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25601 5 4090 219K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 6 22650 1169K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 7 264K 14M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9000 8 443K 23M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10050 9 76183 4095K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10051 10 44 2208 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3000 11 7 284 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:5672 12 2 80 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dp
2、添加規(guī)則:允許127.0.0.1訪問8075
[root@zabbix_server ~]# iptables -I INPUT -s 127.0.0.1 -p tcp --dport 8075 -j ACCEPT
3、查詢規(guī)則:
[root@zabbix_server ~]# iptables --line -nvL INPUT Chain INPUT (policy DROP 20 packets, 1004 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:8075 2 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8075 3 145M 15G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 4 4039 214K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 5 3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25601 6 4096 219K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 7 22660 1170K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306 8 264K 14M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:9000 9 443K 23M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10050
規(guī)則已經(jīng)添加,測試
[root@zabbix_server ~]# telnet 127.0.0.1 8075 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.
本機可以訪問8075,其他機器上不能訪問8075
[root@localhost etc]# telnet 172.28.18.75 8075 Trying 172.28.18.75... telnet: connect to address 172.28.18.75: Connection timed out
4、允許172.28.18.71可以訪問8075,(172.28.18.71是需要訪問8075的服務器)
[root@zabbix_server ~]# iptables -I INPUT -s 172.28.18.71 -p tcp --dport 8075 -j ACCEPT
查看規(guī)則
[root@zabbix_server ~]# iptables --line -nvL INPUT Chain INPUT (policy DROP 9 packets, 456 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 172.28.18.71 0.0.0.0/0 tcp dpt:8075 2 3 132 ACCEPT tcp -- * * 127.0.0.1 0.0.0.0/0 tcp dpt:8075 3 7 420 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8075 4 145M 15G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 5 4040 214K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 6 3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25601 7 4100 219K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80 8 22674 1171K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306
在172.28.18.71上測試telnet 8075
[root@localhost etc]# telnet 172.28.18.75 8075 Trying 172.28.18.75... Connected to 172.28.18.75. Escape character is '^]'.
訪問成功,保存規(guī)則
[root@zabbix_server ~]# service iptables save iptables:將防火墻規(guī)則保存到 /etc/sysconfig/iptables:[確定]
重啟服務
[root@zabbix_server ~]# service iptables save iptables:將防火墻規(guī)則保存到 /etc/sysconfig/iptables:[確定] [root@zabbix_server ~]# service iptables restart iptables:將鏈設置為政策 ACCEPT:filter [確定] iptables:清除防火墻規(guī)則:[確定] iptables:正在卸載模塊:[確定] iptables:應用防火墻規(guī)則:[確定]
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Apache服務器VirtualHost常用配置小結(jié)
Apache服務器中的VirtualHost用來定義虛擬主機,本文主要介紹了Apache服務器VirtualHost常用配置小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-07-07