1. 立即停止邮件服务防止继续发信

2. 查找恶意文件和进程

检查可疑的PHP文件:

# 在Joomla目录中搜索后门文件
find /var/www/html -name "*.php" -type f -exec grep -l "base64_decode\|eval(\$\|shell_exec\|system(\|passthru\|exec(" {} \;

# 查找最近修改的文件(木马通常最近修改)
find /var/www/html -type f -name "*.php" -mtime -3

# 查找包含可疑字符串的文件
find /var/www/html -type f -name "*.php" | xargs grep -l "mail(\|fsockopen\|curl_exec" 2>/dev/null

# 检查.htaccess是否被篡改
find /var/www/html -name ".htaccess" -exec grep -l "RewriteRule.*php" {} \;

检查隐藏文件:

# 查找隐藏的PHP文件
find /var/www/html -name ".*.php" -o -name "*.php.*" -o -name "*.php.bak"

# 检查/tmp目录中的可疑文件
ls -la /tmp/*.php /tmp/*.ph* 2>/dev/null

3. 分析邮件发送的源头

查看哪些PHP进程在运行:

ps aux | grep php
ps aux | grep -E "(mail|exim|sendmail)"

检查Web日志中的异常请求:

# 查看Apache访问日志
tail -100 /var/log/apache2/access.log | grep -E "POST.*mail|GET.*mail|\.php\?"

# 查找大量请求的IP
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head -20

# 查找异常的User-Agent
grep -i "bot\|spider\|curl\|wget\|python\|perl" /var/log/apache2/access.log | head -20

4. 使用专业工具扫描

安装并运行ClamAV:

apt-get install clamav clamav-daemon
freshclam # 更新病毒库
clamscan -r /var/www/html --infected --remove=yes

使用rkhunter检查rootkit:

apt-get install rkhunter
rkhunter --check --skip-keypress

5. 检查Joomla特定位置

Joomla常见后门位置:

# 检查/templates目录
find /var/www/html/templates -name "*.php" -exec grep -l "eval\|base64" {} \;

# 检查/modules目录
find /var/www/html/modules -name "*.php" -exec grep -l "mail\|fsockopen" {} \;

# 检查/components目录
find /var/www/html/components -name "*.php" -mtime -7

# 检查/plugins目录
find /var/www/html/plugins -name "*.php" -exec grep -l "exec\|system" {} \;

6. 创建检测脚本

创建find_malware.sh:

#!/bin/bash
echo "=== Joomla恶意文件扫描 ==="

# 1. 检查文件修改时间
echo "最近3天修改的PHP文件:"
find /var/www/html -name "*.php" -type f -mtime -3 -ls

# 2. 检查包含恶意代码的文件
echo -e "\n包含可疑代码的文件:"
find /var/www/html -name "*.php" -type f -exec grep -l "base64_decode\|@eval\|gzuncompress\|str_rot13" {} \;

# 3. 检查文件权限
echo -e "\n异常权限的文件(可写):"
find /var/www/html -type f -name "*.php" -perm /022 -ls

# 4. 检查异常大小的文件
echo -e "\n异常大的PHP文件(>1MB):"
find /var/www/html -name "*.php" -type f -size +1M -ls

# 5. 检查包含邮件函数的文件
echo -e "\n包含mail()函数的文件:"
find /var/www/html -name "*.php" -type f -exec grep -l "mail(" {} \;

赋予执行权限并运行:

7. 修复步骤

步骤1:备份当前网站

# 备份整个网站
tar czf /tmp/joomla_backup_$(date +%Y%m%d).tar.gz /var/www/html

# 备份数据库
mysqldump -u root -p joomla_db > /tmp/joomla_db_$(date +%Y%m%d).sql

步骤2:清理发现的恶意文件

# 创建隔离目录
mkdir -p /tmp/quarantine

# 移动可疑文件到隔离区
find /var/www/html -name "*.php" -exec grep -l "base64_decode.*@eval" {} \; | while read file; do
echo "隔离文件: $file"
mv "$file" /tmp/quarantine/
done

步骤3:更新Joomla核心文件

# 下载最新版Joomla
cd /tmp
wget https://downloads.joomla.org/cms/joomla4/latest/Joomla_4.x-Stable-Full_Package.zip
unzip Joomla_4.x-Stable-Full_Package.zip

# 替换核心文件(注意:不要覆盖configuration.php和images目录)
cp -r /tmp/joomla/libraries/* /var/www/html/libraries/
cp -r /tmp/joomla/includes/* /var/www/html/includes/
cp -r /tmp/joomla/media/* /var/www/html/media/

步骤4:检查和清理数据库

# 查找数据库中可疑内容
mysql -u root -p -e "USE joomla_db; SHOW TABLES;" | while read table; do
mysql -u root -p -e "USE joomla_db; SELECT COUNT(*) FROM \`$table\` WHERE \`description\` LIKE '%<iframe%' OR \`text\` LIKE '%<iframe%' OR \`introtext\` LIKE '%<iframe%';"
done

# 检查用户表是否有异常用户
mysql -u root -p -e "USE joomla_db; SELECT id, username, email, registerDate FROM #__users ORDER BY registerDate DESC LIMIT 10;"

8. 加强安全措施

更新所有密码:

# 修改数据库密码
mysql -u root -p -e "ALTER USER 'joomla_user'@'localhost' IDENTIFIED BY '新强密码';"

# 修改Joomla管理员密码
# 通过phpMyAdmin或使用以下SQL:
# UPDATE `#__users` SET `password` = MD5('新密码') WHERE username = 'admin';

修复文件权限:

# 设置正确的文件权限
cd /var/www/html
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
chown -R www-data:www-data .
chmod 444 configuration.php

安装安全扩展:

# 可以考虑安装Joomla安全扩展,如:
# 1. Akeeba Admin Tools
# 2. RSFirewall
# 3. JHackGuard

9. 监控和预防

创建监控脚本monitor_joomla.sh:

#!/bin/bash
# 监控Joomla文件变化
while true; do
# 检查是否有新文件创建
find /var/www/html -name "*.php" -cmin -5
# 检查邮件发送
tail -5 /var/log/exim4/mainlog
# 检查Web请求
tail -5 /var/log/apache2/access.log | grep -E "\.php\?|POST"
sleep 60
done

重要建议:

  1. 立即行动:木马正在大量发邮件,立即停止邮件服务
  2. 全面扫描:使用多个工具交叉验证
  3. 逐级恢复:不要一次性全部删除,先隔离再分析
  4. 更新所有:Joomla核心、模板、扩展全部更新到最新版
  5. 启用双因素认证:为Joomla管理员启用2FA
  6. 定期备份:建立自动备份机制
  7. 使用WAF:考虑安装Web应用防火墙

如果问题复杂,建议:

  • 从干净备份恢复
  • 考虑迁移到新服务器

 

 

 

 

 


登陆