Linux 應急響應-溯源-系統日志排查知識點匯總
1. 查看當前已經登錄到系統的用戶 (w
命令)
w
2. 查看所有用戶最近一次登錄 (lastlog
命令)
lastlog
lastlog | grep -v "Never logged in"
3. 查看歷史登錄用戶以及登錄失敗的用戶 (last
和 lastb
命令)
last
lastb
4. SSH 登錄日志分析 (/var/log/secure
)
grep Failed /var/log/secure*
5. 查看系統歷史命令 (.bash_history
)
history
find / -name .bash_history
6. 計劃任務日志 (/var/log/cron
)
cat /var/log/cron* | awk -F':' '{print $NF}' | grep CMD | sort | uniq -c | sort -rn
7. 檢查系統用戶 (/etc/passwd
, /etc/shadow
, /etc/group
)
head -n 1 /etc/passwd
8. 中間件日志 (/var/log/httpd/access_log
)
cat /www/wwwlogs/access_log | less
9. 通過時間檢查站點被黑客修改過的文件
find /www/wwwroot/ecshop.xueshenit.com/ -name "*.php" -mtime -1
10. 檢查服務器已經建立的網絡連接 (netstat
命令)
netstat -anutp
11. 通過 GScan 工具自動排查后門
使用 GScan 工具
unzip GScan-master.zip
cd GScan-master
python GScan.py --help
python GScan.py --pro
12. systemd-journald 服務分析系統日志
systemd-journald 持久化配置
mkdir /etc/systemd/journald.conf.d
vim /etc/systemd/journald.conf.d/99-prophet.conf
# 編輯配置文件,設置 Storage=persistent 等參數
systemctl restart systemd-journald
journalctl 查詢日志
journalctl
journalctl -r
journalctl -u sshd
journalctl -f
13. 實戰清理系統日志后使用 systemd-journald 分析日志
清理日志并使用 journalctl
echo > /var/log/secure
echo > /var/log/messages
# ... 對其他日志文件執行相同操作
journalctl --until "2021-11-05 17:47:00" -o short-precise
附加操作
自定義歷史命令輸出格式
編輯 /etc/profile
或 /etc/bashrc
文件,添加如下內容:
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\\+[ ]*//" ) [$RETRN_VAL]"'
readonly PROMPT_COMMAND
查找特定時間范圍內被修改過的文件
find /www/wwwroot/ecshop.xueshenit.com/ -name "*.php" -newermt '2021-08-01 9:00' ! -newermt '2021-09-15 21:00' | xargs ls -l
這些命令和操作步驟提供了對Linux系統進行應急響應和日志分析的基本方法。實際操作時,可能需要根據具體情況調整命令參數。