forked from tastytea/hashboot
284 lines
9.9 KiB
Bash
Executable File
284 lines
9.9 KiB
Bash
Executable File
#!/bin/bash
|
|
#Hashes all files in /boot to check them during early boot
|
|
#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
|
|
#10 = bios mismatch, 11 == mbr&bios mismatch, 12 = files&bios mismatch
|
|
#13 = mbr&bios&files mismatch
|
|
###############################################################################
|
|
# "THE HUG-WARE LICENSE" (Revision 2): #
|
|
# teldra <teldra@rotce.de> and tastytea <tastytea@tastytea.de> wrote this. #
|
|
# As Long as you retain this notice you can do whatever you want with this. #
|
|
# If we meet some day, and you think this is nice, you can give us a hug. #
|
|
###############################################################################
|
|
|
|
VERSION="0.9.9"
|
|
PATH="/bin:/usr/bin:/sbin:/usr/sbin:${PATH}"
|
|
DIGEST_FILE=""
|
|
BACKUP_FILE=""
|
|
SAVEDIR=""
|
|
DIGEST_FILE_TMP="/tmp/hashboot.digesttmp"
|
|
LOG_FILE="/tmp/hashboot.log"
|
|
MBR_DEVICE="/dev/sda"
|
|
MBR_SIZE=1024
|
|
MBR_TMP="/tmp/mbr"
|
|
BIOS_TMP="/tmp/bios"
|
|
HASHER=""
|
|
BOOT_MOUNTED=0
|
|
CONFIG_FILE="/etc/hashboot.cfg"
|
|
COUNTER=0
|
|
DD_STATUS="none"
|
|
#bitmask:
|
|
# 001=mbr
|
|
# 010=files
|
|
# 100=bios
|
|
CKMODES=011
|
|
|
|
#Umount /boot if we mounted it, exit with given exit code
|
|
die ()
|
|
{
|
|
if [ ${BOOT_MOUNTED} -gt 0 ]
|
|
then
|
|
umount /boot
|
|
fi
|
|
|
|
# Delete temporary files
|
|
rm -f "${DIGEST_FILE_TMP}" "${MBR_TMP}" "${BIOS_TMP}"
|
|
|
|
[ -z "${2}" ] || echo "${2}" >&2
|
|
exit ${1}
|
|
}
|
|
|
|
write_hashes ()
|
|
{
|
|
#Write header to ${1}
|
|
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${1}
|
|
|
|
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
|
|
#Write hash of MBR to ${1}
|
|
${HASHER} ${MBR_TMP} >> ${1}
|
|
fi
|
|
if [ $((${CKMODES} & 010)) -ne 0 ]; then
|
|
#Write hashes of all regular files to ${1}
|
|
find /boot -type f -exec ${HASHER} --binary {} >> ${1} +
|
|
fi
|
|
if [ $((${CKMODES} & 100)) -ne 0 ]; then
|
|
#read bios to file
|
|
flashrom --programmer internal -r ${BIOS_TMP} > /dev/null 2>&1
|
|
#and write hashes of bios files to ${1}
|
|
${HASHER} ${BIOS_TMP} >> ${1}
|
|
|
|
fi
|
|
}
|
|
|
|
|
|
#If we're not root: exit
|
|
if [ ${UID} -ne 0 ]
|
|
then
|
|
die 4 "You have to be root"
|
|
fi
|
|
|
|
#If /boot is in fstab but not mounted: mount, mark as mounted
|
|
if grep -q '/boot' /etc/fstab && ! grep -q /boot /etc/mtab
|
|
then
|
|
mount /boot
|
|
BOOT_MOUNTED=1
|
|
fi
|
|
|
|
# Debian < 8 check
|
|
if which lsb_release > /dev/null 2>&1 && [ "$(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
|
|
source ${CONFIG_FILE} || die 9 "Error reading config file"
|
|
#compatibility to old cfg format
|
|
if [ ! -z "${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
|
|
# 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
|
|
#Create ${CONFIG_FILE} with defaults if noninterctive
|
|
if [ -t "0" ]
|
|
then
|
|
echo -n "Where should backup file and digestfile be stored? [/var/lib/hashboot] "
|
|
read -r SAVEDIR
|
|
[ -z "${SAVEDIR}" ] && SAVEDIR="/var/lib/hashboot"
|
|
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}
|
|
|
|
echo "What do we check?"
|
|
echo "001=mbr"
|
|
echo "010=files"
|
|
echo "100=core-/libreboot bios"
|
|
echo "eg. 101 for mbr and bios: "
|
|
read CKMODES
|
|
echo "#001=mbr,010=files,100=bios" >> ${CONFIG_FILE}
|
|
echo "CKMODES=$CKMODES" >> ${CONFIG_FILE}
|
|
|
|
if [ $((${CKMODES} & 001)) -ne 0 ]; then
|
|
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
|
|
|
|
if [ $((${CKMODES} & 100)) -ne 0 ]; then
|
|
if ! which flashrom; then
|
|
echo "You need to have flashrom installed!"
|
|
echo "Currently it is not installed, don't reboot"
|
|
fi
|
|
fi
|
|
|
|
else
|
|
die 9 "No config file found. Run hashboot interactively to generate one."
|
|
fi
|
|
fi
|
|
|
|
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
|
|
|
|
MBR_SIZE=$(expr ${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
|
|
fi
|
|
|
|
if [ "${1}" == "index" ]
|
|
then
|
|
#Try different hashers, use the most secure
|
|
HASHER=$(/usr/bin/which sha512sum 2> /dev/null)
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha384sum 2> /dev/null)
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha256sum 2> /dev/null)
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha224sum 2> /dev/null)
|
|
#If we found no hasher: exit
|
|
[ -z "${HASHER}" ] && die 5 "No hash calculator found"
|
|
|
|
#Exists ${DIGEST_FILE}, if true run do magic, else write ${DIGEST_FILE}
|
|
if [ -f ${DIGEST_FILE} ]
|
|
then
|
|
|
|
#Collect mbr and /boot hashes and write to $DIGEST_FILE_TMP
|
|
write_hashes $DIGEST_FILE_TMP
|
|
|
|
#Compare $DIGEST_FILE_TMP against ${DIGEST_FILE} and exit, when fine. Otherwise do magic.
|
|
if diff -q --ignore-matching-lines='#hashboot' ${DIGEST_FILE} ${DIGEST_FILE_TMP} ;
|
|
then
|
|
die 0
|
|
else
|
|
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '#hashboot' | grep '<' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
|
|
do
|
|
#delete from tar
|
|
tar --delete -v -P -f $BACKUP_FILE $file
|
|
done
|
|
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '#hashboot' | grep '>' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
|
|
do
|
|
tar -r -v -P -f $BACKUP_FILE $file
|
|
done
|
|
fi
|
|
#nur, wenn das updaten des Backups geklappt hat. *im Hinterkopf behalt*
|
|
mv ${DIGEST_FILE_TMP} ${DIGEST_FILE}
|
|
else
|
|
write_hashes $DIGEST_FILE
|
|
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
|
|
|
|
elif [ "${1}" == "check" ]
|
|
then
|
|
[ -f ${DIGEST_FILE} ] || die 9 "No digestfile"
|
|
HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}')
|
|
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}
|
|
if [ ${PIPESTATUS[2]} -ne 0 ]
|
|
then
|
|
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
|
|
COUNTER=$((COUNTER + 1))
|
|
fi
|
|
fi
|
|
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}
|
|
if [ ${PIPESTATUS[2]} -ne 0 ]
|
|
then
|
|
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
|
|
COUNTER=$((COUNTER + 2))
|
|
fi
|
|
fi
|
|
if [ $((${CKMODES} & 100)) -ne 0 ]; then
|
|
flashrom --programmer internal -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
|
|
grep ${BIOS_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE}
|
|
if [ ${PIPESTATUS[2]} -ne 0 ]
|
|
then
|
|
echo " !! TIME TO PANIK: BIOS WAS MODIFIED !!"
|
|
COUNTER=$((COUNTER + 10))
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ${COUNTER} -gt 0 ]; then
|
|
die ${COUNTER}
|
|
fi
|
|
elif [ "${1}" == "recover" ]
|
|
then
|
|
echo "Restoring files from backup... (type yes or no for each file)"
|
|
|
|
#For each failed file: ask if it should be recovered from backup
|
|
for file in $(cut -d: -f1 ${LOG_FILE})
|
|
do
|
|
tar -xpPvwf ${BACKUP_FILE} ${file}
|
|
[ $? != 0 ] && echo "Error restoring ${file} from backup, continuing" >&2
|
|
#If the MBR is to be recovered, copy to ${MBR_DEVICE}
|
|
if [ "${file}" == ${MBR_TMP} ]
|
|
then
|
|
cp ${MBR_TMP} ${MBR_DEVICE}
|
|
[ $? != 0 ] && echo "Error restoring MBR from backup, continuing" >&2
|
|
fi
|
|
done
|
|
else
|
|
die 6 "Usage: ${0} index|check|recover"
|
|
fi
|
|
|
|
die 0
|