我们需要在 CentOS 7 系统上安装并配置 NTP 服务。可以使用以下命令安装 NTP 软件包:
yum install -y ntp
安装完成后,编辑 NTP 配置文件 /etc/ntp.conf
,添加以下内容:
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
这些配置项可以有效限制 NTP 服务器对外提供的功能,降低被攻击者利用的风险。
为进一步加强 NTP 服务器的防御能力,我们需要配置防火墙规则,仅允许可信的 NTP 客户端访问 NTP 服务器。可以使用以下 iptables 规则:
iptables -A INPUT -p udp --dport 123 -m state --state NEW -j ACCEPT
iptables -A INPUT -p udp --dport 123 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p udp --dport 123 -j DROP
这些规则允许已建立的 NTP 会话通过,拒绝未授权的 NTP 查询请求。
NTP 服务器还支持身份验证机制,可以进一步提高安全性。我们可以生成一个预共享密钥,并在 NTP 配置文件中启用身份验证:
mv /etc/ntp.keys /etc/ntp.keys.bak
ntp-keygen -M
echo "keys /etc/ntp.keys" >> /etc/ntp.conf
echo "trustedkey 1" >> /etc/ntp.conf
这样,只有能提供正确密钥的 NTP 客户端才能与 NTP 服务器进行时间同步。
为及时发现和应对 NTP 攻击,我们还需要监控 NTP 服务器的运行状态。可以使用以下命令定期检查 NTP 服务器的负载情况:
ntpq -p
ntpstat
发现 NTP 服务器负载异常,可以采取相应的措施,如临时关闭 NTP 服务,或调整防火墙规则等。
通过以上几个步骤,我们可以有效地保护 CentOS 7 时间服务器,防御 NTP 放大攻击,确保关键业务系统的正常运转。