-
Notifications
You must be signed in to change notification settings - Fork 0
/
astroid
207 lines (180 loc) · 5.03 KB
/
astroid
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
#!/data/data/com.termux/files/usr/bin/bash
export bin="$PREFIX/bin"
export PATH=export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/usr/local/sbin:$bin:$PATH
export ROOTFS="$HOME/.rootfs"
export ROOTFS_URL="https://github.com/daoudeddy/Astroid/releases/latest/download/rootfs.tar.xz"
unset LD_PRELOAD
# This script need to be run as root
if [[ "$EUID" -ne 0 ]]; then
echo "[!] Please run as root"
exit
fi
# Checking for busybox
if [[ ! -x "$PREFIX/bin/busybox" ]]; then
echo "[!] Busybox not found"
exit
fi
# Checking root
if [[ ! -x $PREFIX/bin/sudo ]]; then
echo "[!] sudo not found!"
exit
fi
# Usage help
function script_usage() {
cat <<EOF
Usage:
-h --help Displays this help
-i --install Download and install the container
-s --start Start the container
-d --daemon Start the container in background
-k --kill Kill and exit the container
-d --delete Delete the container
EOF
}
# Setup the chroot
setup() {
# Creating virtual Kernel file systems.
mkdir -p "$ROOTFS/dev"
mkdir -p "$ROOTFS/dev/pts"
mkdir -p "$ROOTFS/dev/shm"
mkdir -p "$ROOTFS/system"
mkdir -p "$ROOTFS/tmp"
}
# Check if rootfs already mounted
is_mounted() {
if mount | grep "$ROOTFS$1" >/dev/null; then
echo "[~] $1 already mounted"
return 0
else
return 1
fi
}
# Start the required services
start_services() {
printf "[+] Staring services\n"
chroot $ROOTFS servicectl start sshd indiwebmanager virtualgps 2>/dev/null
}
# Stop the running services
stop_services() {
printf "[-] Stopping services\n"
chroot $ROOTFS servicectl stop sshd indiwebmanager virtualgps 2>/dev/null
local pids=$(lsof -t $ROOTFS 2>/dev/null | uniq)
if [ -n "${pids}" ]; then
kill -9 ${pids} 2>/dev/null
fi
}
# mount the chroot env
mount_env() {
# Calling setup before chrooting
setup
mount -o remount,suid /data
if ! is_mounted "/dev"; then
mount -o bind /dev "$ROOTFS/dev" && echo "[+] mounting /dev"
fi
if ! is_mounted "/dev/pts"; then
mount -t devpts devpts "$ROOTFS/dev/pts" && printf "[+] mounting /dev/pts\n"
fi
if ! is_mounted "/dev/shm"; then
mount -t tmpfs tmpfs "$ROOTFS/dev/shm" && printf "[+] mounting /dev/shm\n"
fi
if ! is_mounted "/proc"; then
mount -t proc proc "$ROOTFS/proc" && printf "[+] mounting /proc\n"
fi
if ! is_mounted "/sys"; then
mount -t sysfs sysfs "$ROOTFS/sys" && printf "[+] mounting /sys\n"
fi
if ! is_mounted "/tmp"; then
mount -t tmpfs tmpfs "$ROOTFS/tmp" && printf "[+] mounting /tmp\n"
fi
}
# Start the chroot env in foreground
start() {
mount_env
start_services
printf "[~] Chrooting into Astroid \n"
chroot $ROOTFS /usr/bin/env su -l - astroid
}
# Start the chroot env in background
background() {
mount_env
start_services
printf "[~] Astroid started in background \n"
}
# Stop the chroot env
stop() {
stop_services
# Unmount virtual Kernel file systems on exit.
umount "$ROOTFS/dev/pts" 2>/dev/null && printf "[-] unmounting /dev/pts\n"
umount "$ROOTFS/dev/shm" 2>/dev/null && printf "[-] unmounting /dev/shm\n"
umount "$ROOTFS/dev" 2>/dev/null && printf "[-] unmounting /dev\n"
umount "$ROOTFS/proc" 2>/dev/null && printf "[-] unmounting /proc\n"
umount "$ROOTFS/sys" 2>/dev/null && printf "[-] unmounting /sys\n"
umount "$ROOTFS/tmp" 2>/dev/null && printf "[-] unmounting /tmp\n"
printf "[~] Exiting chroot environment...\n"
}
# Download and install the rootfs
install() {
if [[ -d "$ROOTFS" ]]; then
printf "[!] Astroid rootfs is already installed\n"
else
local tarball="$HOME/rootfs.tar.xz"
printf "[!] Downloading Astroid rootfs\n"
wget -c -q --show-progress --tries=5 -N "$ROOTFS_URL" -O $tarball
printf "[!] Extracting Astroid rootfs\n"
if [[ $? -eq 0 ]]; then
# printf "[!] Cleaning up\n"
# rm $tarball
mkdir $ROOTFS
tar xf $tarball -C $ROOTFS
printf "[!] Run astroid --help for usage info\n"
else
printf "[!] Failed to download\n"
fi
fi
}
# Delete rootfs
delete() {
printf "[?] Are you sure [Y/n]\n"
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
stop
printf "[!] Deleting\n"
rm -rf $ROOTFS
else
printf "[!] Canceling\n"
fi
}
# Parameter parser
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
script_usage
exit 0
;;
-s | --start)
start
exit 0
;;
-b | --background)
background
exit 0
;;
-k | --kill)
stop
exit 0
;;
-i | --install)
install
exit 0
;;
-d | --delete)
delete
exit 0
;;
*)
printf "[!] Invalid option was provided: $1"
exit 0
;;
esac
done
script_usage