-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklist-nginx
65 lines (51 loc) · 1.95 KB
/
blacklist-nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#########################################################################
# add to nginx.conf: #
#########################################################################
# location / {
# include /etc/nginx/blacklist.txt;
# ...
# }
#########################################################################
# crontab: */5 * * * * /usr/local/bin/blacklist-nginx &> /dev/null #
#########################################################################
#########################################################################
### edit below this line ###
blacklist="/etc/nginx/blacklist.txt" #path to nginx blacklist
logfile="/var/log/nginx/access.log" #path to access log
whitelist="127.0.0.1 127.0.0.2 127.0.0.3" #personal ips
#########################################################################
LANG=C
cmdname=`basename $0`
newtmpdir=`mktemp -d /tmp/${cmdname}.XXXXXX`
buffer=${newtmpdir}/buffer1
buffer2=${newtmpdir}/buffer2
day=`date +%d%y%m`
yesterday=`date -d "yesterday" +%d%y%m`
trap 'cleanup' EXIT
trap 'cleanup' SIGTERM
function cleanup () {
rm -rf "${newtmpdir}"
}
if [ -e ${newtmpdir}/lock ]
then
echo "allready running with lockfile ${newtmpdir}/lock";
exit;
else
touch ${newtmpdir}/lock
fi
cat ${blacklist} | sort -u > ${buffer};
grep POST ${logfile} | awk '{print$1}' | sort | uniq -c |sort -rn |awk '$1 > 5 {print"deny "$2";"}' >> ${buffer};
grep GET ${logfile} |awk '{print$1}' | sort | uniq -c |sort -rn |awk '$1 > 50 {print"deny "$2";"}' >> ${buffer};
for white in ${whitelist};
do
cat ${buffer} | grep -v ${white} > ${buffer2}
cat ${buffer2} > ${buffer}
done
cat ${buffer} |sort -u > ${blacklist}
bl=`cat ${buffer} |sort -u |wc -l`
echo "blacklisted ${bl} ips!"
/etc/init.d/nginx reload;
cat ${logfile} >> "${logfile}.${day}"
cat /dev/null > ${logfile}
gzip "${logfile}.${yesterday}" &> /dev/null