- 添加速率限制中间件,防止API滥用和DDoS攻击 - 添加可疑请求检测,基于行为模式识别攻击 - 添加输入清理和验证,防止命令注入和XSS攻击 - 更新middleware.ts,集成所有安全功能 - 添加fail2ban安装脚本,防止SSH暴力破解 - 不依赖IP黑名单,适用于所有攻击者 安全功能: - API速率限制:每分钟100次请求 - 认证速率限制:每分钟5次请求 - 自动检测可疑请求模式 - 输入清理和验证 - 详细的安全日志记录
100 lines
2.6 KiB
Bash
Executable File
100 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
# 安全检查脚本:检查系统配置文件是否被感染
|
|
# 使用方法: source lib/security/check-bashrc.sh && check_system_security
|
|
|
|
check_bashrc_security() {
|
|
local bashrc_file="$HOME/.bashrc"
|
|
local issues=0
|
|
|
|
if [ -f "$bashrc_file" ]; then
|
|
# 检查是否包含恶意代码模式
|
|
if grep -qE '\.update|/usr/bin/\.update|while.*true.*do.*\.update' "$bashrc_file" 2>/dev/null; then
|
|
echo "❌ 发现: $bashrc_file 可能包含恶意代码"
|
|
issues=$((issues + 1))
|
|
fi
|
|
fi
|
|
|
|
return $issues
|
|
}
|
|
|
|
check_profile_security() {
|
|
local profile_file="/etc/profile"
|
|
local issues=0
|
|
|
|
if [ -f "$profile_file" ] && [ -r "$profile_file" ]; then
|
|
if grep -qE '\.update|/usr/bin/\.update|while.*true.*do.*\.update' "$profile_file" 2>/dev/null; then
|
|
echo "❌ 发现: $profile_file 可能包含恶意代码"
|
|
issues=$((issues + 1))
|
|
fi
|
|
fi
|
|
|
|
return $issues
|
|
}
|
|
|
|
check_rc_local_security() {
|
|
local rc_local_file="/etc/rc.d/rc.local"
|
|
local issues=0
|
|
|
|
if [ ! -f "$rc_local_file" ]; then
|
|
rc_local_file="/etc/rc.local"
|
|
fi
|
|
|
|
if [ -f "$rc_local_file" ] && [ -r "$rc_local_file" ]; then
|
|
if grep -qE '\.update|/usr/bin/\.update|while.*true.*do.*\.update' "$rc_local_file" 2>/dev/null; then
|
|
echo "❌ 发现: $rc_local_file 可能包含恶意代码"
|
|
issues=$((issues + 1))
|
|
fi
|
|
fi
|
|
|
|
return $issues
|
|
}
|
|
|
|
check_crontab_security() {
|
|
local issues=0
|
|
|
|
if command -v crontab &> /dev/null; then
|
|
if crontab -l 2>/dev/null | grep -qE '\.update|/usr/bin/\.update'; then
|
|
echo "❌ 发现: crontab 可能包含恶意任务"
|
|
issues=$((issues + 1))
|
|
fi
|
|
fi
|
|
|
|
return $issues
|
|
}
|
|
|
|
check_system_security() {
|
|
echo "🔍 开始系统安全检查..."
|
|
echo ""
|
|
|
|
local total_issues=0
|
|
|
|
check_bashrc_security
|
|
total_issues=$((total_issues + $?))
|
|
|
|
check_profile_security
|
|
total_issues=$((total_issues + $?))
|
|
|
|
check_rc_local_security
|
|
total_issues=$((total_issues + $?))
|
|
|
|
check_crontab_security
|
|
total_issues=$((total_issues + $?))
|
|
|
|
echo ""
|
|
if [ $total_issues -eq 0 ]; then
|
|
echo "✅ 系统安全检查通过,未发现恶意代码"
|
|
return 0
|
|
else
|
|
echo "⚠️ 发现 $total_issues 个潜在安全问题"
|
|
echo " 建议立即检查和清理相关文件"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# 如果直接执行此脚本,运行安全检查
|
|
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
|
|
check_system_security
|
|
exit $?
|
|
fi
|
|
|