-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-setup.sh
327 lines (248 loc) · 6.74 KB
/
proxy-setup.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
######################################################################
## Setup steps for bict proxy 1, a hardened nginx reverse proxy. ##
## Author: Manuel Riesen, Sandro Rüfenacht ##
######################################################################
echo "Do not run this script! Read the description of each step and apply the commands manually."
exit 1
##
## 1.0: basic software installation
##
# update and upgrade system
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get autoremove
sudo apt-get autoclean
# install nginx
sudo apt-get install nginx
# install net-tools
sudo apt-get install net-tools
##
## 1.1: remove unused packages
##
sudo apt autoremove --purge snapd
sudo apt-get --purge remove xinetd nis yp-tools tftpd atftpd tftpd-hpa telnetd rsh-server rsh-redone-server
##
## 1.2: disable root account
##
sudo passwd -l root
##
## 1.3: secure shared memory
##
# edit fstab
sudo nano /etc/fstab
# add this line
:'
tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0
'
# apply changes
sudo mount -a
##
## 2.0: setup unattended upgrades
##
# install package
sudo apt-get install unattended-upgrades
# NOTE: unattended-upgrades is automatically configured for only installing security-related updates.
# configure for all updates, uncomment ...-updates";
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
:'
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
// Extended Security Maintenance; doesnt necessarily exist for
// every release and this system may not have it installed, but if
// available, the policy for updates is such that unattended-upgrades
// should also install from here by default.
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
"${distro_id}:${distro_codename}-updates";
// "${distro_id}:${distro_codename}-proposed";
// "${distro_id}:${distro_codename}-backports";
};
'
##
## 3.0: install and configure ufw
##
# install package
sudo apt-get install ufw
# disable IPv6
sudo nano /etc/default/ufw
:'
IPV6=no
'
# set default rules
sudo ufw default deny incoming
sudo ufw default deny outgoing
# set rules for outgoing requests
sudo ufw allow out 53/tcp
sudo ufw allow out 53/udp
sudo ufw allow out 443/tcp
sudo ufw allow out 80/tcp
sudo ufw allow out 21/tcp
sudo ufw allow out 22/tcp
sudo ufw allow out 25/tcp
# set rules for nginx proxy
sudo ufw allow in 80/tcp
sudo ufw allow in 443/tcp
# enable ufw
sudo ufw enable
##
## 4.0: configure AppArmor
##
# install profiles
sudo apt-get install apparmor-profiles
# install utils
sudo apt-get install apparmor-utils
# get status
sudo apparmor_status
# go to config directory
cd /etc/apparmor.d/
# create nginx config
sudo aa-autodep nginx
# set complain profile
sudo aa-complain nginx
# restart nginx
sudo service nginx restart
# show logs
sudo aa-logprof
# allow all needed restrictions and save profile
# edit profile
sudo nano /etc/apparmor.d/usr.sbin.nginx
:'
#include <tunables/global>
/usr/sbin/nginx {
#include <abstractions/base>
#include <abstractions/nameservice>
#include <abstractions/openssl>
#include <abstractions/nis>
capability dac_override,
capability dac_read_search,
capability net_bind_service,
/usr/sbin/nginx mr,
/var/log/nginx/error.log w,
/etc/group r,
/etc/nginx/conf.d/ r,
/etc/nginx/mime.types r,
/etc/nginx/nginx.conf r,
/etc/nsswitch.conf r,
/etc/passwd r,
/etc/ssl/openssl.cnf r,
/run/nginx.pid rw,
/usr/sbin/nginx mr,
/var/log/nginx/access.log w,
/var/log/nginx/error.log w,
owner /etc/nginx/conf.d/ r,
owner /etc/nginx/mime.types r,
owner /etc/nginx/modules-enabled/ r,
owner /etc/nginx/nginx.conf r,
owner /etc/nginx/sites-available/ r,
owner /etc/nginx/sites-enabled/ r,
owner /run/nginx.pid rw,
owner /usr/share/nginx/modules-available/mod-http-image-filter.conf r,
owner /usr/share/nginx/modules-available/mod-http-xslt-filter.conf r,
owner /usr/share/nginx/modules-available/mod-mail.conf r,
owner /usr/share/nginx/modules-available/mod-stream.conf r,
}
'
# enforce profile
sudo aa-enforce nginx
# restart nginx
sudo service nginx restart
# reload daemon
sudo /etc/init.d/apparmor reload
# check status
sudo apparmor_status
##
## 5.0: setup fail2ban
##
# install fail2ban
sudo apt-get install fail2ban
# copy file
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# edit jail
sudo nano /etc/fail2ban/jail.local
:'
[nginx-http-auth]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
...
[nginx-badbots]
enabled = true
port = http,https
filter = nginx-badbots
logpath = /var/log/nginx/access.log
maxretry = 2
[nginx-nohome]
enabled = true
port = http,https
filter = nginx-nohome
logpath = /var/log/nginx/access.log
maxretry = 2
'
# create configurations
# copy badbots config
sudo cp /etc/fail2ban/filter.d/apache-badbots.conf /etc/fail2ban/filter.d/nginx-badbots.conf
sudo nano /etc/fail2ban/filter.d/nginx-nohome.conf
:'
[Definition]
failregex = ^<HOST> -.*GET .*/~.*
ignoreregex =
'
# restart fail2ban
sudo service fail2ban restart
# check status
sudo fail2ban-client status
##
## 6.0: kernel hardening
##
sudo nano /etc/sysctl.conf
:'
# Turn on execshield
kernel.exec-shield=1
kernel.randomize_va_space=1
# Enable IP spoofing protection
net.ipv4.conf.all.rp_filter=1
# Disable IP source routing
net.ipv4.conf.all.accept_source_route=0
# Ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.icmp_ignore_bogus_error_messages=1
# Make sure spoofed packets get logged
net.ipv4.conf.all.log_martians = 1
'
# reboot system
sudo shutdown -r now
##
## 7.0: setup rootkit hunter
##
# install rkhunter
sudo apt-get install rkhunter
# run rkhunter
sudo rkhunter --check
##
## 8.0 OPTIONAL: use lynis to check for vulnerabilities
##
##
## 9.0: setup nginx reverse-proxy
##
# the following config files have been copied from 30.5:
# - /etc/nginx/sites-available/cloud.bict.ch
# - /etc/nginx/sites-available/dev.bict.ch
# - /etc/nginx/sites-available/env.bict.ch
# - /etc/nginx/sites-available/gitlab.bict.ch
# - /etc/nginx/sites-available/ipa.bict.ch
# - /etc/nginx/sites-available/registry.bict.ch
# all certbot-related configs have been removed
# enable sites
sudo ln -s /etc/nginx/sites-available/{cloud,dev,env,gitlab,ipa,registry}.bict.ch /etc/nginx/sites-enabled/
##
## 10.0: setup nginx https
##
# install certbot
sudo apt-get install certbot
# install nginx plugin certbot
sudo apt-get install python3-certbot-nginx
# generate first certificate
sudo certbot --authenticator standalone --installer nginx --pre-hook "service nginx stop" --post-hook "service nginx start"
# check renewal
sudo certbot renew --dry-run