天天看点

记录一次阿里云ecs被挖矿的经历

作者:fana

早上起来,看到钉钉上出现大量告警,CPU,内存都被占到99%了,马上登录机器检查。

使用top查看占用资源情况,大P,按cpu占用排序,仔细看下有个.system,这个就是伪装的挖坑程序。

记录一次阿里云ecs被挖矿的经历

ps -ef|grep system 查看服务进程信息,pwdx 3343查看程序的目录

记录一次阿里云ecs被挖矿的经历

查看远程连接的IP地址是荷兰的

记录一次阿里云ecs被挖矿的经历

kill -9 后会重新启动,先把名字改掉,再kill。

https://www.virustotal.com/gui/home/upload 免费的在线病毒查杀网站,可以把脚本上传检测

记录一次阿里云ecs被挖矿的经历

后续系统检查

参考:https://www.freebuf.com/column/162604.html
 
 #一般linux系统不会有/etc/ld.so.preload这个问题,先把这个文件修改
 mv /etc/ld.so.preload /etc/ld.so.preloadbak
 
 cat /etc/ld.so.preloadbak 
 /usr/lib/libsystemd-shared-165.so
 
 #果断删除这个so文件           
记录一次阿里云ecs被挖矿的经历
#1.检查定时任务,
 crontab -l
 
 cd /var/spool/cron
 
 ll /etc/cron* #进到每个文件夹都检查下
 
 cat /etc/bashrc #检查是否有特殊命令
 
 #2.检查环境变量
 
 #3.查看开启自动项
 systemctl list-unit-files
 cat /etc/rc.local 或者/etc/rc.d/rc.local            

安全加固

#1.限制用户从tty登录
 vim /etc/pam.d/login
 #在第一行(最前面)添加如下内容:
 auth required pam_tally2.so deny=3 lock_time=300 even_deny_root root_unlock_time=10
 ##deny设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户 
 ##unlock_time设定普通用户锁定后,多少时间后解锁,单位是秒; 
 ##even_deny_root也限制root用户; 
 ##root_unlock_time设定root用户锁定后,多少时间后解锁,单位是秒;
 
 #2.限制远程(SSH)登录
 vim /etc/pam.d/sshd
 #在第一行(最前面)添加如下内容:
 auth required pam_tally2.so deny=3 unlock_time=300 even_deny_root root_unlock_time=10
 
 
 #3.配置ssh的config文件
 用户级(user-specific file):~/.ssh/config,通常不存在,如果需要自己新建
 系统级(system-wide file):/etc/ssh/ssh_config,内置有一些默认配置
 
 vim /etc/ssh/sshd_config
 #禁用root账户登录,非必要,但为了安全性,可以选择配置
 PermitRootLogin no
 
 #是否让sshd去检查用户家目录或相关档案的权限数据,这是为了担心使用者将某些重要档案的权限设错,可能会导致一些问题所致。例如使用者的~/.ssh/权限设错时,某些特殊情况下会不许用户登入
 StrictModes no
 
 #是否允许用户自行使用成对的密钥系统进行登入行为,仅针对version2。至于自制的公钥数据就放置于用户家目录下的.ssh/authorized_keys内
 RSAAuthentication yes
 PubkeyAuthentication yes
 AuthorizedKeysFile %h/.ssh/authorized_keys
 
 #有了证书登录了,就禁用密码登录吧,安全要紧
 PasswordAuthentication no
 
 #在ssh配置文件中查找MaxAuthTries修改为:
 MaxAuthTries 3 #错误3次即断开连接
            

fail2ban

一款安全保护工具,触发限制后会创建防火墙规则封锁IP,诸如对ssh暴力破解、ftp/http密码穷举等场景提供强有力的保护。

#1.安装
 yum install epel-release -y
 yum update -y
 yum install fail2ban-firewalld -y
 
 #2.配置
 cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local #将默认的配置拷贝一份
 vim jail.local
 [DEFAULT]
 ignoreip = 127.0.0.1/8 ::1 #永远不会被禁止的IP地址白名单
 bantime  = 10m #禁止IP地址的时长,m代表分钟、h表示时,不写就是秒,-1将永久禁止
 findtime  = 10m #失败的时间
 maxretry = 5 #失败的次数
 backend = auto #指定用于获取文件修改的后端
 banaction = custom-firewalld #找到banaction,修改为这个
 
 vim /etc/fail2ban/jail.d/sshd.local
 [sshd]
 enabled = true
 
 
 vim /etc/fail2ban/action.d/custom-firewalld.conf
 [INCLUDES]
 before  =
 
 [Definition]
 actionstart =
 actionstop =
 actioncheck =
 
 actionflush = sed -i '/<source address=/d' /etc/firewalld/zones/drop.xml
 actionban = firewall-cmd --change-source=<ip> --zone=drop && firewall-cmd --change-source=<ip> --zone=drop --permanent
 actionunban = firewall-cmd --remove-source=<ip> --zone=drop && firewall-cmd --remove-source=<ip> --zone=drop --permanent || echo 0
 
 [Init]
 
 #3.启动
 systemctl enable --now fail2ban
 systemctl restart firewalld
 
 #禁用ip例:fail2ban-client set sshd banip 1.1.1.1
 fail2ban-client set <jail name here> banip <ip here>
 #解禁ip
 fail2ban-client set <jail name here> unbanip <ip here>
 
 #4.验证,客户段发送暴力破解
 hydra -l root -P 00.txt -vV -o ssh.log -e ns ssh://192.168.10.12:22
 
 #5.过一会 查看日志提示
 WARNING [sshd] 192.168.10.11 already banned
 
 #然后查看防火墙,就会被禁止了
 firewall-cmd --list-all --zone=drop
 drop (active)
   target: DROP
   icmp-block-inversion: no
   interfaces: 
   sources: 192.168.10.11
   services: 
   ports: 
   protocols: 
   forward: yes
   masquerade: no
   forward-ports: 
   source-ports: 
   icmp-blocks: 
   rich rules: