你的阿里云服务器在偷偷挖矿!5个必须检查的安全死角(含自动化监控脚本)
阿里云服务器安全防护:深度排查隐匿挖矿程序与自动化防御方案
最近半年,不少技术团队发现自己的阿里云服务器性能突然下降,排查后发现竟被植入了挖矿程序。这些恶意程序往往采用高级隐匿技术,普通运维手段难以察觉。本文将带您深入分析挖矿程序的常见藏身之处,并提供一套完整的排查与自动化监控方案。
1. 挖矿程序的隐匿技术与检测方法
现代挖矿程序已不再简单地占用CPU资源,而是采用各种技术手段隐藏自身。了解这些技术是有效防御的第一步。
1.1 系统进程伪装技术
大多数挖矿程序会伪装成正常系统进程。我曾遇到一个案例,攻击者将挖矿程序重命名为[kworker/u:0]这样的内核线程名称,普通top命令根本无法识别异常。
要识破这种伪装,我们需要更深入的检测方法:
# 检查异常CPU占用的进程
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 10
# 检查进程的可执行文件路径
ls -l /proc/<PID>/exe
如果发现某个"内核线程"实际指向/tmp下的可疑文件,那几乎可以确定是挖矿程序。
1.2 动态链接库注入(ld.so.preload)
这是目前最高级的隐匿技术之一。攻击者通过修改/etc/ld.so.preload文件,在程序运行时预先加载恶意库,从而劫持系统调用。
检测方法:
# 检查ld.so.preload文件
ls -l /etc/ld.so.preload
cat /etc/ld.so.preload 2>/dev/null
# 如果存在可疑内容,立即清除
rm -f /etc/ld.so.preload
注意:清除ld.so.preload后,之前隐藏的挖矿进程会立即显现,请准备好后续处理
1.3 系统服务伪装
攻击者常将挖矿程序注册为系统服务,实现持久化。检查所有系统服务:
# 列出所有系统服务
systemctl list-units --type=service --all
# 检查可疑服务的详细信息
systemctl status <可疑服务名>
journalctl -u <可疑服务名>
我曾发现一个伪装成nginx-helper的服务,实际是门罗币挖矿程序。
2. 全面排查挖矿程序的五个关键位置
根据安全团队统计,90%的挖矿程序会隐藏在以下五个位置,必须逐一检查。
2.1 启动项检查
| 检查位置 | 命令 | 备注 |
|---|---|---|
| /etc/rc.local | cat /etc/rc.local | 老式启动脚本 |
| /etc/init.d/ | ls -la /etc/init.d/ | 系统服务脚本 |
| crontab | crontab -l | 用户定时任务 |
| systemd | systemctl list-unit-files | 现代服务管理 |
发现可疑项后,不仅要删除,还要检查文件创建时间、修改时间:
stat /path/to/suspicious/file
2.2 临时目录与隐藏目录
挖矿程序常隐藏在以下目录:
/tmp//var/tmp//dev/shm/- 用户home目录下的隐藏目录
检查命令:
# 查找最近修改的可执行文件
find /tmp /var/tmp /dev/shm -type f -executable -mtime -7 -ls
# 检查隐藏目录
ls -la ~/ | grep "^\."
2.3 网络连接分析
挖矿程序必须与矿池通信,网络连接是重要线索:
# 查看异常外连
netstat -tulnp
ss -tulnp
lsof -i
# 检查DNS查询记录
cat /etc/resolv.conf
journalctl -u systemd-resolved
2.4 用户与权限检查
攻击者常创建隐藏用户:
# 检查/etc/passwd中的异常用户
cat /etc/passwd | grep -vE '^#|root|bin|daemon'
# 检查sudo权限
visudo -c
cat /etc/sudoers
2.5 内核模块检查
高级攻击者会加载恶意内核模块:
# 列出已加载模块
lsmod
# 检查模块信息
modinfo <可疑模块名>
3. 自动化监控与防御方案
手动排查只是应急措施,建立自动化监控系统才能长效防护。
3.1 实时CPU监控脚本
以下脚本可监控异常CPU使用并报警:
#!/bin/bash
# 监控CPU使用率超过阈值的进程
THRESHOLD=80 # CPU使用率阈值
LOG_FILE="/var/log/cpu_monitor.log"
ALERT_EMAIL="admin@example.com"
while true; do
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
ABNORMAL_PROCESSES=$(ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | awk -v threshold=$THRESHOLD 'NR>1 && $4 > threshold')
if [ -n "$ABNORMAL_PROCESSES" ]; then
echo "$TIMESTAMP 检测到高CPU使用进程:" >> $LOG_FILE
echo "$ABNORMAL_PROCESSES" >> $LOG_FILE
# 发送邮件报警
echo "$ABNORMAL_PROCESSES" | mail -s "服务器CPU使用警报" $ALERT_EMAIL
# 可选:自动终止进程
# echo "$ABNORMAL_PROCESSES" | awk '{print $1}' | xargs kill -9
fi
sleep 60
done
将脚本设为系统服务:
# 创建服务文件
cat > /etc/systemd/system/cpu_monitor.service <<EOF
[Unit]
Description=CPU Monitor Service
[Service]
ExecStart=/path/to/cpu_monitor.sh
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# 启用服务
systemctl daemon-reload
systemctl enable cpu_monitor
systemctl start cpu_monitor
3.2 文件完整性监控
使用AIDE(Advanced Intrusion Detection Environment)建立文件基线:
# 安装AIDE
yum install aide -y
# 初始化数据库
aide --init
# 将数据库移到正确位置
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 每日检查
aide --check
3.3 网络流量监控
使用iftop监控异常外连:
# 安装iftop
yum install iftop -y
# 监控网络流量
iftop -nNP
4. 服务器安全加固措施
清除挖矿程序后,必须加固服务器防止再次入侵。
4.1 基础安全配置
- 更新系统:
yum update -y && reboot - 防火墙设置:
systemctl enable firewalld systemctl start firewalld firewall-cmd --permanent --add-service=ssh firewall-cmd --permanent --remove-service=http firewall-cmd --reload - SSH加固:
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config systemctl restart sshd
4.2 高级防护方案
-
安装fail2ban:
yum install fail2ban -y systemctl enable fail2ban systemctl start fail2ban -
配置系统审计:
yum install audit -y systemctl enable auditd systemctl start auditd # 监控重要文件 echo "-w /etc/passwd -p wa -k identity" >> /etc/audit/rules.d/audit.rules echo "-w /etc/ssh/sshd_config -p wa -k sshd" >> /etc/audit/rules.d/audit.rules service auditd restart -
使用SELinux:
# 检查状态 sestatus # 如果禁用,启用它 sed -i 's/SELINUX=disabled/SELINUX=enforcing/' /etc/selinux/config reboot
在实际运维中,我发现结合这些措施能有效防御99%的自动化攻击。特别是文件完整性监控,多次帮助我发现早期入侵迹象。
更多推荐


所有评论(0)