linux 一个跟踪文件删除的小技巧

时间:2023-12-29 08:24:50

最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失1到两个。

为了解决这个问题,我让他布置audit,具体可以man一下auditctl。

过了一天,他说audit.log中抓到了,知道是某个pid做的动作,但是由于该pid是瞬间的,无法知道是谁干的,只知道是调用rm干的。

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

 file /usr/bin/rm
/usr/bin/rm: ELF -bit LSB executable, x86-, version (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6., 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
[root@centos7 tmp]# rm
rm_elf:是否删除普通空文件 ""?y
[root@centos7 tmp]# cat /tmp/caq.txt
2018年 09月 05日 星期三 :: CST
PPID of this script:
root : pts/ :: /bin/bash /usr/bin/rm -i
root 9月04 pts/ :: -bash
rm -i now

恩,很小很简单,但是能work。