Skip to content

Commit 6123a56

Browse files
damcav35Damien Cavagnini
andauthored
fix: update record_mac_edit.sh to use apparmor instead of selinux (#262)
Update record_mac_edit.sh to be compliant with debian11 and debian12 CIS recommendations. fix issue #195 Co-authored-by: Damien Cavagnini <damien.cavagnini@corp.ovh.com>
1 parent 99e6694 commit 6123a56

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

bin/hardening/record_mac_edit.sh

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,48 @@ HARDENING_LEVEL=4
1717
# shellcheck disable=2034
1818
DESCRIPTION="Record events that modify the system's mandatory access controls (MAC)."
1919

20-
AUDIT_PARAMS='-w /etc/selinux/ -p wa -k MAC-policy'
21-
FILES_TO_SEARCH='/etc/audit/audit.rules /etc/audit/rules.d/audit.rules'
22-
FILE='/etc/audit/rules.d/audit.rules'
20+
AUDIT_PARAMS=("-w /etc/apparmor/ -p wa -k MAC-policy" "-w /etc/apparmor.d/ -p wa -k MAC-policy")
21+
AUDIT_FILE='/etc/audit/audit.rules'
22+
ADDITIONAL_PATH="/etc/audit/rules.d"
23+
FILE_TO_WRITE='/etc/audit/rules.d/audit.rules'
2324

2425
# This function will be called if the script status is on enabled / audit mode
2526
audit() {
26-
# define custom IFS and save default one
27-
d_IFS=$IFS
28-
c_IFS=$'\n'
29-
IFS=$c_IFS
30-
for AUDIT_VALUE in $AUDIT_PARAMS; do
31-
debug "$AUDIT_VALUE should be in file $FILES_TO_SEARCH"
32-
IFS=$d_IFS
27+
MISSING_PARAMS=()
28+
index=0
29+
# use find here in order to simplify test usage with sudo using secaudit user
30+
FILES_TO_SEARCH="$(sudo_wrapper find $ADDITIONAL_PATH -name '*.rules' | paste -s) $AUDIT_FILE"
31+
for i in "${!AUDIT_PARAMS[@]}"; do
32+
debug "${AUDIT_PARAMS[i]} should be in file $FILES_TO_SEARCH"
3333
SEARCH_RES=0
3434
for FILE_SEARCHED in $FILES_TO_SEARCH; do
35-
does_pattern_exist_in_file "$FILE_SEARCHED" "$AUDIT_VALUE"
36-
IFS=$c_IFS
35+
does_pattern_exist_in_file "$FILE_SEARCHED" "${AUDIT_PARAMS[i]}"
3736
if [ "$FNRET" != 0 ]; then
38-
debug "$AUDIT_VALUE is not in file $FILE_SEARCHED"
37+
debug "${AUDIT_PARAMS[i]} is not in file $FILE_SEARCHED"
3938
else
40-
ok "$AUDIT_VALUE is present in $FILE_SEARCHED"
39+
ok "${AUDIT_PARAMS[i]} is present in $FILE_SEARCHED"
4140
SEARCH_RES=1
4241
fi
4342
done
4443
if [ "$SEARCH_RES" = 0 ]; then
45-
crit "$AUDIT_VALUE is not present in $FILES_TO_SEARCH"
44+
crit "${AUDIT_PARAMS[i]} is not present in $FILES_TO_SEARCH"
45+
MISSING_PARAMS[i]="${AUDIT_PARAMS[i]}"
46+
index=$((index + 1))
4647
fi
4748
done
48-
IFS=$d_IFS
4949
}
5050

5151
# This function will be called if the script status is on enabled mode
5252
apply() {
53-
# define custom IFS and save default one
54-
d_IFS=$IFS
55-
c_IFS=$'\n'
56-
IFS=$c_IFS
57-
for AUDIT_VALUE in $AUDIT_PARAMS; do
58-
debug "$AUDIT_VALUE should be in file $FILES_TO_SEARCH"
59-
IFS=$d_IFS
60-
SEARCH_RES=0
61-
for FILE_SEARCHED in $FILES_TO_SEARCH; do
62-
does_pattern_exist_in_file "$FILE_SEARCHED" "$AUDIT_VALUE"
63-
IFS=$c_IFS
64-
if [ "$FNRET" != 0 ]; then
65-
debug "$AUDIT_VALUE is not in file $FILE_SEARCHED"
66-
else
67-
ok "$AUDIT_VALUE is present in $FILE_SEARCHED"
68-
SEARCH_RES=1
69-
fi
70-
done
71-
if [ "$SEARCH_RES" = 0 ]; then
72-
warn "$AUDIT_VALUE is not present in $FILES_TO_SEARCH, adding it to $FILE"
73-
add_end_of_file "$FILE" "$AUDIT_VALUE"
74-
eval "$(pkill -HUP -P 1 auditd)"
75-
fi
53+
audit
54+
changes=0
55+
for i in "${!MISSING_PARAMS[@]}"; do
56+
info "${MISSING_PARAMS[i]} is not present in $FILES_TO_SEARCH, adding it"
57+
add_end_of_file "$FILE_TO_WRITE" "${MISSING_PARAMS[i]}"
58+
changes=1
7659
done
77-
IFS=$d_IFS
60+
61+
[ "$changes" -eq 0 ] || eval "$(pkill -HUP -P 1 auditd)"
7862
}
7963

8064
# This function will check config parameters required

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Vcs-Browser: https://github.com/ovh/debian-cis/
1010

1111
Package: cis-hardening
1212
Architecture: all
13-
Depends: ${misc:Depends}, patch
13+
Depends: ${misc:Depends}, patch, coreutils
1414
Description: Suite of configurable scripts to audit or harden a Debian.
1515
Modular Debian security hardening scripts based on cisecurity.org
1616
⟨cisecurity.org⟩ recommendations. We use it at OVH ⟨https://www.ovh.com⟩ to

tests/hardening/record_mac_edit.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# run-shellcheck
33
test_audit() {
44
describe Running on blank host
5-
register_test retvalshouldbe 0
6-
dismiss_count_for_test
5+
register_test retvalshouldbe 1
76
# shellcheck disable=2154
87
run blank "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
98

@@ -13,6 +12,6 @@ test_audit() {
1312

1413
describe Checking resolved state
1514
register_test retvalshouldbe 0
16-
register_test contain "[ OK ] -w /etc/selinux/ -p wa -k MAC-policy is present in /etc/audit/rules.d/audit.rules"
15+
register_test contain "[ OK ] -w /etc/apparmor/ -p wa -k MAC-policy is present in /etc/audit/rules.d/audit.rules"
1716
run resolved "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
1817
}

0 commit comments

Comments
 (0)