shell脚本配合定时每天检查有无webshell上传
#近期网站总是有黑客上传webshell,由于找不到漏洞在哪,只能检查有无新文件上传了,写了个shell脚本
#检查每个网站根目录的文件数,排除了runtime那些文件
#!/bin/bash
#定义每个目录名,存为数组
data_dir=("shuju1.com" "shuju2.com" "shuju3.com" "shuju4.com" "shuju5.com" "shuju6.com" "shuju7.com" "shuju8.com" "shuju9.com" "shuju10.com" "shuju11.com" )
#定义函数
check_file(){
#获取该目录的文件数量
file_num=`find /data0/htdocs/$1/ -type f ! -path "*runtime*" ! -path "*Runtime*" |wc -l`
#获取初始的文件数量
last_file=`grep "^$1" /root/shell/check/last_info.txt |awk -F":" '{print $2}'`
#进行对比,判断这次的文件数量和以前的文件数量是否一致
if [ "$file_num" == "$last_file" ];then
#如果一致,调用脚本推送到微信,附加信息,没有新的文件
/root/shell/check/send.sh 1 1 "No new files."
else
#如果不一致,调用脚本推送到微信,附加信息,发现新的文件,上次文件数是多少,这次文件数是多少
/root/shell/check/send.sh 1 1 "discover new files ,last files num is $last_file ,this time files is $file_num"
fi
}
#循环11次,因为11个目录
for i in `seq 0 10`
do
#获取当前下标数组的内容
a=${data_dir[$i]}
#调用脚本,通知当前检查的是哪个目录
/root/shell/check/send.sh 1 1 "now check dir is $a"
#调用函数,将当前目录传参到check_file函数里
check_file $a
#等待两秒进行下一次循环
sleep 2
done
#调用脚本,通知检查已完成
/root/shell/check/send.sh 1 1 "check completed..."
########################################################
last_info.txt内容如下:
shuju1.com:2000
shuju2.com:3082
shuju3.com:2348
#######################################################
脚本推送到微信,通过企业微信跟微信绑定实现的
send.sh脚本内容如下:
#!/bin/bash
CropID='XXXXXXXXXXXXXXX' #企业id
Secret='--k372ERHEyljmRi96xLSTr68pGwu1cZlT8_HLnz6LQ' #应用对应的值
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #获取token 的连接
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}') #获取token值
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" #定义推送的连接
TIME=`date "+%Y-%m-%d %H:%M:%S"` #当前时间
function body() { #定义函数
local int AppID=1000004 #应用的id
local UserID=$1 #定义userid
local PartyID=3 #定义团队id,部门id
local Msg=$(echo "[email protected]" | cut -d" " -f3-) #消息
printf '{\n'
printf '\t"touser": "'"$User"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"webshell检测:\n$Msg \n\n $TIME"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
/usr/bin/curl -s --data-ascii "$(body $1 $2 $3)" $PURL >> /dev/null ##请求连接带参数,直接就会推送到微信,注意看返回是不是正常,成功。
脚本放入crontab即可。
效果图如下: