2014-08-26 17:33:55 +02:00
#!/bin/sh
#################################################################################
#
# Lynis
# ------------------
#
2016-03-13 16:00:39 +01:00
# Copyright 2007-2013, Michael Boelen
2020-03-20 14:50:25 +01:00
# Copyright 2007-2020, CISOfy
2016-03-13 16:00:39 +01:00
#
# Website : https://cisofy.com
# Blog : http://linux-audit.com
# GitHub : https://github.com/CISOfy/lynis
2014-08-26 17:33:55 +02:00
#
# Lynis comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
#################################################################################
#
# Cryptography
#
#################################################################################
#
InsertSection "Cryptography"
#
#################################################################################
#
# Test : CRYP-7902
# Description : check for expired SSL certificates
2019-07-16 13:20:30 +02:00
if [ -n "${OPENSSLBINARY}" ]; then PREQS_MET="YES"; else PREQS_MET="NO"; fi
2016-07-24 17:22:00 +02:00
Register --test-no CRYP-7902 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check expire date of SSL certificates"
2014-08-26 17:33:55 +02:00
if [ ${SKIPTEST} -eq 0 ]; then
2017-09-06 12:56:32 +02:00
COUNT_EXPIRED=0
2016-09-08 21:04:02 +02:00
COUNT_TOTAL=0
2014-08-26 17:33:55 +02:00
FOUNDPROBLEM=0
2019-07-08 21:20:45 +02:00
SKIP=0
2019-07-08 15:08:56 +02:00
sSSL_PATHS=$(echo ${SSL_CERTIFICATE_PATHS} | ${SEDBINARY} 's/:space:/__space__/g' | ${SEDBINARY} 's/:/ /g')
2019-07-26 15:34:02 +02:00
sSSL_PATHS=$(echo ${sSSL_PATHS} | ${SEDBINARY} 's/^ //' | ${SORTBINARY} -u)
2016-09-08 21:04:02 +02:00
LogText "Paths to scan: ${sSSL_PATHS}"
2016-04-13 19:49:30 +02:00
2019-07-26 15:34:02 +02:00
IGNORE_PATHS_PRINT=$(echo ${SSL_CERTIFICATE_PATHS_TO_IGNORE} | ${SEDBINARY} 's/:/, /g' | ${SEDBINARY} 's/__space__/ /g' | ${SEDBINARY} 's/^ //' | ${SORTBINARY} -u)
2019-07-08 15:08:56 +02:00
LogText "Paths to ignore: ${IGNORE_PATHS_PRINT}"
2016-09-08 21:04:02 +02:00
for DIR in ${sSSL_PATHS}; do
COUNT_DIR=0
if [ -d ${DIR} ]; then
FileIsReadable ${DIR}
2014-09-09 14:49:37 +02:00
if [ ${CANREAD} -eq 1 ]; then
2019-07-08 21:20:45 +02:00
LASTSUBDIR=""
2016-09-08 21:04:02 +02:00
LogText "Result: found directory ${DIR}"
2017-09-06 12:56:32 +02:00
# Search for certificate files
2019-07-08 15:08:56 +02:00
FILES=$(${FINDBINARY} ${DIR} -type f 2> /dev/null | ${EGREPBINARY} ".crt$|.pem$|^cert" | ${SORTBINARY} | ${SEDBINARY} 's/ /__space__/g')
2017-09-06 12:56:32 +02:00
for FILE in ${FILES}; do
2019-07-08 15:08:56 +02:00
FILE=$(echo ${FILE} | ${SEDBINARY} 's/__space__/ /g')
# See if we need to skip this path
2019-07-08 21:20:45 +02:00
SUBDIR=$(echo ${FILE} | ${AWKBINARY} -F/ 'sub(FS $NF,x)' | ${SEDBINARY} 's/__space__/ /g')
# If we discover a new directory, do evaluation
#Debug "File : ${FILE}"
#Debug "Lastdir: ${LASTSUBDIR}"
#Debug "Curdir : ${SUBDIR}"
if [ ! "${SUBDIR}" = "${LASTSUBDIR}" ]; then
SKIP=0
# Now check if this path is on the to-be-ignored list
for D in ${SSL_CERTIFICATE_PATHS_TO_IGNORE}; do
if Equals "${D}" "${SUBDIR}"; then
SKIP=1
LogText "Result: skipping directory (${SUBDIR}) as it is on ignore list"
fi
done
fi
2019-07-08 15:08:56 +02:00
if [ ${SKIP} -eq 0 ]; then
2019-07-08 21:20:45 +02:00
#Debug "Testing ${FILE} in path: $SUBDIR"
2019-07-08 15:08:56 +02:00
COUNT_DIR=$((COUNT_DIR + 1))
FileIsReadable "${FILE}"
if [ ${CANREAD} -eq 1 ]; then
# Only check the files that are not installed by a package
if ! FileInstalledByPackage "${FILE}"; then
OUTPUT=$(${GREPBINARY} -q 'BEGIN CERT' "${FILE}")
2018-03-05 11:32:23 +01:00
if [ $? -eq 0 ]; then
2019-07-08 15:08:56 +02:00
LogText "Result: file is a certificate file"
FIND=$(${OPENSSLBINARY} x509 -noout -in "${FILE}" -enddate 2> /dev/null | ${GREPBINARY} "^notAfter")
if [ $? -eq 0 ]; then
# Check certificate where 'end date' has been expired
FIND=$(${OPENSSLBINARY} x509 -noout -checkend 0 -in "${FILE}" -enddate 2> /dev/null)
EXIT_CODE=$?
CERT_CN=$(${OPENSSLBINARY} x509 -noout -subject -in "${FILE}" 2> /dev/null | ${SEDBINARY} -e 's/^subject.*CN=\([a-zA-Z0-9\.\-\*]*\).*$/\1/')
CERT_NOTAFTER=$(${OPENSSLBINARY} x509 -noout -enddate -in "${FILE}" 2> /dev/null | ${AWKBINARY} -F= '{if ($1=="notAfter") { print $2 }}')
Report "certificate[]=${FILE}|${EXIT_CODE}|cn:${CERT_CN};notafter:${CERT_NOTAFTER};|"
if [ ${EXIT_CODE} -eq 0 ]; then
LogText "Result: certificate ${FILE} seems to be correct and still valid"
else
FOUNDPROBLEM=1
COUNT_EXPIRED=$((COUNT_EXPIRED + 1))
LogText "Result: certificate ${FILE} has been expired"
fi
2018-03-05 11:32:23 +01:00
else
2019-07-08 15:08:56 +02:00
LogText "Result: skipping tests for this file (${FILE}) as it is most likely not a certificate (is it a key file?)"
2018-03-05 11:32:23 +01:00
fi
2017-03-12 16:35:50 +01:00
else
2019-07-08 15:08:56 +02:00
LogText "Result: skipping test for this file (${FILE}) as we could not find 'BEGIN CERT'"
2017-03-12 16:35:50 +01:00
fi
2016-09-08 21:04:02 +02:00
fi
2019-07-08 15:08:56 +02:00
else
LogText "Result: can not read file ${FILE} (no permission)"
2014-09-09 14:49:37 +02:00
fi
2017-08-29 14:32:42 +02:00
fi
2019-07-08 21:20:45 +02:00
LASTSUBDIR="${SUBDIR}"
2017-08-29 14:32:42 +02:00
done
2017-09-06 12:56:32 +02:00
COUNT_TOTAL=$((COUNT_TOTAL + COUNT_DIR))
LogText "Result: found ${COUNT_DIR} certificates in ${DIR}"
2016-09-08 21:04:02 +02:00
else
LogText "Result: can not read path ${DIR} (no permission)"
2014-09-09 14:49:37 +02:00
fi
2016-09-08 21:04:02 +02:00
else
LogText "Result: SSL path ${DIR} does not exist"
2014-08-26 17:33:55 +02:00
fi
done
2016-09-08 21:04:02 +02:00
Report "certificates=${COUNT_TOTAL}"
LogText "Result: found a total of ${COUNT_TOTAL} certificates"
2014-08-26 17:33:55 +02:00
if [ ${FOUNDPROBLEM} -eq 0 ]; then
2017-09-06 12:56:32 +02:00
Display --indent 2 --text "- Checking for expired SSL certificates [${COUNT_EXPIRED}/${COUNT_TOTAL}]" --result "${STATUS_NONE}" --color GREEN
2016-09-08 21:04:02 +02:00
else
2017-09-06 12:56:32 +02:00
Display --indent 2 --text "- Checking for expired SSL certificates [${COUNT_EXPIRED}/${COUNT_TOTAL}]" --result "${STATUS_FOUND}" --color RED
2019-12-18 12:17:46 +01:00
ReportSuggestion "${TEST_NO}" "Check available certificates for expiration"
2014-08-26 17:33:55 +02:00
fi
fi
2019-07-18 01:18:12 +02:00
2014-08-26 17:33:55 +02:00
#
#################################################################################
2019-07-09 10:33:51 +02:00
#
# Test : CRYP-7930
2019-07-18 01:18:12 +02:00
# Description : Determine if system uses LUKS block device encryption
2019-08-21 13:50:01 +02:00
Register --test-no CRYP-7930 --os Linux --weight L --network NO --root-only YES --category security --description "Determine if system uses LUKS block device encryption"
2019-07-09 10:33:51 +02:00
if [ ${SKIPTEST} -eq 0 ]; then
2019-08-21 13:50:01 +02:00
CRYPTTABFILE="${ROOTDIR}etc/crypttab"
FOUND=0
# cryptsetup only works as root
2019-10-19 00:34:25 +02:00
if [ -n "${LSBLKBINARY}" ] && [ -n "${CRYPTSETUPBINARY}" ] && [ ${FORENSICS_MODE} -eq 0 ]; then
for BLOCK_DEV in $(${LSBLKBINARY} --noheadings --list -o NAME 2> /dev/null | cut -d' ' -f1); do
if ${CRYPTSETUPBINARY} isLuks $(${FINDBINARY} /dev/ -name "${BLOCK_DEV}" 2> /dev/null) 2> /dev/null; then
2019-08-21 13:50:01 +02:00
LogText "Result: Found LUKS encrypted block device: ${BLOCK_DEV}"
Report "encryption[]=luks,block_device,${BLOCK_DEV}"
FOUND=$((FOUND +1))
else
LogText "Result: block device ${BLOCK_DEV} is not LUKS encrypted"
fi
done
unset BLOCK_DEV
2019-08-29 10:39:43 +02:00
2019-08-21 13:50:01 +02:00
# This will enable us to do a test for forensics or when crypsetup/lsblk are not available
elif [ -f ${CRYPTTABFILE} ]; then
LogText "Result: crypttab (${CRYPTTABFILE}) exists"
DATA=$(${GREPBINARY} "^[a-z]" ${CRYPTTABFILE} | ${TRBINARY} -cd '[:alnum:]_\-=,\n\t ' | ${SEDBINARY} 's/[[:blank:]]/__space__/g')
for LINE in ${DATA}; do
LINE=$(echo ${LINE} | ${SEDBINARY} 's/__space__/ /g')
if ContainsString "luks," "${LINE}"; then
PARTITION=$(echo ${LINE} | ${AWKBINARY} '{print $1}' | ${AWKBINARY} -F_ '{print $1}')
LogText "Result: Found LUKS encryption on partition ${PARTITION}"
Report "encryption[]=luks,partition,${PARTITION}"
FOUND=$((FOUND +1))
fi
done
unset DATA LINE PARTITION
fi
if [ ${FOUND} -gt 0 ]; then
Display --indent 2 --text "- Found ${FOUND} LUKS encrypted block devices." --result OK --color WHITE
fi
unset FOUND
2019-07-09 10:33:51 +02:00
fi
#
#################################################################################
2019-08-29 10:39:43 +02:00
#
# Test : CRYP-8002
# Description : Gather available kernel entropy
Register --test-no CRYP-8002 --os Linux --weight L --network NO --root-only NO --category security --description "Gather available kernel entropy"
if [ ${SKIPTEST} -eq 0 ]; then
if [ -f ${ROOTDIR}proc/sys/kernel/random/entropy_avail ]; then
DATA=$(${AWKBINARY} '$1 ~ /^[0-9]+$/ {print $1}' ${ROOTDIR}proc/sys/kernel/random/entropy_avail)
if [ -n "${DATA}" ]; then
LogText "Result: found kernel entropy value of ${DATA}"
Report "kernel_entropy=${DATA}"
if [ ${DATA} -gt 200 ]; then
Display --indent 2 --text "- Kernel entropy is sufficient" --result "${STATUS_YES}" --color GREEN
else
Display --indent 2 --text "- Kernel entropy is sufficient" --result "${STATUS_NO}" --color YELLOW
# TODO - enable suggestion when information on website is available
fi
fi
fi
fi
#
#################################################################################
2020-01-06 20:22:00 +01:00
#
# Test : CRYP-8004
2020-03-21 13:12:06 +01:00
# Description : Test for presence of hardware random number generators
Register --test-no CRYP-8004 --os Linux --weight L --network NO --root-only NO --category security --description "Presence of hardware random number generators"
2020-01-06 20:22:00 +01:00
if [ ${SKIPTEST} -eq 0 ]; then
2020-02-15 14:09:56 +01:00
LogText "Test: looking for ${ROOTDIR}sys/class/misc/hw_random/rng_current"
if [ -f "${ROOTDIR}sys/class/misc/hw_random/rng_current" ]; then
DATA=$(${HEADBINARY} --lines=1 ${ROOTDIR}sys/class/misc/hw_random/rng_current | ${TRBINARY} -d '[[:cntrl:]]')
2020-01-06 20:22:00 +01:00
if [ "${DATA}" != "none" ]; then
2020-02-15 14:09:56 +01:00
LogText "Result: positive match, found RNG: ${DATA}"
2020-01-06 20:22:00 +01:00
if IsRunning "rngd"; then
Display --indent 2 --text "- HW RNG & rngd" --result "${STATUS_YES}" --color GREEN
LogText "Result: rngd is running"
else
Display --indent 2 --text "- HW RNG & rngd" --result "${STATUS_NO}" --color YELLOW
2020-02-15 14:09:56 +01:00
# TODO - enable suggestion when website has listing for this control
# ReportSuggestion "${TEST_NO}" "Utilize hardware random number generation by running rngd"
2020-01-06 20:22:00 +01:00
fi
else
2020-02-15 14:09:56 +01:00
Display --indent 2 --text "- HW RNG & rngd" --result "${STATUS_NO}" --color YELLOW
2020-01-06 20:22:00 +01:00
LogText "Result: no HW RNG available"
fi
else
Display --indent 2 --text "- HW RNG & rngd" --result "${STATUS_NO}" --color RED
LogText "Result: could not find ${ROOTDIR}sys/class/misc/hw_random/rng_current"
fi
fi
#
#################################################################################
2020-03-21 13:12:06 +01:00
#
# Test : CRYP-8005
# Description : Test for presence of software pseudo random number generators
Register --test-no CRYP-8005 --os Linux --weight L --network NO --root-only NO --category security --description "Presence of software pseudo random number generators"
if [ ${SKIPTEST} -eq 0 ]; then
LogText "Test: looking for software pseudo random number generators"
FOUND=""
for SERVICE in audio-entropyd havegd jitterentropy-rngd; do
if IsRunning "${SERVICE}" --full; then
FOUND="${FOUND} ${SERVICE}"
fi
done
if [ -z "${FOUND}" ]; then
Display --indent 2 --text "- SW prng" --result "${STATUS_NO}" --color YELLOW
ReportSuggestion "${TEST_NO}" "Utilize software pseudo random number generators"
else
Display --indent 2 --text "- SW prng" --result "${STATUS_YES}" --color GREEN
LogText "Result: found ${FOUND} running"
fi
fi
#
#################################################################################
2019-08-29 10:39:43 +02:00
#
2019-08-21 13:50:01 +02:00
2016-04-28 12:31:57 +02:00
WaitForKeyPress
2014-08-26 17:33:55 +02:00
#
#================================================================================
2016-03-13 16:03:46 +01:00
# Lynis - Security Auditing and System Hardening for Linux and UNIX - https://cisofy.com