-
Notifications
You must be signed in to change notification settings - Fork 81
/
build
executable file
·243 lines (204 loc) · 7.12 KB
/
build
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
#!/bin/bash
# Import configuration variables from env file
export $(grep -v '^#' env | xargs)
# ##########################################################################
# Remote access variables (can be adapted)
# ##########################################################################
# set the default values for remote access ports
# ##########################################################################
if [ -z $XRDP_PORT ];
then
XRDP_PORT=13389
fi
if [ -z $XVNC_DISPLAY ];
then
XVNC_DISPLAY=8
fi
if [ -z $XVNC_PORT ];
then
XVNC_PORT=590$XVNC_DISPLAY
fi
if [ -z $XSSH_PORT ];
then
XSSH_PORT=20022
fi
# ##########################################################################
# Variables (For menu choice - should not be changed)
# ##########################################################################
# The following variables contain the default and the possible choices
# for Desktop Environment, Remote Access, Kali Packages, Network, and build
# architecture
# ##########################################################################
XDESKTOP_CHOICE=("xfce" "mate" "kde" "e17" "gnome" "i3" "i3-gaps" "live" "lxde")
XREMOTE_CHOICE=("vnc" "rdp" "x2go")
XKALI_CHOICE=("arm" "core" "default" "everything" "firmware" "headless" "labs" "large" "nethunter")
XNET_CHOICE=("bridge" "host")
XBUILD_CHOICE=("amd64" "arm64")
# ##########################################################################
# menu function
# ##########################################################################
# Takes an array as parameter, e.g. ("Color" "blue" "red")
# it will then prompt "Please select a xxx" and replace xxx with the first
# element of the array (in the above example "Please select a color")
# It then shows numbers starting from 1 and options you may chose
# ( 1 - blue, 2 - red in the example) and return the user's choice
# in the variable $choice
# ##########################################################################
menu() {
choice=0
while true; do
XCHOICE=("$@")
echo -e "\nPlease select a $1"
for (( i=1; i < ${#XCHOICE[@]} ; i++)) ; do
echo "$i - ${XCHOICE[$i]}"
done
read -n 1 -p "Your choice -> " choice
echo -e
if (($choice >=1 && $choice < ${#XCHOICE[@]})); then break ; fi
done
}
# ##########################################################################
# Script starting point
# ##########################################################################
echo "This script will create a custom Kali Linux Docker container for you"
# ##########################################
# ask the user what he/she wants to install
# we call the menu function with each of the
# above variables if not specified in .env
# ##########################################
if [ -z $XDESKTOP_ENVIRONMENT ];
then
menu "Desktop Environment (only xfce for xrdp right now)" ${XDESKTOP_CHOICE[@]}
XDESKTOP_ENVIRONMENT=${XDESKTOP_CHOICE[$choice-1]}
fi
if [ -z $XREMOTE_ACCESS ];
then
menu "Remote Access Option" ${XREMOTE_CHOICE[@]}
XREMOTE_ACCESS=${XREMOTE_CHOICE[$choice-1]}
fi
if [ -z $XKALI_PKG ];
then
menu "Kali Package" ${XKALI_CHOICE[@]}
XKALI_PKG=${XKALI_CHOICE[$choice-1]}
fi
if [ -z $XNETWORK ];
then
menu "Network" ${XNET_CHOICE[@]}
XNETWORK=${XNET_CHOICE[$choice-1]}
fi
if [ -z $XBUILD_PLATFORM ];
then
menu "Build Architecture" ${XBUILD_CHOICE[@]}
XBUILD_PLATFORM=${XBUILD_CHOICE[$choice-1]}
fi
# ##########################################
# additional user config input:
# - Name for local custom Kali Docker image
# - Name for created container
# - Dir of host machine to mount
# - Dir to mount in container
# - Username for container user
# - Password for conatiner user (echo off)
# ##########################################
if [ -z $DOCKERIMG ];
then
printf "Enter desired local Docker image name (e.g. custom/kali-linux): "
read DOCKERIMG
printf "\n"
fi
if [ -z $CONTAINER ];
then
printf "Enter desired local Docker container name (e.g. kali-linux): "
read CONTAINER
printf "\n"
fi
if [ -z $HOSTDIR ];
then
printf "Enter host directory to mount: "
read HOSTDIR
printf "\n"
fi
if [ -z $CONTAINERDIR ];
then
printf "Enter container directory to mount to: "
read CONTAINERDIR
printf "\n"
fi
if [ -z $USERNAME ];
then
printf "Enter desired username: "
read USERNAME
printf "\n"
fi
if [ -z $PASSWORD ];
then
stty -echo
printf "Enter desired password (password will show in Docker output): "
read PASSWORD
stty echo
printf "\n"
fi
# ##########################################
# show a summary of the Installation choices
# and confirm choices
# ##########################################
clear
echo -e "Configuration:\n"
echo "Desktop environment: $XDESKTOP_ENVIRONMENT"
echo "Remote Access: $XREMOTE_ACCESS"
echo "Kali packages: $XKALI_PKG"
echo -e "Network: $XNETWORK"
echo -e "Build platform: $XBUILD_PLATFORM"
echo -e "Image name: $DOCKERIMG"
echo -e "Conatiner name: $CONTAINER"
echo -e "Host dir mount: $HOSTDIR"
echo -e "Container dir mount: $CONTAINERDIR"
echo -e "Username: $USERNAME"
echo -e "Password [redacted]"
printf "\nHit enter to start building the container"
read
# ##########################################
# build the image
# ##########################################
# call docker build and pass on all
# the choices as build-arg to the Dockerfile
# where they will be interpreted
# ##########################################
docker image build --platform linux/$XBUILD_PLATFORM \
-t $DOCKERIMG \
--build-arg DESKTOP_ENVIRONMENT=$XDESKTOP_ENVIRONMENT \
--build-arg REMOTE_ACCESS=$XREMOTE_ACCESS \
--build-arg KALI_PACKAGE=$XKALI_PKG \
--build-arg RDP_PORT=$XRDP_PORT \
--build-arg VNC_PORT=$XVNC_PORT \
--build-arg VNC_DISPLAY=$XVNC_DISPLAY \
--build-arg SSH_PORT=$XSSH_PORT \
--build-arg BUILD_ENV=$XBUILD_PLATFORM \
--build-arg HOSTDIR \
--build-arg CONTAINERDIR \
--build-arg UNAME=$USERNAME \
--build-arg UPASS=$PASSWORD \
.
# ##########################################
# create the container
# ##########################################
# call docker create and pass on all
# the choices for network and ports that the
# user has made in the menu
# ##########################################
docker create --name $CONTAINER \
--network $XNETWORK \
--platform linux/$XBUILD_PLATFORM \
-p $XRDP_PORT:$XRDP_PORT \
-p $XVNC_PORT:$XVNC_PORT \
-p $XSSH_PORT:$XSSH_PORT \
-t \
-v $HOSTDIR:$CONTAINERDIR \
$DOCKERIMG
# ##########################################
# start the container
# ##########################################
echo "Image ($DOCKERIMG) and container ($CONTAINER) build successful. $CONTAINER will now start."
docker start $CONTAINER
# Clear environment variables from env file
unset $(grep -v '^#' env | sed -E 's/(.*)=.*/\1/' | xargs)