ai-chatbot/prevent-reinfection-fixed.sh
Your Name 835aa4a06f
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
修复防重感染脚本的误报问题
- 改进恶意进程检测逻辑,使用pgrep -x精确匹配进程名
- 排除清理脚本的子进程(通过检查父进程)
- 排除系统工具进程(find, grep等)
- 添加详细的日志记录,区分误报和真实威胁
- 对于.system3d等文件名匹配,检查可执行文件路径

修复后,日志将不再显示清理脚本的find进程为恶意进程,
只记录真正的恶意程序检测和终止。
2026-01-01 18:52:28 +08:00

116 lines
4.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 防止恶意软件重新感染的防护脚本(修复误报版本)
LOG="/var/log/prevent-malware-regeneration.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG"
}
# 检查crontab是否被修改
CURRENT_CRONTAB=$(crontab -l 2>/dev/null | md5sum | awk '{print $1}')
CLEAN_CRONTAB=$(cat /root/.crontab.clean 2>/dev/null | md5sum | awk '{print $1}')
if [ "$CURRENT_CRONTAB" != "$CLEAN_CRONTAB" ]; then
log "⚠️ 检测到crontab被修改!"
if crontab -l 2>/dev/null | grep -qE 'est1|b4nd1d0|007idc'; then
log "发现恶意定时任务,正在清理..."
crontab -l 2>/dev/null | grep -vE 'est1|b4nd1d0|007idc' | crontab -
log "已清理恶意定时任务"
fi
fi
# 检查.bash_profile是否被修改
if [ -f /root/.bash_profile.clean ]; then
CURRENT_PROFILE=$(md5sum /root/.bash_profile 2>/dev/null | awk '{print $1}')
CLEAN_PROFILE=$(md5sum /root/.bash_profile.clean 2>/dev/null | awk '{print $1}')
if [ "$CURRENT_PROFILE" != "$CLEAN_PROFILE" ]; then
if grep -qE '007idc|curl.*bash|wget.*bash' /root/.bash_profile; then
log "⚠️ 检测到.bash_profile被恶意修改!"
cp /root/.bash_profile.clean /root/.bash_profile
log "已恢复干净的.bash_profile"
fi
fi
fi
# 检查恶意目录是否重新创建
for dir in /tmp/.est1 /var/tmp/.ladyg0g0 /tmp/.esd101; do
if [ -d "$dir" ] && [ "$(ls -A $dir 2>/dev/null)" ]; then
log "⚠️ 检测到恶意目录重新创建: $dir"
rm -rf "$dir"/*
chmod 000 "$dir"
chattr +i "$dir" 2>/dev/null
log "已清理并锁定目录: $dir"
fi
done
# 改进的恶意进程检测(修复误报)
# 使用更精确的方法,排除清理脚本的子进程
detect_and_kill_malware() {
# 定义恶意进程关键词(只匹配进程名,不匹配命令行)
MALWARE_PATTERNS=('kinsing' 'kdevtmpfsi' 'b4nd1d0')
for pattern in "${MALWARE_PATTERNS[@]}"; do
# 使用pgrep -x只匹配进程名(精确匹配)
pgrep -x "$pattern" 2>/dev/null | while read pid; do
# 获取进程详细信息
if [ -d "/proc/$pid" ]; then
cmd=$(ps -p $pid -o cmd= 2>/dev/null | head -1)
ppid=$(ps -p $pid -o ppid= 2>/dev/null | tr -d ' ')
pcmd=$(ps -p $ppid -o cmd= 2>/dev/null | head -1)
# 检查是否是清理脚本的子进程
if echo "$pcmd" | grep -qE 'cleanup|prevent|monitor|aggressive|enhanced'; then
log "跳过清理脚本的子进程: PID=$pid, 进程名=$pattern, 父进程=${pcmd:0:50}"
continue
fi
# 检查进程的可执行文件路径
exe_path=$(readlink -f /proc/$pid/exe 2>/dev/null)
if echo "$exe_path" | grep -qE 'cleanup|prevent|monitor|find|grep|bash.*script'; then
log "跳过系统工具进程: PID=$pid, 可执行文件=${exe_path:0:50}"
continue
fi
# 这是真正的恶意进程
log "⚠️ 发现真正的恶意进程: PID=$pid, 进程名=$pattern, 命令=${cmd:0:100}, 父进程=${pcmd:0:50}"
kill -9 $pid 2>/dev/null
log "✓ 已终止真正的恶意进程: PID=$pid"
fi
done
# 对于.system3d这种文件名匹配,使用更精确的方法
if [ "$pattern" = 'system3d' ]; then
# 检查是否有进程的可执行文件路径包含.system3d
for pid in $(pgrep -f '\.system3d' 2>/dev/null); do
if [ -d "/proc/$pid" ]; then
exe_path=$(readlink -f /proc/$pid/exe 2>/dev/null)
ppid=$(ps -p $pid -o ppid= 2>/dev/null | tr -d ' ')
pcmd=$(ps -p $ppid -o cmd= 2>/dev/null | head -1)
cmd=$(ps -p $pid -o cmd= 2>/dev/null | head -1)
# 排除清理脚本和系统工具
if echo "$pcmd" | grep -qE 'cleanup|prevent|monitor' || \
echo "$exe_path" | grep -qE '/usr/bin/find|/usr/bin/grep|/bin/bash.*cleanup'; then
continue
fi
# 检查可执行文件路径是否真的是.system3d
if echo "$exe_path" | grep -q '\.system3d'; then
log "⚠️ 发现真正的.system3d恶意进程: PID=$pid, 路径=$exe_path"
kill -9 $pid 2>/dev/null
log "✓ 已终止.system3d进程: PID=$pid"
fi
fi
done
fi
done
}
# 执行恶意进程检测
detect_and_kill_malware
log "防护检查完成"