# 检查每个加载的模块信息 lsmod | tail -n +2 | awk '{print $1}' | whileread mod; do info=$(modinfo "$mod" 2>/dev/null) # 检查模块是否有正常的描述和作者 author=$(echo"$info" | grep "^author:" | head -1) description=$(echo"$info" | grep "^description:" | head -1) filename=$(echo"$info" | grep "^filename:" | head -1) # 标记可疑模块 if [ -z "$author" ] || [ -z "$description" ]; then echo"[!] 可疑模块: $mod (缺少 author 或 description)" echo" $filename" fi done
# 检查模块是否带有有效签名(如果系统启用了模块签名验证) for mod in $(lsmod | tail -n +2 | awk '{print $1}'); do sig=$(modinfo -F sig_id "$mod" 2>/dev/null) if [ -z "$sig" ]; then echo"[?] 未签名模块: $mod" fi done
# 获取内核代码段范围 text_start = None text_end = None with open('/proc/kallsyms') as f: for line in f: parts = line.strip().split() if len(parts) >= 3: if parts[2] == '_text': text_start = int(parts[0], 16) elif parts[2] == '_etext': text_end = int(parts[0], 16)
if text_start and text_end: print(f"内核代码段: 0x{text_start:x} - 0x{text_end:x}") # 检查可疑的系统调用(简化版) with open('/proc/kallsyms') as f: for line in f: parts = line.strip().split() if len(parts) >= 3 and parts[2].startswith('__x64_sys_'): addr = int(parts[0], 16) if addr < text_start or addr > text_end: print(f"[!] 可疑系统调用: {parts[2]} @ 0x{addr:x} (在内核代码段之外)") PYEOF
if [ -f /boot/System.map-$(uname -r) ]; then echo"=== 对比 System.map 与运行时符号 ===" grep " T sys_" /boot/System.map-$(uname -r) | head -20 grep " T sys_" /proc/kallsyms | head -20 # 对比两者的地址 fi
# 检查每个模块的详细信息 for mod in $(ls /sys/module/); do if [ -f /sys/module/$mod/refcnt ]; then refcnt=$(cat /sys/module/$mod/refcnt 2>/dev/null) # 引用计数为 -1 通常表示内置模块 if [ "$refcnt" != "-1" ] 2>/dev/null; then # 检查是否在 lsmod 中 if ! lsmod | grep -q "^$mod "; then echo"[!] 隐藏模块: $mod (在 /sys/module 中但不在 lsmod 中)" fi fi fi done
ROOTDIR is '/' Checking'amd'... not found Checking'basename'... not infected Checking'biff'... not found Checking'chfn'... not infected Checking'chsh'... not infected Checking'cron'... not infected Checking'crontab'... not infected Checking'ifpromisc'... not infected Checking'login'... not infected Checking'ls'... not infected Checking'lsof'... not infected Checking'netstat'... not infected Checking'ps'... not infected Checking'sshd'... not infected
# === 1. 系统命令完整性 === info "1. 检查系统命令完整性 ..." ifcommand -v debsums >/dev/null 2>&1; then modified=$(debsums -c 2>/dev/null) if [ -n "$modified" ]; then alert "以下文件被修改: $modified" else ok "系统命令完整性正常" fi elifcommand -v rpm >/dev/null 2>&1; then modified=$(rpm -Va 2>/dev/null | grep "^..5") if [ -n "$modified" ]; then alert "以下文件 hash 被修改:\n$modified" else ok "系统命令完整性正常" fi fi echo""
# === 2. LD_PRELOAD 检查 === info "2. 检查 LD_PRELOAD ..." preload_content=$(python3 -c " try: print(open('/etc/ld.so.preload').read().strip()) except: pass " 2>/dev/null) if [ -n "$preload_content" ]; then alert "/etc/ld.so.preload 包含内容: $preload_content" else ok "/etc/ld.so.preload 为空或不存在" fi
env_preload=$(grep -r "LD_PRELOAD" /proc/*/environ 2>/dev/null | head -5) if [ -n "$env_preload" ]; then alert "发现进程环境变量中的 LD_PRELOAD:\n$env_preload" fi echo""
# === 3. 隐藏进程检测 === info "3. 检查隐藏进程 ..." ps_count=$(ps -eo pid --no-headers | wc -l) proc_count=$(ls -d /proc/[0-9]* 2>/dev/null | wc -l) diff_count=$((proc_count - ps_count)) if [ ${diff_count#-} -gt 5 ]; then alert "进程数差异较大 (ps=$ps_count, /proc=$proc_count, 差=$diff_count)" else ok "进程数基本一致 (ps=$ps_count, /proc=$proc_count)" fi echo""
# === 4. 内核模块检查 === info "4. 检查内核模块 ..." taint=$(cat /proc/sys/kernel/tainted) if [ "$taint" != "0" ]; then alert "内核已 taint (值=$taint),可能加载了外部模块" else ok "内核未 taint" fi
# 检查未签名模块 unsigned=$(lsmod | tail -n +2 | awk '{print $1}' | whileread mod; do if ! modinfo -F sig_id "$mod" >/dev/null 2>&1; then echo"$mod" fi done) if [ -n "$unsigned" ]; then info "未签名模块: $unsigned" fi echo""
# === 5. PAM 检查 === info "5. 检查 PAM ..." pam_suspicious=$(grep -rn "sufficient.*pam_permit" /etc/pam.d/ | grep "auth" 2>/dev/null) if [ -n "$pam_suspicious" ]; then alert "PAM 配置可疑:\n$pam_suspicious" else ok "PAM 配置未发现明显异常" fi echo""
# === 6. 可疑文件 === info "6. 检查可疑文件 ..." suspicious_so=$(find /tmp /dev/shm /var/tmp -name "*.so" 2>/dev/null) if [ -n "$suspicious_so" ]; then alert "在临时目录发现 .so 文件:\n$suspicious_so" fi
suspicious_ko=$(find / -name "*.ko" -not -path "/lib/modules/*" -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null) if [ -n "$suspicious_ko" ]; then alert "在非标准位置发现 .ko 文件:\n$suspicious_ko" fi echo""
# === 总结 === echo"============================================" if [ $ALERT_COUNT -gt 0 ]; then echo -e "${RED}检测完成: 发现 $ALERT_COUNT 个告警!${NC}" else echo -e "${GREEN}检测完成: 未发现明显异常${NC}" fi echo"============================================"