From 6ae05f3fa2c54051bf10618235494b43d07dd4d3 Mon Sep 17 00:00:00 2001 From: Thibault Ayanides Date: Mon, 8 Feb 2021 13:54:24 +0100 Subject: [PATCH] Add dealing with debian 11 * ADD: add dockerfile for debian11 * FIX: fix crontab file not found on debian11 blank * Add workflow for debian11 * FIX: fix debian version func to manage debian11 * Add dealing with unsupported version and distro * Add 99.99 check that check if distro version is supported * Use global var for debian major and distro fix #26 --- .github/workflows/functionnal-tests.yml | 7 ++ MANUAL.md | 5 ++ README.md | 6 +- bin/hardening.sh | 51 ++++++++++++++- bin/hardening/5.1.2_crontab_perm_ownership.sh | 4 ++ .../5.1.3_cron_hourly_perm_ownership.sh | 4 ++ .../5.1.4_cron_daily_perm_ownership.sh | 4 ++ .../5.1.5_cron_weekly_perm_ownership.sh | 4 ++ .../5.1.6_cron_monthly_perm_ownership.sh | 4 ++ bin/hardening/5.2.15_ssh_cry_kex.sh | 8 +-- bin/hardening/99.5.2.2_ssh_cry_rekey.sh | 5 +- bin/hardening/99.99_check_distribution.sh | 65 +++++++++++++++++++ debian/cis-hardening.8 | 6 ++ lib/common.sh | 25 ------- lib/constants.sh | 29 +++++++++ lib/main.sh | 5 +- lib/utils.sh | 26 +++++++- tests/docker/Dockerfile.debian11 | 21 ++++++ .../hardening/5.1.2_crontab_perm_ownership.sh | 2 + .../5.1.3_cron_hourly_perm_ownership.sh | 2 + .../5.1.4_cron_daily_perm_ownership.sh | 2 + .../5.1.5_cron_weekly_perm_ownership.sh | 2 + .../5.1.6_cron_monthly_perm_ownership.sh | 2 + tests/hardening/99.99_check_distribution.sh | 16 +++++ 24 files changed, 266 insertions(+), 39 deletions(-) create mode 100755 bin/hardening/99.99_check_distribution.sh create mode 100644 tests/docker/Dockerfile.debian11 create mode 100644 tests/hardening/99.99_check_distribution.sh diff --git a/.github/workflows/functionnal-tests.yml b/.github/workflows/functionnal-tests.yml index 074d6db..5c123de 100644 --- a/.github/workflows/functionnal-tests.yml +++ b/.github/workflows/functionnal-tests.yml @@ -18,3 +18,10 @@ jobs: uses: actions/checkout@v2 - name: Run the tests debian10 run: ./tests/docker_build_and_run_tests.sh debian10 + functionnal-tests-docker-debian11: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v2 + - name: Run the tests debian11 + run: ./tests/docker_build_and_run_tests.sh debian11 diff --git a/MANUAL.md b/MANUAL.md index 15748c7..ee6faeb 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -124,6 +124,11 @@ will create a timestamped backup in this directory. captures all output to print only one line once the check is done, formatted like : OK|KO OK|KO|WARN{subcheck results} [OK|KO|WARN{...}] +`--allow-unsupported-distribution` + Must be specified manually in the command line to allow the run on non compatible + version or distribution. If you want to mute the warning change the LOGLEVEL + in /etc/hardening.cfg + # AUTHORS diff --git a/README.md b/README.md index c3ca2b9..df2c072 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ hardening [INFO] Treating /opt/cis-hardening/bin/hardening/6.2.19_check_duplicat $ git clone https://github.com/ovh/debian-cis.git && cd debian-cis $ cp debian/default /etc/default/cis-hardening $ sed -i "s#CIS_ROOT_DIR=.*#CIS_ROOT_DIR='$(pwd)'#" /etc/default/cis-hardening -$ bin/hardening/1.1_install_updates.sh --audit-all +$ bin/hardening/1.1.1.1_disable_freevxfs.sh --audit-all hardening [INFO] Treating /opt/cis-hardening/bin/hardening/1.1.1.1_disable_freevxfs.sh 1.1.1.1_disable_freevxfs [INFO] Working on 1.1.1.1_disable_freevxfs 1.1.1.1_disable_freevxfs [INFO] [DESCRIPTION] Disable mounting of freevxfs filesystems. @@ -123,6 +123,10 @@ Default value is : info ``--create-config-files-only``: create the config files in etc/conf.d. Must be run as root, before running the audit with user secaudit, to have the rights setup well on the conf files. +``--allow-unsupported-distribution``: must be specified manually in the command line to allow +the run on non compatible version or distribution. If you want to mute the warning change the +LOGLEVEL in /etc/hardening.cfg + ## :computer: Hacking **Getting the source** diff --git a/bin/hardening.sh b/bin/hardening.sh index 283e257..c279a6d 100755 --- a/bin/hardening.sh +++ b/bin/hardening.sh @@ -27,6 +27,7 @@ SET_HARDENING_LEVEL=0 SUDO_MODE='' BATCH_MODE='' ASK_LOGLEVEL='' +ALLOW_UNSUPPORTED_DISTRIBUTION=0 usage() { cat < diff --git a/lib/common.sh b/lib/common.sh index 3a5c3d1..1a3f26f 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -113,31 +113,6 @@ sudo_wrapper() { fi } -# -# detect if container based on cgroup detection -# -is_running_in_container() { - awk -F/ '$2 == "'"$1"'"' /proc/self/cgroup -} - -CONTAINER_TYPE="" -IS_CONTAINER=0 - -if [ "$(is_running_in_container "docker")" != "" ]; then - CONTAINER_TYPE="docker" - IS_CONTAINER=1 -fi -if [ "$(is_running_in_container "lxc")" != "" ]; then - CONTAINER_TYPE="lxc" - IS_CONTAINER=1 -fi -if [ "$(is_running_in_container "kubepods")" != "" ]; then - # shellcheck disable=SC2034 - CONTAINER_TYPE="kubepods" - # shellcheck disable=SC2034 - IS_CONTAINER=1 -fi - # # Math functions # diff --git a/lib/constants.sh b/lib/constants.sh index 3226d82..d9efa64 100644 --- a/lib/constants.sh +++ b/lib/constants.sh @@ -31,3 +31,32 @@ BGREEN='\033[1;32m' # Green BYELLOW='\033[1;33m' # Yellow # shellcheck disable=2034 BWHITE='\033[1;37m' # White + +# Debian version variables + +CONTAINER_TYPE="" +IS_CONTAINER=0 + +if [ "$(is_running_in_container "docker")" != "" ]; then + CONTAINER_TYPE="docker" + IS_CONTAINER=1 +fi +if [ "$(is_running_in_container "lxc")" != "" ]; then + CONTAINER_TYPE="lxc" + IS_CONTAINER=1 +fi +if [ "$(is_running_in_container "kubepods")" != "" ]; then + # shellcheck disable=SC2034 + CONTAINER_TYPE="kubepods" + # shellcheck disable=SC2034 + IS_CONTAINER=1 +fi + +get_distribution + +get_debian_major_version + +# shellcheck disable=SC2034 +SMALLEST_SUPPORTED_DEBIAN_VERSION=9 +# shellcheck disable=SC2034 +HIGHEST_SUPPORTED_DEBIAN_VERSION=10 diff --git a/lib/main.sh b/lib/main.sh index e43f140..ef2fad1 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -10,14 +10,15 @@ BATCH_OUTPUT="" status="" forcedstatus="" SUDO_CMD="" -# shellcheck source=constants.sh -[ -r "$CIS_ROOT_DIR"/lib/constants.sh ] && . "$CIS_ROOT_DIR"/lib/constants.sh + # shellcheck source=../etc/hardening.cfg [ -r "$CIS_ROOT_DIR"/etc/hardening.cfg ] && . "$CIS_ROOT_DIR"/etc/hardening.cfg # shellcheck source=../lib/common.sh [ -r "$CIS_ROOT_DIR"/lib/common.sh ] && . "$CIS_ROOT_DIR"/lib/common.sh # shellcheck source=../lib/utils.sh [ -r "$CIS_ROOT_DIR"/lib/utils.sh ] && . "$CIS_ROOT_DIR"/lib/utils.sh +# shellcheck source=constants.sh +[ -r "$CIS_ROOT_DIR"/lib/constants.sh ] && . "$CIS_ROOT_DIR"/lib/constants.sh # Environment Sanitizing export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' diff --git a/lib/utils.sh b/lib/utils.sh index 3421604..a550fb4 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -538,9 +538,33 @@ get_debian_major_version() { DEB_MAJ_VER="" does_file_exist /etc/debian_version if [ "$FNRET" = 0 ]; then - DEB_MAJ_VER=$(cut -d '.' -f1 /etc/debian_version) + if grep -q "sid" /etc/debian_version; then + DEB_MAJ_VER="sid" + else + DEB_MAJ_VER=$(cut -d '.' -f1 /etc/debian_version) + fi else # shellcheck disable=2034 DEB_MAJ_VER=$(lsb_release -r | cut -f2 | cut -d '.' -f 1) fi } + +# Returns the distribution + +get_distribution() { + DISTRIBUTION="" + if [ -f /etc/os-release ]; then + # shellcheck disable=2034 + DISTRIBUTION=$(grep "^ID=" /etc/os-release | sed 's/ID=//' | tr '[:upper:]' '[:lower:]') + FNRET=0 + else + debug "Distribution not found !" + FNRET=127 + fi +} + +# Detect if container based on cgroup detection + +is_running_in_container() { + awk -F/ '$2 == "'"$1"'"' /proc/self/cgroup +} diff --git a/tests/docker/Dockerfile.debian11 b/tests/docker/Dockerfile.debian11 new file mode 100644 index 0000000..277e4ba --- /dev/null +++ b/tests/docker/Dockerfile.debian11 @@ -0,0 +1,21 @@ +FROM debian:bullseye + +LABEL vendor="OVH" +LABEL project="debian-cis" +LABEL url="https://github.com/ovh/debian-cis" +LABEL description="This image is used to run tests" + +RUN groupadd -g 500 secaudit && useradd -u 500 -g 500 -s /bin/bash secaudit && install -m 700 -o secaudit -g secaudit -d /home/secaudit + +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server sudo syslog-ng net-tools auditd cron + +COPY --chown=500:500 . /opt/debian-cis/ + +COPY debian/default /etc/default/cis-hardening +RUN sed -i 's#cis-hardening#debian-cis#' /etc/default/cis-hardening + +COPY cisharden.sudoers /etc/sudoers.d/secaudit +RUN sed -i 's#cisharden#secaudit#' /etc/sudoers.d/secaudit + + +ENTRYPOINT ["/opt/debian-cis/tests/launch_tests.sh"] diff --git a/tests/hardening/5.1.2_crontab_perm_ownership.sh b/tests/hardening/5.1.2_crontab_perm_ownership.sh index c2cd9d5..15b29a9 100644 --- a/tests/hardening/5.1.2_crontab_perm_ownership.sh +++ b/tests/hardening/5.1.2_crontab_perm_ownership.sh @@ -10,6 +10,8 @@ test_audit() { local test_user="testcrontabduser" local test_file="/etc/crontab" + touch "$test_file" + describe Tests purposely failing chmod 777 "$test_file" register_test retvalshouldbe 1 diff --git a/tests/hardening/5.1.3_cron_hourly_perm_ownership.sh b/tests/hardening/5.1.3_cron_hourly_perm_ownership.sh index 931506f..89959d2 100644 --- a/tests/hardening/5.1.3_cron_hourly_perm_ownership.sh +++ b/tests/hardening/5.1.3_cron_hourly_perm_ownership.sh @@ -10,6 +10,8 @@ test_audit() { local test_user="testcrontabuser" local test_file="/etc/cron.hourly" + touch "$test_file" + describe Tests purposely failing chmod 777 "$test_file" register_test retvalshouldbe 1 diff --git a/tests/hardening/5.1.4_cron_daily_perm_ownership.sh b/tests/hardening/5.1.4_cron_daily_perm_ownership.sh index 5a0481c..f476bff 100644 --- a/tests/hardening/5.1.4_cron_daily_perm_ownership.sh +++ b/tests/hardening/5.1.4_cron_daily_perm_ownership.sh @@ -10,6 +10,8 @@ test_audit() { local test_user="testcrontabuser" local test_file="/etc/cron.daily" + touch "$test_file" + describe Tests purposely failing chmod 777 "$test_file" register_test retvalshouldbe 1 diff --git a/tests/hardening/5.1.5_cron_weekly_perm_ownership.sh b/tests/hardening/5.1.5_cron_weekly_perm_ownership.sh index 3fe2533..cae0433 100644 --- a/tests/hardening/5.1.5_cron_weekly_perm_ownership.sh +++ b/tests/hardening/5.1.5_cron_weekly_perm_ownership.sh @@ -10,6 +10,8 @@ test_audit() { local test_user="testcrontabuser" local test_file="/etc/cron.weekly" + touch "$test_file" + describe Tests purposely failing chmod 777 "$test_file" register_test retvalshouldbe 1 diff --git a/tests/hardening/5.1.6_cron_monthly_perm_ownership.sh b/tests/hardening/5.1.6_cron_monthly_perm_ownership.sh index 9062e34..8eb0488 100644 --- a/tests/hardening/5.1.6_cron_monthly_perm_ownership.sh +++ b/tests/hardening/5.1.6_cron_monthly_perm_ownership.sh @@ -10,6 +10,8 @@ test_audit() { local test_user="testcrontabuser" local test_file="/etc/cron.monthly" + touch "$test_file" + describe Tests purposely failing chmod 777 "$test_file" register_test retvalshouldbe 1 diff --git a/tests/hardening/99.99_check_distribution.sh b/tests/hardening/99.99_check_distribution.sh new file mode 100644 index 0000000..a5243cb --- /dev/null +++ b/tests/hardening/99.99_check_distribution.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 /opt/debian-cis/bin/hardening/"${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. # + ################################################################## +}