最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失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/bashdate >>/tmp/caq.txtecho "PPID of this script: $PPID" >>/tmp/caq.txtps -ef|grep $PPID |grep -v grep >>/tmp/caq.txtecho "rm $* now" >>/tmp/caq.txtrm_elf $*
效果如下:
[root@centos7 tmp]# touch 555[root@centos7 tmp]# rm 555rm_elf:是否删除普通空文件 "555"?y[root@centos7 tmp]# cat /tmp/caq.txt2018年 09月 05日 星期三 14:22:58 CSTPPID of this script: 6121root 5707 6121 0 14:22 pts/1 00:00:00 /bin/bash /usr/bin/rm -i 555root 6121 5497 0 9月04 pts/1 00:00:01 -bashrm -i 555 now
恩,很小很简单,但是能work。