forked from e-m-b-a/emba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemba
executable file
·995 lines (884 loc) · 41.8 KB
/
emba
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/bin/bash -p
# see: https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/ShellScriptSecurity/ShellScriptSecurity.html#//apple_ref/doc/uid/TP40004268-CH8-SW29
# EMBA - EMBEDDED LINUX ANALYZER
#
# Copyright 2020-2023 Siemens AG
# Copyright 2020-2024 Siemens Energy AG
#
# EMBA comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
# EMBA is licensed under GPLv3
#
# Author(s): Michael Messner, Pascal Eckmann
# Contributor(s): Stefan Haboeck
# Description: Main script for load all necessary files and call main function of modules
INVOCATION_PATH="."
MODULE_COUNT=0
import_helper()
{
local HELPERS=()
local HELPER_COUNT=0
local HELPER_FILE=""
mapfile -d '' HELPERS < <(find "${HELP_DIR}" -iname "helpers_emba_*.sh" -print0 2> /dev/null)
for HELPER_FILE in "${HELPERS[@]}" ; do
if ( file "${HELPER_FILE}" | grep -q "shell script" ) && ! [[ "${HELPER_FILE}" =~ \ |\' ]] ; then
# https://github.com/koalaman/shellcheck/wiki/SC1090
# shellcheck source=/dev/null
source "${HELPER_FILE}"
(( HELPER_COUNT+=1 ))
fi
done
print_output "==> ""${GREEN}""Imported ""${HELPER_COUNT}"" helper files""${NC}" "no_log"
}
import_module()
{
local MODULES=()
local MODULES_LOCAL=()
local MODULES_EMBA=()
local MODULE_COUNT=0
local MODULE_FILE=""
# to ensure we are only auto load modules from the modules main directory we set maxdepth
# with this in place we can create sub directories per module. For using/loading stuff from
# these sub directories the modules are responsible!
mapfile -t MODULES_EMBA < <(find "${MOD_DIR}" -maxdepth 1 -name "*.sh" | sort -V 2> /dev/null)
if [[ -d "${MOD_DIR_LOCAL}" ]]; then
mapfile -t MODULES_LOCAL < <(find "${MOD_DIR_LOCAL}" -maxdepth 1 -name "*.sh" 2>/dev/null | sort -V 2> /dev/null)
fi
MODULES=( "${MODULES_EMBA[@]}" "${MODULES_LOCAL[@]}" )
for MODULE_FILE in "${MODULES[@]}" ; do
if ( file "${MODULE_FILE}" | grep -q "shell script" ) && ! [[ "${MODULE_FILE}" =~ \ |\' ]] ; then
# https://github.com/koalaman/shellcheck/wiki/SC1090
# shellcheck source=/dev/null
source "${MODULE_FILE}"
(( MODULE_COUNT+=1 ))
fi
done
print_output "==> ""${GREEN}""Imported ""${MODULE_COUNT}"" module/s""${NC}" "no_log"
}
sort_modules()
{
local SORTED_MODULES=()
local MODULE_FILE=""
for MODULE_FILE in "${MODULES[@]}" ; do
if ( file "${MODULE_FILE}" | grep -q "shell script" ) && ! [[ "${MODULE_FILE}" =~ \ |\' ]] ; then
THREAD_PRIO=0
# https://github.com/koalaman/shellcheck/wiki/SC1090
# shellcheck source=/dev/null
source "${MODULE_FILE}"
if [[ ${THREAD_PRIO} -eq 1 ]] ; then
SORTED_MODULES=( "${MODULE_FILE}" "${SORTED_MODULES[@]}" )
else
SORTED_MODULES=( "${SORTED_MODULES[@]}" "${MODULE_FILE}" )
fi
fi
done
MODULES=( "${SORTED_MODULES[@]}" )
}
# $1: module group letter [P, S, L, F, Q]
# $2: 0=single thread 1=multithread
# $3: HTML=1 - generate html file
run_modules()
{
MODULE_GROUP="${1:-}"
printf -v THREADING_SET '%d\n' "${2}" 2>/dev/null
THREADING_MOD_GROUP="${THREADING_SET}"
local SELECT_PRE_MODULES_COUNT=0
for SELECT_NUM in "${SELECT_MODULES[@]}" ; do
if [[ "${SELECT_NUM}" =~ ^["${MODULE_GROUP,,}","${MODULE_GROUP^^}"]{1} ]]; then
(( SELECT_PRE_MODULES_COUNT+=1 ))
fi
done
if [[ "${#SELECT_MODULES[@]}" -eq 0 ]] || [[ "${SELECT_PRE_MODULES_COUNT}" -eq 0 ]]; then
local MODULES=()
local MODULES_LOCAL=()
local MODULES_EMBA=()
mapfile -t MODULES_EMBA < <(find "${MOD_DIR}" -name "${MODULE_GROUP^^}""*_*.sh" | sort -V 2> /dev/null)
if [[ -d "${MOD_DIR_LOCAL}" ]]; then
mapfile -t MODULES_LOCAL < <(find "${MOD_DIR_LOCAL}" -name "${MODULE_GROUP^^}""*.sh" 2>/dev/null | sort -V 2> /dev/null)
fi
MODULES=( "${MODULES_EMBA[@]}" "${MODULES_LOCAL[@]}" )
export MODULES_EXPORTED+=("${MODULES[@]}")
if [[ ${THREADING_SET} -eq 1 && "${MODULE_GROUP^^}" != "P" ]] ; then
sort_modules
fi
for MODULE_FILE in "${MODULES[@]}" ; do
# check if "${MODULE_NAME}" is in blacklist from config directory and skip it
MODULE_NAME=$(basename -s .sh "${MODULE_FILE}")
if [[ " ${MODULE_BLACKLIST[*]} " =~ ${MODULE_NAME} ]]; then
print_output "[*] $(print_date) - ${MODULE_NAME} not executed - blacklist triggered " "main"
continue
fi
if [[ "${SKIP_PRE_CHECKERS}" == 1 ]] && [[ "${MODULE_GROUP}" == "P" ]]; then
print_output "[*] $(print_date) - ${MODULE_NAME} not executed - skip pre-checkers is set " "main"
continue
fi
local MOD_FIN=0
if ( file "${MODULE_FILE}" | grep -q "shell script" ) && ! [[ "${MODULE_FILE}" =~ \ |\' ]] ; then
if [[ "${MODULE_GROUP^^}" == "P" ]] || [[ "${MODULE_GROUP^^}" == "D" ]]; then
# we are able to enable/disable threading on module basis in the the pre-checker modules with the header:
# export PRE_THREAD_ENA=1/0
# shellcheck source=/dev/null
source "${MODULE_FILE}"
if [[ "${PRE_THREAD_ENA}" -eq 0 ]] ; then
THREADING_SET=0
fi
fi
MODULE_BN=$(basename "${MODULE_FILE}")
MODULE_MAIN=${MODULE_BN%.*}
# module_start_log "${MODULE_MAIN}"
if [[ "${RESTART}" -eq 1 ]]; then
if [[ $(grep -i -c "${MODULE_MAIN} finished" "${LOG_DIR}"/"${MAIN_LOG_FILE}") -gt 0 ]]; then
if [[ "${MODULE_MAIN}" == "P99_"* ]] || [[ "${MODULE_MAIN}" == "L1"* ]]; then
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished but essential - rerun it" "main"
MOD_FIN=0
else
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished ... skipping" "main"
MOD_FIN=1
if [[ ${HTML} -eq 1 ]] ; then
# we need to build the web reporter links for skipping modules
mapfile -t LOG_FILES < <(find "${LOG_DIR}" -maxdepth 1 -type f -iname "${MODULE_MAIN}*.txt" | sort)
for LOG_FILE_ in "${LOG_FILES[@]}"; do
if grep -q "nothing reported" "${LOG_FILE_}"; then
continue
fi
MODULE_NAME_=$(basename -s .txt "${LOG_FILE_}")
MODUL_NAME="$( strip_color_tags "$(grep -a -E -B 1 '[=]{65}' "${LOG_FILE_}" | head -n 1 )" | cut -d" " -f2- )"
HTML_FILE="${MODULE_NAME_}.html"
add_link_to_index "${HTML_FILE}" "${MODUL_NAME}"
done
fi
fi
fi
fi
if [[ "${MOD_FIN}" -eq 0 ]]; then
if [[ ${THREADING_SET} -eq 1 ]]; then
"${MODULE_MAIN}" &
MOD_PID="$!"
store_kill_pids "${MOD_PID}"
WAIT_PIDS+=( "${MOD_PID}" )
max_pids_protection "${MAX_MODS}" "${WAIT_PIDS[@]}"
else
"${MODULE_MAIN}"
fi
fi
reset_module_count
fi
if [[ "${MODULE_GROUP^^}" == "P" ]]; then
THREADING_SET="${THREADING_MOD_GROUP}"
fi
done
else
# in full emulation mode we need to ensure that module s24 is always running
if [[ "${MODULE_GROUP}" == "S" ]] && [[ "${FULL_EMULATION}" -eq 1 ]] && [[ ! "${SELECT_MODULES[*]}" =~ [sS]24 ]]; then
print_output "[*] Automatically enable module S24 for system emulation mode"
SELECT_MODULES+=("S24")
fi
for SELECT_NUM in "${SELECT_MODULES[@]}" ; do
local MOD_FIN=0
if [[ "${SELECT_NUM}" =~ ^["${MODULE_GROUP,,}","${MODULE_GROUP^^}"]{1}[0-9]+ ]]; then
local MODULE=""
MODULE=$(find "${MOD_DIR}" -name "${MODULE_GROUP^^}""${SELECT_NUM:1}""_*.sh" | sort -V 2> /dev/null)
# we need the whole module name including path in our array for later checks on it
export MODULES_EXPORTED+=("${MODULE}")
if ( file "${MODULE}" | grep -q "shell script" ) && ! [[ "${MODULE}" =~ \ |\' ]] ; then
MODULE_BN=$(basename "${MODULE}")
MODULE_MAIN=${MODULE_BN%.*}
# module_start_log "${MODULE_MAIN}"
if [[ "${RESTART}" -eq 1 ]]; then
if [[ $(grep -i -c "${MODULE_MAIN} finished" "${LOG_DIR}"/"${MAIN_LOG_FILE}") -gt 0 ]]; then
if [[ "${MODULE_MAIN}" == "P99_"* ]] || [[ "${MODULE_MAIN}" == "L1"* ]]; then
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished but essential - rerun it" "main"
MOD_FIN=0
else
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished ... skipping" "main"
MOD_FIN=1
if [[ ${HTML} -eq 1 ]] ; then
# we need to build the web reporter links for skipping modules
mapfile -t LOG_FILES < <(find "${LOG_DIR}" -maxdepth 1 -type f -iname "${MODULE_MAIN}*.txt" | sort)
for LOG_FILE_ in "${LOG_FILES[@]}"; do
if grep -q "nothing reported" "${LOG_FILE_}"; then
continue
fi
MODULE_NAME_=$(basename -s .txt "${LOG_FILE_}")
MODUL_NAME="$( strip_color_tags "$(grep -a -E -B 1 '[=]{65}' "${LOG_FILE_}" | head -n 1 )" | cut -d" " -f2- )"
HTML_FILE="${MODULE_NAME_}.html"
add_link_to_index "${HTML_FILE}" "${MODUL_NAME}"
done
fi
fi
fi
fi
if [[ "${MOD_FIN}" -eq 0 ]]; then
if [[ ${THREADING_SET} -eq 1 ]]; then
"${MODULE_MAIN}" &
MOD_PID="$!"
store_kill_pids "${MOD_PID}"
WAIT_PIDS+=( "${MOD_PID}" )
max_pids_protection "${MAX_MODS}" "${WAIT_PIDS[@]}"
else
"${MODULE_MAIN}"
fi
fi
reset_module_count
fi
elif [[ "${SELECT_NUM}" =~ ^["${MODULE_GROUP,,}","${MODULE_GROUP^^}"]{1} ]]; then
local MODULES=()
local MODULES_LOCAL=()
local MODULES_EMBA=()
mapfile -t MODULES_EMBA < <(find "${MOD_DIR}" -name "${MODULE_GROUP^^}""*_*.sh" | sort -V 2> /dev/null)
if [[ -d "${MOD_DIR_LOCAL}" ]]; then
mapfile -t MODULES_LOCAL < <(find "${MOD_DIR_LOCAL}" -name "${MODULE_GROUP^^}""*.sh" 2>/dev/null | sort -V 2> /dev/null)
fi
MODULES=( "${MODULES_EMBA[@]}" "${MODULES_LOCAL[@]}" )
[[ ${THREADING_SET} -eq 1 ]] && sort_modules
for MODULE_FILE in "${MODULES[@]}" ; do
# check if "${MODULE_NAME}" is in blacklist from config directory and skip it
MODULE_NAME=$(basename -s .sh "${MODULE_FILE}")
if [[ " ${MODULE_BLACKLIST[*]} " =~ ${MODULE_NAME} ]]; then
print_output "[*] $(print_date) - ${MODULE_NAME} not executed - blacklist triggered " "main"
continue
fi
local MOD_FIN=0
if ( file "${MODULE_FILE}" | grep -q "shell script" ) && ! [[ "${MODULE_FILE}" =~ \ |\' ]] ; then
if [[ "${MODULE_GROUP^^}" == "P" ]] || [[ "${MODULE_GROUP^^}" == "D" ]]; then
# we are able to enable/disable threading on module basis in the the pre-checker modules with the header:
# export PRE_THREAD_ENA=1/0
# shellcheck source=/dev/null
source "${MODULE_FILE}"
if [[ "${PRE_THREAD_ENA}" -eq 0 ]] ; then
THREADING_SET=0
fi
fi
MODULE_BN=$(basename "${MODULE_FILE}")
MODULE_MAIN=${MODULE_BN%.*}
# module_start_log "${MODULE_MAIN}"
if [[ "${RESTART}" -eq 1 ]]; then
if [[ $(grep -i -c "${MODULE_MAIN} finished" "${LOG_DIR}"/"${MAIN_LOG_FILE}") -gt 0 ]]; then
if [[ "${MODULE_MAIN}" == "P99_"* ]] || [[ "${MODULE_MAIN}" == "L1"* ]]; then
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished but essential - rerun it" "main"
MOD_FIN=0
else
print_output "[*] $(print_date) - Module ${ORANGE}${MODULE_MAIN}${NC} already finished ... skipping" "main"
MOD_FIN=1
if [[ ${HTML} -eq 1 ]] ; then
# we need to build the web reporter links for skipping modules
mapfile -t LOG_FILES < <(find "${LOG_DIR}" -maxdepth 1 -type f -iname "${MODULE_MAIN}*.txt" | sort)
for LOG_FILE_ in "${LOG_FILES[@]}"; do
if grep -q "nothing reported" "${LOG_FILE_}"; then
continue
fi
MODULE_NAME_=$(basename -s .txt "${LOG_FILE_}")
MODUL_NAME="$( strip_color_tags "$(grep -a -E -B 1 '[=]{65}' "${LOG_FILE_}" | head -n 1 )" | cut -d" " -f2- )"
HTML_FILE="${MODULE_NAME_}.html"
add_link_to_index "${HTML_FILE}" "${MODUL_NAME}"
done
fi
fi
fi
fi
if [[ "${MOD_FIN}" -eq 0 ]]; then
if [[ ${THREADING_SET} -eq 1 ]]; then
"${MODULE_MAIN}" &
MOD_PID="$!"
store_kill_pids "${MOD_PID}"
WAIT_PIDS+=( "${MOD_PID}" )
max_pids_protection "${MAX_MODS}" "${WAIT_PIDS[@]}"
else
"${MODULE_MAIN}"
fi
fi
reset_module_count
fi
if [[ "${MODULE_GROUP^^}" == "P" ]]; then
THREADING_SET="${THREADING_MOD_GROUP}"
fi
done
fi
done
fi
}
main() {
set -a
trap cleaner INT
INVOCATION_PATH="$(dirname "${0}")"
export EMBA_PID="$$"
# loads default values for EMBA
export HELP_DIR="${INVOCATION_PATH}""/helpers"
import_helper
set_defaults
print_ln "no_log"
import_module
welcome # Print EMBA welcome message
if [[ $# -eq 0 ]]; then
print_output "\\n""${ORANGE}""In order to be able to use EMBA, you have to specify at least a firmware (-f)." "no_log"
print_output "If you don't set a log directory (-l), then ./logs will be used.""${NC}" "no_log"
print_help
exit 1
fi
export EMBA_COMMAND
EMBA_COMMAND="$(dirname "${0}")""/emba ""$*"
emba_parameter_parsing "$@"
print_ln "no_log"
write_notification "EMBA starting"
# WSL support - currently experimental!
if [[ ${IN_DOCKER} -eq 0 ]]; then
if grep -q -i wsl /proc/version; then
print_bar "no_log"
print_output "[*] $(print_date) - INFO: System running in WSL environment!" "no_log"
print_output "[*] $(print_date) - INFO: WSL is currently experimental." "no_log"
print_output "[*] $(print_date) - INFO: Please report issues to https://github.com/e-m-b-a/emba/issues." "no_log"
print_bar "no_log"
export WSL=1
fi
fi
# print it only once per EMBA run - not again from started container
[[ ${IN_DOCKER} -eq 0 ]] && banner_printer
if [[ ${IN_DOCKER} -eq 1 ]] ; then
# set external path new for docker
export EXT_DIR="/external"
fi
# activate the virtual environment - we should have it in external which fits also the docker environment
# shellcheck source=/dev/null
if [[ -f "${EXT_DIR}/emba_venv/bin/activate" ]]; then
print_output "[*] $(print_date) - Enable python virtual environment ${ORANGE}${EXT_DIR}/emba_venv${NC}" "no_log"
source "${EXT_DIR}/emba_venv/bin/activate"
fi
# EMBA should be started with "sudo -E" to access the proxy settings from the user
export PROXY_SETTINGS=""
PROXY_SETTINGS="$(env | grep "http_proxy=http" | cut -d = -f2 | sort -u | head -1)"
if [[ -n "${PROXY_SETTINGS}" ]]; then
# 2nd try with the original user:
PROXY_SETTINGS="$(sudo -E -u "${SUDO_USER:-${USER}}" env | grep -E "http_proxy=http" | cut -d = -f2 | sort -u | head -1)"
else
if [[ -f "${LOG_DIR}"/orig_user.log ]]; then
if grep -q "PROXY: http" "${LOG_DIR}"/orig_user.log; then
PROXY_SETTINGS="$(grep "PROXY: http" "${LOG_DIR}"/orig_user.log | awk '{print $2}')"
fi
fi
fi
# Check all dependencies of EMBA
dependency_check
if [[ "${ONLY_DEP}" -eq 0 ]]; then
if [[ "${UPDATE}" -eq 1 ]]; then
write_notification "EMBA starts with update"
emba_updater
exit 0
fi
if [[ ${USE_DOCKER} -eq 0 && ${IN_DOCKER} -eq 0 ]]; then
print_bar "no_log"
print_output "[!] $(print_date) - WARNING: EMBA running in developer mode!" "no_log"
write_notification "WARNING: EMBA running in developer mode"
print_bar "no_log"
fi
enable_strict_mode "${STRICT_MODE}" 1
# profile handling
if [[ -n "${PROFILE:-}" ]]; then
if [[ -f "${PROFILE}" ]]; then
print_bar "no_log"
if [[ ${IN_DOCKER} -ne 1 ]] ; then
print_output "[*] $(print_date) - Loading EMBA scan profile with the following settings:" "no_log"
else
print_output "[*] $(print_date) - Loading EMBA scan profile." "no_log"
fi
# all profile output and settings are done by the profile file located in ./scan-profiles/
# shellcheck source=/dev/null
source "${PROFILE}"
print_output "[*] $(print_date) - Profile ${PROFILE} loaded." "no_log"
print_bar "no_log"
else
print_output "[!] $(print_date) - Profile ${PROFILE} not found." "no_log"
exit 1
fi
fi
if [[ "${ONLY_DEP}" -eq 0 ]]; then
# check provided paths for validity
check_path_valid "${FIRMWARE_PATH}"
check_path_valid "${KERNEL_CONFIG}"
check_path_valid "${LOG_DIR}"
fi
# restart file gets generated during startup if old log dir is found:
if [[ ${USE_DOCKER} -eq 1 && ${IN_DOCKER} -eq 1 ]] || [[ "${USE_DOCKER}" -eq 0 ]]; then
if [[ -f "${TMP_DIR}"/restart ]]; then
print_output "[!] $(print_date) - Found restart file and backup_vars file ... trying to restart EMBA scan" "no_log"
export RESTART=1
rm "${TMP_DIR}"/restart
# shellcheck source=/dev/null
source "${LOG_DIR}""/backup_vars.log"
fi
fi
# check if LOG_DIR exists and prompt to terminal to delete its content (Y/n)
[[ ${IN_DOCKER} -eq 0 ]] && log_folder
# create log directory, if not exists and needed subdirectories
# do not create a log dir for dep check
export MAIN_LOG="${LOG_DIR}""/""${MAIN_LOG_FILE}"
[[ "${ONLY_DEP}" -eq 0 ]] && create_log_dir
# kernel downloader runs on the host and waits for an identified kernel version. Afterwards
# it tries to download the kernel sources for further analysis
if [[ ${IN_DOCKER} -eq 0 ]]; then
kernel_downloader &
K_DOWN_PID="$!"
store_kill_pids "${K_DOWN_PID}"
print_output "[*] $(print_date) - Started kernel downloader thread with PID ${ORANGE}${K_DOWN_PID}${NC}" "no_log"
fi
if [[ ${IN_DOCKER} -eq 0 ]]; then
echo "${LOG_DIR}" > "${TMP_DIR}"/orig_logdir
fi
if [[ "${IN_DOCKER}" -eq 0 ]]; then
print_notification &
NOTIFICATION_PID="$!"
store_kill_pids "${NOTIFICATION_PID}"
disown "${NOTIFICATION_PID}" 2> /dev/null || true
print_output "[*] $(print_date) - Original user: ${ORANGE}${SUDO_USER:-${USER}}${NC}" "no_log"
print_output "[*] $(print_date) - Notification process started with PID ${ORANGE}${NOTIFICATION_PID}${NC}" "no_log"
PROXY="$(sudo -E -u "${SUDO_USER:-${USER}}" env | grep -E "http(s)_proxy" | cut -d = -f2 || true)"
echo "${SUDO_USER:-${USER}}" > "${LOG_DIR}"/orig_user.log
{
echo "UID: $(id -u "${SUDO_USER:-${USER}}")"
echo "GID: $(id -g "${SUDO_USER:-${USER}}")"
echo "PROXY: ${PROXY}"
} >> "${LOG_DIR}"/orig_user.log
fi
if [[ "${IN_DOCKER}" -eq 0 ]]; then
print_running_modules &
RUN_MOD_PID="$!"
store_kill_pids "${RUN_MOD_PID}"
echo "${RUN_MOD_PID}" > "${LOG_DIR}"/print_running_modules.pid
disown "${RUN_MOD_PID}" 2> /dev/null || true
fi
# Print additional information about the firmware (-Y, -X, -Z, -N)
print_firmware_info "${FW_VENDOR}" "${FW_VERSION}" "${FW_DEVICE}" "${FW_NOTES}"
if [[ "${KERNEL}" -ne 1 ]] && [[ "${CONTAINER_EXTRACT}" -ne 1 ]] && [[ "${ONLY_DEP}" -eq 0 ]]; then
check_init_size
fi
# Now we have the firmware and log path, lets set some additional paths
FIRMWARE_PATH="$(abs_path "${FIRMWARE_PATH}")"
# Check firmware type (file/directory)
# copy the firmware outside of the docker and not a second time within the docker
if [[ -d "${FIRMWARE_PATH}" ]] ; then
PRE_CHECK=1
print_output "[*] $(print_date) - Firmware directory detected." "no_log"
print_output "[*] $(print_date) - EMBA starts with testing the environment." "no_log"
if [[ ${IN_DOCKER} -eq 0 ]] ; then
# in docker environment the firmware is already available
print_output " The provided firmware will be copied to ${ORANGE}""${FIRMWARE_PATH_CP}""/""$(basename "${FIRMWARE_PATH}")""${NC}" "no_log"
cp -R "${FIRMWARE_PATH}" "${FIRMWARE_PATH_CP}""/""$(basename "${FIRMWARE_PATH}")"
FIRMWARE_PATH="${FIRMWARE_PATH_CP}""/""$(basename "${FIRMWARE_PATH}")"
export OUTPUT_DIR="${FIRMWARE_PATH_CP}"
else
# need to set it as fallback:
export OUTPUT_DIR="${FIRMWARE_PATH}"
fi
elif [[ "${CONTAINER_EXTRACT}" -eq 1 ]]; then
PRE_CHECK=1
print_output "[*] $(print_date) - Firmware analysis of docker image starting." "no_log"
print_output " EMBA starts with extracting the docker image ${ORANGE}${CONTAINER_ID}${NC}." "no_log"
export FIRMWARE_PATH="${LOG_DIR}"/firmware/firmware_docker_extracted.tar
export OUTPUT_DIR="${FIRMWARE_PATH}"
export FIRMWARE=1
elif [[ -f "${FIRMWARE_PATH}" ]] && [[ -z "${FIRMWARE_PATH1}" ]]; then
PRE_CHECK=1
if [[ -n "${KERNEL_CONFIG}" && "${KERNEL}" -eq 1 ]]; then
print_output "[*] $(print_date) - Kernel configuration file detected." "no_log"
else
print_output "[*] $(print_date) - Firmware binary detected." "no_log"
fi
print_output " EMBA starts with the pre-testing phase." "no_log"
export OUTPUT_DIR="${FIRMWARE_PATH}"
elif [[ -f "${FIRMWARE_PATH}" ]] && [[ -f "${FIRMWARE_PATH1}" ]]; then
DIFF_MODE=1
print_output "[*] $(print_date) - Multiple firmware binarie detected." "no_log"
print_output " EMBA starts in firmware diff mode ." "no_log"
else
print_output "[!] $(print_date) - Invalid firmware file" "no_log"
print_help
exit 1
fi
# calculate the maximum modules are running in parallel
if [[ ${THREADED} -eq 1 ]] && [[ "${MAX_MODS}" -eq 0 ]]; then
# the maximum modules in parallel
# rule of thumb - per core half a module, minimum 2 modules
MAX_MODS="$(( "$(grep -c ^processor /proc/cpuinfo)" /2 +1))"
# if we have only one core we run two modules in parallel
if [[ "${MAX_MODS}" -lt 2 ]]; then
MAX_MODS=2
fi
export MAX_MODS
fi
# calculate the maximum threads per module
if [[ ${THREADED} -eq 1 ]] && [[ "${MAX_MOD_THREADS}" -eq 0 ]]; then
# the maximum threads per modules - if this value does not match adjust it via
# local MAX_MOD_THREADS=123 in module area
export MAX_MOD_THREADS="$(( 2* "$(grep -c ^processor /proc/cpuinfo)" ))"
fi
# setup non threaded mode:
if [[ ${THREADED} -eq 0 ]]; then
export MAX_MODS=1
export MAX_MOD_THREADS=1
fi
print_output " EMBA is running with ${ORANGE}${MAX_MODS}${NC} modules in parallel and ${ORANGE}${MAX_MOD_THREADS}${NC} threads per module." "no_log"
# Change log output to color for web report and prepare report
if [[ ${HTML} -eq 1 ]] ; then
if [[ ${FORMAT_LOG} -eq 0 ]] ; then
FORMAT_LOG=1
print_output "[*] $(print_date) - Activate colored log for webreport" "no_log"
fi
print_output "[*] $(print_date) - Prepare webreport" "no_log"
prepare_report
fi
if [[ ${LOG_GREP} -eq 1 ]] ; then
# Create grep-able log file
create_grep_log
write_grep_log "sudo ""${EMBA_COMMAND}" "COMMAND"
fi
if [[ "${KERNEL}" -ne 1 ]] && [[ ${FIRMWARE} -eq 1 ]]; then
# Exclude paths from testing and set EXCL_FIND for find command (prune paths dynamicially)
set_exclude
fi
#######################################################################################
# Kernel configuration check
#######################################################################################
if [[ "${KERNEL}" -eq 1 ]]; then
if [[ ${IN_DOCKER} -eq 1 ]] && [[ -f "${LOG_DIR}"/kernel_config ]]; then
export KERNEL_CONFIG="${LOG_DIR}"/kernel_config
fi
if ! [[ -f "${KERNEL_CONFIG}" ]] ; then
print_output "[-] $(print_date) - Invalid kernel configuration file: ${ORANGE}${KERNEL_CONFIG}${NC}" "no_log"
exit 1
else
if [[ ${IN_DOCKER} -eq 0 ]] ; then
# we copy the kernel config file from outside the container into our log directory
# further modules are using LOG_DIR/kernel_config for accessing the kernel config
if [[ -d "${LOG_DIR}" ]] ; then
cp "${KERNEL_CONFIG}" "${LOG_DIR}"/kernel_config
else
print_output "[!] $(print_date) - Missing log directory" "no_log"
exit 1
fi
fi
fi
fi
# disk space monitor not fully working -> removed for now
# Todo: check it and fix it
# disk_space_monitor "${EMBA_PID}" &
# local TMP_PID="$!"
# store_kill_pids "${TMP_PID}"
# disown "${TMP_PID}" 2> /dev/null || true
# if $CONTAINER_EXTRACT is set we extract the docker container with id $CONTAINER_ID outside of the
# EMBA container into log directory
# we do this outside of the EMBA container - otherwise we will not reach the docker environment
if [[ "${CONTAINER_EXTRACT}" -eq 1 && "${IN_DOCKER}" -eq 0 ]] ; then
docker_container_extractor "${CONTAINER_ID}"
fi
fi
#######################################################################################
# Docker
#######################################################################################
if [[ ${USE_DOCKER} -eq 1 ]] ; then
if ! [[ ${EUID} -eq 0 ]] ; then
if ! groups | grep -qw docker; then
print_output "[!] $(print_date) - Using EMBA with docker-compose requires root permissions" "no_log"
print_output "$(indent "Run EMBA with root permissions or add your user to docker group")" "no_log"
print_output "$(indent "e.g., sudo usermod -aG docker [non-root user]")" "no_log"
exit 1
fi
fi
OPTIND=1
ARGUMENTS=()
while getopts a:A:BcC:d:De:Ef:Fghijk:l:m:N:o:p:P:QrsStT:UX:yY:WxzZ: OPT ; do
case ${OPT} in
D|f|i|l|o)
;;
*)
if [[ -v OPTARG[@] ]] ; then
ARGUMENTS=( "${ARGUMENTS[@]}" "-${OPT}" "${OPTARG[@]}" )
else
ARGUMENTS=( "${ARGUMENTS[@]}" "-${OPT}" )
fi
;;
esac
done
if [[ "${IN_DOCKER}" -eq 0 ]] && [[ "${SILENT}" -eq 1 ]]; then
initial_status_bar &
# Trap the window resize signal (handle window resize events).
trap 'initial_status_bar' WINCH
fi
print_output "[*] $(print_date) - EMBA sets up the docker environment.\\n" "no_log"
# try it 10 times before exit
local CNT_DOCK=0
for CNT_DOCK in {1..10}; do
if ! docker images | grep -qE "embeddedanalyzer/emba[[:space:]]"; then
sleep 1
else
break
fi
if [[ "${CNT_DOCK}" -ge 10 ]]; then
print_output "[-] $(print_date) - EMBA docker environment not ready!" "no_log"
exit 1
fi
done
print_output "[*] $(print_date) - EMBA initializes docker container.\\n" "no_log"
if [[ "${ONLY_DEP}" -eq 0 ]]; then
# store some details that we do not have in the docker container:
echo "${FIRMWARE_PATH}" >> "${TMP_DIR}"/fw_name.log
if [[ "${DIFF_MODE}" -gt 0 ]]; then
echo "${FIRMWARE_PATH1}" >> "${TMP_DIR}"/fw_name.log
fi
echo "${LOG_DIR}" >> "${TMP_DIR}"/emba_log_dir.log
echo "${EMBA_COMMAND}" >> "${TMP_DIR}"/emba_command.log
fi
write_notification "EMBA starting docker container"
[[ "${STRICT_MODE}" -eq 1 ]] && set +e
disable_strict_mode "${STRICT_MODE}" 0
if [[ "${ONLY_DEP}" -gt 0 ]]; then
# check dependencies mode:
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="/tmp" "${DOCKER_COMPOSE[@]}" run --rm emba -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
D_RETURN=$?
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="/tmp" "${DOCKER_COMPOSE[@]}" run --rm emba_quest -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
if [[ $? -ne ${D_RETURN} ]]; then
D_RETURN=1
fi
elif [[ "${DIFF_MODE}" -gt 0 ]]; then
FIRMWARE_PATH1="$(abs_path "${FIRMWARE_PATH1}")"
# firmware diff mode - needs two firmware files:
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" "${DOCKER_COMPOSE[@]}" run -v "${FIRMWARE_PATH1}":/firmware2 --rm emba -c './emba -l /logs -f /firmware -o /firmware2 -i "$@"' _ "${ARGUMENTS[@]}"
D_RETURN=$?
else
print_ln "no_log"
welcome
# default EMBA analysis mode:
local QUEST_CONTAINER_=""
QUEST_CONTAINER_="$(EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" "${DOCKER_COMPOSE[@]}" run --detach --rm emba_quest -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}")"
export QUEST_CONTAINER="${QUEST_CONTAINER_}"
print_output "[+] $(print_date) - Quest container ${QUEST_CONTAINER_} started and detached.\\n" "no_log"
# EMBA startup:
if [[ "${SILENT}" -eq 0 ]]; then
# EMBA container starts in the foreground and we can see all the logs
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" "${DOCKER_COMPOSE[@]}" run --rm emba -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
D_RETURN=$?
else
# EMBA container starts in the background
print_output "[*] $(print_date) - EMBA main container starting and detaching.\\n" "no_log"
local MAIN_CONTAINER_=""
MAIN_CONTAINER_="$(EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" "${DOCKER_COMPOSE[@]}" run --detach --rm emba -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}")"
export MAIN_CONTAINER="${MAIN_CONTAINER_}"
print_output "[+] $(print_date) - EMBA main container ${MAIN_CONTAINER_} started and detached." "no_log"
print_ln "no_log"
print_output "[*] $(print_date) - EMBA main container ${MAIN_CONTAINER_} monitoring through ${ORANGE}docker logs ${MAIN_CONTAINER_} -f${NC}." "no_log"
if [[ "$( docker container inspect -f '{{.State.Status}}' "${QUEST_CONTAINER_}" )" == "running" ]]; then
print_output "[*] $(print_date) - EMBA quest container ${QUEST_CONTAINER_} monitoring through ${ORANGE}docker logs ${QUEST_CONTAINER_} -f${NC}." "no_log"
fi
while [[ "$( docker container inspect -f '{{.State.Status}}' "${MAIN_CONTAINER_}" )" == "running" ]]; do
sleep 1
done
remove_status_bar
# reset
printf "\x1Bc"
if [[ -f "${LOG_DIR}"/f50_base_aggregator.txt ]]; then
cat "${LOG_DIR}"/f50_base_aggregator.txt
fi
D_RETURN=0
fi
fi
enable_strict_mode "${STRICT_MODE}" 0
if [[ "${D_RETURN}" -eq 0 ]] ; then
if [[ "${ONLY_DEP}" -eq 0 ]] ; then
print_output "[*] $(print_date) - EMBA finished analysis in default mode (docker container).\\n" "no_log"
write_notification "EMBA finished analysis in default mode"
print_output "[*] $(print_date) - Firmware tested: ${ORANGE}${FIRMWARE_PATH}${NC}" "no_log"
print_output "[*] $(print_date) - Log directory: ${ORANGE}${LOG_DIR}${NC}" "no_log"
if [[ -v HTML_PATH ]] && [[ -f "${HTML_PATH}"/index.html ]]; then
print_output "[*] $(print_date) - Access the web-report with ${ORANGE}firefox $(abs_path "${HTML_PATH}/index.html")${NC}" "main"
fi
cleaner 0
print_output "[!] $(print_date) - Test ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
else
# we do not need the log dir from dependency checker
[[ -d "${LOG_DIR}" ]] && rm -rf "${LOG_DIR}"
print_output "[!] $(print_date) - Test ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
exit 0
else
print_output "[-] $(print_date) - Test ended on ""$(print_date)"" and EMBA failed in docker mode!" "main"
cleaner 0
write_notification "EMBA failed analysis in default mode"
exit 1
fi
fi
#######################################################################################
# Quests (Q-modules)
#######################################################################################
if [[ -v CONTAINER_NUMBER ]] && [[ "${DIFF_MODE}" -ne 1 ]]; then
if [[ "${CONTAINER_NUMBER}" -eq 2 ]] ; then
while ! grep -q "Pre-checking phase started" "${LOG_DIR}"/"${MAIN_LOG_FILE}"; do
sleep 1
done
run_modules "Q" "${THREADED}" "0"
[[ "${THREADED}" -eq 1 ]] && wait_for_pid "${WAIT_PIDS[@]}"
print_output "[*] $(print_date) - Quest container finished" "main"
exit 0
elif [[ "${USE_DOCKER}" -eq 0 && "${IN_DOCKER}" -eq 0 ]] ; then
# currently we do not support the Quest containers in dev mode
while ! grep -q "Pre-checking phase started" "${LOG_DIR}"/"${MAIN_LOG_FILE}"; do
sleep 1
done
# dev-mode
run_modules "Q" "${THREADED}" "0" &
export Q_MOD_PID="$!"
elif [[ "${CONTAINER_NUMBER}" -ne 1 ]]; then
docker logs "${QUEST_CONTAINER}" --follow | grep "ok" && print_output "[*] Quest container ${QUEST_CONTAINER_} failed the dependancy check" "main"
fi
fi
#######################################################################################
# Pre-Check (P-modules)
#######################################################################################
if [[ "${PRE_CHECK}" -eq 1 ]] && [[ "${DIFF_MODE}" -ne 1 ]]; then
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Pre-checking phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware binary path: ""${FIRMWARE_PATH}")" "main"
else
print_output "[!] $(print_date) - Pre-checking phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware binary path: ""${FIRMWARE_PATH}")" "no_log"
fi
write_notification "Pre-checking phase started"
# 'main' functions of imported modules
# in the pre-check phase we execute all modules with P[Number]_Name.sh
run_modules "P" "${THREADED}" "0"
# if we running threaded we ware going to wait for the slow guys here
[[ ${THREADED} -eq 1 ]] && wait_for_pid "${WAIT_PIDS[@]}"
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Pre-checking phase ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
else
print_output "[!] $(print_date) - Pre-checking phase ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
write_notification "Pre-checking phase finished"
# useful prints for debugging:
# print_output "[!] Firmware value: ${FIRMWARE}"
# print_output "[!] Firmware path: ${FIRMWARE}_PATH"
# print_output "[!] Output dir: $OUTPUT_DIR"
# print_output "[!] LINUX_PATH_COUNTER: $LINUX_PATH_COUNTER"
# print_output "[!] LINUX_PATH_ARRAY: ${#ROOT_PATH[@]}"
fi
#######################################################################################
# Diff mode (D-modules)
#######################################################################################
if [[ "${DIFF_MODE}" -eq 1 ]] ; then
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Diff mode started on ""$(print_date)" "main"
print_output "$(indent "${NC}""1st Firmware binary path: ""${FIRMWARE_PATH}")" "main"
print_output "$(indent "${NC}""2nd Firmware binary path: ""${FIRMWARE_PATH1}")" "main"
else
print_output "[!] $(print_date) - Diff mode started on ""$(print_date)"
print_output "$(indent "${NC}""1st Firmware binary path: ""${FIRMWARE_PATH}")" "no_log"
print_output "$(indent "${NC}""2nd Firmware binary path: ""${FIRMWARE_PATH1}")" "no_log"
fi
write_notification "Diff mode started"
run_modules "D" "${THREADED}" "0"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Diff mode ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
else
print_output "[!] $(print_date) - Diff mode ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
write_notification "Diff mode finished"
TESTING_DONE=1
fi
#######################################################################################
# Firmware-Check (S modules)
#######################################################################################
WAIT_PIDS=()
if [[ ${FIRMWARE} -eq 1 ]] && [[ "${DIFF_MODE}" -ne 1 ]]; then
print_output "\n=================================================================\n" "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Testing phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware path: ""${FIRMWARE_PATH}")" "main"
else
print_output "[!] $(print_date) - Testing phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware path: ""${FIRMWARE_PATH}")" "no_log"
fi
write_notification "Testing phase finished"
write_grep_log "$(print_date)" "TIMESTAMP"
run_modules "S" "${THREADED}" "${HTML}"
[[ ${THREADED} -eq 1 ]] && wait_for_pid "${WAIT_PIDS[@]}"
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Testing phase ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
else
print_output "[!] $(print_date) - Testing phase ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
write_notification "Testing phase ended"
TESTING_DONE=1
fi
#######################################################################################
# Live Emulation - Check (L-modules)
#######################################################################################
if [[ "${FULL_EMULATION}" -eq 1 ]] && [[ "${DIFF_MODE}" -ne 1 ]]; then
print_output "\n=================================================================\n" "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - System emulation phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware path: ""${FIRMWARE_PATH}")" "main"
else
print_output "[!] $(print_date) - System emulation phase started on ""$(print_date)""\\n""$(indent "${NC}""Firmware path: ""${FIRMWARE_PATH}")" "no_log"
fi
write_notification "System emulation phase started"
write_grep_log "$(print_date)" "TIMESTAMP"
# these modules are not threaded!
run_modules "L" "0" "${HTML}"
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - System emulation phase ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
else
print_output "[!] $(print_date) - System emulation ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
write_notification "System emulation phase ended"
fi
#######################################################################################
# Reporting (F-modules)
#######################################################################################
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Reporting phase started on ""$(print_date)""\\n" "main"
else
print_output "[!] $(print_date) - Reporting phase started on ""$(print_date)""\\n" "no_log"
fi
write_notification "Reporting phase started"
run_modules "F" "0" "${HTML}"
[[ ${DISABLE_STATUS_BAR} -eq 0 ]] && remove_status_bar
write_notification "Reporting phase ended"
if [[ "${USE_DOCKER}" -eq 0 && "${THREADED}" -eq 1 && "${IN_DOCKER}" -eq 0 ]] && [[ -n "${Q_MOD_PID}" ]]; then
wait_for_pid "${Q_MOD_PID}"
fi
if [[ "${TESTING_DONE}" -eq 1 ]]; then
if [[ "${FINAL_FW_RM}" -eq 1 && -d "${LOG_DIR}"/firmware ]]; then
print_output "[*] $(print_date) - Removing temp firmware directory\\n" "no_log"
rm -r "${LOG_DIR}"/firmware 2>/dev/null
fi
if [[ "${FINAL_FW_RM}" -eq 1 && -d "${LOG_DIR}"/p61_unblob_eval/unblob_extracted ]]; then
print_output "[*] $(print_date) - Removing unblob firmware directory\\n" "no_log"
rm -r "${LOG_DIR}"/p61_unblob_eval/unblob_extracted 2>/dev/null
fi
print_ln "no_log"
if [[ -d "${LOG_DIR}" ]]; then
print_output "[!] $(print_date) - Test ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "main"
write_notification "EMBA finished analysis"
rm -r "${TMP_DIR}" 2>/dev/null || true
else
print_output "[!] $(print_date) - Test ended on ""$(print_date)"" and took about ""$(show_runtime)"" \\n" "no_log"
fi
write_grep_log "$(print_date)" "TIMESTAMP"
write_grep_log "$(date -d@"${SECONDS}" -u +%d:%H:%M:%S)" "DURATION"
else
print_output "[!] $(print_date) - No extracted firmware found" "no_log"
print_output "$(indent "Try using binwalk or something else to extract the firmware")"
exit 1
fi
[[ "${HTML}" -eq 1 ]] && update_index
if [[ -f "${HTML_PATH}"/index.html ]] && [[ "${IN_DOCKER}" -eq 0 ]]; then
print_output "[*] $(print_date) - Web report created HTML report in ${ORANGE}${LOG_DIR}/html-report${NC}\\n" "main"
print_output "[*] $(print_date) - Open the web-report with${ORANGE} firefox $(abs_path "${HTML_PATH}/index.html")${NC}\\n" "main"
fi
# deactivate the python virtual env
[[ -v VIRTUAL_ENV ]] && deactivate
# we need to change the permissions of the LOG_DIR to the orig. user from the host
[[ "${IN_DOCKER}" -eq 1 ]] && restore_permissions
cleaner 0
exit 0
}
main "$@"