From 679df5b9cfb387c81b00841a57686ddc96565717 Mon Sep 17 00:00:00 2001 From: "thibault.dewailly" Date: Tue, 24 Dec 2024 09:13:16 +0000 Subject: [PATCH] feat: add restrict_ptrace_scope --- bin/hardening/1.6.5_restrict_ptrace_scope.sh | 71 +++++++++++++++++++ .../hardening/1.6.5_restrict_ptrace_scope.sh | 16 +++++ 2 files changed, 87 insertions(+) create mode 100755 bin/hardening/1.6.5_restrict_ptrace_scope.sh create mode 100644 tests/hardening/1.6.5_restrict_ptrace_scope.sh diff --git a/bin/hardening/1.6.5_restrict_ptrace_scope.sh b/bin/hardening/1.6.5_restrict_ptrace_scope.sh new file mode 100755 index 0000000..3dc20d0 --- /dev/null +++ b/bin/hardening/1.6.5_restrict_ptrace_scope.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +# run-shellcheck +# +# CIS Debian Hardening +# + +# +# 1.6.2 Ensure ptrace_scope is restricted +# + +set -e # One error, it's over +set -u # One variable unset, it's over + +# shellcheck disable=2034 +HARDENING_LEVEL=2 +# shellcheck disable=2034 +DESCRIPTION="Ensure ptrace_scope is restricted." + +SYSCTL_PARAM='kernel.yama.ptrace_scope' +SYSCTL_EXP_RESULT=2 + +# This function will be called if the script status is on enabled / audit mode +audit() { + has_sysctl_param_expected_result "$SYSCTL_PARAM" "$SYSCTL_EXP_RESULT" + if [ "$FNRET" != 0 ]; then + crit "$SYSCTL_PARAM was not set to $SYSCTL_EXP_RESULT" + elif [ "$FNRET" = 255 ]; then + warn "$SYSCTL_PARAM does not exist -- Typo?" + else + ok "$SYSCTL_PARAM correctly set to $SYSCTL_EXP_RESULT" + fi +} + +# This function will be called if the script status is on enabled mode +apply() { + has_sysctl_param_expected_result "$SYSCTL_PARAM" "$SYSCTL_EXP_RESULT" + if [ "$FNRET" != 0 ]; then + warn "$SYSCTL_PARAM was not set to $SYSCTL_EXP_RESULT -- Fixing" + set_sysctl_param "$SYSCTL_PARAM" "$SYSCTL_EXP_RESULT" + elif [ "$FNRET" = 255 ]; then + warn "$SYSCTL_PARAM does not exist -- Typo?" + else + ok "$SYSCTL_PARAM correctly set to $SYSCTL_EXP_RESULT" + fi +} + +# This function will check config parameters required +check_config() { + : +} + +# Source Root Dir Parameter +if [ -r /etc/default/cis-hardening ]; then + # shellcheck source=../../debian/default + . /etc/default/cis-hardening +fi +if [ -z "$CIS_LIB_DIR" ]; then + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." + echo "Cannot source CIS_LIB_DIR variable, aborting." + exit 128 +fi + +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) +if [ -r "${CIS_LIB_DIR}"/main.sh ]; then + # shellcheck source=../../lib/main.sh + . "${CIS_LIB_DIR}"/main.sh +else + echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_LIB_DIR in /etc/default/cis-hardening" + exit 128 +fi diff --git a/tests/hardening/1.6.5_restrict_ptrace_scope.sh b/tests/hardening/1.6.5_restrict_ptrace_scope.sh new file mode 100644 index 0000000..4ad9ef8 --- /dev/null +++ b/tests/hardening/1.6.5_restrict_ptrace_scope.sh @@ -0,0 +1,16 @@ +# shellcheck shell=bash +# run-shellcheck +test_audit() { + describe Running on blank host + register_test retvalshouldbe 0 + dismiss_count_for_test + # shellcheck disable=2154 + run blank "${CIS_CHECKS_DIR}/${script}.sh" --audit-all + + ################################################################## + # For this test, we only check that it runs properly on a blank # + # host, and we check root/sudo consistency. But, we don't test # + # the apply function because it can't be automated or it is very # + # long to test and not very useful. # + ################################################################## +}