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。

猜你喜欢

转载自www.cnblogs.com/10087622blog/p/9591867.html
今日推荐