-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblockip.sh
143 lines (121 loc) · 3.8 KB
/
blockip.sh
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
#
# Script to monitor and block IPs spaming in varios ways my WPress and SQUID installation
# Felipe Ferreira
# Update 07/2016
# tested on Centos/RedHat/LinuxAMI and lighttpd/squid with iptables
#PREREQ
# ipset
# iptables
DEBUG=0 # 1 for verbose, 0 for quite
CRIT=20 #Number of entries before it adds to iptables DENY rule
CRIT_SQUID=4 #Same As Above
RST=0 #RESTART webserver = 1 or not = 0
WHITELIST="/usr/bin/whitelist.txt" #IPs to never add into blacklist/block
dia=$(date +%d)
mesd=$(date +%m)
ano=$(date +%Y)
hora=$(date +%H)
tday="$ano-$mesd-$dia $hora"
####################################### FUNCTION #################
function pt()
{
if [ "$DEBUG" -eq "1" ]; then
echo "$1"
fi
}
function blockip()
{
IP=$1
#check if ipset list blacklist exists or not, if not present creates it and add to iptables
if [[ $(/usr/sbin/ipset list -n |grep -c blacklist) = 0 ]]; then
pt "ERRO - ipset blacklist not found"
if [ -f /etc/ipset.conf ]; then
/usr/sbin/ipset restore < /etc/ipset.conf
fi
/usr/sbin/ipset create blacklist hash:ip hashsize 4096
if [[ $(grep -c blacklist /etc/sysconfig/iptables) = 0 ]]; then
/sbin/iptables -I INPUT -m set --match-set blacklist src -j DROP
/sbin/iptables-save > /etc/sysconfig/iptables
fi
fi
if [[ $(grep -c "$IP" "$WHITELIST") = 0 ]] && [[ $(/usr/sbin/ipset list blacklist |grep -c "$IP") = 0 ]]; then
echo "----------------------------------------------------------"
date
echo "OK - $IP has been blocked"
/usr/sbin/ipset add blacklist $IP
/usr/sbin/ipset save > /etc/ipset.conf
if [ "$RST" -eq "1" ]; then
service $WEBSRV stop
sleep 20
service $WEBSRV start
fi
else
echo "UNKOWN - $IP already in the blacklist or in whitelist: $WHITELIST"
fi
}
function checkfile() {
if [ ! -f $1 ]; then
return 1
elif [[ $(/usr/bin/du -k $1| cut -f 1) < 2 ]]; then
return 1
else
return 0
fi
}
function check_lighttpd_error()
{
#Must have lighttpd and mod_evasive enabled it will then log to error.log
L=$1
if checkfile $L ; then L=$1; else pt "$L has no data or not found" && return 0; fi
IPS=$(grep "$tday" $L |grep "Too many connections" |awk -F")" '{ print $NF}' |awk '{ print $1 }' |sort -rn |uniq |head -n 5)
if [ -z "$IPS" ]; then
return
fi
for IP in $IPS;
do
#Check how many times it happened
CI=$(grep "$tday" $L |grep "Too many connections"|grep -c "$IP")
if [ "$CI" -gt "$CRIT" ]; then
echo "$L - Found $CI many connections, Blocking IP: $IP more then $CRIT found on $tday"
blockip $IP
fi
done
}
function check_lighttpd_access()
{
L=$1
if checkfile $L ; then L=$1; else pt "$L has no data or not found" && return 0; fi
IP=$( tail -n 1000 $L |grep "/wp-login.php" |grep POST |grep "wp-login.php " |awk '{ print $1 }' |sort -rn |uniq -c |sort -rn |head -n 1)
if [ ! -z "$IP" ]; then
IPT=$(pt "$IP" |awk '{ print $1 }')
IPA=$(pt "$IP" |awk '{ print $NF }')
else
return
fi
if [ "$IPT" -gt "$CRIT" ]; then
echo "$L - Tried $IPA $IPT ( $CRIT ) Blocking $IPA"
blockip $IPA
fi
}
function check_squid()
{
L=$1
if checkfile $L ; then L=$1; else pt "$L has no data or not found" && return 0; fi
IPS=$(egrep 'NONE\/400|TCP_DENIED\/407' $L |awk '{ print $3 }' |sort -rn |uniq -c |sort -rn |head -n 5 | awk '{ print $NF }')
for IP in $IPS;
do
IPC=$(egrep 'NONE\/400|TCP_DENIED\/407' $L|grep -c $IP )
if [ "$IPC" -gt "$CRIT_SQUID" ]; then
echo "$L - $IP found $IPC more then $CRIT_SQUID "
blockip $IP
else
pt "$L - Not blocking $IP found only: $IPC"
fi
done
}
################################################################## MAIN
check_squid /var/log/squid/access.log
check_lighttpd_error /var/log/lighttpd/error.log
check_lighttpd_access /var/log/lighttpd/felipeferreira_access.log
exit 0