hashboot/hashboot

296 lines
10 KiB
Plaintext
Raw Normal View History

2015-09-29 20:13:16 -04:00
#!/bin/bash
2015-06-05 08:04:44 -04:00
#Hashes all files in /boot to check them during early boot
2015-09-26 05:57:14 -04:00
#Exit codes: 0 = success, 1 = checksum mbr mismatch, 2 = checksum /boot mismatch,
#3 = checksum mbr/boot mismatch, 4 = not root, 5 = no hasher found, 6 = wrong usage,
#7 = write error, 8 = dd error, 9 = file not found
2016-09-26 11:37:51 -04:00
#10 = bios mismatch, 11 == mbr&bios mismatch, 12 = files&bios mismatch
#13 = mbr&bios&files mismatch
2019-02-24 09:28:17 -05:00
###############################################################################
# "THE HUG-WARE LICENSE" (Revision 2): #
# teldra <teldra@rotce.de> and tastytea <tastytea@tastytea.de> wrote this. #
2019-03-29 19:35:10 -04:00
# As Long as you retain this notice you can do whatever you want with this. #
2019-02-24 09:28:17 -05:00
# If we meet some day, and you think this is nice, you can give us a hug. #
###############################################################################
2015-10-03 13:54:53 -04:00
2019-06-20 14:38:26 -04:00
# Disable warnings about $?.
# shellcheck disable=SC2181
2019-06-20 14:31:41 -04:00
2019-06-20 11:32:26 -04:00
VERSION="0.9.14"
2015-09-29 18:26:27 -04:00
PATH="/bin:/usr/bin:/sbin:/usr/sbin:${PATH}"
DIGEST_FILE=""
BACKUP_FILE=""
SAVEDIR=""
DIGEST_FILE_TMP="/tmp/hashboot.digesttmp"
2019-02-24 11:29:05 -05:00
LOG_FILE="/var/log/hashboot.log"
2015-10-04 14:31:50 -04:00
MBR_DEVICE="/dev/sda"
MBR_SIZE=1024
2015-09-26 05:57:14 -04:00
MBR_TMP="/tmp/mbr"
2016-09-26 11:37:51 -04:00
BIOS_TMP="/tmp/bios"
2015-06-05 08:04:44 -04:00
HASHER=""
2015-09-29 10:14:21 -04:00
BOOT_MOUNTED=0
2015-09-29 14:17:52 -04:00
CONFIG_FILE="/etc/hashboot.cfg"
2015-10-04 13:22:12 -04:00
COUNTER=0
DD_STATUS="none"
2019-03-26 04:50:47 -04:00
PROGRAMMER=${PROGRAMMER:=internal}
2016-10-04 15:19:20 -04:00
#bitmask:
# 001=mbr
# 010=files
# 100=bios
CKMODES=011
2015-06-05 08:04:44 -04:00
#Umount /boot if we mounted it, exit with given exit code
2015-09-29 20:13:16 -04:00
die ()
2015-06-05 08:04:44 -04:00
{
2015-09-29 10:35:59 -04:00
if [ ${BOOT_MOUNTED} -gt 0 ]
then
2015-09-29 10:14:21 -04:00
umount /boot
fi
2015-06-05 08:04:44 -04:00
2019-02-24 11:09:42 -05:00
# Delete temporary files
rm -f "${DIGEST_FILE_TMP}" "${MBR_TMP}" "${BIOS_TMP}"
2015-09-29 15:05:29 -04:00
[ -z "${2}" ] || echo "${2}" >&2
2019-06-20 10:28:09 -04:00
exit "${1}"
2015-06-05 08:04:44 -04:00
}
write_hashes ()
{
2019-06-20 10:28:09 -04:00
local file="${1}"
#Write header to ${file}
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > "${file}"
2016-10-04 14:49:35 -04:00
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 001)) -ne 0 ]; then
#copy mbr to file
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8
2019-06-20 10:28:09 -04:00
#Write hash of MBR to ${file}
${HASHER} ${MBR_TMP} >> "${file}"
2016-10-04 15:19:20 -04:00
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 010)) -ne 0 ]; then
#Write hashes of all regular files to ${file}
2019-06-20 14:31:41 -04:00
# shellcheck disable=SC2227
find /boot -type f -exec ${HASHER} --binary {} >> "${file}" +
2016-10-04 15:19:20 -04:00
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 100)) -ne 0 ]; then
2019-02-24 07:55:10 -05:00
#read bios to file
2019-03-26 06:06:04 -04:00
flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1
2019-06-20 10:28:09 -04:00
#and write hashes of bios files to ${file}
${HASHER} ${BIOS_TMP} >> "${file}"
2019-02-24 07:55:10 -05:00
2016-10-04 15:19:20 -04:00
fi
}
2016-09-26 11:37:51 -04:00
2016-10-04 15:19:20 -04:00
2015-06-05 08:04:44 -04:00
#If we're not root: exit
2019-02-24 04:39:36 -05:00
if [ ${UID} -ne 0 ]
2015-06-05 08:04:44 -04:00
then
2015-09-29 15:05:29 -04:00
die 4 "You have to be root"
2015-06-05 08:04:44 -04:00
fi
#If /boot is in fstab but not mounted: mount, mark as mounted
2015-10-04 14:35:47 -04:00
if grep -q '/boot' /etc/fstab && ! grep -q /boot /etc/mtab
2015-06-05 08:04:44 -04:00
then
mount /boot
BOOT_MOUNTED=1
2015-06-05 08:04:44 -04:00
fi
# Debian < 8 check
2019-06-20 10:28:09 -04:00
if command -v lsb_release > /dev/null \
&& [ "$(lsb_release -si)" == "Debian" ] \
&& [ "$(lsb_release -sr | cut -d'.' -f1)" -lt 8 ]
then
DD_STATUS="noxfer"
fi
#Look for config file and set ${MBR_DEVICE}.
if [ -f ${CONFIG_FILE} ]
then
2019-06-20 10:28:09 -04:00
# shellcheck source=/dev/null
2015-10-04 18:07:43 -04:00
source ${CONFIG_FILE} || die 9 "Error reading config file"
#compatibility to old cfg format
2019-06-20 10:28:09 -04:00
if [ -n "${BACKUP_FILE}" ]; then
SAVEDIR="/var/lib/hashboot"
echo "SAVEDIR=${SAVEDIR}" >> ${CONFIG_FILE}
mkdir -p ${SAVEDIR}
mv ${BACKUP_FILE} ${SAVEDIR}
mv /var/lib/hashboot.digest ${SAVEDIR}
ln -s "${SAVEDIR}/boot-backup.tar" "${BACKUP_FILE}"
ln -s "${SAVEDIR}/hashboot.digest" "/var/lib/hashboot.digest"
sed -i '/BACKUP_FILE/d' ${CONFIG_FILE}
echo "The backup und the digests have been moved to ${SAVEDIR}"
fi
2019-02-24 08:35:14 -05:00
# here we extrapolate paths from savedir.
DIGEST_FILE="${SAVEDIR}/hashboot.digest"
BACKUP_FILE="${SAVEDIR}/boot-backup.tar"
#If not found, create one and ask for ${MBR_DEVICE}
else
2015-10-04 14:27:14 -04:00
#Create ${CONFIG_FILE} with defaults if noninterctive
2015-10-04 14:44:47 -04:00
if [ -t "0" ]
2015-10-04 14:20:02 -04:00
then
echo -n "Where should backup file and digestfile be stored? [/var/lib/hashboot] "
read -r SAVEDIR
2019-02-24 06:47:03 -05:00
[ -z "${SAVEDIR}" ] && SAVEDIR="/var/lib/hashboot"
2019-02-24 07:55:10 -05:00
echo "#Where the Backup files are stored" > ${CONFIG_FILE}
echo "SAVEDIR=${SAVEDIR}" >> ${CONFIG_FILE}
DIGEST_FILE="${SAVEDIR}/hashboot.digest"
BACKUP_FILE="${SAVEDIR}/boot-backup.tar"
mkdir -p ${SAVEDIR}
2019-02-24 07:55:10 -05:00
2016-10-04 15:19:20 -04:00
echo "What do we check?"
echo "001=mbr"
echo "010=files"
2019-02-24 07:55:10 -05:00
echo "100=core-/libreboot bios"
2016-10-04 15:19:20 -04:00
echo "eg. 101 for mbr and bios: "
2019-06-20 10:28:09 -04:00
read -r CKMODES
2019-02-24 07:55:10 -05:00
echo "#001=mbr,010=files,100=bios" >> ${CONFIG_FILE}
2016-10-04 15:19:20 -04:00
echo "CKMODES=$CKMODES" >> ${CONFIG_FILE}
2019-02-24 07:55:10 -05:00
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 001)) -ne 0 ]; then
2019-02-24 07:55:10 -05:00
echo -n "Which device contains the MBR? [/dev/sda] "
read -r MBR_DEVICE
[ -z "${MBR_DEVICE}" ] && MBR_DEVICE="/dev/sda"
echo "#Device with the MBR on it" >> ${CONFIG_FILE}
echo "MBR_DEVICE=${MBR_DEVICE}" >> ${CONFIG_FILE}
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 100)) -ne 0 ]; then
if ! command -v flashrom > /dev/null; then
2019-02-24 07:55:10 -05:00
echo "You need to have flashrom installed!"
echo "Currently it is not installed, don't reboot"
2019-03-26 04:50:47 -04:00
echo "If you need another programmer than internal"
2019-06-20 14:55:07 -04:00
echo "use the variable PROGRAMMER in ${CONFIG_FILE}!"
2019-02-24 07:55:10 -05:00
fi
2019-03-29 19:35:10 -04:00
fi
2019-02-24 07:55:10 -05:00
2015-10-04 14:47:15 -04:00
else
die 9 "No config file found. Run hashboot interactively to generate one."
2015-10-04 14:20:02 -04:00
fi
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 001)) -ne 0 ]; then
# Find out where the first partition starts and set ${MBR_SIZE} in KiB
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $8}' )
if [ "${sectorsize}" == "=" ] # Older versions of util-linux
then
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $9}' )
fi
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $2}' )
if [ "${startsector}" == "*" ] # If partition is marked as boot, read next field
then
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $3}' )
fi
2019-06-20 10:28:09 -04:00
MBR_SIZE=$((sectorsize * startsector / 1024))
if [ ${?} != 0 ]
then
echo "Something went wrong. Most likely your partition table is corrupt." >&2
die 1 "You have to recover the MBR manually by copying the mbr from ${BACKUP_FILE}"
fi
2016-10-04 15:19:20 -04:00
fi
2015-06-05 08:04:44 -04:00
if [ "${1}" == "index" ]
then
#Try different hashers, use the most secure
HASHER=$(command -v sha512sum)
test -z "${HASHER}" && HASHER=$(command -v sha384sum)
test -z "${HASHER}" && HASHER=$(command -v sha256sum)
test -z "${HASHER}" && HASHER=$(command -v sha224sum)
#If we found no hasher: exit
2015-09-29 20:13:16 -04:00
[ -z "${HASHER}" ] && die 5 "No hash calculator found"
2016-09-26 11:37:51 -04:00
2015-10-04 16:37:44 -04:00
#Exists ${DIGEST_FILE}, if true run do magic, else write ${DIGEST_FILE}
2015-10-04 11:56:36 -04:00
if [ -f ${DIGEST_FILE} ]
2015-10-04 12:00:48 -04:00
then
2015-10-04 12:53:51 -04:00
#Collect mbr and /boot hashes and write to $DIGEST_FILE_TMP
2015-10-04 11:56:36 -04:00
write_hashes $DIGEST_FILE_TMP
2015-10-04 12:53:51 -04:00
#Compare $DIGEST_FILE_TMP against ${DIGEST_FILE} and exit, when fine. Otherwise do magic.
2015-10-04 17:26:21 -04:00
if diff -q --ignore-matching-lines='#hashboot' ${DIGEST_FILE} ${DIGEST_FILE_TMP} ;
then
2015-10-04 11:56:36 -04:00
die 0
else
2016-09-26 11:37:51 -04:00
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '#hashboot' | grep '<' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
do
#delete from tar
2019-06-20 10:28:09 -04:00
tar --delete -v -P -f ${BACKUP_FILE} "${file}"
2015-10-04 16:37:44 -04:00
done
2015-10-04 17:26:21 -04:00
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '#hashboot' | grep '>' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
2015-10-04 16:43:47 -04:00
do
2019-06-20 10:28:09 -04:00
tar -r -v -P -f $BACKUP_FILE "${file}"
2015-10-04 16:37:44 -04:00
done
fi
2015-10-04 18:07:43 -04:00
#nur, wenn das updaten des Backups geklappt hat. *im Hinterkopf behalt*
2015-10-04 17:58:55 -04:00
mv ${DIGEST_FILE_TMP} ${DIGEST_FILE}
2015-10-04 11:56:36 -04:00
else
write_hashes $DIGEST_FILE
2019-02-24 11:20:35 -05:00
INCLUDE_FILES=""
if [ -f "${MBR_TMP}" ]; then
INCLUDE_FILES="${INCLUDE_FILES} ${MBR_TMP}"
fi
if [ -f "${BIOS_TMP}" ]; then
INCLUDE_FILES="${BIOS_TMP}"
fi
tar -cpPf "${BACKUP_FILE}" ${INCLUDE_FILES} /boot ${DIGEST_FILE} || die 7 "Error writing ${BACKUP_FILE}"
echo "Backup written to ${BACKUP_FILE}"
fi
2016-09-26 11:37:51 -04:00
2015-06-05 08:04:44 -04:00
elif [ "${1}" == "check" ]
then
[ -f ${DIGEST_FILE} ] || die 9 "No digestfile"
HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}')
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 001)) != 0 ]; then
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8
grep ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee ${LOG_FILE}
2019-06-20 10:28:09 -04:00
if [ "${PIPESTATUS[2]}" -ne 0 ]
then
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
COUNTER=$((COUNTER + 1))
fi
2016-10-04 15:19:20 -04:00
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 010)) -ne 0 ]; then
grep -v ${MBR_TMP} ${DIGEST_FILE} | grep -v ${BIOS_TMP} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE}
2019-06-20 10:28:09 -04:00
if [ "${PIPESTATUS[2]}" -ne 0 ]
then
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
COUNTER=$((COUNTER + 2))
fi
2016-10-04 15:19:20 -04:00
fi
2019-06-20 10:28:09 -04:00
if [ $((CKMODES & 100)) -ne 0 ]; then
2019-03-26 06:06:04 -04:00
flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1
#if we set an programmer chip in config, find line with hash for bios and compare. if smthg wrong, panic
2019-02-24 07:55:10 -05:00
grep ${BIOS_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE}
2019-06-20 10:28:09 -04:00
if [ "${PIPESTATUS[2]}" -ne 0 ]
2019-02-24 07:55:10 -05:00
then
echo " !! TIME TO PANIK: BIOS WAS MODIFIED !!"
COUNTER=$((COUNTER + 10))
fi
2019-02-24 07:55:10 -05:00
fi
if [ ${COUNTER} -gt 0 ]; then
die ${COUNTER}
2016-10-04 15:19:20 -04:00
fi
elif [ "${1}" == "recover" ]
then
2015-09-29 10:14:21 -04:00
echo "Restoring files from backup... (type yes or no for each file)"
#For each failed file: ask if it should be recovered from backup
2019-06-20 14:31:41 -04:00
# shellcheck disable=2013
2015-09-29 10:14:21 -04:00
for file in $(cut -d: -f1 ${LOG_FILE})
do
2019-06-20 10:28:09 -04:00
tar -xpPvwf ${BACKUP_FILE} "${file}"
[ ${?} != 0 ] && echo "Error restoring ${file} from backup, continuing" >&2
2015-09-29 13:44:01 -04:00
#If the MBR is to be recovered, copy to ${MBR_DEVICE}
if [ "${file}" == ${MBR_TMP} ]
2015-09-29 11:00:37 -04:00
then
2015-09-29 13:44:01 -04:00
cp ${MBR_TMP} ${MBR_DEVICE}
2019-06-20 10:28:09 -04:00
[ ${?} != 0 ] && echo "Error restoring MBR from backup, continuing" >&2
2015-09-29 11:00:37 -04:00
fi
2015-09-29 10:14:21 -04:00
done
2015-06-05 08:04:44 -04:00
else
2015-09-29 15:05:29 -04:00
die 6 "Usage: ${0} index|check|recover"
fi
die 0