在日常巡检中发现其中一台服务器启动java服务后进程自动关闭,查看服务日志无异常,查看系统日志时发现报错:

[root@localhost ~]# tail /var/log/messages
Nov 14 14:25:48 localhost kernel: nf_conntrack: table full, dropping packet

查看相关文档得知是由于网络链接过多导致,使用top命令时发现存在异常进程名称,通过ps命令定位后得知服务器被入侵。

排查思路

检查防火墙

由于项目中的服务器设备均设置了iptables及ip6tables网络防火墙,故优先查看防火墙配置是否正常。

[root@localhost ~]# iptables -L -n
[root@localhost ~]# ip6tables -L -n
恢复防火墙

通过查看得知,防火墙规则均已被清空。服务器已经暴露敏感端口。
重启防火墙服务,恢复防火墙规则。

[root@localhost ~]# systemctl restart iptables
[root@localhost ~]# systemctl restart ip6tables
查看系统用户

通过查看/etc/passwd文件得知,存在多个用户ID为0的用户,分别为root、gpadmin

删除异常用户

查看存在/bin/bash及/bin/sh的用户,发现异常用户即删除。

userdel -r postgres
userdel -r gpadmin
userdel -r amandabackup
userdel -r admin
userdel -r noc

由于gpadmin用户为超级用户,则需手动修改/etc/passwd文件修改用户ID后继续删除。

[root@localhost ~]# userdel -r gpadmin
userdel: user gpadmin is currently used by process 1
检查开机启动项

检查/etc/rc.d/目录下所有可执行文件,查看是否存在异常名称,异常命令。
通过查看发现,存在多个可执行文件异常,其中包含关闭防火墙及恶意命令,发现后清理对应文件及进程。

[root@localhost ~]# egrep -v '^#|^$' /etc/rc.d/rc.local
touch /var/lock/subsys/local
service iptables stop
iptables -P INPUT ACCEPT;iptables -F
[root@localhost init.d]# ll
总用量 52
-rwxr-xr-x. 1 root root    20 11月 10 18:29 DbSecuritySpt
-rw-r--r--. 1 root root 18281 5月  22 2020 functions
-rwxr-xr-x. 1 root root  4569 5月  22 2020 netconsole
-rwxr-xr-x. 1 root root  7928 5月  22 2020 network
-rw-r--r--. 1 root root  1160 10月  2 2020 README
-rwxr-xr-x. 1 root root    36 11月 10 18:29 selinux
-rwxr-xr-x. 1 root root  2177 11月  8 20:13 zabbix_agentd
[root@localhost init.d]# egrep -v '^#|^$' DbSecuritySpt 
/etc/mm
[root@localhost init.d]# egrep -v '^#|^$' selinux 
/usr/bin/bsd-port/getty
检查异常链接

通过执行命令查看当前网络链接进程

[root@localhost ~]# netstat -anptlu|grep '/'|grep -v 'LISTEN'
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      1 110.52.49.105:47488      35.205.61.67:6001       SYN_SENT    3478/getty          
tcp        0      0 110.52.49.105:44702      221.206.127.7:5000      ESTABLISHED 2699/mm             
udp        0      0 0.0.0.0:56566           0.0.0.0:*                           2112/avahi-daemon:  
udp        0      0 0.0.0.0:111             0.0.0.0:*                           2049/rpcbind        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           2104/chronyd        
udp        0      0 0.0.0.0:939             0.0.0.0:*                           2049/rpcbind        
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           2112/avahi-daemon:  
udp6       0      0 :::111                  :::*                                2049/rpcbind        
udp6       0      0 ::1:323                 :::*                                2104/chronyd        
udp6       0      0 :::939                  :::*                                2049/rpcbind
阻断异常链接
iptables -I INPUT -s 221.206.127.0/24 -j DROP
iptables -I OUTPUT -s 221.206.127.0/24 -j DROP

iptables -I INPUT -s 35.205.61.0/24 -j DROP
iptables -I OUTPUT -s 35.205.61.0/24 -j DROP
检查最近修改过的文件

检查最近15天内修改过的文件,发现异常文件进行清理并终止相关进程,常出现的目录为/usr/etc/tmp/lib/var/opt及用户家目录。

[root@localhost ~]# find /usr/ -mtime -15 -type f -print
/usr/bin/ps
/usr/bin/netstat
/usr/bin/bsd-port/getty
/usr/bin/bsd-port/getty.lock
/usr/bin/bsd-port/conf.n
/usr/bin/.sshd
/usr/bin/dpkgd/netstat
/usr/bin/dpkgd/ps
/usr/bin/dpkgd/lsof
/usr/bin/dpkgd/ss
/usr/sbin/ss
/usr/sbin/lsof


[root@localhost ~]# find /etc/ -mtime -15 -type f -print
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
/etc/subgid
/etc/subuid
/etc/sysconfig/ip6tables
/etc/rc.d/init.d/DbSecuritySpt
/etc/rc.d/init.d/selinux
/etc/rc.d/init.d/zabbix_agentd
/etc/group-
/etc/gshadow-
/etc/passwd-
/etc/cups/subscriptions.conf.O
/etc/cups/subscriptions.conf
/etc/shadow-
/etc/ks
/etc/mm
/etc/csm
/etc/subuid-
/etc/subgid-
/etc/conf.n
/etc/fake.cfg
/etc/cmd.n
查看进程运行信息

在发现异常文件时可以不着急删除,可以用lsof命令查看异常PID的运行进程,以便获得更多的信息。

lsof -n -p 2699
终止相关进程
pkill -9 freeBSD
pkill -9 mm
pkill -9 getty
pkill -9 kg
pkill -9 ks
pkill -9 cs
pkill -9 txma
pkill -9 fwupd
pkill -9 agetty
pkill -9 ps
清理相关文件
chattr -i /usr/bin/bsd-port/
chattr -i /usr/bin/bsd-port/*
chattr -i /usr/libexec/fwupd/*
rm -f /etc/gs /etc/rc.d/init.d/DbSecuritySpt /etc/rc.d/init.d/selinux /etc/mm /usr/bin/bsd-port/getty /etc/ooo /etc/cs /etc/freeBSD /etc/mm /usr/bin/Xorg /etc/kg /etc/254 /etc/ks /etc/cmd.n /etc/conf.n /etc/csm /etc/fake.cfg /etc/kos /etc/ca
rm -rf /usr/bin/.sshd /usr/bin/bsd-port/ /usr/bin/dpkgd/
rm -rf /opt/cs /opt/conf.n /opt/csm /opt/fake.cfg
修复被篡改命令

Centos7系统netstat、ss、ps和lsof命令存在被篡改情况,需要进行重新安装。

rpm -e net-tools --nodeps
yum -y install net-tools
rpm -e lsof --nodeps
yum -y install lsof
rpm -e iproute --nodeps
yum -y install iproute
rpm -e procps-ng --nodeps
yum -y install procps-ng
检查异常运行进程
检查sshd异常开放端口
ps ax -e -o pid,command|grep 'sshd[[:space:]]-p'
检查ssh异常端口占用

通过该命令可以看到ssh进程,根据实际情况处理

ps ax -e -o pid,command|grep 'ssh[[:space:]]'
检查反向Shell
ps ax -e -o pid,command|grep 'sh[[:space:]]-i'
检查异常PID残留
Check_Deleted_Process_Pid_List=`ps ax -e -o pid|awk 'NR>1'`
for Check_Deleted_Pid in ${Check_Deleted_Process_Pid_List};do
    Check_Process_Deleted_Info=`lsof -n -p "${Check_Deleted_Pid}" -a -d txt +c 15 2>/dev/null| grep -F '(deleted)'`
    if [ ! -z "${Check_Process_Deleted_Info}" ];then
        echo "存在疑似异常PID ${Check_Deleted_Pid} 的进程:${Check_Process_Deleted_Info}"    fi
done
END

本文标题:记一次服务器被侵入排查及处理过程

本文作者:宇宙最帅的男人

本文链接:https://lolicp.com/linux/202215469.html

版权声明:转载或者引用本文内容请注明来源及原作者,本文著作权归作者 (宇宙最帅的男人) 所有。

除非另有说明,本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

最后修改:2022 年 11 月 28 日
如果觉得我的文章对你有用,请随意赞赏