linux 一個跟蹤文件刪除的小技巧


最近有同事問我說他有個現場環境,經常會丟失業務文件,每天都出現,幾百個里面丟失1到兩個。

為了解決這個問題,我讓他布置audit,具體可以man一下auditctl。

過了一天,他說audit.log中抓到了,知道是某個pid做的動作,但是由於該pid是瞬間的,無法知道是誰干的,只知道是調用rm干的。

然后,我file查看一下rm的屬性。

 file /usr/bin/rm
/usr/bin/rm: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=7d9d4d6f6883e3638816d898d389e797814a1a1c, stripped

然后將rm 通過mv 重命名為rm_elf.

再touch一個文件,叫rm,寫入腳本,先記錄日志,再執行真正的rm,類似如下:

[root@centos7 tmp]# cd /usr/bin/
[root@centos7 bin]# ls rm*
rm  rmail  rmail.postfix  rmdir  rm_elf  rmic  rmid  rmiregistry
[root@centos7 bin]# cat rm
#!/bin/bash
date >>/tmp/caq.txt
echo "PPID of this script: $PPID" >>/tmp/caq.txt
ps -ef|grep $PPID |grep -v grep >>/tmp/caq.txt
echo "rm $* now" >>/tmp/caq.txt
rm_elf $*

效果如下:

[root@centos7 tmp]# touch 555
[root@centos7 tmp]# rm 555
rm_elf:是否刪除普通空文件 "555"?y
[root@centos7 tmp]# cat /tmp/caq.txt
2018年 09月 05日 星期三 14:22:58 CST
PPID of this script: 6121
root      5707  6121  0 14:22 pts/1    00:00:00 /bin/bash /usr/bin/rm -i 555
root      6121  5497  0 9月04 pts/1   00:00:01 -bash
rm -i 555 now

恩,很小很簡單,但是能work。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM