作为IT工程师,确保Linux服务器的安全性至关重要。以下是一套使用命令行工具进行入侵检测的全面方法:
# 检查异常用户账户
cat /etc/passwd | grep -v "nologin\|false\|sync\|shutdown\|halt"
# 检查空密码账户
awk -F: '($2 == "") {print $1}' /etc/shadow
# 检查UID为0的账户(除root外)
awk -F: '($3 == 0) {print $1}' /etc/passwd
# 检查最近登录的用户
last -n 10
# 查看异常进程
ps aux --sort=-%cpu | head -n 11
ps aux --sort=-%mem | head -n 11
# 检查隐藏进程
ps -ef | awk '{print $2}' | sort -n | uniq -d
# 检查网络连接
netstat -tulnp
ss -tulnp
lsof -i
# 查找SUID/SGID文件
find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \;
# 查找可写文件
find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -print
# 查找近期修改的文件
find / -mtime -3 -type f -exec ls -la {} \;
# 检查认证日志
grep -i "fail\|invalid\|refused" /var/log/auth.log
journalctl -u sshd --no-pager | grep -i "fail\|invalid"
# 检查暴力破解尝试
grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr
# 检查root登录
grep "Accepted password for root" /var/log/auth.log
# 检查iptables/nftables规则
iptables -L -n -v
nft list ruleset
# 检查异常监听端口
netstat -tulnp | grep -v "127.0.0.1"
# 检查路由表
route -n
ip route show
# 检查cron任务
ls -la /etc/cron*
cat /etc/crontab
ls -la /var/spool/cron/crontabs/
# 检查systemd定时器
systemctl list-timers --all
# 检查加载的内核模块
lsmod
# 检查异常内核模块
find /lib/modules/$(uname -r) -type f -name '*.ko' | xargs ls -la
# 使用rkhunter检查
rkhunter --check --sk
# 使用chkrootkit检查
chkrootkit
# 检查二进制文件完整性
for file in /bin/* /sbin/* /usr/bin/* /usr/sbin/*; do rpm -Vf $file 2>/dev/null; done
#!/bin/bash
# 简易入侵检测脚本
echo "=== 系统用户检查 ==="
awk -F: '($3 == 0) {print $1}' /etc/passwd
echo ""
echo "=== 异常进程检查 ==="
ps aux --sort=-%cpu | head -n 11
echo ""
echo "=== 网络连接检查 ==="
netstat -tulnp
echo ""
echo "=== 最近修改的文件 ==="
find / -mtime -3 -type f -exec ls -la {} \; 2>/dev/null | head -n 20
通过以上命令行工具和技术,您可以有效地监控Linux服务器的安全性,及时发现潜在的入侵行为并采取相应措施。