我正在整理一个项目,收集和汇总了一些应急响应案例(不断更新中)。
GitHub 地址:https://github.com/Bypass007/Emergency-Response-Notes
本文主要介绍Linux日志分析的技巧,更多详细信息请访问Github地址,欢迎Star。
Linux系统拥有非常灵活和强大的日志功能,可以保存几乎所有的操作记录,并可以从中检索出我们需要的信息。本文简介一下Linux系统日志及日志分析技巧。
日志默认存放位置:/var/log/
查看日志配置情况:more /etc/rsyslog.conf
登录失败记录:/var/log/btmp //lastb
最后一次登录:/var/log/lastlog //lastlog
登录成功记录: /var/log/wtmp //last
登录日志记录:/var/log/secure
目前登录用户信息:/var/run/utmp //w、who、users
历史命令记录:history 仅清理当前用户:history -c
Linux下常用的shell命令如:find、grep 、egrep、awk、sed
grep -rn “hello,world!”
* : 表示当前目录所有文件,也可以是某个文件名
-r 是递归查找
-n 是显示行号
-R 查找所有文件包含子目录
-i 忽略大小写
cat input_file | tail -n +1000 | head -n 2000
#从第1000行开始,显示2000行。即显示1000~2999行
//在目录/etc中查找文件init
cat /etc/passwd |awk -F ‘:’ ‘{print $1}’
//awk -F指定域分隔符为’:’,将记录按指定的域分隔符划分域,填充域,•$0则表示所有域,$1表示
删除历史操作记录,只保留前153行
以这个日志为例:/var/log/secure
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
爆破用户名字典是什么?
grep “Failed password” /var/log/secure|perl -e ‘while($_=<>){ /for(.*?) from/; print “$1\n”;}’|uniq -c|sort -nr
grep "Accepted " /var/log/secure | awk ‘{print $11}’ | sort | uniq -c | sort -nr | more
登录成功的日期、用户名、IP:
grep "Accepted " /var/log/secure | awk ‘{print $1,$2,$3,$9,$11}’
Jul 10 00:12:15 localhost useradd[2382]: new group: name=kali, GID=1001
Jul 10 00:12:15 localhost useradd[2382]: new user: name=kali, UID=1001, GID=1001, home=/home/kali
, shell=/bin/bash
Jul 10 00:12:58 localhost passwd: pam_unix(passwd:chauthtok): password changed for kali
#grep “useradd” /var/log/secure
Jul 10 00:14:17 localhost userdel[2393]: delete user ‘kali’
Jul 10 00:14:17 localhost userdel[2393]: removed group ‘kali’ owned by ‘kali’
Jul 10 00:14:17 localhost userdel[2393]: removed shadow group ‘kali’ owned by ‘kali’
#grep “userdel” /var/log/secure
Jul 10 00:38:13 localhost su: pam_unix(su-l:session): session opened for user good by root(uid=0)
sudo授权执行:
sudo -l
Jul 10 00:43:09 localhost sudo: good : TTY=pts/4 ; PWD=/home/good ; USER=root ; COMMAND=/sbin/shutdown -r now
2、/var/log/yum.log
软件安装升级卸载日志:
yum install gcc
[root@bogon ~]# more /var/log/yum.log
Jul 10 00:18:23 Updated: cpp-4.8.5-28.el7_5.1.x86_64
Jul 10 00:18:24 Updated: libgcc-4.8.5-28.el7_5.1.x86_64
Jul 10 00:18:24 Updated: libgomp-4.8.5-28.el7_5.1.x86_64
Jul 10 00:18:28 Updated: gcc-4.8.5-28.el7_5.1.x86_64
Jul 10 00:18:28 Updated: libgcc-4.8.5-28.el7_5.1.i686