Iptables是Linux操作系統(tǒng)常用的防火墻軟件台舱,用于服務(wù)器的訪問控制颅湘。Centos7默認(rèn)的防火墻是Firewalle适贸。個人理解兩者都是對規(guī)則的配置馆蠕,F(xiàn)irewalle只是在Iptables基礎(chǔ)上進行封裝,讓操作更簡單是尔。內(nèi)核netfilter執(zhí)行規(guī)則殉了。
本教程使用于 RHEL 7 / CentOS 7,首選查看操作系統(tǒng)的版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]#
本次測試用到的虛擬機地址:192.168.56.5;192.168.56.5;192.168.56.6
1 安裝iptables
首先檢查是否安裝iptables拟枚,如果已經(jīng)安裝薪铜,或者版本比Centos7低,請略過安裝Iptables的步驟恩溅,直接看使用和配置隔箍。
(1)檢查是否安裝iptables
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.
[root@localhost ~]#
(2)安裝前的準(zhǔn)備
#停止firewalle
[root@localhost ~]# systemctl stop firewalld
#禁用firewalld服務(wù)
[root@localhost ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@localhost ~]#
1.1 yum安裝
如果目標(biāo)服務(wù)器能夠聯(lián)網(wǎng),可選擇yum的方式安裝脚乡。
#yum安裝iptables
[root@localhost ~]# yum install iptables-services
已加載插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
(1/4): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 200 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.0 MB 00:00:02
(4/4): 3/3
……省略中間安裝日志……
已安裝:
iptables-services.x86_64 0:1.4.21-28.el7
作為依賴被升級:
iptables.x86_64 0:1.4.21-28.el7
完畢蜒滩!
[root@localhost ~]#
(2)安裝后驗證
#查看是否安裝成功
[root@springboot sysconfig]# rpm -qa |grep iptables
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@springboot sysconfig]#
1.2 手動安裝
如果目標(biāo)服務(wù)器不能聯(lián)網(wǎng),請手工下載安裝包奶稠。下載地址
(1) iptables-1.4.21-28.el7.x86_64下載鏈接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-1.4.21-28.el7.x86_64.rpm]
(2) iptables-services-1.4.21-28.el7.x86_64.rpm下載鏈接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-services-1.4.21-28.el7.x86_64.rpm]
(1) 安裝前的操作
#創(chuàng)建目錄
[root@ZabbixServer ~]# cd /usr
[root@ZabbixServer usr]# mkdir iptables
#將本地下載的安裝包上傳到目標(biāo)服務(wù)器
[root@ZabbixServer iptables]# ll
總用量 488
-rw-r--r-- 1 root root 442964 5月 29 18:26 iptables-1.4.21-28.el7.x86_64.rpm
-rw-r--r-- 1 root root 53128 5月 29 18:26 iptables-services-1.4.21-28.el7.x86_64.rpm
#為安裝添加執(zhí)行權(quán)限
[root@ZabbixServer iptables]# chmod +x iptables-1.4.21-28.el7.x86_64.rpm
[root@ZabbixServer iptables]# chmod +x iptables-services-1.4.21-28.el7.x86_64.rpm
(2) 安裝iptables
#開始安裝
[root@ZabbixServer iptables]# rpm -Uvh iptables-1.4.21-28.el7.x86_64.rpm
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:iptables-1.4.21-28.el7 ################################# [ 50%]
正在清理/刪除...
2:iptables-1.4.21-17.el7 ################################# [100%]
[root@ZabbixServer iptables]# rpm -Uvh iptables-services-1.4.21-28.el7.x86_64.rpm
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:iptables-services-1.4.21-28.el7 ################################# [100%]
(3) 安裝后驗證
#驗證是否安裝成功
[root@ZabbixServer iptables]# rpm -qa |grep iptables
iptables-services-1.4.21-28.el7.x86_64
iptables-1.4.21-28.el7.x86_64
[root@ZabbixServer iptables]#
#查看狀態(tài)俯艰,Active: inactive (dead)為未啟動
[root@ZabbixServer iptables]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@ZabbixServer iptables]#
2 啟動iptables
(1) 設(shè)置開機啟動
[root@localhost ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@localhost ~]#
(2)啟動iptables
#可用命令:systemctl [stop|start|restart] iptables
[root@localhost ~]# systemctl start iptables
(3)查看狀態(tài)
#Active: active (exited)表示已經(jīng)啟動
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since 三 2019-05-29 15:06:27 CST; 6s ago
Process: 2409 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 2409 (code=exited, status=0/SUCCESS)
5月 29 15:06:27 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
5月 29 15:06:27 localhost.localdomain iptables.init[2409]: iptables: Applying firewall rules: [ 確定 ]
5月 29 15:06:27 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
[root@localhost ~]#
3 設(shè)置白名單
3.1 查看默認(rèn)規(guī)則
#查看iptables現(xiàn)有規(guī)則
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#
3.2 設(shè)置白名單
iptables配置文件目錄/etc/sysconfig/iptables。由于本次安全加固生產(chǎn)環(huán)境服務(wù)器較多窒典,所以定義腳本蟆炊,執(zhí)行腳本使策略生效。運維的同學(xué)可在目標(biāo)服務(wù)器上做如下操作:
(1) 新建腳本文件瀑志,命令:vi /opt/iptables.sh
(2) 拷貝下面的腳本內(nèi)容到/opt/iptables.sh
(3) 執(zhí)行腳本:sh /opt/iptables.sh
需修改腳本中開放的端口和白名單地址
腳本內(nèi)容:
#腳本名稱:iptables.sh涩搓,腳本內(nèi)容如下:
#!/bin/sh
iptables -P INPUT ACCEPT
#清空所有默認(rèn)規(guī)則
iptables -F
#清空所有自定義規(guī)則
iptables -X
#所有計數(shù)器歸0
iptables -Z
#定義白名單
iptables -N whitelist
#設(shè)置白名單的ip或ip段
iptables -A whitelist -s 192.168.56.3 -j ACCEPT
#iptables -A whitelist -s 192.168.56.0 -j ACCEPT
#重新設(shè)置iptables
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#只有白名單才能訪問,測試開發(fā)了22劈猪、8010昧甘,其它應(yīng)用根據(jù)需求添加即可。
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j whitelist
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8010 -j whitelist
iptables -A INPUT -p icmp -j whitelist
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
#配置寫入文件战得,重啟也生效充边。
service iptables save
#重啟iptables服務(wù)
systemctl restart iptables.service
3.3 查看規(guī)則
再次查看配置是否已經(jīng)生效,whitelist為白名單,下面的配置表示icmp浇冰、tcp的22贬媒、8010端口只能白名單中192.168.56.3訪問。
[root@springboot sysconfig]# iptables -L -n
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
whitelist tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
whitelist tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8010
whitelist icmp -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain whitelist (3 references)
target prot opt source destination
ACCEPT all -- 192.168.56.3 0.0.0.0/0
[root@springboot sysconfig]#
4 測試白名單
目標(biāo)機器:192.168.56.5【白名單生效的機器】
源機器1:192.168.56.3【在白名單中】
源機器2:192.168.56.6【不在白名單中】
4.1 測試ICMP
(1)在源機器1上可以ping通肘习,表示icmp配置生效际乘。
PS C:\WINDOWS\system32> ping 192.168.56.5
正在 Ping 192.168.56.5 具有 32 字節(jié)的數(shù)據(jù):
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
192.168.56.5 的 Ping 統(tǒng)計信息:
數(shù)據(jù)包: 已發(fā)送 = 3,已接收 = 3漂佩,丟失 = 0 (0% 丟失)脖含,
往返行程的估計時間(以毫秒為單位):
最短 = 0ms,最長 = 0ms投蝉,平均 = 0ms
Control-C
PS C:\WINDOWS\system32>
(2)在源機器2上不能ping通养葵,表示icmp配置生效。
[root@appos /]# ping 192.168.56.5
PING 192.168.56.5 (192.168.56.5) 56(84) bytes of data.
4.2 測試TCP
目標(biāo)機器192.168.56.5部署了REST測試接口瘩缆、端口是8010关拒。其它應(yīng)用根據(jù)需求添加即可。
(1)在源機器1的訪問接口咳榜,可以返回數(shù)據(jù)夏醉,表示tcp配置生效爽锥。
(2)在源機器2的訪問接口涌韩,不能返回數(shù)據(jù),表示tcp配置生效氯夷。
[root@appos /]# curl 192.168.56.5:8010/speech/test
執(zhí)行后沒有反應(yīng)臣樱。