对某个目录创建,删除文件监控

挖矿病毒;应用程序和系统漏洞
勒索病毒:

可执行文件/usr/bin 串改 注入
网站根目录/wwwroot 串改 注入

#安装inotify-tools工具
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum -y install inotify-tools

inotifywait
    用法:inotifywait [-hcmrq] [-e <event> ] [-t <seconds> ] [--format <fmt> ] [--timefmt <fmt> ] <file> [ ... ]
    选项:
      -m:监视
      -r:递归监视
      -q:减少冗余信息
      -e/--event:要监视的事件列表;
        可监视的事件:create,delete,modify,move,access,attrib(元数据被修改),open,close
      -t seconds:过期时长,即超出该时长退出监视,缺省为0,表示无限期监视
      --format:指定事件信息的输出格式
        %w:发生事件的目录或文件
        %f:发生事件的文件
        %e:发生的事件
        %T:使用由-timefmt定义的时间格式
      --timefmt:指定时间格式,具体用法可man inotifywait
      --exclude <pattern>:指定排除不需要监视的文件模式
      --fromfile <file>:从file中读取要监视或排除监视的文件(或目录),一行一个,不能使用正则表达式,以@开头的表示排除监视
    例:inotifywait -mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f %e' -e modify,delete,create,move,attrib /tmp/test
#!/bin/bash
#指定要监控的目录
MON_DIR=/opt
#持续递归监视发生事件的文件,-e实时打印监听的文件,\换行
inotifywait -mqr --format %f -e create $MON_DIR |\
#读取接收事件产生的文件
while read files; do
#/opt 与/tmp/opt下产生的文件保持实时同步(例如实时备份的场景可以用到)
rsync -avz /opt /tmp/opt
#echo "$(date +'%F %T') create $files" | mail -s "dir monitor" xxx@163.com
done
#后台保持实时运行
nohup bash 18.sh &>/dev/null &
ps -ef |grep 18.sh