-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLEF.sh
1549 lines (1282 loc) · 45.7 KB
/
CLEF.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
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
996
997
998
999
1000
#!/usr/bin/env bash
###################
# This script collects maximum evidence for forensics (investigation) on Linux.
# Copyright (C) 2022 Maurice Lambert
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
#
# This script collect evidence on a Linux system (Debian based, RedHat based and others).
# This script work on minimal systems like containers (tested on docker).
# This script work offline, in chroot or other method to isolate your Linux.
#
SHELL=${_} # Get the shell path used to run this script
DEBUG=1 # Active breakpoint
COLOR=1 # Active color
LOG_LEVEL=10 # Mode debug
FILENAME="${0##*/}" # "$(basename \"${0}\")"
DATE="$(date +'%Y%m%d_%H%M%S')" # Date in filename format
DIRNAME="${FILENAME%.*}_${DATE}" # Report directory name
LAUNCH_PWD=$(pwd) # Get the current directory
LOG_FILE="${LAUNCH_PWD}/${DIRNAME}_LOG.csv" # Log filename (logs are in CSV format)
COMMAND_FILE="file" # Command file does not exists on minimal OS
TOTAL_FILE=0
ONE_POURCENT=0
_ADD=1
POURCENT=0
STDOUT="/dev/stdout"
VERSION="0.0.2"
AUTHOR='Maurice LAMBERT'
DESCRIPTION='This script collects maximum evidence for forensic investigations.'
COPYRIGHT='
CLEF (Collect Linux Evidence for Forensics) Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
'
declare -A show_info_actions=(
[OK]="\x1b[32m[+]"
[NOK]="\x1b[33m[-]"
[ERROR]="\x1b[31m[!]"
[INFO]="\x1b[34m[*]"
[TODO]="\x1b[35m[#]"
[ASK]="\x1b[36m[?]"
)
declare -A LOG_LEVELS=(
[10]="DEBUG (10)"
[20]="INFO (20)"
[30]="WARNING (30)"
[40]="ERROR (40)"
[50]="CRITICAL (50)"
)
set -e # Stop process on errors
function to_CSV() {
#
# @desc :: This function prints a CSV syntax from arguments
# @params :: string[] $@ - each string of array are columns
#
local string=""
while [[ $# > 0 ]]; do
string+=",\"${1//\"/\'}\""
shift
done
echo "${string:1}" # first character is ","
return 0
}
function logging() {
#
# @desc :: This function prints colored informations
# @param :: string $1 - [REQUIRED] Log message
# @param :: string $2 - [REQUIRED] Log level
# @param :: string $3 - Localization (caller: "<line> <function> <filename>")
#
local log_message="${1}"
local level="${2}"
local localization="${3}"
if [[ $level -lt $LOG_LEVEL ]]; then
return 0
else
level="${LOG_LEVELS[${level}]}"
fi
if [[ -z "${localization}" ]]; then
localization=$(caller)
fi
to_CSV "$(date +'%Y-%m-%d %T')" "${level}" "${FILENAME}" "${$}" "${SHELL}" "${localization}" "${log_message}" >> "${LOG_FILE}"
return 0
}
function log_debug() {
#
# @desc :: This function logs debug messages
# @param :: string $1 - [REQUIRED] Log message
#
logging "${1}" 10 "$(caller)"
return 0
}
function log_info() {
#
# @desc :: This function logs debug messages
# @param :: string $1 - [REQUIRED] Log message
#
logging "${1}" 20 "$(caller)"
return 0
}
function log_warning() {
#
# @desc :: This function logs debug messages
# @param :: string $1 - [REQUIRED] Log message
#
logging "${1}" 30 "$(caller)"
return 0
}
function log_error() {
#
# @desc :: This function logs debug messages
# @param :: string $1 - [REQUIRED] Log message
#
logging "${1}" 40 "$(caller)"
return 0
}
function log_critical() {
#
# @desc :: This function logs debug messages
# @param :: string $1 - [REQUIRED] Log message
#
logging "${1}" 50 "$(caller)"
return 0
}
# Init CSV logging
to_CSV "Date Time" "Log Level" "Filename" "PID" "Shell" "Localization" "Log Message" > "${LOG_FILE}"
log_debug "Logger is configured"
function show_info() {
#
# @desc :: This function prints colored informations
# @param :: string $1 - [REQUIRED] Information to print
# @param :: string $2 - Default: OK, {OK,NOK,ERROR,INFO,TODO,ASK} -> Type of information
# @param :: int $3 - The percentage of items performed from the current task
# @param :: string $4 - Start of the print
# @param :: string $5 - End of the print
# @param :: string $6 - Default: 0, {0,1} -> Pourcent on the same line
#
local string="${1}"
local state="${2}"
local pourcent="${3}"
local start="\x1b[K${4}"
local end="${5}"
if [[ -z "${state}" ]]; then
state=${show_info_actions["OK"]}
else
state=${show_info_actions["${state}"]}
fi
if [[ -z "${end}" ]]; then
end="\n"
fi
if [[ -n "${pourcent}" ]]; then
progressbar=" |"
for i in {1..20}
do
if [[ ${pourcent} -ge $((i * 5)) ]]
then
progressbar+="\xe2\x96\x88"
else
progressbar+=" "
fi
done
progressbar+="|"
if [[ $COLOR -eq 0 ]]; then
pourcent_state=${show_info_actions["INFO"]:8}
color_end=""
else
pourcent_state=${show_info_actions["INFO"]}
color_end="\x1b[0m"
fi
if [[ "${6}" -eq 0 ]]; then
end="${end}${pourcent_state} ${pourcent}%${progressbar}${color_end}\r"
else
end=" ${pourcent_state:0:-3} ${pourcent}%${progressbar}${color_end}${end}"
fi
fi
if [[ $COLOR -eq 0 ]]; then
echo -en "${start}${state:8} ${string}${end}\x1b[F" > "${STDOUT}"
else
echo -en "${start}${state} ${string}\x1b[0m${end}\x1b[F" > "${STDOUT}"
fi
}
function BREAKPOINT() {
#
# @desc :: This function implements a breakpoint for debugging bash script
# @params :: string[] $@ - [REQUIRED] The name of the breakpoint
#
if [[ $DEBUG -eq 0 ]]; then
return 1
fi
local BREAKPOINT_NAME="${@}"
show_info "Enter breakpoint $BREAKPOINT_NAME" "INFO" $POURCENT
set +e # custom exit code
/bin/bash
local BREAKPOINT_EXIT_CODE=$?
set -e
if [[ $BREAKPOINT_EXIT_CODE -eq 0 ]]; then
show_info "Continue after breakpoint $BREAKPOINT_NAME" "INFO" $POURCENT
return 0
else
show_info "Terminate after breakpoint $BREAKPOINT_NAME" "ERROR" $POURCENT
trap - EXIT
exit $BREAKPOINT_EXIT_CODE
fi
}
function get_commands() {
#
# @desc :: This function saves all commands (and aliases)
#
show_info "Processing commands and aliases..." "INFO" $POURCENT
local dir="commands"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
log_debug "Get commands..."
set +e # Invalid path raise an error
commands=`echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 -executable \( -type f -o -type l \) -printf '%P\n' 2>error.txt`
set -e
log_debug "Get aliases..."
local aliases=`alias | cut -d '=' -f 1`
local output=$(echo "${commands[@]}"$'\n'"${aliases}" | sort -u)
log_debug "Save commands and aliases..."
echo "${output}" > commands_and_aliases.txt
echo "${commands[@]}" > commands.txt
echo "${aliases}" > aliases_name.txt
echo "$(alias)" > aliases.txt
log_debug "Set not installed commands with /bin/true..."
if [[ ! "${commands[@]}" =~ $'\nfile\n' ]]; then
log_warning "'File' command not found ... Some suspicious files will not be detected and the file type will not be present in the file report."
show_info "'File' command not found ... Some suspicious files will not be detected and the file type will not be present in the file report." "NOK"
COMMAND_FILE=/bin/true
fi
log_info "Commands and aliases are successfully saved."
cd ..
show_info "COLLECTED: commands and aliases" "OK" $POURCENT
return 0
}
function get_files() {
#
# @desc :: This function saves files (names, type, hash, metadata...)
#
show_info "Processing files..." "INFO" $POURCENT
local dir="files"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
if [[ "${commands[@]}" =~ $'\nlsof\n' ]]; then
log_debug "Use lsof command..."
lsof +L > "lsof_L.txt" 2>>errors.txt
fi
log_debug "Init files.csv file"
to_CSV path filename permissions user group size type birthdate accessdate modificationdate statusdate hash type > "files.csv"
log_info "Start the file processing, this step take a long time..."
shopt -s globstar
local counter=1
local counter_total=1
for filename in /**; do
# path filename permissions user group size type birthdate accessdate modificationdate statusdate hash type
if [[ -f "${filename}" && ! -h "${filename}" && "/proc/" != "${filename:0:6}" && "/sys/" != "${filename:0:5}" ]]; then
local _hash=$(md5sum "${filename}" | grep -oE "^[0-9a-f]+")
to_CSV "${filename}" "$(stat -c '%N %A %U %G %s %F %w %x %y %z' "${filename}")" "${_hash}" "$($COMMAND_FILE ${filename} 2>/dev/null)" >> "files.csv"
((counter++))
((counter_total++))
if [[ ${counter} -ge ${ONE_POURCENT} ]]; then
log_debug "Add one % of collection..."
local counter=1 # a=0 && ((a++)) -> exit code == 1
((POURCENT++))
show_info "${counter_total} / ${TOTAL_FILE} files are processed" "OK" "${POURCENT}" "" "\r" "1"
fi
elif [[ -e "${filename}" ]]; then
to_CSV "${filename}" "$(stat -c '%N %A %U %G %s %F %w %x %y %z' "${filename}")" "" "$($COMMAND_FILE ${filename} 2>/dev/null)" >> "files.csv"
fi
done
shopt -u globstar
log_info "Files are processed."
log_info "Files are successfully saved."
cd ..
show_info "COLLECTED: files" "OK" $POURCENT
return 0
}
function get_regex_match() {
#
# @desc :: This function saves regex matchs in all files
#
show_info "Processing regex match..." "INFO" $POURCENT
local dir="regex"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
log_info "Start the regex processing, this step take a long time..."
shopt -s globstar
local counter=1
local counter_total=1
for filename in /**; do
if [[ -f "${filename}" && ! -h "${filename}" && "/proc/" != "${filename:0:6}" && "/sys/" != "${filename:0:5}" ]]; then
data=""
while IFS= read -r -d '' substring || [[ $substring ]]; do
data+="$substring"
done < "${filename}"
echo "${data}" | grep -oE '(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}' | awk ' { print $0 } ' >> ipv4.txt
echo "${data}" | grep -oP '(?!-)[A-Za-z0-9-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,6}' | awk ' { print $0 } ' >> domain.txt
echo "${data}" | grep -oP '((http|https)://)(www.)?[a-zA-Z0-9@:%._\+~#?&//=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%._\+~#?&//=]*)' | awk ' { print $0 } ' >> urls.txt
echo "${data}" | grep -oP '(?:[A-Za-z\d+/]{4})*(?:[A-Za-z\d+/]{3}=|[A-Za-z\d+/]{2}==)?' | awk ' { print $0 } ' >> base64.txt
echo "${data}" | grep -oP '(([^<>()[\]\.,;:\s@"]+(\.[^<>()[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))' | awk ' { print $0 } ' >> email.txt
echo "${data}" | grep -oP '\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?' | awk ' { print $0 } ' >> uuid.txt
echo "${data}" | grep -oP '(%(25)?[0-9A-Fa-f]{2}){5,}' | awk ' { print $0 } ' >> urlencode.txt
echo "${filename}" | grep -oP '\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?' | awk ' { print $0 } ' >> uuid.txt
((counter++))
((counter_total++))
if [[ ${counter} -ge ${ONE_POURCENT} ]]; then
log_debug "Add one % of collection..."
local counter=1 # a=0 && ((a++)) -> exit code == 1
((POURCENT++))
show_info "${counter_total} / ${TOTAL_FILE} files are processed" "OK" "${POURCENT}" "" "\r" "1"
fi
fi
done
shopt -u globstar
log_info "Regex are processed."
log_info "Regex are successfully saved."
cd ..
show_info "COLLECTED: regex match" "OK" $POURCENT
return 0
}
function get_environment_vars() {
#
# @desc :: This function saves all installed environment variable and functions
#
show_info "Processing environment variables..." "INFO" $POURCENT
local dir="environment_vars"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_env=(
[printenv]="printenv"
[export]="export"
[env]="env"
[declare]="declare -f"
[set]="set -o posix"
)
for command in "${!_get_env[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_env[$command]} > "${command}.txt" 2>>errors.txt
fi
done
set > "set2.txt" 2>>errors.txt
log_info "Environment variables are successfully saved."
cd ..
show_info "COLLECTED: environment variables" "OK" $POURCENT
return 0
}
function get_modules() {
#
# @desc :: This function saves loaded modules
#
show_info "Processing modules..." "INFO" $POURCENT
local dir="modules"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_modules=(
[lsmod]="lsmod"
[cat]="cat /proc/modules"
)
for command in "${!_get_modules[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_modules[$command]} > "${command}.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\nlsmod\n' && "${commands[@]}" =~ $'\nmodprobe\n' ]]; then
log_info "Detect 'lsmod' and 'modprobe' are installed."
for module in $(lsmod | awk '$2 ~ /[0-9]+/ { print $1 }'); do
modprobe --show-depends "${module}" >> "modprobe.txt" 2>>errors.txt
done
fi
if [[ "${commands[@]}" =~ $'\nlsmod\n' && "${commands[@]}" =~ $'\nmodinfo\n' ]]; then
log_info "Detect 'lsmod' and 'modinfo' are installed."
for module in $(lsmod | awk '$2 ~ /[0-9]+/ { print $1 }'); do
modinfo "${module}" >> "modinfo.txt" 2>>errors.txt
done
fi
log_info "Modules are successfully saved."
cd ..
show_info "COLLECTED: modules" "OK" $POURCENT
return 0
}
function get_system() {
#
# @desc :: This function saves system
#
show_info "Processing system..." "INFO" $POURCENT
local dir="system"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_system=(
[cat]="cat /proc/version"
[uname]="uname -a"
[hostname]="hostname"
)
for command in "${!_get_system[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_system[$command]} > "${command}.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\nhostname\n' ]]; then
hostname -I > ip.txt
fi
log_info "System state are successfully saved."
cd ..
show_info "COLLECTED: system" "OK" $POURCENT
return 0
}
function get_disks() {
#
# @desc :: This function saves disks state
#
show_info "Processing disks state..." "INFO" $POURCENT
local dir="disks"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_disks=(
[fdisk]="fdisk -l"
[df]="df -h"
[findmnt]="findmnt -a -A"
[vgdisplay]="vgdisplay -v"
[lvdisplay]="lvdisplay -v"
[vgs]="vgs --all"
[lvs]="lvs --all"
[free]="free"
[cat]="cat /proc/partitions"
)
for command in "${!_get_disks[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_disks[$command]} > "${command}.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect 'du' is installed."
set +e # permissions error on non existant files
du -sh / > "du.txt" 2>>errors.txt
set -e
fi
if [[ -e "/dev/mapper" ]]
then
ls -l /dev/mapper > "mapper.txt" 2>>errors.txt
fi
log_debug "Copy files fstab mtab..."
copy_files "/etc/fstab"
copy_files "/etc/mtab"
log_info "Disks state are successfully saved."
cd ..
show_info "COLLECTED: disks" "OK" $POURCENT
return 0
}
function get_packages() {
#
# @desc :: This function saves all installed packages
#
show_info "Processing packages..." "INFO" $POURCENT
local dir="packages"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_packages=(
[pacman]="pacman -Q"
[apk]="apk info -vv"
[apt]="apt list --installed"
[dpkg]="dpkg -l"
[dpkg-query]="dpkg-query -l"
[yum]="yum list installed"
[dnf]="dnf list installed"
[zypper]="zypper se --installed-only"
[rpm]="rpm -qa"
[snap]="snap list"
[flatpak]="flatpak list --app"
)
for command in "${!_get_packages[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_packages[$command]} > "${command}.txt" 2>>errors.txt
fi
done
_get_packages=(
[dpkg]="dpkg -V"
)
for command in "${!_get_packages[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_packages[$command]} > "${command}_verify.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\nrpm\n' ]]; then
set +e # -V raise an error
rpm="rpm -Va"
set -e
fi
log_info "Packages are successfully saved."
cd ..
show_info "COLLECTED: packages" "OK" $POURCENT
return 0
}
function get_accounts() {
#
# @desc :: This function saves users and groups
#
show_info "Processing accounts..." "INFO" $POURCENT
local dir="accounts"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
log_debug "Copy account files..."
copy_files /etc/passwd
copy_files /etc/passwd-
copy_files /etc/group
copy_files /etc/group-
copy_files /etc/shadow
copy_files /etc/shadow-
copy_files /etc/gshadow
copy_files /etc/gshadow-
log_debug "Use who command..."
who -alpu > who.txt
log_info "Accounts are successfully saved."
cd ..
show_info "COLLECTED: accounts" "OK" $POURCENT
return 0
}
function get_process() {
#
# @desc :: This function saves running process
#
show_info "Processing process..." "INFO" $POURCENT
local dir="process"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_process=(
[pstree]="pstree"
[ps]="ps faux"
[top]="top -H -b -n 1"
)
for command in "${!_get_process[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_process[$command]} > "${command}.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\nps\n' ]]; then
ps -ewo "%p,%P,%x,%t,%u,%c,%a" > "ps2.txt"
fi
if [[ "${commands[@]}" =~ $'\ntop\n' ]]; then
top -b -n 1 > "top2.txt"
fi
log_info "Process are successfully saved."
cd ..
show_info "COLLECTED: process" "OK" $POURCENT
return 0
}
function get_services() {
#
# @desc :: This function saves services
#
show_info "Processing services..." "INFO" $POURCENT
local dir="services"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
if [[ "${commands[@]}" =~ $'\nfirewalld\n' ]]; then
log_info "Detect 'firewalld' is installed."
set +e # return code -> 1
firewalld
set -e
fi
declare -A _get_services=(
[systemctl]="systemctl list-units --all"
[ls]="echo ""$(ls -la /etc/systemd/system/**/*.service /usr/lib/systemd/**/*.service 2>>/dev/null)"
[service]="service --status-all"
[firewall-cmd]="firewall-cmd --list-services"
[sysv-rc-conf]="sysv-rc-conf -list"
[chkconfig]="chkconfig --list"
)
shopt -s globstar
for command in "${!_get_services[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_services[$command]} > "${command}.txt" 2>>errors.txt
fi
done
shopt -u globstar
if [[ "${commands[@]}" =~ $'\nsystemctl\n' ]]; then
log_debug "Use systemctl filters..."
systemctl --type=service --state=failed > "systemctl_services_failed.txt" 2>>errors.txt
systemctl --type=service --state=active > "systemctl_services_active.txt" 2>>errors.txt
systemctl --type=service --state=running > "systemctl_services_running.txt" 2>>errors.txt
fi
if [[ "${commands[@]}" =~ $'\nls\n' ]]; then
ls -l /etc/init.d/* > "ls_etc_initd.txt" 2>>errors.txt
fi
log_info "Services are successfully saved."
cd ..
show_info "COLLECTED: services" "OK" $POURCENT
return 0
}
function get_opened_ports() {
#
# @desc :: This function saves opened ports
#
show_info "Processing opened ports..." "INFO" $POURCENT
local dir="ports"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
if [[ "${commands[@]}" =~ $'\nfirewalld\n' ]]; then
log_info "Detect 'firewalld' is installed."
set +e # return code -> 1
firewalld
set -e
fi
declare -A _get_ports=(
[ss]="ss --all"
[netstat]="netstat -a"
[lsof]="lsof -i -n -P"
)
for command in "${!_get_ports[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_ports[$command]} > "${command}.txt" 2>>errors.txt
fi
done
_get_ports[ss]="ss -lntu"
_get_ports[netstat]="netstat -lntu"
_get_ports[lsof]="lsof -i -n -P | awk ' \$0 ~ \"LISTEN\" { print \$0 } '"
_get_ports["firewall-cmd"]="firewall-cmd --list-ports"
for command in "${!_get_ports[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
eval "${_get_ports[$command]} > '${command}_listen_udp_tcp.txt' 2>>errors.txt"
fi
done
log_info "Ports are successfully saved."
cd ..
show_info "COLLECTED: opened ports" "OK" $POURCENT
return 0
}
function get_arp_cache() {
#
# @desc :: This function saves arp cache
#
show_info "Processing arp cache..." "INFO" $POURCENT
local dir="arp"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_arp=(
[arp]="arp -a"
[cat]="cat /proc/net/arp"
)
for command in "${!_get_arp[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_arp[$command]} > "${command}.txt" 2>>errors.txt
fi
done
log_info "Arp cache are successfully saved."
cd ..
show_info "COLLECTED: arp cache" "OK" $POURCENT
return 0
}
function get_network_traffic() {
#
# @desc :: This function saves network traffic statistics
#
show_info "Processing network traffic statistics..." "INFO" $POURCENT
local dir="traffic"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_traffic=(
[grep]="grep -H . /sys/class/net/*/statistics/rx_packets"
[ip]="ip -s -s link"
[ethtool]="ethtool -S {network}"
)
if [[ "${commands[@]}" =~ $'\nip\n' ]]; then
_interfaces=$(ip addr show | awk ' $0 ~ /^[0-9]+:\s+\w+:/ { print $2 } ' ) # ls /sys/class/net
else
_interfaces=""
fi
for command in "${!_get_traffic[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
if [[ -n "${_interfaces}" && ${_get_traffic[$command]} =~ "{network}" ]]; then
for _interface in ${_interfaces[@]}; do
if [[ "${_interface}" == "lo:" ]]; then continue; fi
_interface=${_interface/:/}
${_get_traffic[$command]//"{network}"/"${_interface}"} > "${command}_${_interface}.txt" 2>>errors.txt
done
else
${_get_traffic[$command]} > "${command}.txt" 2>>errors.txt
fi
fi
done
log_info "Network traffic statistics are successfully saved."
cd ..
show_info "COLLECTED: network traffic statistics" "OK" $POURCENT
return 0
}
function get_network_interfaces() {
#
# @desc :: This function saves network interfaces
#
show_info "Processing network interfaces..." "INFO" $POURCENT
local dir="interfaces"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_interfaces=(
[ifconfig]="ifconfig -a"
[iwconfig]="iwconfig"
[ip]="ip addr"
[cat]="cat /proc/net/dev"
[netstat]="netstat -i"
[nmcli]="nmcli device status"
[lshw]="lshw -class network -short"
[hwinfo]="hwinfo --short --network"
[inxi]="inxi -N"
[lspci]="lspci | awk ' tolower(\$0) ~ /network|ethernet|wireless|wi-fi/ { print \$0 } '"
)
for command in "${!_get_interfaces[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
eval "${_get_interfaces[$command]} > '${command}.txt' 2>>errors.txt"
fi
done
log_debug "Copy hosts files..."
if [[ "${commands[@]}" =~ $'\ncat\n' && -f "/etc/hosts" ]]; then
cat /etc/hosts > "hosts.txt" 2>>errors.txt
fi
if [[ "${commands[@]}" =~ $'\ncat\n' && -f "hosts.allow.txt" ]]; then
cat /etc/hosts.allow > "hosts.allow.txt" 2>>errors.txt
fi
if [[ "${commands[@]}" =~ $'\ncat\n' && -f "hosts.deny.txt" ]]; then
cat /etc/hosts.deny > "hosts.deny.txt" 2>>errors.txt
fi
log_info "Interfaces are successfully saved."
cd ..
show_info "COLLECTED: process" "OK" $POURCENT
return 0
}
function get_tasks() {
#
# @desc :: This function saves scheduled tasks (servicse, cron, rc, .profile ...)
#
show_info "Processing scheduled tasks..." "INFO" $POURCENT
local dir="tasks"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
shopt -s globstar
declare -A files
files[bashrc]="/etc/*bashrc* /home/*/.bashrc* /home/*/.bash_profile* /home/*/.profile* /home/*/.bash_login /root/.bashrc* /home/*/.profile* /root/.profile* /root/.bash_login"
files[cron]="/etc/*cron**/* /etc/cron* /var/spool/**/cron*"
files[service]="/etc/systemd/system/**/*.service /usr/lib/systemd/**/*.service"
files[rc]="/etc/rc*.d**/* /etc/rc.local*"
for directory in "${!files[@]}"
do
mkdir "${directory}"
for filename in ${files[$directory]}; do
log_debug "Scheduled file: ${filename} is detected and save it..."
copy_files "${filename}" "" "${directory}/"
done
done
shopt -u globstar
set +e # user without cron will raise an error
cut -d: -f1 /etc/passwd | while read username
do
crontab -u "${username}" -l > "${username}_crontab.txt" 2>>errors.txt
done
set -e
log_info "Scheduled tasks state are successfully saved."
cd ..
show_info "COLLECTED: scheduled tasks" "OK" $POURCENT
return 0
}
function get_hardware() {
#
# @desc :: This function saves hardware
#
show_info "Processing hardware..." "INFO" $POURCENT
local dir="hardware"
mkdir "${dir}" && cd "${dir}"
log_info "The ${dir} report directory is created and it is the new current directory"
declare -A _get_hardware=(
[lspci]="lspci -kDq"
[dmidecode]="dmidecode"
[lscpu]="lscpu -a -p"
[cat]="cat /proc/cpuinfo"
)
for command in "${!_get_hardware[@]}"
do
if [[ "${commands[@]}" =~ $'\n'"${command}"$'\n' ]]; then
log_info "Detect '${command}' is installed."
${_get_hardware[$command]} > "${command}.txt" 2>>errors.txt
fi
done
if [[ "${commands[@]}" =~ $'\nlscpu\n' ]]; then
lscpu > "lscpu2.txt" 2>>errors.txt
fi
log_info "Hardware state are successfully saved."
cd ..
show_info "COLLECTED: hardware" "OK" $POURCENT
return 0
}
function get_logs() {
#
# @desc :: This function saves logs