#!/bin/bash #_日志位置_log_path="/data0/nginx/weblogs/" #_日志文件名称_log_filename="Access_blog.kinggoo.com.log" #_要被屏蔽的ip访问端口, default_port="a"_nginx_deny="/opt/webserver/nginx/conf/deny.conf"_nginx_bin="/opt/webserver/nginx/sbin/nginx"_logfilepath=${_log_path}${_log_filename} #初始化被禁ip变量_drop_ip="" #检测文件Test-e ${_nginx_deny} ||Touch ${_nginx_deny} for_drop_ipinch $(Tail-n50000"${_logfilepath}" |awk' {print $1,$12} ' |grep-I-v-E"google|yahoo|baidu|msnbot| feedsky|sogou| WordPress " |awk' {print '} '|Sort|Uniq-C|Sort-RN|awk' {if ($1>1000) print ' " $" "} ' ); Dogrep-Q"${_drop_ip}" ${_nginx_deny} &&eg=1 ||eg=0 ; if (( ${eg}==0 )); ThenEcho"Deny ${_drop_ip};" >> ${_nginx_deny} ${_nginx_bin} -s Reload iptables-I INPUT-p TCP--Dport ${_port} -S ${_drop_ip} -J DROP Echo">>>>> ' Date ' +%y-%m-%d%h%m%s"-found attack source address, ${_drop_ip} " >> /tmp/Nginx_deny.Log;Echo"Iptables-i input-p tcp--dport ${_port}-S ${_DROP_IP}-j Drop" >> /tmp/Nginx_deny.Logfi Done
Nginx Log found a large number of malicious IP automatically added firewall script