在CentOS系统中,防止SFTP暴力破解的方法主要有以下几种:
Fail2Ban是一个入侵防御软件框架,可以监控系统日志并根据配置的规则阻止恶意IP地址。
sudo yum install fail2ban -y
编辑Fail2Ban的配置文件 /etc/fail2ban/jail.local
或 /etc/fail2ban/jail.d/sshd.conf
,添加以下内容:
[sshd]
enabled = true
port = sshd
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 600
enabled = true
:启用该jail。port = sshd
:指定要监控的端口。filter = sshd
:使用默认的sshd过滤器。logpath = /var/log/secure
:指定日志文件路径。maxretry = 3
:允许的最大失败尝试次数。bantime = 600
:封禁时间(秒)。sudo systemctl start fail2ban
sudo systemctl enable fail2ban
PAM可以配置额外的认证模块来增强安全性。
sudo yum install libpam-modules -y
编辑 /etc/pam.d/sshd
文件,添加以下行:
auth required pam_tally2.so onerr=fail deny=3 unlock_time=600
deny=3
:允许的最大失败尝试次数。unlock_time=600
:封禁时间(秒)。使用SSH密钥认证可以减少暴力破解的风险,因为攻击者需要获取用户的私钥才能登录。
ssh-keygen -t rsa -b 4096
ssh-copy-id user@server_ip
编辑 /etc/ssh/sshd_config
文件,进行以下配置:
PermitRootLogin no
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
PermitRootLogin no
:禁止root用户直接登录。PasswordAuthentication no
:禁用密码认证。ChallengeResponseAuthentication no
:禁用挑战响应认证。UsePAM yes
:启用PAM认证。sudo systemctl restart sshd
使用iptables或firewalld限制SSH连接速率。
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -m state --state NEW -m recent --set
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
sudo firewall-cmd --reload
通过以上方法,可以有效地防止SFTP暴力破解,提高系统的安全性。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: CentOS Node.js 怎样提升安全性