Skip to content

Linux应急响应指南 #7

Open
@AI0TSec

Description

@AI0TSec

一、排查思路

什么时间?什么现象?确认事件是否属实?LINUX发行版?命令是否被替换?系统命令完整性检测?做过什么处理?当前状态?受影响主机范围?该现象可能产生的原因?可能留下的痕迹?是否部署安全设备/产品?有无相关记录?网络环境架构?是否提供账号密码可登陆受影响主机?是否存在过某些漏洞/弱口令/数据库/中间件/高危端口?

二、排查项目

1. 账户

1.1 异常账户

cat /etc/passwd
cat /etc/shadow

注意文件修改时间,UID和GID为0的账户:grep "0" /etc/passwd

image

1.2 特权用户

awk -F: '$3==0 {print $1}' /etc/passwd

1.3 ROOT/SUDO SU权限用户

image

1.4 远程登陆用户

awk '/\$1|\$6/{print $1}' /etc/shadow

1.5 空口令帐户

awk -F: 'length($2)==0 {print $1}' /etc/shadow

本地登陆:tty、远程登录:pts

  • 查看当前登录用户的信息:who
  • 查看当前已登录用户列表、系统信息、执行命令:w
  • 查看当前用户数、登录时长、负载信息:uptime
  • 查看与当前有效用户ID关联的用户名:whoami
  • 查看所有用户最后一次登录的时间:lastlog
  • 查看所有用户的登录注销信息及系统的启动、重启及关机事件:last
  • 查看用户错误的登录列表:lastb

image
image

2. 端口/进程/网络连接

2.1 查看端口及网络连接情况

netstat -anltp | grep "pid/port/string"

image

2.2 查看异常进程

ps aux | grep "PID"
ps -ef | grep "PID"

image

top

image

top -> M
M – 以内存占用率大小的顺序排列进程列表
top -> N
N – 以 PID 的大小的顺序排列表示进程列表
top -> P
P – 以 CPU 占用率大小的顺序排列进程列表

top -> h

image

top -> f

image

2.3 查看下PID所对应的进程文件路径

ls -la /proc/PID/exe
file /proc/PID/exe

image

2.4 查看隐藏进程

ps -ef | awk '{print}' | sort -n | uniq >1
ls /proc | sort -n |uniq >2
diff 1 2

2.5 查看进程打开的文件

lsof -p PID

image

列出全部进程打开的文件:lsof

2.6 跟踪异常进程运行情况

strace -tt -T -e trace=all -p PID

2.7 终止异常进程

kill -9 PID/ProcessName

image

3. 历史命令

输出:history
清除:history -c

history -c无法清除保存在文件.bash_history中的记录,rm -rf .bash_history可彻底删除保存的历史纪录

HISTFILESIZE的值决定.bash_history文件可保存记录总数
HISTSIZE的值决定history命令输出/显示记录总数

image

4. 服务、启动项

查看系统服务:

service --status-all
ps aux
netstat -anlp
cd /etc/init.d
ls -alt

image

yum install ntsysv
ntsysv

*表示开机自启

image

chkconfig --list

image

查看运行级别命令:runlevel

运行级别 Name(含义) Description(描述)
0 Halt(关机) Shuts down the system(关闭系统)
1 Single-user mode(单用户模式) Mode for administrative tasks(管理模式,类比Windows安全模式)
2 Multi-user mode(多用户模式) Does not configure network interfaces and does not export networks services(未配置网络接口且不提供网络服务)
3 Multi-user mode with networking(存在网络连接的多用户模式) Starts the system normally(正常启动系统)
4 Not used/user-definable(不可用/用户可定义) For special purposes(用于特殊需求/目的)
5 Start the system normally with appropriate display manager (with GUI)(具有图形界面且存在网络连接的多用户模式) Same as runlevel 3 + display manager(运行级别3+图形界面)
6 Reboot(重启) Reboots the system(重新启动操作系统)

开机启动配置文件:

/etc/rc.local
/etc/rc.d/rc.local
/etc/rc.d/rc[0~6].d

image

查看开机启动项:
chkconfig --list | grep "3:on\|5:on"

5. 计划任务

crontab -u root -l
cat /etc/crontab
ls /etc/cron.*

image
image

6. 异常文件、目录

查看/tmp/目录文件、中是否异常

ls –alt /tmp/

查找cron文件中是否存在恶意脚本

/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/*

查找某段时间内被修改/访问的系统文件

更改:find /etc/ /usr/bin/ /usr/sbin/ /bin/ /usr/local/bin/ -type f -mtime 0
访问:find /tmp -iname "*" -atime 1 -type f

-type [参数]
f 普通文件
l 符号连接
d 目录
c 字符设备
b 块设备
s 套接字
p Fifo

modify time -mtime [值]:修改文件内容,mtime/ctime/atime变更;
change time -ctime [值]:修改文件属性/权限,ctime/atime变更;
access time -atime [值]:命令/应用程序读取/访问文件,atime变更
-mtime 0:返回24小时内修改过的文件
-mtime 1:返回48-24小时内修改过的文件
-mtime 2:返回72-48小时内修改过的文件

stat 可显示文件的状态信息

image
image

查看是否有命令文件被替换

按时间进行排序,结合RPM。

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

查看文件属性是否改变

lsattr命令用于显示文件属性
chattr命令用于改变文件属性

查看某个文件属性:lsattr 目录/文件

用chattr命令防止系统中某个关键文件被修改:chattr +i 目录/文件,此时命令操作该文件所得结果提示为Operation not permitted,VIM编辑该文件时会提示W10: Warning: Changing a readonly file错误。要想修改此文件就要把i属性去掉:chattr -i 目录/文件

设置某个文件只能往里面追加数据,但不能删除,适用于各种日志文件:chattr +a 目录/文件

6. 漏洞

弱口令、未授权访问漏洞、Web漏洞、系统漏洞

7. 日志

日志 说明
/var/log/syslog /var/log/messages 显示有关系统的常规消息和信息
/var/log/auth.log /var/log/secure 保留成功或失败登录以及身份验证过程的身份验证日志
/var/log/boot.log 启动消息和启动信息
/var/log/maillog var/log/mail.log 用于邮件服务器日志,方便用于服务器上运行的postfix,smtpd或电子邮件相关服务信息
/var/log/kern 保留内核日志和警告信息
/var/log/dmesg 设备驱动程序消息的存储库
/var/log/faillog 记录失败登录信息
/var/log/cron 记录计划任务相关的信息
/var/log/daemon.log 跟踪正在运行的后台服务
/var/log/btmp 记录所有失败的登录尝试
/var/log/utmp 用户的当前登录状态
/var/log/wtmp 每次登录/注销的记录
/var/log/lastlog 保存每个用户的上次登录信息
/var/log/yum.log 保存使用yum命令的任何软件包安装的数据
/var/log/httpd/ 包含Apache httpd守护程序的error_log和access_log文件的目录
/var/log/mysqld.log /var/log/mysql.log 记录每个调试失败和成功消息的MySQL日志文件,包括启动,停止和重新启动MySQL守护程序mysqld
/var/log/pureftp.log 使用pureftp进程监视FTP连接,包含FTP登录和身份验证失败的数据
/var/log/spooler 通常不包含任何内容,除了来自USENET的罕见消息
/var/log/xferlog 保持FTP文件传输会话,包括文件名和用户启动的FTP传输等信息
  • 查看登录成功的日期、用户名及IP:grep "Accepted " /var/log/secure* | awk '{print $1,$2,$3,$9,$11}'
  • 查看试图爆破主机的IP:
    开启iptables:grep "refused" /var/log/secure* | awk {'print $9'} | sort | uniq -c |sort -nr | more
    未开启iptables: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
  • 查看爆破主机的ROOT账号的IP: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

攻击者清除痕迹:

rm -rf /root/.bash_history
echo > /var/log/日志

三、安全工具

1. Rootkit查杀

Chkrootkit

下载:wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz

image

解压:tar zxvf chkrootkit.tar.gz

编译:

Centos编译环境:yum install gcc gcc-c++ make glibc-static

cd chkrootkit-版本
make sense

image

将编译好的文件移动至根目录下:

mv /root/chkrootkit-版本 /

执行:

若存在INFECTED提示,则说明存在Rootkit。

./chkrootkit | grep "INFECTED" > scanresult.txt

chkrootkit扫描过程使用部分系统命令,若服务器被入侵后,恶意攻击者修改系统命令,则chkrootkit扫描结果不可信。

Rootkit Hunter

下载:wget https://sourceforge.net/projects/rkhunter/files/rkhunter/1.4.6/rkhunter-1.4.6.tar.gz
解压:tar -zxvf rkhunter-1.4.6.tar.gz
安装:

cd rkhunter-1.4.6
./installer.sh --install

image

运行:rkhunter -c --rwo
扫描结果:

cat /var/log/rkhunter.log
rkhunter -c --rwo > scanresult.txt

image

2. 病毒查杀

Clamav

方法一

下载:wget http://www.clamav.net/downloads/production/clamav-0.101.2.tar.gz
解压:tar -zxvf clamav-0.101.2.tar.gz
安装:

cd clamav-0.101.2
./configure
make
make check
make install

运行:

cd clamscan
clamscan -h

方法二

下载安装:yum install -y clamav
病毒库攻击:freshclam
使用:clamscan -h

Clamav文档:https://www.clamav.net/documentation

Webshell查杀

CloudWalker

下载:https://github.com/chaitin/cloudwalker/releases
添加可执行权限:chmod +x webshell-detector-1.0.0-linux-amd64
使用:./webshell-detector-1.0.0-linux-amd64 /path/to/your-web-root/ /var/www/html /path/to/some-file
输出:./webshell-detector-1.0.0-linux-amd64 -html -output result.html /path/to/web-root/

SangforWebshell

下载:wget https://edr.sangfor.com.cn/api/download/WebShellKillerForLinux.tar.gz
解压:tar -zxvf WebShellKillerForLinux.tar.gz

image

添加可执行权限:chmod +x ./wscan

image

河马Webshell

下载:wget -O hm-linux.tgz http://down.shellpub.com/hm/latest/hm-linux-amd64.tgz

Linux系统32位/64位确认:arch、uname -r、uname -m、uname -a

image

image

解压:

ls
tar xvf hm-linux.tgz

使用:

./hm -h
./hm version
./hm version
./hm scan /your-web-root

image

3. 系统完整性/RPM check检查

RPM

用法:

rpm --usage
rpm --help
rpm -?

image

使用:
rpm -Va > rpm.log

单个.表示测试通过,单个?表示无法执行测试
http://ftp.rpm.org/max-rpm/rpm.8.html

image

输出格式:8个字符

  • S file Size differs
  • M Mode differs (includes permissions and file type)
  • 5 MD5 sum differs
  • D Device major/minor number mismatch
  • L readLink path mismatch
  • U User ownership differs
  • G Group ownership differs
  • T mTime differs

属性标记:

  • c %config configuration file
  • d %doc documentation file
  • g %ghost file
  • l %license license file
  • r %readme readme file

4. 恶意流量

Maltrail

5. 自动化检测/安全检查脚本/基线检查脚本

GScansecurity_checklinux安全检查LynisLinEnumosquery

五、参考链接

https://bypass007.github.io/Emergency-Response-Notes/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions