Repository navigation
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·120 lines (105 loc) · 5.3 KB
/
Copy pathuninstall.sh
File metadata and controls
executable file
·120 lines (105 loc) · 5.3 KB
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
#!/usr/bin/env bash
# Remove the Hackrange Local Lab from this machine.
#
# sudo /opt/hackrange-mentorship/uninstall.sh
#
# Removes the lab services and ALL of their data (your lab repositories, the
# package cache, your findings), the Kubernetes cluster, the CI runner, the
# lab network address, the lab certificate authority and the lab images.
# Docker itself is left installed, because other things may use it.
#
# Author: Tim Rice - tim.j.rice@hackrange.com
set -uo pipefail
[ "$(id -u)" -eq 0 ] || { echo "Run this with sudo." >&2; exit 1; }
HR_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
HR_ETC=/etc/hackrange-mentorship
if [ "${1:-}" != -y ] && [ "${1:-}" != --yes ]; then
printf 'This removes the Local Lab and everything in it, including your lab\n'
printf 'repositories on the lab Git server. Anything you have not copied\n'
printf 'elsewhere (for example to GitHub) is gone for good.\n\n'
read -r -p 'Type REMOVE to continue: ' a < /dev/tty
[ "$a" = REMOVE ] || { echo "Nothing was removed."; exit 0; }
fi
say() { printf ' %s\n' "$*"; }
say "stopping the lab session"
docker rm -f hackrange-lab >/dev/null 2>&1
say "removing the lab services and their data"
if [ -f "$HR_ETC/services.env" ]; then
docker compose --project-directory "$HR_ETC" -f "$HR_ROOT/config/compose.yml" \
--env-file "$HR_ETC/services.env" down -v --remove-orphans >/dev/null 2>&1
fi
say "removing the CI runner"
# Job containers the runner left behind.
say "removing Kubernetes"
if [ -f /etc/sysctl.d/90-hackrange-x86-emulation.conf ]; then
rm -f /etc/sysctl.d/90-hackrange-x86-emulation.conf
sysctl -qw vm.legacy_va_layout=0
fi
say "removing the control panel"
systemctl disable --now hackrange-mentorship-control >/dev/null 2>&1
rm -f /etc/systemd/system/hackrange-mentorship-control.service
rm -rf /usr/local/lib/hackrange-mentorship /run/hackrange-mentorship
say "removing nginx's lab sites"
# Every name here is one the installer really writes. They did not match
# before, which left this machine's nginx holding a site that named a
# certificate this script had just deleted: nginx then refused to start at
# all, so uninstalling the lab broke whatever else the machine was serving.
rm -rf /etc/nginx/mentorship-brand
rm -f /etc/apt/apt.conf.d/90hackrange-wait-for-lock
rm -f /etc/nginx/conf.d/mentorship-access.conf \
/etc/nginx/conf.d/mentorship-hostcheck.conf \
/etc/nginx/mentorship-private-only.conf \
/etc/nginx/modules-enabled/99-mentorship-stream.conf
systemctl disable --now hackrange-access >/dev/null 2>&1
rm -f /etc/systemd/system/hackrange-access.service /usr/local/sbin/hackrange-access
rm -rf /etc/nginx/mentorship-tls /etc/systemd/system/nginx.service.d/hackrange.conf
# Reload only if the remaining configuration is valid, and say so if it is
# not: a silent "|| true" here is how a machine ends up with nginx down and
# nothing to explain it.
if systemctl is-active --quiet nginx; then
if nginx -t >/dev/null 2>&1; then
systemctl reload nginx >/dev/null 2>&1 || true
else
say "nginx's remaining configuration is not valid, so it was left alone."
say "Check it with: sudo nginx -t"
fi
fi
say "removing the lab network address and names"
command -v nft >/dev/null 2>&1 && nft delete table inet hackrange_guard 2>/dev/null || true
kept="$(grep -v '# Hackrange Local Lab$' /etc/hosts)"; printf '%s\n' "$kept" > /etc/hosts
say "removing the lab certificate authority from this machine's trust"
rm -f /usr/local/share/ca-certificates/hackrange-lab-ca.crt
update-ca-certificates --fresh >/dev/null 2>&1
say "removing the lab images"
for i in kali-lab-ssh:latest netlab:latest analyst-lab:latest splunk-lab:latest webapp-lab:latest mentorship-webapp-gui:latest cloud-audit-lab:latest nextgen-dast-lab:latest nextgen-dast:2.1.1; do
docker rmi -f "$i" >/dev/null 2>&1
done
# And the published images the lab downloaded. Without -f: Docker refuses to
# remove an image a container still uses, so a student's own containers keep
# theirs.
if [ -f "$HR_ROOT/versions.env" ]; then
# shellcheck disable=SC1091
. "$HR_ROOT/versions.env"
# The base images the lab builds pulled: Kali for two labs, Debian for four,
# and Python for the scanner. A student may well have their own containers
# on these, so a removal that is in use simply fails and is ignored.
for i in "${GUACAMOLE_IMAGE:-}" "${GUACD_IMAGE:-}" \
hello-world:latest kalilinux/kali-rolling:latest debian:12-slim python:3.12-slim; do
[ -n "$i" ] && docker rmi "$i" >/dev/null 2>&1
done
fi
docker image prune -f >/dev/null 2>&1
# The build cache the image builds left behind, about 20 GB. It is only a
# cache: a student's own images and containers are not touched, and their
# own builds simply rebuild what they need.
docker builder prune -af >/dev/null 2>&1
say "removing the lab's files"
rm -rf "$HR_ETC" /var/cache/hackrange /var/lib/hackrange \
/usr/local/bin/hackrange-lab /usr/local/bin/hackrange-check \
/usr/local/sbin/hackrange-diag \
/usr/share/applications/hackrange-lab.desktop
groupdel hackrange-mentorship-guac 2>/dev/null || true
systemctl daemon-reload
# Last, because this script lives there.
[ "$HR_ROOT" = /opt/hackrange-mentorship ] && rm -rf /opt/hackrange-mentorship
printf '\nThe Local Lab has been removed. Docker is still installed.\n'