forked from finalduty/cis-benchmarks-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cis-audit.sh
executable file
·3146 lines (2664 loc) · 121 KB
/
cis-audit.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
#!/bin/bash
##
## Copyright 2019 Andy Dustin
##
## Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
## in compliance with the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software distributed under the License is
## distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and limitations under the License.
##
## This script checks for compliance against CIS CentOS Linux 7 Benchmark v2.1.1 2017-01-31 measures
## Each individual standard has it's own function and is forked to the background, allowing for
## multiple tests to be run in parallel, reducing execution time.
## You can obtain a copy of the CIS Benchmarks from https://www.cisecurity.org/cis-benchmarks/
## [email protected] [rev: a44ceb7]: First Release
## [email protected] [rev: ad63750]: Improved progress ticker display logic
## [email protected] [rev: ad80cd7]: Updated state tracking on some tests incorrectly failing
## [email protected] [rev: ae68d08]: User reported UX issue - Now includes both level 1 and level 2 tests when called with both '--level 1' and '--level 2' arguments. Thanks Darren Foster
## [email protected] [rev: ae700df]: Fixed output for tests that are skipped using the 'skip_test' function
## [email protected] [rev: ae700e9]: Updated test 6.2.8 to use `stat` instead of `ls`
## [email protected] [rev: ae70116]: Updated tests 5.2.11 and 5.2.12 to support a wider range of configurations of approved Ciphers and MACs in sshd_config. Also added error state for tests. Thanks Darren Foster
### Variables ###
## This section defines global variables used in the script
args=$@
count=0
exit_code=0
me=$(basename $0)
result=Fail
state=0
tmp_file_base="/tmp/.cis_audit"
tmp_file="$tmp_file_base-$(date +%y%m%d%H%M%S).output"
started_counter="$tmp_file_base-$(date +%y%m%d%H%M%S).started.counter"
finished_counter="$tmp_file_base-$(date +%y%m%d%H%M%S).finished.counter"
wait_time="0.25"
progress_update_delay="0.1"
max_running_tests=10
debug=False
trace=False
renice_bool=True
renice_value=5
start_time=$(date +%s)
color=True
test_level=0
### Functions ###
## This section defines functions used in the script
is_test_included() {
id=$1
level=$2
state=0
write_debug "Checking whether to run test $id"
[ -z $level ] && level=$test_level
## Check if the $level is one we're going to run
if [ $test_level -ne 0 ]; then
if [ "$test_level" != "$level" ]; then
write_debug "Excluding level $level test $id"
state=1
fi
fi
## Check if there were explicitly included tests
if [ $(echo "$include" | wc -c ) -gt 3 ]; then
## Check if the $id is in the included tests
if [ $(echo " $include " | grep -c " $id ") -gt 0 ]; then
write_debug "Test $id was explicitly included"
state=0
elif [ $(echo " $include " | grep -c " $id\.") -gt 0 ]; then
write_debug "Test $id is the parent of an included test"
state=0
elif [ $(for i in $include; do echo " $id" | grep " $i\."; done | wc -l) -gt 0 ]; then
write_debug "Test $id is the child of an included test"
state=0
elif [ $test_level == 0 ]; then
write_debug "Excluding test $id (Not found in the include list)"
state=1
fi
fi
## If this $id was included in the tests check it wasn't then excluded
if [ $(echo " $exclude " | grep -c " $id ") -gt 0 ]; then
write_debug "Excluding test $id (Found in the exclude list)"
state=1
elif [ $(for i in $exclude; do echo " $id" | grep " $i\."; done | wc -l) -gt 0 ]; then
write_debug "Excluding test $id (Parent found in the exclude list)"
state=1
fi
[ $state -eq 0 ] && write_debug "Including test $id"
return $state
} ## Checks whether to run a particular test or not
get_id() {
echo $1 | sed -e 's/test_//' -e 's/\.x.*$//'
} ## Returns a prettied id for a calling function
help_text() {
cat << EOF |fmt -sw99
This script runs tests on the system to check for compliance against the CIS CentOS 7 Benchmarks.
No changes are made to system files by this script.
Options:
EOF
cat << EOF | column -t -s'|'
||-h,|--help|Prints this help text
|||--debug|Run script with debug output turned on
|||--level (1,2)|Run tests for the specified level only
|||--include "<test_ids>"|Space delimited list of tests to include
|||--exclude "<test_ids>"|Space delimited list of tests to exclude
|||--nice |Lower the CPU priority for test execution. This is the default behaviour.
|||--no-nice|Do not lower CPU priority for test execution. This may make the tests complete faster but at
||||the cost of putting a higher load on the server. Setting this overrides the --nice option.
|||--no-colour|Disable colouring for STDOUT. Output redirected to a file/pipe is never coloured.
EOF
cat << EOF
Examples:
Run with debug enabled:
$me --debug
Exclude tests from section 1.1 and 1.3.2:
$me --exclude "1.1 1.3.2"
Include tests only from section 4.1 but exclude tests from section 4.1.1:
$me --include 4.1 --exclude 4.1.1
Run only level 1 tests
$me --level 1
Run level 1 tests and include some but not all SELinux questions
$me --level 1 --include 1.6 --exclude 1.6.1.2
EOF
exit 0
} ## Outputs help text
now() {
echo $(( $(date +%s%N) / 1000000 ))
} ## Short function to give standardised time for right now (saves updating the date method everywhere)
outputter() {
write_debug "Formatting and writing results to STDOUT"
echo
echo " CIS CentOS 7 Benchmark v2.2.0 Results "
echo "---------------------------------------"
if [ -t 1 -a $color == "True" ]; then
(
echo "ID,Description,Scoring,Level,Result,Duration"
echo "--,-----------,-------,-----,------,--------"
sort -V $tmp_file
) | column -t -s , |\
sed -e $'s/^[0-9]\s.*$/\\n\e[1m&\e[22m/' \
-e $'s/^[0-9]\.[0-9]\s.*$/\e[1m&\e[22m/' \
-e $'s/\sFail\s/\e[31m&\e[39m/' \
-e $'s/\sPass\s/\e[32m&\e[39m/' \
-e $'s/^.*\sSkipped\s.*$/\e[2m&\e[22m/'
else
(
echo "ID,Description,Scoring,Level,Result,Duration"
sort -V $tmp_file
) | column -t -s , | sed -e '/^[0-9]\ / s/^/\n/'
fi
tests_total=$(grep -c "Scored" $tmp_file)
tests_skipped=$(grep -c ",Skipped," $tmp_file)
tests_ran=$(( $tests_total - $tests_skipped ))
tests_passed=$(egrep -c ",Pass," $tmp_file)
tests_failed=$(egrep -c ",Fail," $tmp_file)
tests_errored=$(egrep -c ",Error," $tmp_file)
tests_duration=$(( $( date +%s ) - $start_time ))
echo
echo "Passed $tests_passed of $tests_total tests in $tests_duration seconds ($tests_skipped Skipped, $tests_errored Errors)"
echo
write_debug "All results written to STDOUT"
} ## Prettily prints the results to the terminal
parse_args() {
args=$@
## Call help_text function if -h or --help present
$(echo $args | egrep -- '-h' &>/dev/null) && help_text
## Check arguments for --debug
$(echo $args | grep -- '--debug' &>/dev/null) && debug="True" || debug="False"
write_debug "Debug enabled"
## Full noise output
$(echo $args | grep -- '--trace' &>/dev/null) && trace="True" && set -x
[ $trace == "True" ] && write_debug "Trace enabled"
## Renice / lower priority of script execution
$(echo $args | grep -- '--nice' &>/dev/null) && renice_bool="True"
$(echo $args | grep -- '--no-nice' &>/dev/null) && renice_bool="False"
[ $renice_bool == "True" ] && write_debug "Tests will run with reduced CPU priority"
## Disable colourised output
$(echo $args | egrep -- '--no-color|--no-colour' &>/dev/null) && color="False" || color="True"
[ $color == "False" ] && write_debug "Coloured output disabled"
## Check arguments for --exclude
## NB: The whitespace at the beginning and end is required for the greps later on
exclude=" $(echo "$args" | sed -e 's/^.*--exclude //' -e 's/--.*$//') "
if [ $(echo "$exclude" | wc -c ) -gt 3 ]; then
write_debug "Exclude list is populated \"$exclude\""
else
write_debug "Exclude list is empty"
fi
## Check arguments for --include
## NB: The whitespace at the beginning and end is required for the greps later on
include=" $(echo "$args" | sed -e 's/^.*--include //' -e 's/--.*$//') "
if [ $(echo "$include" | wc -c ) -gt 3 ]; then
write_debug "Include list is populated \"$include\""
else
write_debug "Include list is empty"
fi
## Check arguments for --level
if [ $(echo $args | grep -- '--level 2' &>/dev/null; echo $?) -eq 0 ]; then
test_level=$(( $test_level + 2 ))
write_debug "Going to run Level 2 tests"
fi
if [ $(echo $args | grep -- '--level 1' &>/dev/null; echo $?) -eq 0 ]; then
test_level=$(( $test_level + 1 ))
write_debug "Going to run Level 1 tests"
fi
if [ "$test_level" -eq 0 -o "$test_level" -eq 3 ]; then
test_level=0
write_debug "Going to run tests from any level"
fi
} ## Parse arguments passed in to the script
progress() {
## We don't want progress output while we're spewing debug or trace output
write_debug "Not displaying progress ticker while debug is enabled" && return 0
[ $trace == "True" ] && return 0
array=(\| \/ \- \\)
while [ "$(running_children)" -gt 1 -o "$(cat $tmp_file_base-stage)" == "LOADING" ]; do
started=$( wc -l $started_counter | awk '{print $1}' )
finished=$( wc -l $finished_counter | awk '{print $1}' )
running=$(( $started - $finished ))
tick=$(( $tick + 1 ))
pos=$(( $tick % 4 ))
char=${array[$pos]}
script_duration="$(date +%T -ud @$(( $(date +%s) - $start_time )))"
printf "\r[$script_duration] ($char) $finished of $started tests completed " >&2
#ps --ppid $$ >> ~/tmp/cis-audit
#running_children >> ~/tmp/cis-audit
#echo Stage: $test_stage >> ~/tmp/cis-audit
sleep $progress_update_delay
done
## When all tests have finished, make a final update
finished=$( wc -l $finished_counter | awk '{print $1}' )
script_duration="$(date +%T -ud @$(( $(date +%s) - $start_time )))"
#printf "\r[✓] $finished of $finished tests completed\n" >&2
printf "\r[$script_duration] (✓) $started of $started tests completed\n" >&2
} ## Prints a pretty progress spinner while running tests
run_test() {
id=$1
level=$2
test=$3
args=$(echo $@ | awk '{$1 = $2 = $3 = ""; print $0}' | sed 's/^ *//')
if [ $(is_test_included $id $level; echo $?) -eq 0 ]; then
write_debug "Requesting test $id by calling \"$test $id $args &\""
while [ "$(pgrep -P $$ 2>/dev/null | wc -l)" -ge $max_running_tests ]; do
write_debug "There were already max_running_tasks ($max_running_tests) while attempting to start test $id. Pausing for $wait_time seconds"
sleep $wait_time
done
write_debug "There were $(( $(pgrep -P $$ 2>&1 | wc -l) - 1 ))/$max_running_tests max_running_tasks when starting test $id."
## Don't try to thread script if trace is enabled so it's output is tidier :)
if [ $trace == "True" ]; then
$test $id $level $args
else
$test $id $level $args &
fi
fi
return 0
} ## Compares test id against includes / excludes list and returns whether to run test or not
running_children() {
## Originally tried using pgrep, but it returned one line even when output was "empty"
search_terms="PID|ps$|grep$|wc$|sleep$"
[ $debug == True ] && ps --ppid $$ | egrep -v "$search_terms"
ps --ppid $$ | egrep -v "$search_terms" | wc -l
} ## Ghetto implementation that returns how many child processes are running
setup() {
write_debug "Script was started with PID: $$"
if [ $renice_bool = "True" ]; then
if [ $renice_value -gt 0 -a $renice_value -le 19 ]; then
renice_output="$(renice +$renice_value $$)"
write_debug "Renicing $renice_output"
fi
fi
write_debug "Creating tmp files with base $tmp_file_base*"
cat /dev/null > $tmp_file
cat /dev/null > $started_counter
cat /dev/null > $finished_counter
} ## Sets up required files for test
test_start() {
id=$1
level=$2
write_debug "Test $id started"
echo "." >> $started_counter
write_debug "Progress: $( wc -l $finished_counter | awk '{print $1}' )/$( wc -l $started_counter | awk '{print $1}' ) tests."
now
} ## Prints debug output (when enabled) and returns current time
test_finish() {
id=$1
start_time=$2
duration="$(( $(now) - $start_time ))"
write_debug "Test "$id" completed after "$duration"ms"
echo "." >> $finished_counter
write_debug "Progress: $( wc -l $finished_counter | awk '{print $1}' )/$( wc -l $started_counter | awk '{print $1}' ) tests."
echo $duration
} ## Prints debug output (when enabled) and returns duration since $start_time
test_stage() {
echo $test_stage
} ## Shim to get up to date $test_stage value
tidy_up() {
[ $debug == "True" ] && opt="-v"
rm $opt "$tmp_file_base"* 2>/dev/null
} ## Tidys up files created during testing
write_cache() {
write_debug "Writing to $tmp_file - $@"
printf "$@\n" >> $tmp_file
} ## Writes additional rows to the output cache
write_debug() {
[ $debug == "True" ] && printf "[DEBUG] $(date -Ins) $@\n" >&2
} ## Writes debug output to STDERR
write_err() {
printf "[ERROR] $@\n" >&2
} ## Writes error output to STDERR
write_result() {
write_debug "Writing result to $tmp_file - $@"
echo $@ >> $tmp_file
} ## Writes test results to the output cache
### Benchmark Tests ###
## This section defines the benchmark tests that are called by the script
## Tests used in multiple sections
skip_test() {
## This function is a blank for any tests too complex to perform
## or that rely too heavily on site policy for definition
id=$1
level=$2
description=$( echo $@ | awk '{$1=$2=""; print $0}' | sed 's/^ *//')
scored="Skipped"
result=""
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_is_enabled() {
id=$1
level=$2
service=$3
name=$4
description="Ensure $name service is enabled"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $( systemctl is-enabled $service ) == "enabled" ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_is_installed() {
id=$1
level=$2
pkg=$3
name=$4
description="Ensure $name is installed"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(rpm -q $pkg &>/dev/null; echo $?) -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_is_not_installed() {
id=$1
level=$2
pkg=$3
name=$4
description="Ensure $name is not installed"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(rpm -q $pkg &>/dev/null; echo $?) -eq 0 ] || result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_perms() {
id=$1
level=$2
perms=$3
file=$4
description="Ensure permissions on $file are configured"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
u=$(echo $perms | cut -c1)
g=$(echo $perms | cut -c2)
o=$(echo $perms | cut -c3 )
file_perms="$(stat $file | awk '/Access: \(/ {print $2}')"
file_u=$(echo $file_perms | cut -c3)
file_g=$(echo $file_perms | cut -c4)
file_o=$(echo $file_perms | cut -c5)
[ "$(ls -ld $file | awk '{ print $3" "$4 }')" == "root root" ] || state=1
[ $file_u -le $u ] || state=1
[ $file_g -le $g ] || state=1
[ $file_o -le $o ] || state=1
[ $state -eq 0 ] && result=Pass
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
## Section 1 - Initial Setup
test_1.1.1.x() {
id=$1
level=$2
filesystem=$3
description="Ensure mounting of $filesystem is disabled"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(diff -qsZ <(modprobe -n -v $filesystem 2>/dev/null | tail -1) <(echo "install /bin/true") &>/dev/null; echo $?) -ne 0 ] && state=$(( $state + 1 ))
[ $(lsmod | grep $filesystem | wc -l) -ne 0 ] && state=$(( $state + 2 ))
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.1.x-check_partition() {
id=$1
level=$2
partition=$3
description="Ensure separate partition exists for $partition"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
mount | grep "$partition " &>/dev/null && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.1.x-check_fs_opts() {
id=$1
level=$2
partition=$3
fs_opt=$4
description="Ensure $fs_opt option set on $partition"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
mount | egrep "$partition .*,$fs_opt," &>/dev/null && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.1.x-check_removable() {
id=$1
level=$2
fs_opt=$3
description="Ensure $fs_opt option set on removable media partitions"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
## Note: Only usb media is supported at the moment. Need to investigate what
## difference a CDROM, etc. can make, but I've set it up ready to add
## another search term. You're welcome :)
devices=$(lsblk -pnlS | awk '/usb/ {print $1}')
filesystems=$(for device in "$devices"; do lsblk -nlp $device | egrep -v '^$device|[SWAP]' | awk '{print $1}'; done)
for filesystem in $filesystems; do
fs_without_opt=$(mount | grep "$filesystem " | grep -v $fs_opt &>/dev/null | wc -l)
[ $fs_without_opt -ne 0 ] && state=1
done
[ $state -eq 0 ] && result=Pass
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.1.21() {
id=$1
level=$2
description="Ensure sticky bit is set on all world-writable dirs"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
dirs=$(df --local -P | awk '{if (NR!=1) print $6}' | xargs -I '{}' find '{}' -xdev -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null | wc -l)
[ $dirs -eq 0 ] && result=Pass
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.1.22() {
id=$1
level=$2
description="Disable Automounting"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
service=$(systemctl | awk '/autofs/ {print $1}')
[ -n "$service" ] && systemctl is-enabled $service
[ $? -ne 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.2.1() {
id=$1
level=$2
description="Ensure package manager repositories are configured"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
repolist=$(yum repolist 2>/dev/null)
[ $(echo "$repolist" | egrep -c '^base/7/') -ne 0 -a $(echo "$repolist" | egrep -c '^updates/7/') -ne 0 ] && result="Pass"
## Tests End
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.2.2() {
id=$1
level=$2
description="Ensure GPG keys are configured"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(rpm -q gpg-pubkey | wc -l) -ne 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.2.3() {
id=$1
level=$2
description="Ensure gpgcheck is globally activated"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(grep -R ^gpgcheck=0 /etc/yum.conf /etc/yum.repos.d/ | wc -l) -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.3.2() {
id=$1
level=$2
description="Ensure filesystem integrity is regularly checked"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ $(grep -Rl 'aide' /var/spool/cron/root /etc/cron* 2>/dev/null | wc -l) -ne 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.4.2() {
id=$1
level=$2
description="Ensure bootloader password is set"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=1
## Note: This test includes checking /boot/grub2/user.cfg which is not defined in the standard,
## however this file is created by performing the remediation step in the standard so is
## included in the test here as well.
[ $(grep "^GRUB2_PASSWORD=" /boot/grub2/grub.cfg /boot/grub2/user.cfg | wc -l) -ne 0 ] && state=0
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.4.3() {
id=$1
level=$2
description="Ensure authentication required for single user mode"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
str='ExecStart=-/bin/sh -c "/usr/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"'
[ "$(grep /sbin/sulogin /usr/lib/systemd/system/rescue.service)" == "$str" ] || state=1
[ "$(grep /sbin/sulogin /usr/lib/systemd/system/emergency.service)" == "$str" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.5.1() {
id=$1
level=$2
description="Ensure core dumps are restricted"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
str='ExecStart=-/bin/sh -c "/usr/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"'
[ "$(grep "hard core" /etc/security/limits.conf /etc/security/limits.d/* | sed 's/^.*://' )" == "* hard core 0" ] || state=1
[ "$(sysctl fs.suid_dumpable)" == "fs.suid_dumpable = 0" ] || state=1
[ "$(grep "fs.suid_dumpable" /etc/sysctl.conf /etc/sysctl.d/*.conf | sed 's/^.*://')" == "fs.suid_dumpable = 0" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.5.2() {
id=$1
level=$2
description="Ensure XD/NX support is enabled"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
str='ExecStart=-/bin/sh -c "/usr/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"'
[ "$(dmesg | grep -o "NX (Execute Disable).*")" == "NX (Execute Disable) protection: active" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.5.3() {
id=$1
level=$2
description="Ensure address space layout randomisation (ASLR) is enabled"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
[ "$(sysctl kernel.randomize_va_space)" == "kernel.randomize_va_space = 2" ] || state=1
[ "$(grep "kernel.randomize_va_space" /etc/sysctl.conf /etc/sysctl.d/*.conf | sed 's/^.*://')" == "kernel.randomize_va_space = 2" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.5.4() {
id=$1
level=$2
description="Ensure prelink is disabled"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
str='ExecStart=-/bin/sh -c "/usr/sbin/sulogin; /usr/bin/systemctl --fail --no-block default"'
[ "$(rpm -q prelink)" == "package prelink is not installed" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.1() {
id=$1
level=$2
description="Ensure SELinux is not disabled in bootloader configuration"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ $(grep "^\s+linux" /boot/grub2/grub.cfg | egrep 'selinux=0|enforcing=0' | wc -l) -eq 0 ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.2() {
id=$1
level=$2
description="Ensure the SELinux state is enforcing"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ "$(grep SELINUX=enforcing /etc/selinux/config)" == "SELINUX=enforcing" ] || state=1
[ "$(sestatus | awk '/Current mode/ {print $3}')" == "enforcing" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.3() {
id=$1
level=$2
description="Ensure SELinux policy is configured"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ "$(grep SELINUXTYPE=targeted /etc/selinux/config)" == "SELINUXTYPE=targeted" ] || state=1
[ "$(sestatus | awk '/Loaded policy name/ {print $4}')" == "targeted" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.4() {
id=$1
level=$2
description="Ensure SETroubleshoot is not installed"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ "$(rpm -q setroubleshoot)" == "package setroubleshoot is not installed" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.5() {
id=$1
level=$2
description="Ensure MCS Translation Service (mcstrans) is not installed"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ "$(rpm -q mcstrans)" == "package mcstrans is not installed" ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.6.1.6() {
id=$1
level=$2
description="Ensure no unconfined daemons exist"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ "$(ps -eZ | egrep "initrc" | egrep -vw "tr|ps|egrep|bash|awk" | tr ':' ' ' | awk '{print $NF}' | wc -l)" -eq 0 ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.7.1.1() {
id=$1
level=$2
description="Ensure message of the day is configured properly"
scored="Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ $(wc -l /etc/motd | awk '{print $1}') -gt 0 ] || state=1
[ $(egrep '(\\v|\\r|\\m|\\s)' /etc/motd | wc -l) -eq 0 ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.7.1.2() {
id=$1
level=$2
description="Ensure local login warning banner is configured properly"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ $(wc -l /etc/issue | awk '{print $1}') -gt 0 ] || state=1
[ $(egrep '(\\v|\\r|\\m|\\s)' /etc/issue | wc -l) -eq 0 ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.7.1.3() {
id=$1
level=$2
description="Ensure remote login warning banner is configured properly"
scored="Not Scored"
test_start_time=$(test_start $id)
## Tests Start ##
state=0
[ $(wc -l /etc/issue.net | awk '{print $1}') -gt 0 ] || state=1
[ $(egrep '(\\v|\\r|\\m|\\s)' /etc/issue.net | wc -l) -eq 0 ] || state=1
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.7.2() {
id=$1
level=$2
description="Ensure GDM login banner is configured"
scored="Scored"
test_start_time="$(test_start $id)"
## Tests Start ##
state=0
gdm_file="/etc/dconf/profile/gdm"
banner_file="/etc/dconf/db/gdm.d/01-banner-message"
if [ "$(rpm -q gdm)" != "package gdm is not installed" ]; then
if [ -f $file ]; then
diff -qs $file <( echo -e "user-db:user\nsystem-db:gdm\nfile-db:/usr/share/gdm/greeter-dconf-defaults\n") || state=1
else
state=2
fi
egrep '^banner-message-enable=true' $banner_file || state=4
egrep '^banner-message-text=.*' $banner_file || state=8
fi
[ $state -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_1.8() {
id=$1
level=$2
description="Ensure updates are installed"
scored="Scored"
test_start_time="$(test_start $id)"
## Tests Start ##
[ $(yum check-update --security &>/dev/null; echo $?) -eq 0 ] && result="Pass"
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
## Section 2 - Services
test_2.1.x() {
id=$1
level=$2
service=$3
description="Ensure $service services are not enabled"
scored="Scored"
test_start_time="$(test_start $id)"
## Tests Start ##
str=$(chkconfig --list 2>&1)
state=0
dgram="$(chkconfig --list $service-dgram 2>/dev/null | awk '{print $2}')"
stream="$(chkconfig --list $service-stream 2>/dev/null | awk '{print $2}')"
if [ "$dgram" != "" -o "$stream" != "" ]; then
[ "$dgram" != "off" ] && state=1
[ "$stream" != "off" ] && state=1
fi
[ $state -eq 0 ] && result=Pass
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_2.1.6() {
id=$1
level=$2
description="Ensure tftp server is not enabled"
scored="Scored"
test_start_time="$(test_start $id)"
## Tests Start ##
state=0
str=$(chkconfig --list 2>&1)
[ "$(chkconfig --list 2>&1 | awk '/tftp/ {print $2}')" == "on" ] && state=1
[ $state -eq 0 ] && result=Pass
## Tests End ##
duration="$(test_finish $id $test_start_time)ms"
write_result "$id,$description,$scored,$level,$result,$duration"
}
test_2.1.7() {
id=$1
level=$2
description="Ensure xinetd is not enabled"
scored="Scored"
test_start_time="$(test_start $id)"