-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy.sh
executable file
·336 lines (286 loc) · 10.8 KB
/
deploy.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
#!/bin/bash
usage()
{
echo "usage: wget --no-cache -q -O- https://raw.githubusercontent.com/sec4us-training/web-api-linux/main/deploy.sh | sudo bash"
}
info()
{
whoami
uname -a
cat /etc/lsb-release
lsb_release -a
ip addr show
}
W='\033[0m' # white (normal)
R='\033[31m' # red
G='\033[32m' # green
O='\033[33m' # orange
B='\033[34m' # blue
P='\033[35m' # purple
C='\033[36m' # cyan
GR='\033[37m' # gray
D='\033[2m' # dims current color. {W} resets.
OK=$(echo -e "${W}${D}[${W}${G}+${W}${D}]${W}")
ERROR=$(echo -e "${O}[${R}!${O}]${W}")
WARN=$(echo -e "${W}[${C}?${W}]")
DEBUG=$(echo -e "${D}[${W}${B}*${W}${D}]${W}")
unset ansible_user
unset key
unset ip
unset status_file
unset PYTHON3
echo "IF9fX19fICAgICAgICAgICAgIF9fXyBfICAgXyBfX19fXwovICBfX198ICAgICAgICAgICAvICAgfCB8IHwgLyAgX19ffApcIGAtLS4gIF9fXyAgX19fIC8gL3wgfCB8IHwgXCBgLS0uCiBgLS0uIFwvIF8gXC8gX18vIC9ffCB8IHwgfCB8YC0tLiBcCi9cX18vIC8gIF9fLyAoX19cX19fICB8IHxffCAvXF9fLyAvClxfX19fLyBcX19ffFxfX198ICAgfF8vXF9fXy9cX19fXy8K" | base64 -d
echo " "
echo "Treinamento: Web API Exploitation"
echo "Linux Deploy"
echo " "
echo "Copyright © Sec4US® - Todos os direitos reservados. Nenhuma parte dos materiais disponibilizadas, incluindo este script, servidor, suas aplicações e seu código fonte, podem ser copiadas, publicadas, compartilhadas, redistribuídas, sublicenciadas, transmitidas, alteradas, comercializadas ou utilizadas para trabalhos sem a autorização por escrito da Sec4US"
if [ "$(id -u)" -ne 0 ]; then
usage
echo -e "\n${ERROR} ${O}Execute este script como root${W}\n"
info
exit 1;
fi
ansible_user=$(whoami)
status_file=$(pwd)/executed.txt
echo -e "\n${OK} Atualizando servidor"
#Remove CDROM line from apt source list
# deb [check-date=no] file:///cdrom jammy main restricted
cp /etc/apt/sources.list /tmp/
grep -vE '^deb.*/cdrom.*' /tmp/sources.list > /etc/apt/sources.list
DEBIAN_FRONTEND=noninteractive apt update
DEBIAN_FRONTEND=noninteractive apt -y upgrade
DEBIAN_FRONTEND=noninteractive apt install -y ansible openssh-client openssh-server git wget vim zip unzip python3 python3-pip
echo -e "\n${OK} Configurando SSH"
systemctl enable ssh
systemctl start ssh
echo -e "\n${OK} Gerando chaves SSH"
key="/tmp/sshkey"
if [ ! -f "$key" ]; then
hostname WebAPIExploitation
hostnamectl set-hostname WebAPIExploitation
ssh-keygen -b 2048 -t rsa -f $key -q -N ""
fi
PYTHON3="/usr/bin/python3"
echo -e "\n${OK} Install/update PIP3"
$PYTHON3 -m pip install -U pip
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro atualizando PIP${W}\n"
info
exit 1
fi
echo -e "\n${OK} Instalando/atualizando versão do ansible core"
$PYTHON3 -m pip install -U ansible 'ansible-core>=2.13.9' 'jinja2>=3.1.2'
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro atualizando PIP${W}\n"
info
exit 1
fi
echo -e "\n${OK} Iniciando deploy"
ip="127.0.0.1"
SSH_FILE=$key
# Verifica se o arquivo da chave SSH existe
if [ ! -f "$SSH_FILE" ]; then
echo -e "${ERROR} ${O}Arquivo de chave privada do SSH inexistente: ${C}${SSH_FILE}${W}\n"
info
exit 1
fi
SSH_FILE_PUB="$key.pub"
# Verifica se o arquivo da chave SSH existe
if [ ! -f "$SSH_FILE_PUB" ]; then
echo -e "${ERROR} ${O}Arquivo de chave pública do SSH inexistente: ${C}${SSH_FILE_PUB}${W}\n"
info
exit 1
fi
echo -e "\n${OK} Realizando o download dos scripts"
git clone https://github.com/sec4us-training/web-api-linux.git /tmp/web-api-linux
pushd /tmp/web-api-linux
cp -f $SSH_FILE ssh_key.pem
cp -f $SSH_FILE_PUB ssh_key.pub
cp -f $SSH_FILE /tmp/web_api_ssh_key.pem
cp -f $SSH_FILE_PUB /tmp/web_api_ssh_key.pub
chmod 444 /tmp/web_api_ssh*
mkdir /root/.ssh/
cat ssh_key.pub >> /root/.ssh/authorized_keys
PK=$(cat ssh_key.pem)
PUBK=$(cat ssh_key.pub)
ansible_path=`command -v ansible-playbook 2> /dev/null`
if [ "$?" -ne "0" ] || [ "W$ansible_path" = "W" ]; then
ansible_path=`which ansible-playbook 2> /dev/null`
if [ "$?" -ne "0" ]; then
ansible_path=""
fi
fi
if [ "W$ansible_path" = "W" ]; then
echo -e "${ERROR} ${O}A aplicação ansible-playbook parece não estar instalada.${W}\n\n"
echo "Em ambientes debian/ubuntu, realize a instalação com os comandos abaixo:"
echo "sudo apt install ansible"
echo "ansible-galaxy collection install community.general"
echo "ansible-galaxy collection install ansible.posix"
echo "ansible-galaxy collection install ansible.windows"
echo "ansible-galaxy collection install community.windows"
echo " "
info
exit 1
fi
echo -e "\n${OK} Instalando dependencias do ansible"
grep "ansible_deps" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando instalação...${W}"
else
ansible-galaxy collection install community.general
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install ansible.windows
ansible-galaxy collection install community.windows
echo "ansible_deps" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# remove a linha ansible_user do vars.yml
cp vars.yml vars_old.yml
grep -i -v "ansible_user" vars_old.yml > vars.yml
echo -e "\n${OK} Verificando senha padrão no arquivo vars.yml"
password=$(cat vars.yml | grep default_password | cut -d '"' -f2)
if [ "$?" -ne "0" ] || [ "W$password" = "W" ]; then
echo -e "\n${ERROR} ${O}Senha do usuário ${C}webapi ${O}não definida no parâmetro ${C}'default_password' ${O}do arquivo vars.yml${W}\n"
info
exit 1
else
echo -e "${OK} ${G}OK${W}"
fi
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
# Check conectivity and if user has root privileges
echo -e "\n${OK} Verificando usuário"
ansible-playbook -vvv -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' check_user.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro verificando usuário${W}\n"
info
exit 1
fi
# Step 1 - base
echo -e "\n${OK} Executando passo 1 - setup_base.yml"
grep "step1_base" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 1...${W}"
else
if [ `uname -s` == "Darwin" ]; then
sed -i "" "s/PasswordAuthentication no/PasswordAuthentication yes/g" "setup_base.yml"
else
sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" "setup_base.yml"
fi
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_base.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible setup base${W}\n"
info
exit 1
fi
echo "step1_base" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 2 - Tools
echo -e "\n${OK} Executando passo 2 - setup_tools.yml"
grep "step2_tools" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 2...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_tools.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible tools${W}\n"
info
exit 1
fi
echo "step2_tools" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 3 - API1
echo -e "\n${OK} Executando passo 3 - setup_api1.yml"
grep "step3_api1" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 3...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_api1.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible API1 setup${W}\n"
info
exit 1
fi
echo "step3_api1" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 4 - API2
echo -e "\n${OK} Executando passo 4 - setup_api2.yml"
grep "step4_api2" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 4...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_api2.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible API2 setup${W}\n"
info
exit 1
fi
echo "step4_api2" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 5 - API Auth
echo -e "\n${OK} Executando passo 5 - setup_apiauth.yml"
grep "step5_apiauth" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 5...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_apiauth.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible API Auth setup${W}\n"
info
exit 1
fi
echo "step5_apiauth" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 6 - OAuth Bank
echo -e "\n${OK} Executando passo 6 - setup_bank.yml"
grep "step6_bank" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 6...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_bank.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible Bank setup${W}\n"
info
exit 1
fi
echo "step6_bank" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
# Step 7 - RDP
echo -e "\n${OK} Executando passo 7 - setup_rdp.yml"
grep "step7_rdp" "$status_file" >/dev/null 2>&1
if [ "$?" == "0" ]; then
echo -e "${DEBUG} ${C}Pulando passo 7...${W}"
else
ansible-playbook -i $ip, --private-key $SSH_FILE --extra-vars ansible_user=$ansible_user --ssh-extra-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' setup_rdp.yml
if [ "$?" != "0" ]; then
echo -e "${ERROR} ${O} Erro executando ansible RDP setup${W}\n"
info
exit 1
fi
echo "step7_rdp" >> "$status_file"
echo -e "${OK} ${G}OK${W}"
fi
popd
echo -e "\n\n${O}===================================================${W}"
echo -e "${O} Deploy finalizado! ${W}"
echo -e "${O}===================================================${W}"
echo -e "${OK} Credenciais"
echo -e " ${C}Usuário.:${O} webapi${W}"
echo -e " ${C}Senha...:${O} ${password}${W}"
echo ""
echo -e "${OK} Acessos"
echo -e " ${C}IP......:${O} ${ip}${W}"
echo -e " ${C}SSH.....:${O} Porta 22${W}"
echo -e " ${C}RDP.....:${O} Porta 48389${W}"
echo -e " ${C}Proxy...:${O} Porta 48284${W}"
echo " "
echo -e "${OK} Chaves"
echo -e " ${C}Chave privada SSH.:${O}\n${PK}\n${W}"
echo -e " ${C}Chave pública SSH.:${O}\n${PUBK}v${W}"
echo ""