防止服务器被攻击_李超超超超的技术博客_51CTO博客
source link: https://blog.51cto.com/chao/5333967
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
防止服务器被攻击
原创一、编写脚步自动添加添加暴力破解IP地址。
1、编写shell脚本查看登录失败记录添加到/etc/hosts.deny下。
#!/bin/bash
cat /var/log/wtmp >> log-wtmp.back #备份失败记录,下面会清空
lastb >> lastb.bak #备份系统登入失败的用户相关信息
lastb | awk '{print $3}' | sort | uniq -c >> black.ip
list=$(lastb | awk '{print $3}'| sort | uniq -c | awk '{if ($1 > 2) print $2}')
for ip in ${list}
echo ALL: ${ip} >> /etc/hosts.deny #加入黑名单
echo > /var/log/btmp #清空失败记录,防止脚本下次执行重复统计IP
2、执行lastb列出登入系统失败的用户相关信息。
lastb (可以看到登录2次失败的用户信息)
3、登入失败次数大于1次的IP追加到/etc/hosts/deny下。
#!/bin/bash
cat /var/log/wtmp >> log-wtmp.back #备份失败记录,下面会清空
lastb >> lastb.bak #备份系统登入失败的用户相关信息
lastb | awk '{print $3}' | sort | uniq -c >> black.ip
list=$(lastb | awk '{print $3}'| sort | uniq -c | awk '{if ($1 > 1) print $2}')
for ip in ${list}
echo ALL: ${ip} >> /etc/hosts.deny #加入黑名单
echo > /var/log/btmp #清空失败记录,防止脚本下次执行重复统计IP
4、执行lastb列出登入系统失败的用户相关信息和etc/hosts/deny信息。
5、设置计划任务,并模拟ssh登录失败。
二、开启防火墙和selinux
1、开启防火墙并设置开机自启动
systemctl start firewalld
systemctl enable firewalld
2、开启selinux
vim /etc/selinux/config
SELINUX=enforcing
修改完selinux配置文件后要重启服务器。
三、更改ssh端口禁用ping
1、更改ssh端口
vim /etc/ssh/sshd_config
原配置文件 Port 22这行默认是注释的,添加一个新的端口
重启ssd服务 systemctl restart sshd
2、禁用ping
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all
注意,icmp_echo_ignore_all 不是真实文件,因此不能通过 vim 来编辑修改。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK