如何在 Debian 12上安装 CSF (Config Server Firewall) ?

Config-Server-Firewall

CSF (Config Server Firewall) 是一款基于 IPtables 和 Perl 的 SPI (Stateful
Packet Inspection) 防火墙。它提供了一个守护进程,将监视您的服务的失败身份验证,还提供 WebUI 管理界面。

在本教程中,我们将在 Debian 12 服务器上安装 CSF,学习 CSF 的基本配置,使用两种不同的方法阻止 IP 地址,以及设置 CSF Web UI 以方便管理和监控。

更新系统

在安装 CSF 之前,必须确保安装了依赖项,这包括 Perl 和 iptables 等包。

sudo apt update

更新存储库后,使用下面的命令为 CSF 安装以下依赖项。

sudo apt install libio-socket-inet6-perl libsocket6-perl sendmail dnsutils unzip libio-socket-ssl-perl libcrypt-ssleay-perl git perl iptables libnet-libidn-perl libwww-perl liblwp-protocol-https-perl libgd-graph-perl

当系统上运行了其它防火墙 (比如UFW) 时,您需要禁用它。

sudo ufw disable

安装 CSF

使用下面的 wget 命令下载 CSF 源代码。

wget http://download.configserver.com/csf.tgz

使用如下命令解压下载的文件

sudo tar -xvzf csf.tgz

切换到 csf 目录,执行安装脚本

cd csf; sh install.sh

安装完成后,使用下面的命令验证。

perl /usr/local/csf/bin/csftest.pl

确保每个特性测试的输出是 OK

最后,执行以下命令验证 CSF 的位置和版本。

which csf
csf -v

配置 CSF

使用以下 nano 编辑器打开 CSF 配置。

sudo nano /etc/csf/csf.conf

允许流量通过 CSF

找到 TCPUDP 选项并添加端口。

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,853,80,110,143,443,465,587,993,995"

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,853,80,110,113,443,587,993,995"

# Allow incoming UDP ports
UDP_IN = "20,21,53,853,80,443"

# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list
UDP_OUT = "20,21,53,853,113,123"

Allow/Deny Ping or ICMP 请求

如果您确实需要禁用 Ping 或 ICMP ‘IN and OUT’,请使用以下选项。ICMP_IN = 1 表示允许 ping, ICMP_OUT = 1 表示服务器可以 ping 到另一个网络。

# Allow incoming PING. Disabling PING will likely break external uptime
# monitoring
ICMP_IN = "1"
...
# Allow outgoing PING
#
# Unless there is a specific reason, this option should NOT be disabled as it
# could break OS functionality
ICMP_OUT = "1"

Synflood Protection

仅在需要时启用 此选项,例如针对服务器上的 DOS 攻击。

###############################################################################
# SECTION:Port Flood Settings
###############################################################################
# Enable SYN Flood Protection. This option configures iptables to offer some
# protection from tcp SYN packet DOS attempts. 
...
SYNFLOOD = "0"
SYNFLOOD_RATE = "100/s"
SYNFLOOD_BURST = "150"

限制并发连接

接下来,使用 CONNLIMIT 选项来限制特定端口的并发连接。格式为: PORT;LIMIT,例如, 22;5 将限制 SSH 端口仅为 5 个并发连接。

# Connection Limit Protection. This option configures iptables to offer more
# protection from DOS attacks against specific ports. It can also be used as a
# way to simply limit resource usage by IP address to specific server services.
# Note: Run /etc/csf/csftest.pl to check whether this option will function on
# this server
CONNLIMIT = "22;5,21;10"

禁用测试模式 和 Syslog 访问限制

现在,如果您已经配置了所有内容并添加了端口,请将 TESTING 更改为 1,并使用 RESTRICT_SYSLOG = 3 来限制对 rsyslog 套接字的访问。

# lfd will not start while this is enabled
TESTING = "0"

...
# 0 = Allow those options listed above to be used and configured
# 1 = Disable all the options listed above and prevent them from being used
# 2 = Disable only alerts about this feature and do nothing else
# 3 = Restrict syslog/rsyslog access to RESTRICT_SYSLOG_GROUP ** RECOMMENDED **
RESTRICT_SYSLOG = "3"

完成所有操作后,保存文件并退出编辑器。

测试并启动 CSF 服务

运行下面的 csf 命令来验证您的配置。

csf -v

接下来,运行下面的 systemctl 命令启动 csf 和 lfd 服务。

sudo systemctl start csf lfd

一旦 csf 服务运行,您将自动与服务器断开连接。您可以再次登录服务器,然后使用下面的命令验证 csf 和 lfd 服务。

sudo systemctl status csf lfd

Blocking IP addresses via IP BLOCK Lists

使用 nano 编辑器打开默认的 blocklists

sudo nano /etc/csf/csf.blocklists

取消以下几行注释,以阻止来自 Spamhaus 数据库的 IP 地址。

# Spamhaus Don't Route Or Peer List (DROP)
# Details: http://www.spamhaus.org/drop/
SPAMDROP|86400|0|http://www.spamhaus.org/drop/drop.txt

# Spamhaus IPv6 Don't Route Or Peer List (DROPv6)
# Details: http://www.spamhaus.org/drop/
SPAMDROPV6|86400|0|https://www.spamhaus.org/drop/dropv6.txt

# Spamhaus Extended DROP List (EDROP)
# Details: http://www.spamhaus.org/drop/
SPAMEDROP|86400|0|http://www.spamhaus.org/drop/edrop.txt

下面是一些选项说明:

  • SPAMDROP : block的名称,大写字母,最多25个字符
  • 86400 : 下载和更新 blocklist IP 的刷新间隔
  • MAX : blocklist 中将使用的最大 IP 数,值为 0 表示将包括所有 ip
  • URL : 下载 blocklist IP 的 URL

保存文件并退出编辑器。

Blocking IP Addresses via GeoIP

另一种阻止 IP 地址的方法是通过 GeoIP,它允许您阻止来自特定国家的传入流量。

使用 nano 编辑器打开 CSF 配置

sudo nano /etc/csf/csf.conf

找到 CC_DENYCC_ALLOW 参数,设置允许和阻止的国家

# Each option is a comma-separated list of CC's, e.g. "US,GB,DE"
CC_DENY = "RU,CN"
CC_ALLOW = "US,GB,DE,NL,SG"

默认情况下,CSF 使用 db-ip、ipdeny 和 iptoasn 中的数据库 GeoIP,但是您也可以修改为其他 GeoIP 数据库。例如:要使用 MaxMind 数据库,将 CC_SRC更改为 1,然后设置MM_LICENSE_KEY 参数

# MaxMind License Key:
MM_LICENSE_KEY = ""

...

# Set the following to your preferred source:
#
# "1" - MaxMind
# "2" - db-ip, ipdeny, iptoasn
#
# The default is "2" on new installations of csf, or set to "1" to use the
# MaxMind databases after obtaining a license key
CC_SRC = "1"

保存文件并在完成后退出编辑器。

执行以下命令验证 CSF 配置。如果没有错误,你应该得到 CSF 版本。

sudo csf -v

接下来,使用以下命令重新启动 csf 和 lfd 服务。

sudo csf -ra

为了确保 csf 和 lfs 都在运行,使用以下命令验证这两个服务。

sudo systemctl status csf lfd

开启 CSF Web UI

使用 nano 编辑器打开 CSF 配置

sudo nano /etc/csf/csf.conf

将 UI 选项更改为 1 启用 CSF Web UI,调整 UI PORT, UI IP, UI USER,UI PASS 等参数。

#
# 1 to enable, 0 to disable
UI = "1"

...

# Do NOT enable access to this port in TCP_IN, instead only allow trusted IPs
# to the port using Advanced Allow Filters (see readme.txt)
UI_PORT = "1048"

...

# If the server is configured for IPv6 but the IP to bind to is IPv4, then the
# IP address MUST use the IPv6 representation. For example, 1.2.3.4 must use
# ::ffff:1.2.3.4
#
# Leave blank to bind to all IP addresses on the server
UI_IP = "127.0.0.1"

...

# This should be a secure, hard to guess username
#
# This must be changed from the default
UI_USER = "alice"
...
# numbers and non-alphanumeric characters
#
# This must be changed from the default
UI_PASS = "passw0rd"

保存并退出

使用下面的命令找到您的公共 IP 地址

curl https://ipinfo.io/

将您的 IP 地址添加到 /etc/csf/csf.allow 和 /etc/csf/ui/ui/ui.allow 配置中。

# single ip
192.168.5.1

# subnet
192.168.5.0/24

运行以下命令验证 CSF 配置,并重启 CSF 和 lfd 服务

sudo csf -v
sudo csf -ra

接下来,使用下面的命令验证 CSF 上的白名单地址列表。

sudo csf -l

现在打开您的 Web 浏览器并访问服务器 IP 地址,本例中是:https://192.168.5.15:1048,如果您的配置成功,则应查看 CSF 登录页面。

输入用户名和密码并登录,你将看到如下页面

我的开源项目

酷瓜云课堂-开源知识付费解决方案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值