如何在 Debian 11 上安装配置服务器防火墙 (CSF)
在此页
- 先决条件
- 开始
- 在 Debian 11 上安装 CSF
- 配置 CSF
- 基本 CSF 命令
- 结论
CSF 也称为 \Config Server Firewall\ 是 Linux 系统的免费高级防火墙。它带有一些高级安全功能,例如入侵、洪水和登录检测。它旨在防御多种攻击,例如端口扫描、SYN 泛洪和登录暴力攻击。它还为 cPanel、DirectAdmin 和 Webmin 提供集成。
本教程将解释 CSF 在 Debian 11 上的安装、基本配置和 CSF 的基本命令。
先决条件
- 运行 Debian 11 的服务器。
- 在服务器上配置了根密码。
入门
在开始之前,建议将您的系统包更新到更新版本。您可以使用以下命令执行此操作:
apt-get update -y
更新所有包后,使用以下命令安装其他所需的依赖项:
apt-get install sendmail dnsutils unzip libio-socket-ssl-perl libcrypt-ssleay-perl git perl iptables libnet-libidn-perl libio-socket-inet6-perl libsocket6-perl -y
在 Debian 11 上安装 CSF
默认情况下,CSF 包不包含在 Debian 11 默认存储库中。您需要从其官方网站下载它。
您可以使用以下命令下载最新版本的 CSF:
wget http://download.configserver.com/csf.tgz
下载完成后,使用以下命令解压缩下载的文件:
tar -xvzf csf.tgz
接下来,将目录更改为 CSF 并运行 install.sh 脚本以在您的服务器中安装 CSF。
cd csf
bash install.sh
安装 CSF 后,您应该得到以下输出:
Don't forget to:
1. Configure the following options in the csf configuration to suite your server: TCP_*, UDP_*
2. Restart csf and lfd
3. Set TESTING to 0 once you're happy with the firewall, lfd will not run until you do so
Adding current SSH session IP address to the csf whitelist in csf.allow:
*WARNING* URLGET set to use LWP but perl module is not installed, fallback to using CURL/WGET
Adding 106.222.22.32 to csf.allow only while in TESTING mode (not iptables ACCEPT)
*WARNING* TESTING mode is enabled - do not forget to disable it in the configuration
'lfd.service' -> '/usr/lib/systemd/system/lfd.service'
'csf.service' -> '/usr/lib/systemd/system/csf.service'
Created symlink /etc/systemd/system/multi-user.target.wants/csf.service → /lib/systemd/system/csf.service.
Created symlink /etc/systemd/system/multi-user.target.wants/lfd.service → /lib/systemd/system/lfd.service.
Failed to disable unit: Unit file firewalld.service does not exist.
Failed to stop firewalld.service: Unit firewalld.service not loaded.
Unit firewalld.service does not exist, proceeding anyway.
Created symlink /etc/systemd/system/firewalld.service → /dev/null.
'/etc/csf/csfwebmin.tgz' -> '/usr/local/csf/csfwebmin.tgz'
Installation Completed
安装后,使用以下命令验证 CSF 所需的 iptables 模块:
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
配置 CSF
接下来,您需要根据您的要求配置 CSF。您可以通过编辑 /etc/csf/csf.conf 文件来配置它。
nano /etc/csf/csf.conf
首先,找到 TESTING=\1\ 行,并将值更改为 \0\ 以启用 CSF:
TESTING = "0"
接下来,找到 RESTRICT_SYSLOG=\0\ 行,并将值更改为 3 以设置 syslog/rsyslog 文件的访问权限仅供 RESTRICT_SYSLOG_GROUP 的成员使用:
RESTRICT_SYSLOG = "3"
在以下行中添加所需的传入 TCP 打开端口:
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995"
在以下行中添加所需的传出 TCP 端口:
# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,443,587,993,995"
在以下行中添加所需的传入 UDP 打开端口:
# Allow incoming UDP ports
UDP_IN = "20,21,53,80,443"
在以下行中添加所需的传出 UDP 端口:
# Allow outgoing UDP ports
UDP_OUT = "20,21,53,113,123"
保存并关闭文件,然后重新加载 CSF 防火墙以应用更改:
csf -r
基本 CSF 命令
要停止 CSF 防火墙,请运行以下命令:
csf -s
要刷新 CSF 防火墙,请运行以下命令:
csf -f
要列出 CSF 添加的所有 IPTABLES 规则,请运行以下命令:
csf -l
要启动 CSF 并使其能够在系统重启时启动,请运行以下命令:
systemctl start csf
systemctl enable csf
要检查 CSF 防火墙的状态,请运行以下命令:
systemctl status csf
您应该得到以下输出:
? csf.service - ConfigServer Firewall & Security - csf
Loaded: loaded (/lib/systemd/system/csf.service; enabled; vendor preset: enabled)
Active: active (exited) since Sat 2021-09-18 15:42:04 UTC; 11s ago
Process: 8022 ExecStart=/usr/sbin/csf --initup (code=exited, status=0/SUCCESS)
Main PID: 8022 (code=exited, status=0/SUCCESS)
CPU: 705ms
Sep 18 15:42:04 debian11 csf[8022]: ACCEPT all opt in * out lo ::/0 -> ::/0
Sep 18 15:42:04 debian11 csf[8022]: LOGDROPOUT all opt in * out !lo ::/0 -> ::/0
Sep 18 15:42:04 debian11 csf[8022]: LOGDROPIN all opt in !lo out * ::/0 -> ::/0
Sep 18 15:42:04 debian11 csf[8022]: csf: FASTSTART loading DNS (IPv4)
Sep 18 15:42:04 debian11 csf[8022]: csf: FASTSTART loading DNS (IPv6)
Sep 18 15:42:04 debian11 csf[8022]: LOCALOUTPUT all opt -- in * out !lo 0.0.0.0/0 -> 0.0.0.0/0
Sep 18 15:42:04 debian11 csf[8022]: LOCALINPUT all opt -- in !lo out * 0.0.0.0/0 -> 0.0.0.0/0
Sep 18 15:42:04 debian11 csf[8022]: LOCALOUTPUT all opt in * out !lo ::/0 -> ::/0
Sep 18 15:42:04 debian11 csf[8022]: LOCALINPUT all opt in !lo out * ::/0 -> ::/0
Sep 18 15:42:04 debian11 systemd[1]: Finished ConfigServer Firewall & Security - csf.
要通过 IP 地址允许特定主机,请运行以下命令:
csf -a 192.168.100.10
要通过 IP 地址拒绝特定主机,请运行以下命令:
csf -d 192.168.100.11
要从允许列表中删除 IP,请运行以下命令:
csf -ar 192.168.100.10
要从拒绝列表中删除 IP,请运行以下命令:
csf -dr 192.168.100.11
您可以通过编辑 /etc/csf/csf.allow 文件来添加受信任的 IP:
nano /etc/csf/csf.allow
添加您信任的 IP:
192.168.100.10
您可以通过编辑 /etc/csf/csf.deny 文件来添加不受信任的 IP:
nano /etc/csf/csf.deny
添加不受信任的 IP:
192.168.100.11
结论
在上面的指南中,我们解释了如何在 Debian 11 上安装 CSF 防火墙。我们还向您展示了一些基本的 CSF 命令来管理您的流量。有关详细信息,请访问 CSF 文档。