|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# run-shellcheck |
| 4 | +# |
| 5 | +# CIS Debian Hardening |
| 6 | +# |
| 7 | + |
| 8 | +# |
| 9 | +# Ensure file deletion events by users are collected |
| 10 | +# |
| 11 | + |
| 12 | +set -e # One error, it's over |
| 13 | +set -u # One variable unset, it's over |
| 14 | + |
| 15 | +# shellcheck disable=2034 |
| 16 | +HARDENING_LEVEL=4 |
| 17 | +# shellcheck disable=2034 |
| 18 | +DESCRIPTION="Ensure file deletion events are audited" |
| 19 | + |
| 20 | +AUDIT_RULES_FILE="/etc/audit/rules.d/50-delete.rules" |
| 21 | +AUDIT_RULES_DIR='/etc/audit/rules.d' |
| 22 | + |
| 23 | +# Global state |
| 24 | +AFD_RULES_OK=0 |
| 25 | +AFD_UID_MIN="" |
| 26 | + |
| 27 | +# This function will be called if the script status is on enabled / audit mode |
| 28 | +audit() { |
| 29 | + AFD_RULES_OK=0 |
| 30 | + |
| 31 | + # Get UID_MIN |
| 32 | + AFD_UID_MIN=$(awk '/^\s*UID_MIN/{print $2}' /etc/login.defs) |
| 33 | + if [ -z "$AFD_UID_MIN" ]; then |
| 34 | + crit "Unable to determine UID_MIN from /etc/login.defs" |
| 35 | + return |
| 36 | + fi |
| 37 | + |
| 38 | + # Check on disk configuration |
| 39 | + local l_ondisk_result |
| 40 | + l_ondisk_result=$(awk "/^ *-a *always,exit/ &&/ -F *arch=b(32|64)/ &&(/ -F *auid!=unset/||/ -F *auid!=-1/||/ -F *auid!=4294967295/) &&/ -F *auid>=${AFD_UID_MIN}/ &&/ -S/ &&(/unlink/||/rename/||/unlinkat/||/renameat/) &&(/ key= *[!-~]* *$/||/ -k *[!-~]* *$/)" "$AUDIT_RULES_DIR"/*.rules 2>/dev/null || true) |
| 41 | + |
| 42 | + # Check running configuration |
| 43 | + local l_running_result |
| 44 | + l_running_result=$(auditctl -l 2>/dev/null | awk "/^ *-a *always,exit/ &&/ -F *arch=b(32|64)/ &&(/ -F *auid!=unset/||/ -F *auid!=-1/||/ -F *auid!=4294967295/) &&/ -F *auid>=${AFD_UID_MIN}/ &&/ -S/ &&(/unlink/||/rename/||/unlinkat/||/renameat/) &&(/ key= *[!-~]* *$/||/ -k *[!-~]* *$/)" || true) |
| 45 | + |
| 46 | + # We need both b64 and b32 rules in both configurations |
| 47 | + local l_ondisk_b64 l_ondisk_b32 l_running_b64 l_running_b32 |
| 48 | + l_ondisk_b64=$(echo "$l_ondisk_result" | grep -c "b64" || true) |
| 49 | + l_ondisk_b32=$(echo "$l_ondisk_result" | grep -c "b32" || true) |
| 50 | + l_running_b64=$(echo "$l_running_result" | grep -c "b64" || true) |
| 51 | + l_running_b32=$(echo "$l_running_result" | grep -c "b32" || true) |
| 52 | + |
| 53 | + if [ "$l_ondisk_b64" -ge 1 ] && [ "$l_ondisk_b32" -ge 1 ] && [ "$l_running_b64" -ge 1 ] && [ "$l_running_b32" -ge 1 ]; then |
| 54 | + ok "File deletion events are correctly configured on disk and running" |
| 55 | + AFD_RULES_OK=1 |
| 56 | + else |
| 57 | + if [ "$l_ondisk_b64" -eq 0 ] || [ "$l_ondisk_b32" -eq 0 ]; then |
| 58 | + crit "File deletion audit rules not found or incomplete in on-disk configuration" |
| 59 | + fi |
| 60 | + if [ "$l_running_b64" -eq 0 ] || [ "$l_running_b32" -eq 0 ]; then |
| 61 | + crit "File deletion audit rules not found or incomplete in running configuration" |
| 62 | + fi |
| 63 | + AFD_RULES_OK=0 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# This function will be called if the script status is on enabled mode |
| 68 | +apply() { |
| 69 | + if [ "$AFD_RULES_OK" -eq 1 ]; then |
| 70 | + ok "File deletion audit rules already correctly configured" |
| 71 | + return |
| 72 | + fi |
| 73 | + |
| 74 | + if [ -z "$AFD_UID_MIN" ]; then |
| 75 | + crit "Unable to determine UID_MIN, cannot apply" |
| 76 | + return |
| 77 | + fi |
| 78 | + |
| 79 | + info "Configuring file deletion audit rules" |
| 80 | + mkdir -p "$AUDIT_RULES_DIR" |
| 81 | + |
| 82 | + # Remove any existing delete rules to avoid duplicates |
| 83 | + if [ -f "$AUDIT_RULES_FILE" ]; then |
| 84 | + sed -i '/\-k delete/d' "$AUDIT_RULES_FILE" |
| 85 | + fi |
| 86 | + |
| 87 | + # Create file with header if it doesn't exist |
| 88 | + if [ ! -f "$AUDIT_RULES_FILE" ]; then |
| 89 | + echo "## File deletion events" >"$AUDIT_RULES_FILE" |
| 90 | + fi |
| 91 | + |
| 92 | + # Add the rules |
| 93 | + { |
| 94 | + echo "-a always,exit -F arch=b64 -S rename,unlink,unlinkat,renameat -F auid>=${AFD_UID_MIN} -F auid!=unset -k delete" |
| 95 | + echo "-a always,exit -F arch=b32 -S rename,unlink,unlinkat,renameat -F auid>=${AFD_UID_MIN} -F auid!=unset -k delete" |
| 96 | + } >>"$AUDIT_RULES_FILE" |
| 97 | + |
| 98 | + # Load the rules |
| 99 | + info "Loading audit rules" |
| 100 | + augenrules --load |
| 101 | + ok "File deletion audit rules configured and loaded" |
| 102 | +} |
| 103 | + |
| 104 | +# This function will check config parameters required |
| 105 | +check_config() { |
| 106 | + : |
| 107 | +} |
| 108 | + |
| 109 | +# Source Root Dir Parameter |
| 110 | +if [ -r /etc/default/cis-hardening ]; then |
| 111 | + # shellcheck source=../../debian/default |
| 112 | + . /etc/default/cis-hardening |
| 113 | +fi |
| 114 | +if [ -z "$CIS_LIB_DIR" ]; then |
| 115 | + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." |
| 116 | + echo "Cannot source CIS_LIB_DIR variable, aborting." |
| 117 | + exit 128 |
| 118 | +fi |
| 119 | + |
| 120 | +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) |
| 121 | +if [ -r "${CIS_LIB_DIR}"/main.sh ]; then |
| 122 | + # shellcheck source=../../lib/main.sh |
| 123 | + . "${CIS_LIB_DIR}"/main.sh |
| 124 | +else |
| 125 | + echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_LIB_DIR in /etc/default/cis-hardening" |
| 126 | + exit 128 |
| 127 | +fi |
0 commit comments