zoukankan      html  css  js  c++  java
  • 网站目录监控脚本,shell,awd用

    #! /bin/bash
    #网站目录监控脚本
    #CTF赛用
    #功能如下:
    #1、网站根目录备份。
    #2、监控网站目录文件新增、删除、修改,并且恢复,记录日志,支持多级目录的恢复,支持隐藏文件的新增修改和删除。
    
    #不足点:
    #1、脚本执行后关停,再次重启会屏显报错,且无法建立日志,但不影响网站监控恢复功能。如需生成日志,需要删除对应的文件夹再重启脚本。
    #2、监控恢复周期不稳定,0~5s不等。
    
    #检查当前目录下是否存在配置文件,若未发现control.conf文件,通过交互式输入获取配置信息,并保存到新建的配置文件中。
    #四个参数分别为:监控的网站根目录webroot,网站根目录备份目录bak_content,网站目录中被篡改或删除的文件以及日志保存的文件夹changefile_contentf,网站目录中新增的文件以及日志保存的文件夹newfile_contentf。
    if [ ! -f control.conf ];then
    	read -p "Please input your websiteroot:" webroot
    	echo "set websiteroot $webroot!"
    	read -p "Please set your baksite content:" bak_content
    	echo "set baksite content $bak_content!"
    	read -p "Please set changefile content:" changefile_contentf
    	echo "set changefile content $changefile_contentf!"
    	read -p "Please set newfile content:" newfile_contentf
    	echo "set newfile content $newfile_contentf!"
    	echo $webroot >> control.conf
    	echo $bak_content >> control.conf
    	echo $changefile_contentf >> control.conf
    	echo $newfile_contentf >> control.conf
    	echo "Your configuration are saved in control.conf!"
    #发现配置文件,配置信息从配置文件中读取。
    else 
    	webroot=`sed -n 1p control.conf`
    	bak_content=`sed -n 2p control.conf`
    	changefile_contentf=`sed -n 3p control.conf`
    	newfile_contentf=`sed -n 4p control.conf`
    	echo "Load configuration from control.conf success!"
    fi
    
    #根据配置文件新建文件夹,检测文件夹是否存在,若不存在则新建。
    if [ ! -d $bak_content ];then
    #        mkdir $bak_content
    #        echo "Make content $bak_content success!"
            cp -r $webroot $bak_content
            echo "Backup your site success!"
    #else
    #        echo "$bak_content is aleardy exist!"
    #        exit
    fi
    if [ ! -d $changefile_contentf ];then
    	changefile_content=${changefile_contentf%*/}	#截取目录名后的/,解决用户输入目录名带/和不带/对脚本功能的影响。
            mkdir -p $changefile_content
            echo "Make content $changefile_content success!"
    #else
    #        echo "$changefile_content is aleardy exist!"
    #        exit
    fi
    if [ ! -d $newfile_contentf ];then
    	newfile_content=${newfile_contentf%*/}
            mkdir -p $newfile_content
            echo "Make content $newfile_content success!"
    #else
    #        echo "$newfile_content is aleardy exist!"
    #        exit
    fi
    
    cd $webroot
    cd ../
    
    #建立监控检测死循环
    while :
    do
            if [ ! -f file.md5 ];then	#检测是否存在md5统计文件,若不存在,对网站目录下所有文件进行md5加密保存
    		find $webroot -type f -exec md5sum {} ; >>file.md5
            else
    		
    		for file in $(md5sum -c file.md5|awk -F':' '/FAILED/||/失败/{print $1}')	#校验md5值,获取校验失败的文件
    		do
                    if [ -f $file ];then	#判断校验失败的文件是否存在
            #      		filename_z=$(echo $file|sed 's#/#\/#g')
            #      		sed -i "/ $filename_z/"d file.md5
            #      		md5sum $file >> file.md5
            #      		echo $file >> rsync_file
            #      		else
                  		echo -n  "Find $file changed!" >> $changefile_content/rsync_change.log	#rsync_change.log为被篡改文件名保存的日志
                  		echo -n "  `date`" >> $changefile_content/rsync_change.log
                  		cp $file $changefile_content >> /dev/null	#将被篡改的文件备份到对应日志文件夹中
                  		file_name=${file##*/}	#截取被篡改文件的文件名
                  		bak_file=`find $bak_content -name $file_name`	#查找被篡改文件在备份网站中对应的文件名
    #              		echo $bak_file
    #					echo $file
    					cp -f $bak_file $file >> /dev/null	#从备份网站中恢复被篡改文件,并强制覆盖已被篡改的文件
    					echo "  Backup and replace $file success!" >> $changefile_content/rsync_change.log
    					tail -1 $changefile_content/rsync_change.log	#屏显上述处理日志
    	      		else	#处理被恶意删除的文件
    	      			file2=${file%:*}	#定义file2和file3是为了解决系统过滤出现中文冒号和英文冒号的问题
    	      			file3=${file%:*}
    	      			if [ ${#file2} -ge ${#file3} ];then	#截取后取字段长度较小的值并赋值给file1变量,该变量为网站根目录下被删除文件的绝对路径值
    	      	  			file1=$file3
    	      			else
    	      				file1=$file2
    	      			fi
                  		file_name=${file1##*/}	#获取网站根目录下被删除文件的文件名
    					file_content=${file1%/*}	#获取网站根目录下被删除文件的目录名
    					if [ ! -d $file_content ];then	#考虑到部分恶意删除会删除整个文件夹,此处判断目录是否存在
    						mkdir -p $file_content
    					fi
                  		for bak_file in $(find $bak_content -name $file_name)	#在备份网站中查找被删除的文件名,使用循环是因为不同的目录下可能存在相同的文件名,以此解决备份失败的问题
    					do
    #						echo $file_content
    #             			echo $bak_file
    #						echo $file
                  			cp -f $bak_file $file1 >> /dev/null	#从网站备份路径下恢复被删除的文件
    	      				echo -n "Find $file1 miss!" >> $changefile_content/rsync_miss.log
                  			echo -n "  `date`" >> $changefile_content/rsync_miss.log
    	      				echo "  Backup and readd $file1 success!" >> $changefile_content/rsync_miss.log
    	      				tail -1 $changefile_content/rsync_miss.log	#屏显上述操作
    					done
                  	fi
            done
           	#搜索恶意新建的文件,发现新建恶意文件立即删除,并将该恶意文件传到对应的日志文件夹中,记录并屏显相应日志。 	
    		for newfile in $(find $webroot -type f)	
            do
            	grep -w $newfile file.md5  >/dev/null
            	if [ $? -gt 0 ];then
            	    echo -n "Find $newfile added!" >> $newfile_content/rsync_new.log
            		echo -n "  `date`" >> $newfile_content/rsync_new.log
            		cp $newfile $newfile_content >> /dev/null
            		rm -rf $newfile
    				echo "  Backup and delete $newfile success!" >> $newfile_content/rsync_new.log
    				tail -1 $newfile_content/rsync_new.log			     
            	fi
            done
            	
    		fi
    done
    
    
  • 相关阅读:
    《不生不熟》读后感 读书笔记
    《亚洲与一战》读后感 读书笔记
    《厨房》读后感 读书笔记
    《娇惯的心灵》读后感 读书笔记
    《实践理性批判》读后感 读书笔记
    嵌入式三级知识点整理
    C语言:输入一个数,输出比这个数小的所有素数,并求出个数。
    C语言知识点记录
    C语言-实现矩阵的转置-随机函数产生随机数并赋予数组中-190222
    将数字字符转为数字的两种方法。
  • 原文地址:https://www.cnblogs.com/ytssjbhy616/p/12836545.html
Copyright © 2011-2022 走看看