forked from centminmod/centminmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csfincapsula.sh
executable file
·168 lines (152 loc) · 5.42 KB
/
csfincapsula.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
###############################
# written by George Liu (eva2000)
# centminmod.com
###############################
CFIPLOG='/root/incapsula-ips.txt'
CFIPNGINXLOG='/root/incapsula-nginxlog.log'
CFIPCSFLOG='/root/incapsula_log.log'
CFINCLUDEFILE='/usr/local/nginx/conf/incapsula.conf'
CURL_TIMEOUTS='--max-time 20 --connect-timeout 20'
###############################
if [ ! -f /usr/bin/curl ]; then
echo "Installing curl please wait..."
yum -y -q install curl
fi
###############################
ipv4get() {
/usr/bin/curl -s ${CURL_TIMEOUTS} --data "resp_format=text" https://my.incapsula.com/api/integration/v1/ips > $CFIPLOG
CFIPS=$(cat $CFIPLOG)
echo "--------------------------------------------"
echo " Downloading Incapsula P list"
echo " from: Incapsula API"
echo "--------------------------------------------"
echo ""
echo "--------------------------------------------"
echo " Format for Centminmod.com Nginx Installer"
echo " 1). add to nginx.conf"
echo " 2). add to /etc/csf/csf.allow"
echo "--------------------------------------------"
for ip in $CFIPS;
do
if [[ "$(ipcalc -c "$ip" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
if [[ -f /etc/sysconfig/network && "$(awk -F "=" '/NETWORKING_IPV6/ {print $2}' /etc/sysconfig/network | grep 'yes' >/dev/null 2>&1; echo $?)" = '0' ]]; then
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
echo "set_real_ip_from $ip;" >> $CFIPNGINXLOG
echo "csf -a $ip incapsula" >> $CFIPCSFLOG
elif [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
echo "set_real_ip_from $ip;" >> $CFIPNGINXLOG
echo "csf -a $ip incapsula" >> $CFIPCSFLOG
fi
else
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
echo "set_real_ip_from $ip;" >> $CFIPNGINXLOG
echo "csf -a $ip incapsula" >> $CFIPCSFLOG
fi
fi
fi
done
echo "real_ip_header X-Forwarded-For;" >> $CFIPNGINXLOG
echo "--------------------------------------------"
echo " 1). add to nginx.conf"
echo "--------------------------------------------"
cat $CFIPNGINXLOG
echo ""
echo "--------------------------------------------"
echo " 2). add to /etc/csf/csf.allow"
echo "--------------------------------------------"
cat $CFIPCSFLOG
rm -rf $CFIPLOG
rm -rf $CFIPNGINXLOG
rm -rf $CFIPCSFLOG
echo "--------------------------------------------"
}
###############################
csfadd() {
/usr/bin/curl -s ${CURL_TIMEOUTS} --data "resp_format=text" https://my.incapsula.com/api/integration/v1/ips > $CFIPLOG
CFIPS=$(cat $CFIPLOG)
echo "--------------------------------------------"
echo " Add Incapsula P list to CSF"
echo " from: Incapsula API"
echo "--------------------------------------------"
echo ""
echo "--------------------------------------------"
echo " Add to /etc/csf/csf.allow"
echo "--------------------------------------------"
for ip in $CFIPS;
do
if [[ "$(grep "$ip" /etc/csf/csf.allow >/dev/null 2>&1; echo $?)" = '1' ]] || [[ "$(grep "$ip" /etc/csf/csf.ignore >/dev/null 2>&1; echo $?)" = '1' ]]; then
if [[ "$(ipcalc -c "$ip" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
if [[ -f /etc/sysconfig/network && "$(awk -F "=" '/NETWORKING_IPV6/ {print $2}' /etc/sysconfig/network | grep 'yes' >/dev/null 2>&1; echo $?)" = '0' ]]; then
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
csf -a "$ip" incapsula
echo "$ip" >> /etc/csf/csf.ignore
elif [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
csf -a "$ip" incapsula
echo "$ip" >> /etc/csf/csf.ignore
fi
else
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
csf -a "$ip" incapsula
echo "$ip" >> /etc/csf/csf.ignore
fi
fi
fi
fi
done
}
###############################
nginxsetup() {
echo
# echo "create $CFINCLUDEFILE include file"
if [ -f "$CFINCLUDEFILE" ]; then
\cp -af "$CFINCLUDEFILE" "${CFINCLUDEFILE}.bak"
fi
echo > $CFINCLUDEFILE
cflista=$(/usr/bin/curl -s ${CURL_TIMEOUTS} --data "resp_format=text" https://my.incapsula.com/api/integration/v1/ips)
if [ ! -f /usr/local/nginx/conf/incapsula_customips.conf ]; then
touch /usr/local/nginx/conf/incapsula_customips.conf
fi
echo "include /usr/local/nginx/conf/incapsula_customips.conf;" >> $CFINCLUDEFILE
for i in $cflista; do
if [[ "$(ipcalc -c "$i" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
if [[ -f /etc/sysconfig/network && "$(awk -F "=" '/NETWORKING_IPV6/ {print $2}' /etc/sysconfig/network | grep 'yes' >/dev/null 2>&1; echo $?)" = '0' ]]; then
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '0' ]]; then
echo "set_real_ip_from $i;" >> $CFINCLUDEFILE
elif [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
echo "set_real_ip_from $i;" >> $CFINCLUDEFILE
fi
else
if [[ "$(ipcalc -6 "$i" >/dev/vull 2>&1; echo $?)" -eq '1' ]]; then
echo "set_real_ip_from $i;" >> $CFINCLUDEFILE
fi
fi
fi
done
echo "real_ip_header X-Forwarded-For;" >> $CFINCLUDEFILE
if [[ "$(diff -u "${CFINCLUDEFILE}.bak" "$CFINCLUDEFILE" >/dev/vull 2>&1; echo $?)" -ne '0' ]]; then
service nginx reload >/dev/null 2>&1
fi
rm -rf "${CFINCLUDEFILE}.bak"
echo "created $CFINCLUDEFILE include file"
}
###############################
case "$1" in
ips)
ipv4get
;;
csf)
csfadd
;;
nginx)
nginxsetup
;;
auto)
csfadd
nginxsetup
;;
*)
echo "$0 {ips|csf|nginx|auto}"
;;
esac
exit