-
Notifications
You must be signed in to change notification settings - Fork 1
/
rusthunter.sh
executable file
·681 lines (585 loc) · 19.6 KB
/
rusthunter.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#!/bin/bash
###########################################################
# COLORS
RESET="\e[0m"
BOLD="\e[1m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
###########################################################
###########################################################
# VARIABLES
INITIAL_SNAPSHOT="NONE"
CURRENT_SNAPSHOT="NONE"
HOSTS_FILE="NONE"
ENCRYPT_HOSTS="NONE"
REKEY_HOSTS="NONE"
VIEW_HOSTS="NONE"
EDIT_HOSTS="NONE"
DECRYPT_HOSTS="NONE"
CONFIG_FILE="NONE"
SNAPSHOT_TAG="NONE"
PRINT_STATS="NONE"
FILTERED_HOST="NONE"
FILTERED_PLUGIN="NONE"
UNIT_TESTS="NONE"
INTEGRATION_TESTS="NONE"
VALIDATION_TESTS="NONE"
EXECUTABLE_NAME="rusthunter"
BUILDER_IMAGE_NAME="rusthunter/builder"
BUILDER_IMAGE_PATH="./builder"
LAUNCHER_IMAGE_NAME="rusthunter/launcher"
LAUNCHER_IMAGE_PATH="./launcher"
INSTALLATION_PATH="/usr/bin"
APP_PATH="./app"
ANSIBLE_PATH="./launcher/ansible"
LINUX_BINARIES_PATH="$ANSIBLE_PATH/roles/linux/files"
MACOS_BINARIES_PATH="$ANSIBLE_PATH/roles/macos/files"
WINDOWS_BINARIES_PATH="$ANSIBLE_PATH/roles/windows/files"
SNAPSHOT_PATH="./launcher/snapshots"
DEFAULT_CONFIG_FILE="config.ini"
DEFAULT_HOSTS_FILE="hosts"
###########################################################
function ShowBanner {
clear
echo -e "$BLUE$BOLD /####### /## /## /## /## $RESET"
echo -e "$BLUE$BOLD | ##__ ## | ## | ## | ## | ## $RESET"
echo -e "$BLUE$BOLD | ## \ ## /## /## /####### /###### | ## | ## /## /## /######$ /###### /###### /###### $RESET"
echo -e "$BLUE$BOLD | #######/| ## | ## /##_____/|_ ##_/ | ########| ## | ##| ##__ ##|_ ##_/ /##__ ## /##__ ## $RESET"
echo -e "$BLUE$BOLD | ##__ ##| ## | ##| ###### | ## | ##__ ##| ## | ##| ## \ ## | ## | ########| ## \__/ $RESET"
echo -e "$BLUE$BOLD | ## \ ##| ## | ## \____ ## | ## /##| ## | ##| ## | ##| ## | ## | ## /##| ##_____/| ## $RESET"
echo -e "$BLUE$BOLD | ## | ##| ######/ /#######/ | ####/| ## | ##| ######/| ## | ## | ####/| #######| ## $RESET"
echo -e "$BLUE$BOLD |__/ |__/ \______/ |_______/ \___/ |__/ |__/ \______/ |__/ |__/ \___/ \_______/|__/ $RESET"
echo
}
function ShowHelp {
echo "usage: $0 [SUBCOMMAND] [ARGS]"
echo
echo "SUBCOMMANDS:"
echo
echo " install Install RustHunter on the system"
echo " list List all available plugins"
echo " local Take a local snapshot"
echo " hosts Protect the hosts inventory file"
echo " global Take a global snapshot based on hosts file (installs Docker)"
echo " compare Compare two snapshots"
echo " uninstall Uninstall RustHunter from the system"
echo " build Build RustHunter from code (installs Docker)"
echo " update Get the latest RustHunter updates"
echo " help This help"
echo
echo "usage: $0 hosts -HostsFile HOSTS_FILE (-h |--hosts) (-e |--encrypt) (-r |--rekey) (-v |--view) (-e |--edit) (-d |--decrypt)"
echo
echo " -h |--hosts Hosts file"
echo " -e |--encrypt Add encryption"
echo " -r |--rekey Change encryption key"
echo " -v |--view View encrypted file"
echo " -e |--edit Edit encrypted file"
echo " -d |--decrypt Decrypt file"
echo
echo "usage: $0 local (-c|--config) CONFIG_FILE (-t|--tag) SNAPSHOT_TAG"
echo
echo " -c |--config Configuration file"
echo " -t |--tag Snapshot tag"
echo
echo "usage: $0 global (-h|--hosts) HOSTS_FILE (-c|--config) CONFIG_FILE (-t|--tag) SNAPSHOT_TAG"
echo
echo " -h |--hosts Hosts file"
echo " -c |--config Configuration file"
echo " -t |--tag Snapshot tag"
echo
echo "usage: $0 compare (-i|--initial) INITIAL_SNAPSHOT (-c|--current) CURRENT_SNAPSHOT (-s |--stats) (-h |--host) HOST (-p |--plugin) PLUGIN"
echo
echo " -i |--initial Initial snapshot"
echo " -c |--current Current snapshot"
echo " -s |--stats Show statistics"
echo " -h |--host Filter host"
echo " -p |--plugin Filter plugin"
echo
}
###########################################################
# GENERAL FUNCTIONS
function print_error {
echo -e "$RED$BOLD [-] $1 $RESET"
echo
exit 1
}
function print_warning {
echo -e "$YELLOW$BOLD [!] $1 $RESET"
}
function print_info {
echo -e "$GREEN$BOLD [+] $1 $RESET"
}
function is_executable_installed {
if [ ! -f $INSTALLATION_PATH/$EXECUTABLE_NAME ]; then
print_error "The tool has not been installed yet!"
fi
}
function install_docker {
if [ ! -x "$(command -v docker)" ]; then
print_warning "Installing docker daemon"
apt update
apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
apt update
apt -y install docker-ce docker-ce-cli containerd.io
docker run hello-world
fi
}
function build_builder_image {
if [[ -z $(docker images -q ${BUILDER_IMAGE_NAME}:latest 2> /dev/null) ]];
then
print_info "Building builder docker image"
docker build -t $BUILDER_IMAGE_NAME $BUILDER_IMAGE_PATH || { print_error "Builder docker image building failed" ; exit 1; }
fi
}
function build_launcher_image {
if [[ -z $(docker images -q ${LAUNCHER_IMAGE_NAME}:latest 2> /dev/null) ]];
then
print_info "Building launcher docker image"
docker build -t $LAUNCHER_IMAGE_NAME $LAUNCHER_IMAGE_PATH || { print_error "Launched docker image building failed" ; exit 1; }
fi
}
function is_file_encrypted {
if [[ -n $(grep "ANSIBLE_VAULT" $1) ]];
then
IS_FILE_ENCRYPTED="True"
else
IS_FILE_ENCRYPTED="False"
fi
}
###########################################################
# SUBCOMMAND BUSINESS LOGIC
function execute_install_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
if [ ! -f $LINUX_BINARIES_PATH/$EXECUTABLE_NAME ]; then
print_error "The tool has not been built yet!"
else
print_info "Installing"
cp $LINUX_BINARIES_PATH/$EXECUTABLE_NAME $INSTALLATION_PATH
fi
build_launcher_image
print_info "Successfully installed"
}
function execute_list_subcommand {
is_executable_installed
$EXECUTABLE_NAME list
}
function execute_local_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
is_executable_installed
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
-c|--config)
CONFIG_FILE="${2}"
shift
shift
;;
-t|--tag)
SNAPSHOT_TAG="${2}"
shift
shift
;;
*)
ShowHelp
exit 1
;;
esac
done
if [ "$CONFIG_FILE" == "NONE" ]; then
print_error "Please specify the config file"
fi
print_info "Collecting data"
if [ "$SNAPSHOT_TAG" != "NONE" ]; then
sudo $EXECUTABLE_NAME run --config $CONFIG_FILE --binary-dir $LINUX_BINARIES_PATH --tag $SNAPSHOT_TAG
else
$EXECUTABLE_NAME run --config $CONFIG_FILE --binary-dir $LINUX_BINARIES_PATH
fi
}
function execute_hosts_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
install_docker
build_launcher_image
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
-h|--hosts)
HOSTS_FILE=${2}
shift
shift
;;
-e|--encrypt)
ENCRYPT_HOSTS="True"
shift
shift
;;
-r|--rekey)
REKEY_HOSTS="True"
shift
shift
;;
-v|--view)
VIEW_HOSTS="True"
shift
shift
;;
-t|--edit)
EDIT_HOSTS="True"
shift
shift
;;
-d|--decrypt)
DECRYPT_HOSTS="True"
shift
shift
;;
*)
ShowHelp
exit 1
;;
esac
done
if [ "$HOSTS_FILE" == "NONE" ]; then
print_error "Please specify the host file"
fi
if [[ "$ENCRYPT_HOSTS" == "NONE" && "$REKEY_HOSTS" == "NONE" && "$VIEW_HOSTS" == "NONE" && "$EDIT_HOSTS" == "NONE" && "$DECRYPT_HOSTS" == "NONE" ]]; then
print_error "Please specify an action on the hosts inventory file"
fi
is_file_encrypted $HOSTS_FILE
if [[ "$ENCRYPT_HOSTS" == "True" && "$IS_FILE_ENCRYPTED" == "True" ]]; then
print_error "$HOSTS_FILE is already encrypted"
fi
if [[ ( "$REKEY_HOSTS" == "True" || "$VIEW_HOSTS" == "True" || "$EDIT_HOSTS" == "True" || "$DECRYPT_HOSTS" == "True" ) && "$IS_FILE_ENCRYPTED" == "False" ]]; then
print_error "$HOSTS_FILE is not encrypted"
fi
if [[ "$ENCRYPT_HOSTS" == "True" && "$IS_FILE_ENCRYPTED" == "False" ]]; then
COMMAND="encrypt"
print_info "Encrypting hosts file"
elif [ "$REKEY_HOSTS" == "True" ]; then
COMMAND="rekey"
print_info "Rekeying hosts file"
elif [ "$VIEW_HOSTS" == "True" ]; then
COMMAND="view"
print_info "Showing hosts file"
elif [ "$EDIT_HOSTS" == "True" ]; then
COMMAND="edit"
print_info "Editing hosts file"
elif [ "$DECRYPT_HOSTS" == "True" ]; then
COMMAND="decrypt"
print_info "Decrypting hosts file"
fi
docker run --rm -v $PWD/${HOSTS_FILE}:/tmp/hosts -it ${LAUNCHER_IMAGE_NAME}:latest bash -c "cp /tmp/hosts /tmp/host.tmp;env EDITOR=nano ansible-vault $COMMAND /tmp/host.tmp; cp /tmp/host.tmp /tmp/hosts"
}
function execute_global_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
is_executable_installed
install_docker
build_launcher_image
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
-h|--hosts)
HOSTS_FILE=${2}
shift
shift
;;
-c|--config)
CONFIG_FILE="${2}"
shift
shift
;;
-t|--tag)
SNAPSHOT_TAG="${2}"
shift
shift
;;
*)
ShowHelp
exit 1
;;
esac
done
if [ "$HOSTS_FILE" == "NONE" ]; then
print_error "Please specify the host file"
fi
if [ "$CONFIG_FILE" == "NONE" ]; then
print_error "Please specify the config file"
fi
cp $HOSTS_FILE $ANSIBLE_PATH/$DEFAULT_HOSTS_FILE
cp $CONFIG_FILE $LINUX_BINARIES_PATH/$DEFAULT_CONFIG_FILE
cp $CONFIG_FILE $MACOS_BINARIES_PATH/$DEFAULT_CONFIG_FILE
cp $CONFIG_FILE $WINDOWS_BINARIES_PATH/$DEFAULT_CONFIG_FILE
print_info "Creating snapshots directory"
mkdir -p $SNAPSHOT_PATH
print_info "Collecting data"
is_file_encrypted $HOSTS_FILE
args=""
if [ "$IS_FILE_ENCRYPTED" == "True" ]; then
args=" --ask-vault-pass"
fi
if [ "$SNAPSHOT_TAG" != "NONE" ]; then
args="$args --extra-vars 'snapshot_tag=$SNAPSHOT_TAG'"
fi
eval "docker run --rm -v $PWD/$ANSIBLE_PATH:/etc/ansible -v $PWD/$SNAPSHOT_PATH:/snapshots -w /etc/ansible -it $LAUNCHER_IMAGE_NAME:latest ansible-playbook playbook.yml $args"
print_info "Merging data"
if [ "$SNAPSHOT_TAG" != "NONE" ]; then
$EXECUTABLE_NAME merge --directory $SNAPSHOT_PATH --tag $SNAPSHOT_TAG
else
$EXECUTABLE_NAME merge --directory $SNAPSHOT_PATH
fi
print_info "Cleaning up"
rm -rf $SNAPSHOT_PATH
}
function execute_compare_subcommand {
is_executable_installed
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
-i|--initial)
INITIAL_SNAPSHOT=${2}
shift
shift
;;
-c|--current)
CURRENT_SNAPSHOT="${2}"
shift
shift
;;
-s|--stats)
PRINT_STATS="True"
shift
shift
;;
-h|--host)
FILTERED_HOST="${2}"
shift
shift
;;
-p|--plugin)
FILTERED_PLUGIN="${2}"
shift
shift
;;
*)
ShowHelp
exit 1
;;
esac
done
ARGS=""
if [ "$INITIAL_SNAPSHOT" == "NONE" ]; then
print_error "Please specify the initial snapshot"
else
ARGS="$ARGS --initial $INITIAL_SNAPSHOT"
fi
if [ "$CURRENT_SNAPSHOT" == "NONE" ]; then
print_error "Please specify the current snapshot"
else
ARGS="$ARGS --current $CURRENT_SNAPSHOT"
fi
if [[ "$FILTERED_PLUGIN" != "NONE" && "$FILTERED_HOST" == "NONE" ]]; then
print_error "Please filter also by host"
fi
if [ "$FILTERED_HOST" != "NONE" ]; then
ARGS="$ARGS --host $FILTERED_HOST"
fi
if [ "$FILTERED_PLUGIN" != "NONE" ]; then
ARGS="$ARGS --plugin $FILTERED_PLUGIN"
fi
if [ "$PRINT_STATS" != "NONE" ]; then
ARGS="$ARGS --stats"
fi
$EXECUTABLE_NAME compare $ARGS
}
function execute_uninstall_subcommand {
is_executable_installed
echo -e "$YELLOW$BOLD [-] Removing executable"
rm -f $INSTALLATION_PATH/$EXECUTABLE_NAME
if [ -x "$(command -v docker)" ]; then
echo -e "$YELLOW$BOLD [-] Removing docker images"
docker rmi $BUILDER_IMAGE_NAME $LAUNCHER_IMAGE_NAME --force
fi
}
function execute_build_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
install_docker
build_builder_image
print_info "Building release for Linux target"
docker run --rm -v $PWD/$APP_PATH:/app -w /app $BUILDER_IMAGE_NAME:latest cargo build --target x86_64-unknown-linux-gnu --release || { print_error "Linux release building failed" ; exit 1; }
print_info "Building release for macOS target"
docker run --rm -v $PWD/$APP_PATH:/app -w /app $BUILDER_IMAGE_NAME:latest cargo build --target x86_64-apple-darwin --release || { print_error "macOS release building failed" ; exit 1; }
print_info "Building release for Windows target"
docker run --rm -v $PWD/$APP_PATH:/app -w /app $BUILDER_IMAGE_NAME:latest cargo build --target x86_64-pc-windows-msvc --release || { print_error "Windows release building failed" ; exit 1; }
print_info "Moving executables to the launcher folders"
cp $APP_PATH/target/x86_64-unknown-linux-gnu/release/rusthunter $LINUX_BINARIES_PATH
cp $APP_PATH/target/x86_64-apple-darwin/release/rusthunter $MACOS_BINARIES_PATH
cp $APP_PATH/target/x86_64-pc-windows-msvc/release/rusthunter.exe $WINDOWS_BINARIES_PATH
print_info "Installing executable"
cp $APP_PATH/target/x86_64-unknown-linux-gnu/release/rusthunter $INSTALLATION_PATH
print_info "Cleaning up"
rm -rf $APP_PATH/target
}
function execute_update_subcommand {
print_info "Downloading latest updates"
if [ "$(git pull)" == "Already up to date." ];
then
print_warning "No updates available"
else
print_info "Installing new executable"
sudo cp $LINUX_BINARIES_PATH/$EXECUTABLE_NAME $INSTALLATION_PATH
print_info "Update completed"
fi
}
function execute_test_subcommand {
if [ ! "$UID" -eq "0" ]; then
print_error "Superuser privileges required"
fi
is_executable_installed
install_docker
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
-u|--unit)
UNIT_TESTS="True"
shift
shift
;;
-i|--intergration)
INTEGRATION_TESTS="True"
shift
shift
;;
-v|--validation)
VALIDATION_TESTS="True"
shift
shift
;;
*)
ShowHelp
exit 1
;;
esac
done
if [[ "$UNIT_TESTS" == "NONE" && "$INTEGRATION_TESTS" == "NONE" && "$VALIDATION_TESTS" == "NONE" ]]; then
print_error "No tests specified"
fi
if [ "$UNIT_TESTS" == "True" ]; then
build_builder_image
print_info "Unit testing" # OS-independent
docker run --rm -v $PWD/$APP_PATH:/app -w /app $BUILDER_IMAGE_NAME:latest cargo test --lib || { print_error "Unit testing failed" ; exit 1; }
fi
if [ "$INTEGRATION_TESTS" == "True" ]; then
build_builder_image
print_info "Integration testing" # OS-dependent
docker run --rm -v $PWD/$APP_PATH:/app -w /app $BUILDER_IMAGE_NAME:latest cargo test --test integration || { print_error "Integration testing failed" ; exit 1; }
fi
if [ "$VALIDATION_TESTS" == "True" ]; then
print_info "Creating target dockers"
N=4
CONFIG_FILE=$DEFAULT_CONFIG_FILE
HOSTS_FILE="test.hosts"
SNAPSHOT_TAG="validation"
echo "[linux]" > $HOSTS_FILE
for i in $(seq 2 $N);
do
TARGET_NAME="target-$i"
docker run --name $TARGET_NAME -d peco602/ssh-linux-docker:latest
TARGET_IP=$(docker inspect -f "{{ .NetworkSettings.Networks.bridge.IPAddress }}" $TARGET_NAME)
echo "$TARGET_IP ansible_connection=ssh ansible_user=user ansible_ssh_password=Pa\$\$w0rd123! ansible_become_password=Pa\$\$w0rd123!" >> $HOSTS_FILE
done
execute_global_subcommand -h $HOSTS_FILE -c $CONFIG_FILE -t $SNAPSHOT_TAG
print_info "Destroying target dockers"
for i in $(seq 2 $N);
do
TARGET_NAME="target-$i"
docker rm $TARGET_NAME --force
done
rm $HOSTS_FILE
fi
}
###########################################################
# MAIN
ShowBanner
if [[ $# -gt 0 ]]; then
while [[ $# -gt 0 ]]; do
key="${1}"
case ${key} in
install)
shift
execute_install_subcommand $@
exit 0
;;
list)
shift
execute_list_subcommand $@
exit 0
;;
local)
shift
execute_local_subcommand $@
exit 0
;;
hosts)
shift
execute_hosts_subcommand $@
exit 0
;;
global)
shift
execute_global_subcommand $@
exit 0
;;
compare)
shift
execute_compare_subcommand $@
exit 0
;;
uninstall)
shift
execute_uninstall_subcommand $@
exit 0
;;
build)
shift
execute_build_subcommand $@
exit 0
;;
update)
shift
execute_update_subcommand $@
exit 0
;;
test)
shift
execute_test_subcommand $@
exit 0
;;
help)
ShowHelp
exit 0
;;
*)
ShowHelp
exit 0
;;
esac
done
else
ShowHelp
fi