在 RHEL/CentOS 8/7 上安装和使用 CSF 防火墙
如何在 RHEL/CentOS 8 上安装和使用 CSF 防火墙?安装 RHEL/CentOS 8 服务器后要做的第一件事是配置防火墙并强化服务器以防止任何形式的恶意访问。基于 RHEL 的系统中最流行的防火墙服务是 Firewalld。 ConfigServer 安全和防火墙 (CSF) 是一款功能强大的开源状态数据包检测 (SPI) 防火墙、适用于 Linux 服务器的登录/入侵检测和安全应用程序。
脑脊液的特点
以下是 ConfigServer 安全和防火墙的主要功能。
- 直接的 SPI iptables 防火墙脚本
- 检查 openSSH 登录身份验证失败、Mod_security 失败、htpasswd、ftp、imap 等的守护进程进程。
- 过多的连接阻塞
- BOGON 数据包保护
- 目录和文件监视
- SYN洪水保护
- IDS(入侵检测系统)——最后一行检测会提醒您系统和应用程序二进制文件的更改
- 死亡保护 Ping
- 端口扫描跟踪和阻止
- 端口泛洪检测 – 每个 IP、每个端口连接泛洪检测和缓解,以帮助阻止 DOS 攻击
- Cpanel、Webmin 和 DirectAdmin 的 UI 集成
- 分布式登录失败攻击检测
- ip6tables 的 IPv6 支持
- 集成 UI – 无需单独的控制面板或 Apache 即可使用 csf 配置
- 与 CloudFlare 防火墙集成
- 还有更多
在 RHEL/CentOS 8 上安装 CSF 防火墙
如果系统没有运行关键应用程序,您可以升级已安装的软件包并重新启动。
sudo dnf -y update
完成后,安装 Perl。
sudo dnf -y install @perl
检查 Perl 版本。
$ perl -v
This is perl 5, version 26, subversion 2 (v5.26.2) built for x86_64-linux-thread-multi
(with 52 registered patches, see perl -V for more detail)
Copyright 1987-2018, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
CSF 使用自动化脚本安装。使用curl命令下载它。
curl -SL https://download.configserver.com/csf.tgz | tar -xzf -
导航到创建的文件夹 - csf :
cd csf
运行安装程序。
sudo sh install.sh
请参阅下面的示例输出。
测试安装
$ sudo perl /usr/local/csf/bin/csftest.pl
Testing ip_tables/iptable_filter…OK
Testing ipt_LOG…OK
Testing ipt_multiport/xt_multiport…OK
Testing ipt_REJECT…OK
Testing ipt_state/xt_state…OK
Testing ipt_limit/xt_limit…OK
Testing ipt_recent…OK
Testing xt_connlimit…OK
Testing ipt_owner/xt_owner…OK
Testing iptable_nat/ipt_REDIRECT…OK
Testing iptable_nat/ipt_DNAT…OK
RESULT: csf should function on this server
在 RHEL/CentOS 8 上配置并启动 CSF
现在我们已经在 RHEL/CentOS 8 上安装了 CSF。主要配置文件是 /etc/csf/csf.conf
。修改此文件以调整您的防火墙规则和策略。请参阅以下部分了解允许的 TCP 和 UDP 端口。
禁用测试。
sudo perl -pi -w -e "s/TESTING = \"1\"/TESTING = \"0\"/" /etc/csf/csf.conf
忽略允许列表中的 IP 地址。
sudo perl -pi -w -e "s/IGNORE_ALLOW = \"0\"/IGNORE_ALLOW = \"1\"/" /etc/csf/csf.conf
安装程序将为您添加 systemd 服务文件。您可以通过运行来启动它。
sudo systemctl enable --now csf
确认服务状态。
$ sudo systemctl status csf
● csf.service - ConfigServer Firewall & Security - csf
Loaded: loaded (/usr/lib/systemd/system/csf.service; enabled; vendor preset: disabled)
Active: active (exited) since Sun 2019-03-17 09:10:19 EAT; 10h ago
Main PID: 783 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 11510)
Memory: 0B
CGroup: /system.slice/csf.service
Mar 17 09:10:19 rhel8.local csf[783]: ACCEPT all opt in * out lo ::/0 -> ::/0
Mar 17 09:10:19 rhel8.local csf[783]: LOGDROPOUT all opt in * out !lo ::/0 -> ::/0
Mar 17 09:10:19 rhel8.local csf[783]: LOGDROPIN all opt in !lo out * ::/0 -> ::/0
Mar 17 09:10:19 rhel8.local csf[783]: csf: FASTSTART loading DNS (IPv4)
Mar 17 09:10:19 rhel8.local csf[783]: csf: FASTSTART loading DNS (IPv6)
Mar 17 09:10:19 rhel8.local csf[783]: LOCALOUTPUT all opt -- in * out !lo 0.0.0.0/0 -> 0.0.0.0/0
Mar 17 09:10:19 rhel8.local csf[783]: LOCALINPUT all opt -- in !lo out * 0.0.0.0/0 -> 0.0.0.0/0
Mar 17 09:10:19 rhel8.local csf[783]: LOCALOUTPUT all opt in * out !lo ::/0 -> ::/0
Mar 17 09:10:19 rhel8.local csf[783]: LOCALINPUT all opt in !lo out * ::/0 -> ::/0
Mar 17 09:10:19 rhel8.local systemd[1]: Started ConfigServer Firewall & Security - csf.
CSF 用法示例
阻止 IP 或子网
sudo csf -d 192.168.0.20
sudo csf -d 192.168.0.0/24
示例输出:
Adding 192.168.0.20 to csf.deny and iptables DROP…
DROP all opt -- in !lo out * 192.168.0.20 -> 0.0.0.0/0
LOGDROPOUT all opt -- in * out !lo 0.0.0.0/0 -> 192.168.0.20
从阻止列表中删除 IP/子网。
$ sudo csf -dr 192.168.0.20
Removing rule…
DROP all opt -- in !lo out * 192.168.0.20 -> 0.0.0.0/0
LOGDROPOUT all opt -- in * out !lo 0.0.0.0/0 -> 192.168.0.20
在防火墙上允许 IP 地址。
$ sudo csf -a 192.168.15.15
Adding 192.168.15.15 to csf.allow and iptables ACCEPT…
ACCEPT all opt -- in !lo out * 192.168.15.15 -> 0.0.0.0/0
ACCEPT all opt -- in * out !lo 0.0.0.0/0 -> 192.168.15.15
屏蔽某些国家的
编辑行CC_DENY
# Each option is a comma separated list of CC's, e.g. "US,GB,DE"
CC_DENY = ""
CC_ALLOW = ""
查看侦听连接的端口。
$ sudo csf -p
Ports listening for external connections and the executables running behind them:
Port/Proto Open Conn PID/User Command Line Executable
22/tcp 4/6 4 (789/root) /usr/sbin/sshd -D -oCiphers=aes256-g... /usr/sbin/sshd
80/tcp 4/6 - (2580/root) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
80/tcp 4/6 - (2583/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
80/tcp 4/6 - (2584/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
80/tcp 4/6 - (2585/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
80/tcp 4/6 - (2804/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
443/tcp 4/6 - (2580/root) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
443/tcp 4/6 - (2583/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
443/tcp 4/6 - (2584/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
443/tcp 4/6 - (2585/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
443/tcp 4/6 - (2804/apache) /usr/sbin/httpd -DFOREGROUND /usr/sbin/httpd
3306/tcp -/- - (2474/mysql) /usr/libexec/mysqld --basedir=/usr /usr/libexec/mysqld
68/udp -/- - (761/root) /usr/sbin/NetworkManager --no-daemon /usr/sbin/NetworkManager
161/udp -/- - (3129/root) /usr/sbin/snmpd -LS0-6d -f /usr/sbin/snmpd
323/udp -/- - (768/chrony) /usr/sbin/chronyd /usr/sbin/chronyd
当用户通过 ssh 登录时发送电子邮件
LF_SSH_EMAIL_ALERT = "1"
设置警报电子邮件
LF_ALERT_TO = "[email "
向自己发送有关安全检查的电子邮件。
csf -m [email
访问 CSF Web UI
CSF 集成了可用于配置的 Web UI。在 /etc/csf/csf.conf
文件上启用 UI。
UI = "1"
您还可以设置自定义值:
# Set this to the port that want to bind this service to
UI_PORT = "6666"
# Leave blank to bind to all IP addresses on the server
UI_IP = ""
UI_USER = "admin"
UI_PASS = "StrongAdminPassword"
然后将IP地址添加到白名单。
sudo echo "YOUR_IP_ADDRESS" >> /etc/csf/ui/ui.allow
启动或重新启动 lfd 服务
$ sudo systemctl enable --now lfd
$ systemctl status lfd
● lfd.service - ConfigServer Firewall & Security - lfd
Loaded: loaded (/usr/lib/systemd/system/lfd.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-03-17 20:05:10 EAT; 33s ago
Process: 21213 ExecStart=/usr/sbin/lfd (code=exited, status=0/SUCCESS)
Main PID: 21226 (lfd - sleeping)
Tasks: 1 (limit: 11510)
Memory: 392.1M
CGroup: /system.slice/lfd.service
└─21226 lfd - sleeping >
Mar 17 20:05:10 rhel8.local systemd[1]: Starting ConfigServer Firewall & Security - lfd…
Mar 17 20:05:10 rhel8.local systemd[1]: Started ConfigServer Firewall & Security - lfd.
在配置的端口上访问 CSF 防火墙。
另请检查:
如何在 RHEL/CentOS 8 上安装和配置 AIDE 基于主机的 IDS
如何安装和配置 OPNSense 防火墙
Algo VPN – 在云端设置个人 IPSEC VPN