Skip to content

Commit 93ab7eb

Browse files
author
Damien Cavagnini
committed
feat: add "--set-version" option
This feature will allow to chose a specific cis version to run, like debian 11 or debian 12
1 parent 99bc575 commit 93ab7eb

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

bin/hardening.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ BATCH_MODE=''
2929
SUMMARY_JSON=''
3030
ASK_LOGLEVEL=''
3131
ALLOW_UNSUPPORTED_DISTRIBUTION=0
32+
USED_VERSION="default"
3233

3334
usage() {
3435
cat <<EOF
@@ -105,6 +106,13 @@ OPTIONS:
105106
This option sets LOGLEVEL, you can choose : info, warning, error, ok, debug or silent.
106107
Default value is : info
107108
109+
--set-version <version>
110+
This option allows to run the scripts as defined for a specific CIS debian version.
111+
Supported version are the folders listed in the "versions" folder.
112+
examples:
113+
--set-version debian_11
114+
--set-version ovh_legacy
115+
108116
--summary-json
109117
While performing system audit, this option sets LOGLEVEL to silent and
110118
only output a json summary at the end
@@ -163,6 +171,10 @@ while [[ $# -gt 0 ]]; do
163171
ASK_LOGLEVEL=$2
164172
shift
165173
;;
174+
--set-version)
175+
USED_VERSION=$2
176+
shift
177+
;;
166178
--only)
167179
TEST_LIST[${#TEST_LIST[@]}]="$2"
168180
shift
@@ -217,9 +229,20 @@ if [ "$ASK_LOGLEVEL" ]; then LOGLEVEL=$ASK_LOGLEVEL; fi
217229
# shellcheck source=../lib/constants.sh
218230
[ -r "${CIS_LIB_DIR}"/constants.sh ] && . "${CIS_LIB_DIR}"/constants.sh
219231

232+
# ensure the CIS version exists
233+
does_file_exist "$CIS_VERSIONS_DIR/$USED_VERSION"
234+
if [ "$FNRET" -ne 0 ] ; then
235+
echo "$USED_VERSION is not a valid version"
236+
echo "Please use '--set-version' with one of $(ls "$CIS_VERSIONS_DIR" --hide=default -m)"
237+
exit 1
238+
fi
239+
220240
# If we're on a unsupported platform and there is no flag --allow-unsupported-distribution
221241
# print warning, otherwise quit
222242

243+
# update path for the remaining of the script
244+
CIS_CHECKS_DIR="$CIS_VERSIONS_DIR/$USED_VERSION"
245+
223246
if [ "$DISTRIBUTION" != "debian" ]; then
224247
echo "Your distribution has been identified as $DISTRIBUTION which is not debian"
225248
if [ "$ALLOW_UNSUPPORTED_DISTRIBUTION" -eq 0 ]; then

debian/default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CIS_LIB_DIR='/opt/cis-hardening/lib'
55
CIS_CHECKS_DIR="/opt/cis-hardening/bin/hardening"
66
CIS_CONF_DIR='/opt/cis-hardening/etc'
77
CIS_TMP_DIR='/opt/cis-hardening/tmp'
8+
CIS_VERSIONS_DIR='/opt/cis-hardening/versions'

lib/main.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# shellcheck shell=bash
22
# run-shellcheck
33

4+
SCRIPT_FULL_PATH=$(realpath -s "$0")
45
LONG_SCRIPT_NAME=$(basename "$0")
56
SCRIPT_NAME=${LONG_SCRIPT_NAME%.sh}
67
# Variable initialization, to avoid crash
@@ -71,18 +72,40 @@ done
7172
info "Working on $SCRIPT_NAME"
7273
info "[DESCRIPTION] $DESCRIPTION"
7374

75+
# check if the script is a link
76+
# if a file, script is executed from "bin/hardening", create a cfg file (if not already exists)
77+
# if a link, script is executed from "version"/X", create a link, or update it if already exits
78+
if [ -L "${SCRIPT_FULL_PATH}" ] ; then
79+
# script is a link
80+
script_real_path=$(readlink -f "${SCRIPT_FULL_PATH}")
81+
script_real_name=$(basename "$script_real_path")
82+
cfg_file=$(basename -s .sh "$script_real_path").cfg
83+
cfg_link="$SCRIPT_NAME".cfg
84+
else
85+
# script is a file
86+
script_real_name=$LONG_SCRIPT_NAME
87+
cfg_file="$SCRIPT_NAME".cfg
88+
cfg_link=""
89+
fi
90+
7491
# Source specific configuration file
75-
if ! [ -r "${CIS_CONF_DIR}"/conf.d/"$SCRIPT_NAME".cfg ]; then
92+
if ! [ -r "${CIS_CONF_DIR}"/conf.d/"$cfg_file" ]; then
7693
# If it doesn't exist, create it with default values
77-
echo "# Configuration for $SCRIPT_NAME, created from default values on $(date)" >"${CIS_CONF_DIR}"/conf.d/"$SCRIPT_NAME".cfg
94+
echo "# Configuration for $script_real_name, created from default values on $(date)" >"${CIS_CONF_DIR}"/conf.d/"$cfg_file"
7895
# If create_config is a defined function, execute it.
7996
# Otherwise, just disable the test by default.
8097
if type -t create_config | grep -qw function; then
81-
create_config >>"${CIS_CONF_DIR}"/conf.d/"$SCRIPT_NAME".cfg
98+
create_config >>"${CIS_CONF_DIR}"/conf.d/"$cfg_file"
8299
else
83-
echo "status=audit" >>"${CIS_CONF_DIR}"/conf.d/"$SCRIPT_NAME".cfg
100+
echo "status=audit" >>"${CIS_CONF_DIR}"/conf.d/"$cfg_file"
84101
fi
102+
fi
85103

104+
if [ -n "$cfg_link" ] ; then
105+
if [ -f "${CIS_CONF_DIR}"/conf.d/"$cfg_link" ] ; then
106+
rm -f "${CIS_CONF_DIR}"/conf.d/"$cfg_link"
107+
fi
108+
ln -fs "${CIS_CONF_DIR}"/conf.d/"$cfg_file" "${CIS_CONF_DIR}"/conf.d/"$cfg_link"
86109
fi
87110

88111
if [ "$forcedstatus" = "createconfig" ]; then

lib/utils.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ has_sysctl_param_expected_result() {
1111
local SYSCTL_PARAM=$1
1212
local EXP_RESULT=$2
1313

14-
# shellcheck disable=SC2319
1514
if [ "$($SUDO_CMD sysctl "$SYSCTL_PARAM" 2>/dev/null)" = "$SYSCTL_PARAM = $EXP_RESULT" ]; then
1615
FNRET=0
1716
elif [ "$?" = 255 ]; then
@@ -36,7 +35,6 @@ set_sysctl_param() {
3635
local SYSCTL_PARAM=$1
3736
local VALUE=$2
3837
debug "Setting $SYSCTL_PARAM to $VALUE"
39-
# shellcheck disable=SC2319
4038
if [ "$(sysctl -w "$SYSCTL_PARAM"="$VALUE" 2>/dev/null)" = "$SYSCTL_PARAM = $VALUE" ]; then
4139
FNRET=0
4240
elif [ $? = 255 ]; then

versions/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Here, we'll add some folders to represent a specific CIS version to use.
2+
Each folder will contains links to adequat scripts
3+
4+
Ex:
5+
debian12/
6+
1.1.1.1_disable_cramfs.sh ->../../bin/hardening/disable_cramfs.sh
7+
1.1.1.2_disable_freevxfs.sh ->../../bin/hardening/disable_freevxfs.sh
8+
etc.

versions/default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bin/hardening

0 commit comments

Comments
 (0)