-
Notifications
You must be signed in to change notification settings - Fork 1
/
ubuntu_aegis_waf_install.sh
288 lines (195 loc) · 12.2 KB
/
ubuntu_aegis_waf_install.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
#The script below assumes you have a fully installed and updated Ubuntu 18.04 server
#Ensure Script is run as root and if not exit
if [ `id -u` -ne 0 ]; then
echo "==== ERROR ====" | boxes -d stone -p a2v1
echo "This script must be executed as root, Exiting..."
exit 1
fi
#Set the script path
SCRIPTPATH=$(pwd)
#Set install_log Date/Time Stamp
TIMESTAMP=`date +%m-%d-%Y-%H%M`
#Set Font Colors
RED=`tput setaf 1`
GREEN=`tput setaf 2`
RESET=`tput sgr0`
#Set Latest Nginx Version as of 12/26/2019
#NGINXVER=1.17.9
#Script Debug Set Variables. Do not enable unless you are troubleshooting
echo "Installing Boxes Prerequisite"
#Install boxes
apt-get install boxes -y > /dev/null 2>&1
ERR=$?
if [ $ERR != 0 ]; then
THEERROR=$(($THEERROR+$ERR))
echo "${RED}Error $THEERROR, occurred Installing Boxes Prerequisite ${RESET}"
exit 1
else
echo "${GREEN}Completed Installing Boxes Prerequisite ${RESET}"
fi
echo "Installing Spinner Prerequisite"
#Install spinner
apt-get install spinner -y > /dev/null 2>&1
ERR=$?
if [ $ERR != 0 ]; then
THEERROR=$(($THEERROR+$ERR))
echo "${RED}Error $THEERROR, occurred Installing Spinner Prerequisite ${RESET}"
exit 1
else
echo "${GREEN}Completed Installing Spinner Prerequisite ${RESET}"
fi
echo "Aegis WAF Installation" | boxes -d stone -p a2v1
PS3='Do you wish to continue the installation of Aegis WAF? (Enter y or Y. Warning!! Entering n or N will exit this script and the installation will stop!):'
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes")
echo "Starting Aegis WAF Installation" | boxes -d stone -p a2v1
echo "${GREEN}During installation a $SCRIPTPATH/install_log-$TIMESTAMP.log log file will be created. It's highly recommended that you open a separate shell window and tail that file in order to view progress of the installation and/or any errors that may occur.${RESET}"
echo "[`date +%m/%d/%Y-%H:%M`] Starting Aegis WAF Installation." >> $SCRIPTPATH/install_log-$TIMESTAMP.log
break
;;
"No")
echo "Exiting Aegis WAF Installation.";
exit
;;
*) echo "Invalid option $REPLY ";;
esac
done
echo -e "\n"
read -p "Browse to https://nginx.org/en/download.html to get the latest ${GREEN}STABLE${RESET} version of Nginx. The latest version will appear in the format nginx-1.XX.X (Example: nginx-1.22.0). Enter the latest stable version (Example: nginx-1.22.0):" NGINXVER
source "$(pwd)/spinner.sh"
# test success
start_spinner 'sleeping for 2 secs...'
sleep 2
stop_spinner $?
#START CONFIGURATION
echo "[`date +%m/%d/%Y-%H:%M`] STEP 1 OF 13. Installing Prerequisites" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Installing Prerequisites...'
sleep 1
#Install prerequisites
/usr/bin/apt-get install jq build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev libgeoip-dev libgeoip1 libmaxminddb-dev libmaxminddb0 libyajl-dev libyajl2 liblmdb-dev automake pkgconf haveged unzip unrar sendemail libio-socket-ssl-perl zlib1g-dev libnet-ssleay-perl perl cifs-utils rar -y >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 2 OF 13. Cloning and building Modsecurity" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Cloning and building Modsecurity...'
sleep 1
#Clone and Build Modsecurity
cd /opt >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/git clone https://github.com/SpiderLabs/ModSecurity >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
cd ModSecurity >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/git checkout v3.0.4 >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
sh build.sh >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/git submodule init >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/git submodule update >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
./configure >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
make >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
make install >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 3 OF 13. Cloning Modsecurity Nginx Connector" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Cloning Modsecurity Nginx Connector...'
sleep 1
#Clone Modsecurity Nginx Connector
cd /opt >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/git clone https://github.com/SpiderLabs/ModSecurity-nginx.git >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 4 OF 13. Downloading and Extracting Nginx Version $NGINXVER" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Downloading and Extracting Nginx Version...'
sleep 1
#Download Latest Nginx Version
cd $SCRIPTPATH >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/wget http://nginx.org/download/$NGINXVER.tar.gz >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/tar -zxf $NGINXVER.tar.gz >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 5 OF 13. Downloding and Extracting headers-more-nginx-module" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Downloding and Extracting headers-more-nginx-module...'
sleep 1
#Download and extract headers-more-nginx-module
cd /opt >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/wget https://github.com/openresty/headers-more-nginx-module/archive/master.zip >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/unzip master.zip >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/rm master.zip >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 6 OF 13. Configuring Nginx with headers-more-nginx-module and Modsecurity-nginx connector" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Configuring Nginx with headers-more-nginx-module and Modsecurity-nginx connector...'
sleep 1
#Configure Nginx with headers-more-nginx-module and Modsecurity-nginx connector
cd $SCRIPTPATH/$NGINXVER >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
./configure --user=www-data --group=www-data --with-pcre-jit --with-debug --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --add-module=/opt/headers-more-nginx-module-master --add-module=/opt/ModSecurity-nginx --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
make >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
make install >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp /opt/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 7 OF 13. Creating Necessary Directories, Symlinks and Files" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Creating Necessary Directories, Symlinks and Files...'
sleep 1
#Create necessary directories, Symlinks and files
/bin/mkdir -p /opt/aegis-waf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/conf/sites-available >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/conf/sites-enabled >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/conf/ssl >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/conf/listen >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/conf/modsecurity >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/mkdir -p /usr/local/nginx/logs/modsecurity >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp -r $SCRIPTPATH/dirstructure/opt/aegis-waf/* /opt/aegis-waf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp $SCRIPTPATH/dirstructure/usr/local/nginx/conf/modsecurity/unicode.mapping /usr/local/nginx/conf/modsecurity/unicode.mapping >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp $SCRIPTPATH/dirstructure/usr/local/nginx/conf/sites-available/default* /usr/local/nginx/conf/sites-available/ >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/chmod +x /opt/aegis-waf/scripts/*.sh >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
#/bin/ln -s /usr/local/nginx/sbin/nginx /bin/nginx && \
#/bin/ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx && \
#/bin/ln -s /usr/local/nginx/ /etc/nginx && \
/bin/ln -s /usr/local/nginx/conf/sites-available/default.conf /usr/local/nginx/conf/sites-enabled/default.conf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 8 OF 13. Backing up and Re-configuring Nginx" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Backing up and Re-configuring Nginx...'
sleep 1
#Backup /usr/local/nginx/conf/nginx.conf and replace with $SCRIPTPATH/dirstructure/usr/local/nginx/nginx.conf file
/bin/cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.ORIGINAL >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp $SCRIPTPATH/dirstructure/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 9 OF 13. Configuring Nginx Service" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Configuring Nginx Service...'
sleep 1
#Configure Nginx service
/bin/cp $SCRIPTPATH/dirstructure/etc/systemd/system/nginx.service /etc/systemd/system/nginx.service >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/systemctl enable /etc/systemd/system/nginx.service >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 10 OF 13. Configuring Nginx with OWASP Modsecurity Core Rule Set" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Configuring Nginx with OWASP Modsecurity Core Rule Set...'
sleep 1
#configure Nginx with OWASP Modsecurity Core Rule Set
cd /opt/ && \
/usr/bin/git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
cd owasp-modsecurity-crs/ >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp -R rules/ /usr/local/nginx/conf/ >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp /opt/owasp-modsecurity-crs/crs-setup.conf.example /usr/local/nginx/conf/crs-setup.conf >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 11 OF 13. Installing Certbot" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Installing Certbot...'
sleep 1
#Install Certbot
/usr/bin/apt-get remove certbot -y >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/snap install --classic certbot >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/ln -s /snap/bin/certbot /usr/bin/certbot >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/certbot --version >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 12 OF 13. Configuring Certbot" >> $SCRIPTPATH/install_log-$TIMESTAMP.log
start_spinner 'Configuring Certbot...'
sleep 1
#Configure Certbot
/bin/mkdir -p /etc/letsencrypt >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/bin/cp $SCRIPTPATH/dirstructure/etc/letsencrypt/cli.ini /etc/letsencrypt/cli.ini >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] STEP 13 OF 13: Creating Diffie-Hellman (DH) Key Exchange File" >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
echo "==== WARNING ====" | boxes -d stone -p a2v1
echo "${GREEN}The DH Key Exchange File creation process will take a VERY long time to complete (about ~10 minutes on 1 CPU Machine). ${RESET}"
start_spinner 'Creating DH Key Exchange File. Please be very patient...'
sleep 1
cd /usr/local/nginx/conf/ssl/ >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1 && \
/usr/bin/openssl dhparam -out dhparam.pem 4096 >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
stop_spinner $?
echo "[`date +%m/%d/%Y-%H:%M`] ==== FINISHED INSTALLATION ==== Ensure no errors were logged during installation" >> $SCRIPTPATH/install_log-$TIMESTAMP.log 2>&1
echo "FINISHED INSTALLATION. PLEASE REBOOT YOUR MACHINE!!" | boxes -d stone -p a2v1
echo "Take a look at the $SCRIPTPATH/install_log-$TIMESTAMP.log file for any errors"
echo "Get started creating sites by running /opt/aegis-waf/start.sh"