lovart3/脚本修复说明.md

206 lines
5.3 KiB
Markdown
Raw Permalink 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.

# 防重感染脚本误报修复说明
## 修复时间
2025年1月1日
## 问题描述
### 原始问题
防重感染脚本 (`prevent-reinfection.sh`) 使用 `pgrep -f` 匹配整个命令行,导致:
- 清理脚本的 `find / -name .system3d` 进程被误判为恶意进程
- 日志中频繁出现"发现恶意进程"的记录(实际是误报)
- 无法区分真正的威胁和清理脚本的正常操作
### 误报示例
```bash
# 清理脚本执行:
find / -name .system3d -type f -exec rm -f {} ;
# 防重感染脚本检测:
pgrep -f 'system3d' # 匹配到了find进程的命令行
# 结果:误报为恶意进程
```
---
## 修复方案
### 1. 使用精确的进程名匹配
**修改前**:
```bash
pgrep -f 'kinsing|kdevtmpfsi|system3d|b4nd1d0'
```
**修改后**:
```bash
# 对于明确的进程名,使用精确匹配
pgrep -x 'kinsing' # 只匹配进程名为kinsing的进程
pgrep -x 'kdevtmpfsi' # 只匹配进程名为kdevtmpfsi的进程
```
### 2. 排除清理脚本的子进程
添加父进程检查:
```bash
ppid=$(ps -p $pid -o ppid= | tr -d ' ')
pcmd=$(ps -p $ppid -o cmd= | head -1)
# 如果是清理脚本的子进程,跳过
if echo "$pcmd" | grep -qE 'cleanup|prevent|monitor|aggressive|enhanced'; then
log "跳过清理脚本的子进程: PID=$pid"
continue
fi
```
### 3. 排除系统工具进程
检查可执行文件路径:
```bash
exe_path=$(readlink -f /proc/$pid/exe 2>/dev/null)
# 如果是系统工具,跳过
if echo "$exe_path" | grep -qE '/usr/bin/find|/usr/bin/grep|/bin/bash.*cleanup'; then
log "跳过系统工具进程: PID=$pid"
continue
fi
```
### 4. 改进日志记录
添加详细的进程信息:
```bash
log "⚠️ 发现真正的恶意进程: PID=$pid, 进程名=$pattern, 命令=${cmd:0:100}, 父进程=${pcmd:0:50}"
log "跳过清理脚本的子进程: PID=$pid, 进程名=$pattern, 父进程=${pcmd:0:50}"
```
---
## 修复后的脚本逻辑
### 检测流程
1. **精确匹配进程名**(kinsing, kdevtmpfsi, b4nd1d0)
- 使用 `pgrep -x` 只匹配进程名
- 不匹配命令行参数
2. **检查父进程**
- 如果是清理脚本的子进程,跳过
- 记录为"跳过清理脚本的子进程"
3. **检查可执行文件**
- 如果是系统工具(find, grep等),跳过
- 记录为"跳过系统工具进程"
4. **真正的恶意进程**
- 通过所有检查后,确认为真正的恶意进程
- 记录详细信息并终止
### 特殊处理:.system3d
对于 `.system3d` 这种文件名匹配:
- 检查可执行文件路径是否真的包含 `.system3d`
- 排除清理脚本的 `find` 进程
- 只终止真正的 `.system3d` 可执行文件
---
## 修复效果
### 修复前
- 日志中每1-2分钟出现"发现恶意进程"
- 实际是清理脚本的 `find` 进程
- 无法区分误报和真实威胁
- 使用 `pgrep -f` 匹配整个命令行,导致误报
### 修复后
- ✅ 清理脚本的 `find` 进程被正确识别并跳过(不记录日志)
- ✅ 日志中只记录真正的恶意进程(如果有)
- ✅ 可以清楚区分误报和真实威胁
- ✅ 使用 `pgrep -x` 精确匹配进程名
- ✅ 通过检查父进程和可执行文件路径排除误报
### 当前状态
- ✅ 脚本已成功部署
- ✅ 脚本语法正确
- ✅ 当前无真正的恶意进程(系统安全)
- ⚠️ 由于当前没有真正的恶意进程,日志中不会显示新的检测记录
- ✅ 当清理脚本的 `find` 进程运行时,新脚本会静默跳过,不会产生误报日志
---
## 验证方法
### 1. 检查日志
```bash
# 查看最新日志
tail -30 /var/log/prevent-malware-regeneration.log
# 应该看到:
# - "跳过清理脚本的子进程"(正常)
# - "跳过系统工具进程"(正常)
# - "发现真正的恶意进程"(只有在真正有威胁时才会出现)
```
### 2. 监控系统资源
```bash
# CPU使用率应该正常(挖矿程序会导致接近100%)
top -bn1 | grep '%Cpu'
# 系统负载应该正常
uptime
```
### 3. 检查进程
```bash
# 不应该有真正的恶意进程
ps aux | grep -E 'kinsing|kdevtmpfsi' | grep -v grep
# 应该返回空(无结果)
```
---
## 部署说明
### 文件位置
- **脚本1路径**: `/root/scripts/prevent-reinfection.sh` (已修复)
- **执行方式**: 每2分钟(通过crontab)
- **脚本2路径**: `/root/scripts/prevent-malware-regeneration.sh` (已修复)
- **执行方式**: 持续运行(通过systemd服务)
- **日志路径**: `/var/log/prevent-malware-regeneration.log` (两个脚本共用)
### 备份
修复前已创建备份:
- `/root/scripts/prevent-reinfection.sh.backup`
### 回滚方法
如果需要回滚到旧版本:
```bash
cp /root/scripts/prevent-reinfection.sh.backup /root/scripts/prevent-reinfection.sh
chmod +x /root/scripts/prevent-reinfection.sh
```
---
## 后续建议
1. **持续监控**
- 观察日志1-2天,确认误报已消除
- 关注是否有真正的恶意进程检测
2. **优化清理脚本**
- 考虑修改清理脚本,使用不包含关键词的命令
- 或使用更精确的查找方法
3. **增强检测**
- 可以添加更多检测指标(CPU使用率、网络连接等)
- 结合多个指标综合判断
---
**修复完成时间**: 2025年1月1日
**修复人员**: AI Assistant
**脚本版本**: v2.0 (修复误报版本)