本页内容

  • 前提条件
  • 开始使用
  • 在 Debian 11 上安装 CSF
  • 配置 CSF
  • CSF 基本命令
  • 结论

CSF 也称为“Config Server Firewall”,是一款免费且高级的 Linux 系统防火墙。它带有一些高级安全功能,例如入侵检测、洪水攻击检测和登录检测。它旨在抵御多种攻击,例如端口扫描、SYN 洪水攻击和登录暴力破解攻击。它还提供与 cPanel、DirectAdmin 和 Webmin 的集成。

本教程将讲解 Debian 11 上 CSF 的安装、基本配置以及 CSF 的基本命令。

前提条件

  • 一台运行 Debian 11 的服务器。
  • 服务器上已配置 root 密码。

开始使用

在开始之前,建议将系统软件包更新到最新版本。您可以使用以下命令来完成:

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 文档。