一、前言
应急响应 Linux 篇幅,由于我们平常使用 Windows 系统较为熟练,对于 Linux 平台通常或会仅限于普通服务搭建使用,不会深入的了解其。日常工作会遇到 Linux 服务器的应急响应工作,于是记录下以便后续的工作能顺利的开展。
二、应急流程
接到应急事件,应急人员应先充分了解事件详情,具体应急事件的类型是恶意攻击、蠕虫攻击、上级通报还是疑似攻击等。充分了解事件过程对应急排查起关键作用,免得做无用功事。
应急前期
开始排查前,应询问是否有安全设备,若有,可通过安全平台针对应急事件类型查看日志获取有用信息,若已捕获相关样本请求提供开展分析。
1、排查是否有异常的系统用户
1 2 3 4
| # 查看用户 cat /etc/passwd 格式:用户名:密码:用户ID:组ID:用户说明:家(home)目录:登陆之后shell 无密码只允许本机登陆,远程不允许ssh登陆
|

1 2 3
| # 查看加密密码 cat /etc/shadow 用户名:加密密码:密码最后一次修改日期:两次密码的修改时间间隔:密码有效期:密码修改到期到的警告天数:密码过期之后的宽限天数:账号失效时间:保留字段
|

2、排查账号信息(按照实际情况查询)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| # 查询特权账户 & 组 cat /etc/passwd | awk -F: '$3==0{print $1}'
# 可以远程登录的账号 awk '/\$1|\$6/{print $1}' /etc/shadow
# 查看哪些用户使用shell cat /etc/passwd | grep /bin/bash
# 查看哪些用户为root权限 cat /etc/passwd | grep x:0
# 查看除了不可登录以外的用户都有哪些,有没有新增的 cat /etc/passwd | grep -v nologin
# 查询系统中所有账户 cat /etc/passwd | awk -F: '{print $1}'
# 查看当前登录用户(tty 本地登陆 pts 远程登录) who
# 查看系统信息,想知道某一时刻用户的行为 w
# 查看登陆多久、多少用户,负载 uptime
# 查看密码文件上一次修改的时间 stat /etc/passwd
# 查询 sudo 权限账户 more /etc/sudoers | grep -v "^#\|^$" | grep "ALL=(ALL)"
|
1 2
| # 查询特权账户 & 组 cat /etc/passwd | awk -F: '$3==0{print $1}'
|

1 2
| # 可以远程登录的账号 awk '/\$1|\$6/{print $1}' /etc/shadow
|

1 2
| # 查看哪些用户使用shell cat /etc/passwd | grep /bin/bash
|

1 2
| # 查看哪些用户为root权限 cat /etc/passwd | grep x:0
|

1 2
| # 查看除了不可登录以外的用户都有哪些,有没有新增的 cat /etc/passwd | grep -v nologin
|

1 2
| # 查询系统中所有账户 cat /etc/passwd | awk -F: '{print $1}'
|

1 2
| # 查看当前登录用户(tty 本地登陆 pts 远程登录) who
|



1 2
| # 查看密码文件上一次修改的时间 stat /etc/passwd
|

1 2
| # 查询 sudo 权限账户 more /etc/sudoers | grep -v "^#\|^$" | grep "ALL=(ALL)"
|

3、排查可疑进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| # 查看全部进程 ps -aux
# 查看关联进程 ps -aux | grep pid
# 查看cpu占用情况(动态) top -c -o %CPU
# 查看CPU占用前十的进程 ps -eo user,pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -n 10
# 按CPU 使用率从高到底排序 ps -ef --sort -pcpu
# 按内存从高到低 ps -ef --sort -pmem
|






4、定位可疑程序路径
1 2 3 4 5 6
| # 定位可疑进程的程序是什么 ps -ef | grep pid号
# 定位可疑进程文件位置 lsof -p PID ls -al /proc/9109/exe
|



5、排查外联情况
lsof
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 显示已建立连接信息 lsof -i -sTCP:ESTABLISHED
# 显示正在监听的端 lsof -i -sTCP:LISTEN
# 查看端口绑定的应用 lsof -i :22
# 显示指定主机连接的信息 lsof -i@1.1.1.1
# 显示基于主机与端口连接的信息 lsof -i@1.1.1.1:3333
|




netstat
1 2 3 4 5 6 7 8 9
| # 查看端口开放和连接情况 netstat -pantu netstat -anulpt
# 指定tcp进程 netstat -tnap
# 指定udp进程 netstat -unap
|


6、监控指定IP的通讯进程
1
| while true; do netstat -antp | grep 10.10.10.1; done
|

7、排查自启项
1 2 3 4 5 6 7 8 9 10 11
| # 查看所有的服务项目 systemctl list-unit-files
# 过滤出所有的开机启动的项目 systemctl list-unit-files |grep enabled
# 查看开机启动项目 chkconfig
# 查看服务自启状态 chkconfig --list
|


8、排查计划任务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| # 查看计划任务 crontab -l
# 查看指定用户计划任务 crontab -u 用户名 -l
# 查看计划任务 cat /var/spool/cron/* # centos cat /var/spool/cron/crontabs/* # ubuntu的 cat /etc/crontab cat /etc/cron.d/* cat /etc/cron.daily/* cat /etc/cron.hourly/* cat /etc/cron.monthly/* cat /etc/cron.weekly/ cat /etc/anacrontab # 异步定时 cat /var/spool/anacron/*
cat crontab -l # 查看定时任务 cat crontab –e # 编辑定时任务 ls /var/spool/cron/ #查看每个用户自己的执行计划 sed '/gcc.sh/d' /etc/crontab && chmod 0000 /etc/crontab && chattr +i /etc/crontab #删除计划任务且控制计划任务不能写东西,慎用。
|





9、排查环境变量

10、查看host文件是否被篡改

11、可疑文件排查
1 2 3 4 5 6 7 8 9 10 11
| # 查找指定目录下的指定时间下生产的文件 find /指定路径 -name "*.jsp" -newermt "2023-10-01 00:00:00" ! -newermt "2024-01-01 00:00:00" -exec ls -l {} \; > /保存路径/result.txt
# 全局搜索指定文件名后缀 find / -name "*.log"
# 指定时间段内搜索具有 ".log" 扩展名的文件 find / -name "*.log" -exec grep -E "^(YYYY-MM-DD HH:MM:SS|YYYY-MM-DD HH:MM:SS)" {} \;
# 全局搜索指定时间段生成的文件 find / -type f -newermt "2022-01-01 00:00:00" ! -newermt "2023-12-31 23:59:59" -exec ls -l --time-style=long-iso {} \; >> search_results.log 2>&1
|
12、排查日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| # 终端输出指定日志指定时间的日志 cat /var/log/syslog | grep "YYYY-MM-DD HH:MM:SS"
# 终端输出日志中指定 ip 的日志 cat /var/log/syslog | grep "外部IP地址"
# 统计爆破主机root账号的失败次数及ip grep "Failed password for root" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more
# 定位哪些IP在爆破 grep "Failed password" /var/log/secure|grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"|uniq -c
# 查看成功登录的日期、用户名、IP grep "Accepted " /var/log/secure | awk '{print $1,$2,$3,$9,$11}'
# 爆破用户名字典是什么? grep "Failed password" /var/log/secure|perl -e 'while($_=<>){ /for(.*?) from/; print "$1\n";}'|uniq -c|sort -nr
# 日志默认存放位置 /var/log
# 查看日志配置情况 more /etc/rsyslog.conf
# 登录日志记录 /var/log/secure
# 登录失败记录 /var/log/btmp
# 登录成功记录 /var/log/wtmp
# 最后一次登录 /var/log/lastlog
# 历史命令 history
# 文件内容中的恶意函数 PHP:eval(、system(、assert( JSP:getRunTime(、 FileOutputStream( ASP:eval(、execute(、 ExecuteGlobal(
# 保存历史命令 cat .bash_history >>history.txt
|

13、webshell 查杀
1 2 3
| # 河马 https://www.shellpub.com,扫描完成之后结果会保存为result.csv文件,使用记事本或者excel打开查看 ./hm scan 你的web目录 ./hm deepscan 你的web目录(扫描时开启深度解码 )
|
14、内存马查杀
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 河马内存马专杀,自动扫描java进程,自动模式下仅支持扫描 tomcat/weblogic/jboss 应用进程 sudo ./hmj javascan
# 指定进程扫描模式,程序仅扫描 -p 指定的进程 sudo ./hmj javascan -p 3361
# 也可以一次指定多个进程 id 进行扫描 sudo ./hmj javascan -p 3361,3356
# 默认情况下在当前执行目录下生成 result.csv 和 data.db 文件,未联网情况下还会生成 result.zip 文件 - result.csv: 恶意内容的列表,包括恶意类型和路径 - data.db: 检测结果的详细信息,sqlite 文件 - result.zip: 检测到的恶意内容 - output 指定输出扫描结果的目录
|
15、清除可疑进程与文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| # 清除进程: ps -aux ps -elf | grep 66579 kill -9 66579
# 清除文件: ps -aux ls -al /proc/67730/exe rm -f /home/c2test
# 使用 ps 查看启动 时间并 杀掉危险进 ps -p 7224(进程PID) -o lstart kill -9 7224(进程PID)
|
三、特定场景
挖矿事件
通过模拟挖矿事件开展应急响应,首先我们知道通常挖矿一个明显特征就是资源占用,内存飙升。通过者一特征很容易判断是否为挖矿事件,当然具体案例得具体分析。
基本流程
1、查看占用情况。
1 2 3 4 5 6 7 8 9 10 11
| # 查看cpu占用情况(动态) top -c -o %CPU
# 查看CPU占用前十的进程 ps -eo user,pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -n 10
# 按CPU 使用率从高到底排序 ps -ef --sort -pcpu
# 按内存从高到低 ps -ef --sort -pmem
|
2、定位恶意程序
1 2 3 4 5 6 7
| # 定位可疑进程的程序是什么 ps -aux ps -ef | grep pid号
# 定位可疑进程文件位置 lsof -p PID ls -al /proc/9109/exe
|
3、清除进程
1 2 3 4 5 6 7 8 9 10 11 12 13
| # 杀掉进程 kill -9 66579
# 查看是否存在子进程 ps ajfx systemctl status
# 杀掉进程组 kill -9 -pid
# 使用 ps 查看启动时间并杀掉危险进 ps -p 7224(进程PID) -o lstart kill -9 7224(进程PID)
|
4、病毒分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 微步 https://s.threatbook.com/
# PCHunter https://scan.anxinsec.com/
# virustotal https://www.virustotal.com/gui/home/upload
# 360 https://ata.360.net/
# 哈勃 https://habo.qq.com/
|
勒索病毒
对于专业的勒索病毒,想要通过解密恢复数据的几率比较渺茫,剩下的就是妥协与不妥协。
基本流程
一旦发现疑似中病毒,无论是否为勒索病毒首先还是得先隔离主机,物理隔离或者资源隔离均可以,把损害降到最低。有备份清除病毒后重新拉取就系,没有备份那就……
解密平台
1 2
| # 360 https://lesuobingdu.360.cn/
|
六、参考
1 2 3 4 5
| https://github.com/wpsec/Emergency-response-notes/blob/main/Linux-%E5%BA%94%E6%80%A5%E5%93%8D%E5%BA%94.md https://blog.csdn.net/qq_44874645/article/details/118459837 https://mp.weixin.qq.com/s/iQSEvvLtpW_39UoymliYPA https://mp.weixin.qq.com/s/eGaQDLVo9yn3fIPFN2bH6w https://mp.weixin.qq.com/s/x6XKzXsnaY6bg_kZn_O3oA
|