标签:ddr type bin grep 内容 code cat b站 fine
监控web站点目录(/var/html/www)下所有文件是否被tampered(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)#define variable
wwwdir=/opt/shell/www
logdir=/opt/shell/filelog
md5sum_before=$logdir/md5sum_before.log
fileinfo_before=$logdir/fileinfo_before.log
#监控tampered之前文件md5sum值
if [ ! -d $logdir ];then
mkdir -p $logdir
fi
if [ ! -f $md5sum_before ];then
touch $md5sum_before
fi
find $wwwdir -type f|xargs md5sum > $md5sum_before
#记录tampered之前www目录下面文件数量
if [ ! -f $fileinfo_before ];then
touch $fileinfo_before
fi
find $wwwdir -type f > $fileinfo_before
获取tampered后的信息的脚本如下:
-bash-4.1# vim ../tampered.sh
#!/bin/sh
#######################################################
#ShellName:Is www tampered with
#Author:zkg
#Created Time:2019-08-28
#Blog Address:https://blog.51cto.com/1009516
#######################################################
#define variable
wwwdir=/opt/shell/www
logdir=/opt/shell/filelog
md5sum_before="${logdir}/md5sum_before.log"
fileinfo_before="${logdir}/fileinfo_before.log"
fileinfo_after="${logdir}/fileinfo_after.log"
email=632223282.qq.com
time=date "+%Y-%m-%d %H:%M:%S"
if [ ! -d $logdir ];then
echo "存储文件变化目录不存在,无法进行比对"
exit 1
fi
if [ ! -f $md5sum_before -o ! -f $fileinfo_before ];then
echo "存储文件变化的文件不存在,无法进行比对"
exit 1
fi
filenum_before=cat $fileinfo_before | wc -l
#记录tampered之后www目录下面文件数量
if [ ! -f $fileinfo_after ];then
touch $fileinfo_after
fi
find $wwwdir -type f > $fileinfo_after
filenum_after=cat $fileinfo_after | wc -l
#记录tampered 信息到文件中,邮件告警
emaillog=$logdir/emaillog.log
if [ ! -f $emaillog ];then
touch $emaillog
fi
#tampered标志可以通过两个方面进行比较:
#将错误信息追加空设备
changemd5sum=md5sum -c $md5sum_before 2>/dev/null |grep -Ei "FAILED" |wc -l
if [ $changemd5sum -ne 0 -o "$filenum_before" -ne "$filenum_after" ];then
echo md5sum -c $md5sum_before 2>/dev/null |grep -Ei "FAILED"
> $emaillog
diff $fileinfo_after $fileinfo_before >> $emaillog
#mail -s "网站目录被tampered ,时间为 $time" $email < $emaillog
fi
标签:ddr type bin grep 内容 code cat b站 fine
原文地址:https://blog.51cto.com/1009516/2433149