倘若服务器被入侵了,要检查哪些地方?

其实可以一句话概括:
检查可疑网络链接,可疑进程,可疑文件,可疑脚本任务
不过对于小白,还是建议重装,重装可以解决 99%问题。

1.查看异常进程活动-查找是否有异常进程和端口占用

1.1 查找占用 cpu 最多的进程

运行 top 命令后,键入大写字母 P 按 cpu 排序

1.2 查找占用内存最多的进程

运行 top 命令后,键入大写字母 M
ps aux | sort -k4nr

1.3 查找进程对应的文件

ls -la /proc/$pid/exe
ls -la /proc/$pid

1.4 跟踪异常进程运行情况

strace -tt -T -e trace=all -p $pid

1.5 查看进程打开的文件

lsof -p $pid

1.6 查看进程端口情况

查看所有端口
netstat -anltp
查看指定进程的端口
netstat -anltp | grep $pid
netstat -apn|more

1.7 清除恶意进程

清除可疑进程的进程链: ps -elf | grep [pid] kill -9 [pid]

2.查看账号安全

2.1 查看是否有存在新增异常账号

查找特权用户
awk -F ":" '$3==0{print $1}' /etc/passwd
查找可以远程登录的账号信息
awk '/\$1|\$6/{print $1}' /etc/shadow
查找 sudo 权限账户
cat /etc/sudoers | grep -v "^#\|^$" | grep "ALL=(ALL)"

2.2 查看是否有账号异常登录情况

查看当前登录用户和其行为
w
查看所有用户最后一次登录的时间
lastlog
查看错误登陆信息
lasstb
查看所有用户的登录注销信息及系统的启动、重启及关机事件
last
查看登录成功的日期、用户名及 ip
grep "Accepted " /var/log/secure* | awk '{print $1,$2,$3,$9,$11}'
查看试图爆破主机的 ip
grep refused /var/log/secure* | awk {'print $9'} | sort | uniq -c |sort -nr | more
grep "Failed password" /var/log/secure* | grep -E -o "(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}))" | uniq -c
查看有哪些 ip 在爆破主机的 root 账号
grep "Failed password for root" /var/log/secure | awk '{print $11}' | sort
查看爆破使用的用户名字典
grep "Failed password" /var/log/secure | awk {'print $9'} | sort | uniq -c | sort -nr

3.检查开机启动项

一般通过crontab -l命令即可检测到定时任务后门。不同的 linux 发行版可能查看开机启动项的文件不大相同,Debian 系 linux 系统一般是通过查看/etc/init.d目录有无最近修改和异常的开机启动项。而 Redhat 系的 linux 系统一般是查看/etc/rc.d/init.d或者/etc/systemd/system等目录。
依次排查:

1
2
3
4
5
6
7
8
9
/var/spool/cron/*
/etc/crontab
/etc/cron.d/*
/etc/cron.daily/*
/etc/cron.hourly/*
/etc/cron.monthly/*
/etc/cron.weekly/
/etc/anacrontab
/var/spool/anacron/*

其后键入
cat /etc/rc.local
cat /etc/init.d/rc.local
chkconfig --list
ls -alt /etc/init.d
查看 anacron 异步定时任务 cat/etc/anacrontab
枚举主机所有服务 service--status-all

4.查找异常文件

4.1 查看最近一段时间内被修改的系统文件

查找find /etc/ /usr/bin/ /usr/sbin/ /bin/ /usr/local/bin/ -type f -mtime -T | xargs ls -la

4.2 按时间排序,确认最近是否有命令被替换,可以结合 rpm -Va 命令

ls -alt /usr/bin /usr/sbin /bin /usr/local/bin
rpm -Va>rpm.log

4.3 查看指定目录下文件时间的排序

ls -alt | head -n 10

4.4 使用 find 指令查找限定时间范围的文件

sudo find ./ -cmin -10 -name "*.php"

5.排查恶意 alias

检查 ~/.bashrc~/.bash_profile

6.排查恶意 ssh 公钥

/etc/.ssh/AuthorizedKeys
~/.ssh/AuthorizedKeys

7.系统文件被替换的情况下使用busybox

被替换的系统文件通常为 ld.so.preload
busybox ps -ef|grep watchdogs
busybox ps -ef|grep ksoftirqds

8.借助工具查杀病毒和 rootkit

8.1 查杀 rootkit

chkrootkit
(下载地址:http://www.chkrootkit.org)
rkhunter
(下载地址:http://rkhunter.sourceforge.net)

8.2 查杀病毒

clamav
(下载地址:http://www.clamav.net/download.html)

8.3 查杀 webshell

cloudwalker
(下载地址:http://github.com/chaitin/cloudwalker)

9. 添加命令审计

为历史的命令增加登录的 IP 地址、执行命令时间等信息

9.1 保存 1 万条命令:

sed -i 's/^HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile

9.2 在/etc/profile 的文件尾部添加如下行数配置信息

1
2
3
4
5
6
7
8
9
USER_IP=`who -u am i 2>/dev/null | awk '{print $NF}' | sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi

export HISTTIMEFORMAT="%F %T $USER_IP `whoami` "
shopt -s histappend
export PROMPT_COMMAND="history -a"

写在最后

RootKit Hunter 後端偵測軟體之架設與執行
emergency-response-checklist

作者

Catooilg

发布于

2023-07-31

更新于

2023-09-14

许可协议

评论