-
Notifications
You must be signed in to change notification settings - Fork 6
/
wireguard_pi-hole_unbound.sh
353 lines (308 loc) · 16.3 KB
/
wireguard_pi-hole_unbound.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env bash
# Cloud-Init script to run Wireguard+Unbound+PiHole on a fresh virtual server.
# Use case: run this on a fresh cheap Scaleway stardust instance.
cat <<'EOF' > /dev/console
██╗ ██╗██╗██████╗ ███████╗ ██████╗ ██╗ ██╗ █████╗ ██████╗ ██████╗
██║ ██║██║██╔══██╗██╔════╝██╔════╝ ██║ ██║██╔══██╗██╔══██╗██╔══██╗
██║ █╗ ██║██║██████╔╝█████╗ ██║ ███╗██║ ██║███████║██████╔╝██║ ██║
██║███╗██║██║██╔══██╗██╔══╝ ██║ ██║██║ ██║██╔══██║██╔══██╗██║ ██║
╚███╔███╔╝██║██║ ██║███████╗╚██████╔╝╚██████╔╝██║ ██║██║ ██║██████╔╝
╚══╝╚══╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝
██╗ ██╗██████╗ ███████╗
██║ ██║██╔══██╗██╔════╝
██║ ██║██████╔╝███████╗
╚██╗ ██╔╝██╔═══╝ ╚════██║
╚████╔╝ ██║ ███████║
╚═══╝ ╚═╝ ╚══════╝
EOF
MY_USER=user
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get upgrade -y
apt-get install -y --no-install-recommends \
python3 pwgen qrencode fail2ban \
dnsutils jq wireguard docker.io \
docker-compose git rsyslog
cat <<'EOF' > /dev/console
███████╗██╗ ██╗███████╗████████╗███████╗███╗ ███╗
██╔════╝╚██╗ ██╔╝██╔════╝╚══██╔══╝██╔════╝████╗ ████║
███████╗ ╚████╔╝ ███████╗ ██║ █████╗ ██╔████╔██║
╚════██║ ╚██╔╝ ╚════██║ ██║ ██╔══╝ ██║╚██╔╝██║
███████║ ██║ ███████║ ██║ ███████╗██║ ╚═╝ ██║
╚══════╝ ╚═╝ ╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
██████╗ ██████╗ ███╗ ██╗███████╗██╗ ██████╗
██╔════╝██╔═══██╗████╗ ██║██╔════╝██║██╔════╝
██║ ██║ ██║██╔██╗ ██║█████╗ ██║██║ ███╗
██║ ██║ ██║██║╚██╗██║██╔══╝ ██║██║ ██║
╚██████╗╚██████╔╝██║ ╚████║██║ ██║╚██████╔╝
╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝
EOF
# Root random password, needed for console access
ROOT_PWD=$(pwgen 24 -1)
echo "root:${ROOT_PWD}" | chpasswd
# Where we store config files
mkdir -p /docker/{wireguard,unbound,etc-pihole,etc-dnsmasq.d}
# Unbound config file, butchered
cat <<'EOF' > /docker/unbound/unbound.conf
server:
cache-max-ttl: 86400
cache-min-ttl: 60
edns-buffer-size: 1472
interface: 0.0.0.0@53
rrset-roundrobin: yes
log-local-actions: no
log-queries: no
log-replies: no
log-servfail: no
verbosity: 1
aggressive-nsec: yes
delay-close: 10000
do-daemonize: no
do-not-query-localhost: no
neg-cache-size: 4M
qname-minimisation: yes
access-control: 127.0.0.1/32 allow
access-control: 192.168.0.0/16 allow
access-control: 172.16.0.0/12 allow
access-control: 10.0.0.0/8 allow
harden-algo-downgrade: yes
harden-below-nxdomain: yes
harden-dnssec-stripped: yes
harden-glue: yes
harden-large-queries: yes
harden-referral-path: no
harden-short-bufsize: yes
hide-identity: yes
hide-version: yes
identity: "DNS"
private-address: 10.0.0.0/8
private-address: 172.16.0.0/12
private-address: 192.168.0.0/16
private-address: 169.254.0.0/16
private-address: fd00::/8
private-address: fe80::/10
private-address: ::ffff:0:0/96
unwanted-reply-threshold: 10000000
val-clean-additional: yes
msg-cache-size: 260991658
num-queries-per-thread: 4096
outgoing-range: 8192
rrset-cache-size: 260991658
minimal-responses: yes
prefetch: yes
prefetch-key: yes
serve-expired: yes
so-reuseport: yes
so-rcvbuf: 1m
remote-control:
control-enable: no
EOF
touch /docker/unbound/unbound.log
# endlessh: https://github.com/skeeto/endlessh - the image will be build using docker-compose
mkdir -p /docker/endlessh
git clone https://github.com/skeeto/endlessh.git /docker/endlessh
# https://github.com/itskenny0/fail2ban-endlessh
mkdir /tmp/fail2ban-endlessh
git clone https://github.com/itskenny0/fail2ban-endlessh.git /tmp/fail2ban-endlessh
cp /tmp/fail2ban-endlessh/action.d/endlessh.conf /etc/fail2ban/action.d/endlessh.conf
cp /tmp/fail2ban-endlessh/jail.d/endlessh.conf /etc/fail2ban/jail.d/endlessh.conf
rm -rf /tmp/fail2ban-endlessh
# Harden fail2ban
sed -i 's/<iptables>/iptables/g' /etc/fail2ban/action.d/endlessh.conf #FIXME?
cat <<EOF >> /etc/fail2ban/jail.d/endlessh.conf
bantime = 24h
findtime = 12h
maxretry = 3
EOF
# docker-compose file, Cf. https://github.com/IAmStoxe/wirehole
cat <<EOF > /docker/docker-compose.yml
---
version: "3"
networks:
private_network:
ipam:
driver: default
config:
- subnet: 10.2.0.0/24
services:
unbound:
image: "alpinelinux/unbound:latest"
container_name: unbound
restart: unless-stopped
hostname: "unbound"
volumes:
- "./unbound:/etc/unbound"
networks:
private_network:
ipv4_address: 10.2.0.200
wireguard:
depends_on: [unbound, pihole]
image: ghcr.io/linuxserver/wireguard
container_name: wireguard
cap_add:
- NET_ADMIN
- SYS_MODULE
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Paris
- SERVERPORT=51820
- PEERS=${MY_USER}
- PEERDNS=10.2.0.100
- INTERNAL_SUBNET=10.6.0.0
volumes:
- ./wireguard:/config
- /lib/modules:/lib/modules
ports:
- 51820:51820/udp
dns:
- 10.2.0.100 # Points to pihole
- 10.2.0.200 # Points to unbound
sysctls:
- net.ipv4.conf.all.src_valid_mark=1
restart: unless-stopped
networks:
private_network:
ipv4_address: 10.2.0.3
pihole:
depends_on: [unbound]
container_name: pihole
image: pihole/pihole:latest
restart: unless-stopped
hostname: pihole
dns:
- 127.0.0.1
- 10.2.0.200 # Points to unbound
environment:
TZ: "Europe/Paris"
WEBPASSWORD: "" # Blank password
ServerIP: 10.1.0.100 # Internal IP of pihole
DNS1: 10.2.0.200 # Unbound IP
DNS2: 10.2.0.200 # If we don't specify two, it will auto pick google.
volumes:
- "./etc-pihole/:/etc/pihole/"
- "./etc-dnsmasq.d/:/etc/dnsmasq.d/"
# Recommended but not required (DHCP needs NET_ADMIN)
# https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
#cap_add:
# - NET_ADMIN
networks:
private_network:
ipv4_address: 10.2.0.100
watchtower:
container_name: watchtower
image: containrrr/watchtower
restart: unless-stopped
hostname: watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
WATCHTOWER_POLL_INTERVAL: 86400
WATCHTOWER_CLEANUP: "true"
endlessh:
image: endlessh:latest
build: ./endlessh
container_name: endlessh
hostname: endlessh
restart: unless-stopped
ports:
- 2222:2222
EOF
cd /docker || return
cat <<'EOF' > /dev/console
██████╗ ██╗ ██╗██╗██╗ ██████╗
██╔══██╗██║ ██║██║██║ ██╔══██╗
██████╔╝██║ ██║██║██║ ██║ ██║
██╔══██╗██║ ██║██║██║ ██║ ██║
██████╔╝╚██████╔╝██║███████╗██████╔╝
╚═════╝ ╚═════╝ ╚═╝╚══════╝╚═════╝
██╗███╗ ███╗ █████╗ ██████╗ ███████╗███████╗
██║████╗ ████║██╔══██╗██╔════╝ ██╔════╝██╔════╝
██║██╔████╔██║███████║██║ ███╗█████╗ ███████╗
██║██║╚██╔╝██║██╔══██║██║ ██║██╔══╝ ╚════██║
██║██║ ╚═╝ ██║██║ ██║╚██████╔╝███████╗███████║
╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝
EOF
docker-compose build endlessh 2>/dev/null
cat <<'EOF' > /dev/console
██████╗ ██╗ ██╗██╗ ██╗
██╔══██╗██║ ██║██║ ██║
██████╔╝██║ ██║██║ ██║
██╔═══╝ ██║ ██║██║ ██║
██║ ╚██████╔╝███████╗███████╗
╚═╝ ╚═════╝ ╚══════╝╚══════╝
██╗███╗ ███╗ █████╗ ██████╗ ███████╗███████╗
██║████╗ ████║██╔══██╗██╔════╝ ██╔════╝██╔════╝
██║██╔████╔██║███████║██║ ███╗█████╗ ███████╗
██║██║╚██╔╝██║██╔══██║██║ ██║██╔══╝ ╚════██║
██║██║ ╚═╝ ██║██║ ██║╚██████╔╝███████╗███████║
╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝
EOF
docker-compose pull
cat <<'EOF' > /dev/console
███████╗████████╗ █████╗ ██████╗ ████████╗
██╔════╝╚══██╔══╝██╔══██╗██╔══██╗╚══██╔══╝
███████╗ ██║ ███████║██████╔╝ ██║
╚════██║ ██║ ██╔══██║██╔══██╗ ██║
███████║ ██║ ██║ ██║██║ ██║ ██║
╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
███████╗████████╗ █████╗ ██████╗██╗ ██╗
██╔════╝╚══██╔══╝██╔══██╗██╔════╝██║ ██╔╝
███████╗ ██║ ███████║██║ █████╔╝
╚════██║ ██║ ██╔══██║██║ ██╔═██╗
███████║ ██║ ██║ ██║╚██████╗██║ ██╗
╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝
EOF
docker-compose up -d
# Wait for wireguard image container to create config file
CONF="/docker/wireguard/peer_${MY_USER}/peer_${MY_USER}.conf"
while :
do
[ -e ${CONF} ] && break
sleep 2
done
# Add some blocklists and entries in the whiltelist in Pi-Hole
docker exec pihole sqlite3 /etc/pihole/gravity.db "INSERT INTO adlist (address, enabled, comment) VALUES ('https://www.github.developerdan.com/hosts/lists/ads-and-tracking-extended.txt', 1, 'Developer Dan - Ads & Tracking');"
docker exec pihole sqlite3 /etc/pihole/gravity.db "INSERT INTO adlist (address, enabled, comment) VALUES ('https://www.github.developerdan.com/hosts/lists/dating-services-extended.txt', 1, 'Developer Dan - Dating Services');"
for domain in spclient.wg.spotify.com fonts.gstatic.com; do
docker exec pihole pihole -w $domain
done
# Store credentials information in /root/banner file
SEPARATOR=$(perl -le 'print "─" x 80')
(
echo -e "${SEPARATOR}\nWireguard configuration\n${SEPARATOR}\n"
qrencode -t ansiutf8 < ${CONF} | sed 's/^/ /'
echo -e "\n${SEPARATOR}"
cat ${CONF}
echo -e "${SEPARATOR}\nRoot password for console access: ${ROOT_PWD}\n${SEPARATOR}"
echo -e "Connect to this server:\n ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@$(curl -sL https://ifconfig.co/)\n${SEPARATOR}"
) > /root/banner
# Systemd service to print credentials on the console at boot time
cat <<'EOF' > /lib/systemd/system/banner-console.service
[Unit]
Description=Show information on the console
After=cloud-init.target
[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c "/usr/bin/cat /root/banner > /dev/console"
[Install]
WantedBy=cloud-init.target
EOF
systemctl daemon-reload
systemctl enable banner-console
#restart VM
cat <<'EOF' > /dev/console
██████╗ ███████╗██████╗ ██████╗ ██████╗ ████████╗
██╔══██╗██╔════╝██╔══██╗██╔═══██╗██╔═══██╗╚══██╔══╝
██████╔╝█████╗ ██████╔╝██║ ██║██║ ██║ ██║
██╔══██╗██╔══╝ ██╔══██╗██║ ██║██║ ██║ ██║
██║ ██║███████╗██████╔╝╚██████╔╝╚██████╔╝ ██║
╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚═════╝ ╚═╝
██████╗ ███████╗███╗ ██╗██████╗ ██╗███╗ ██╗ ██████╗
██╔══██╗██╔════╝████╗ ██║██╔══██╗██║████╗ ██║██╔════╝
██████╔╝█████╗ ██╔██╗ ██║██║ ██║██║██╔██╗ ██║██║ ███╗
██╔═══╝ ██╔══╝ ██║╚██╗██║██║ ██║██║██║╚██╗██║██║ ██║
██║ ███████╗██║ ╚████║██████╔╝██║██║ ╚████║╚██████╔╝
╚═╝ ╚══════╝╚═╝ ╚═══╝╚═════╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝
EOF
reboot