mbr check added

pull/1/head
oh 2015-09-26 11:57:14 +02:00
parent 27e787c366
commit d59eaf7ef8
1 changed files with 73 additions and 54 deletions

View File

@ -1,46 +1,48 @@
#!/bin/sh #!/bin/sh
#Hashes all files in /boot to check them during early boot #Hashes all files in /boot to check them during early boot
#Exit codes: 0 = success, 1 = wrong usage, 2 = not root, 3 = no hasher found, #Exit codes: 0 = success, 1 = checksum mbr mismatch, 2 = checksum /boot mismatch,
#4 = checksum mismatch, 5 = write error #3 = checksum mbr/boot mismatch, 4 = not root, 5 = no hasher found, 6 = wrong usage,
#7 = write error, 8 = dd error
VERSION="0.6" VERSION="0.7"
PATH="/bin:/usr/bin:/sbin:/usr/sbin" PATH="/bin:/usr/bin:/sbin:/usr/sbin"
DIGEST_FILE="/var/lib/hashboot.digest" DIGEST_FILE="/var/lib/hashboot.digest"
LOG_FILE="/tmp/hashboot.log" LOG_FILE="/tmp/hashboot.log"
MBR_FILE="/var/lib/mbr.digest"
MBR_TMP="/tmp/mbr"
BACKUP_FILE="/var/cache/boot-backup.tar.gz" BACKUP_FILE="/var/cache/boot-backup.tar.gz"
HASHER="" HASHER=""
BOOT_MOUNTED="" BOOT_MOUNTED=""
#Umount /boot if we mounted it, exit with given exit code #Umount /boot if we mounted it, exit with given exit code
function die function die
{ {
if [ ! -z ${BOOT_MOUNTED} ] if [ ! -z ${BOOT_MOUNTED} ]
then then
umount /boot umount /boot
fi fi
exit ${1} exit ${1}
} }
function recover function recover
{ {
echo "Restoring files from backup... (type yes or no for each file)" 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 each failed file: ask if it should be recovered from backup
for file in $(cut -d: -f1 ${LOG_FILE}) for file in $(cut -d: -f1 ${LOG_FILE})
do do
tar -xzpPvwf ${BACKUP_FILE} ${file} tar -xzpPvwf ${BACKUP_FILE} ${file}
[ $? != 0 ] && echo "Error restoring ${file} from backup, continuing" [ $? != 0 ] && echo "Error restoring ${file} from backup, continuing"
done done
} }
#If we're not root: exit #If we're not root: exit
if [ ${UID} -ne 0 ] if [ ${UID} -ne 0 ]
then then
echo "You have to be root" >&2 echo "You have to be root" >&2
die 2 die 4
fi fi
#Try different hashers, use the most secure #Try different hashers, use the most secure
@ -51,59 +53,76 @@ test -z ${HASHER} && HASHER=$(/usr/bin/which --skip-dot sha224sum 2> /dev/null)
#It gets insecure below here, but better than nothing? #It gets insecure below here, but better than nothing?
test -z ${HASHER} && HASHER=$(/usr/bin/which --skip-dot sha1sum 2> /dev/null) test -z ${HASHER} && HASHER=$(/usr/bin/which --skip-dot sha1sum 2> /dev/null)
test -z ${HASHER} && HASHER=$(/usr/bin/which --skip-dot md5sum 2> /dev/null) test -z ${HASHER} && HASHER=$(/usr/bin/which --skip-dot md5sum 2> /dev/null)
#If we found no hasher: exit #If we found no hasher: exit
if [ -z ${HASHER} ] if [ -z ${HASHER} ]
then then
echo "No hash calculator found" >&2 echo "No hash calculator found" >&2
die 3 die 5
fi fi
#If /boot is in fstab but not mounted: mount, mark as mounted #If /boot is in fstab but not mounted: mount, mark as mounted
if grep -q '/boot.*noauto' /etc/fstab && ! grep -q /boot /etc/mtab if grep -q '/boot.*noauto' /etc/fstab && ! grep -q /boot /etc/mtab
then then
mount /boot mount /boot
BOOT_MOUNTED=1 BOOT_MOUNTED=1
fi fi
if [ "${1}" == "index" ] if [ "${1}" == "index" ]
then then
#Write header #Write header
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${DIGEST_FILE} echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${DIGEST_FILE}
#Write hashes of all regular files to ${DIGEST_FILE} #Write hashes of all regular files to ${DIGEST_FILE}
find /boot -type f -exec ${HASHER} --binary {} >> ${DIGEST_FILE} + err=$(dd if=/dev/sda of=${MBR_TMP} bs=1M count=1 status=noxfer 2>&1) || die 8 >&2
if [ $? == 0 ] ${HASHER} ${MBR_TMP} > ${MBR_FILE}
then find /boot -type f -exec ${HASHER} --binary {} >> ${DIGEST_FILE} +
echo "List of hashes written to ${DIGEST_FILE}" if [ $? == 0 ]
else then
echo "Error writing ${DIGEST_FILE}" >&2 echo "List of hashes written to ${DIGEST_FILE}"
die 5 else
fi echo "Error writing ${DIGEST_FILE}" >&2
die 7
#Backup of good files fi
tar -czpPf ${BACKUP_FILE} /boot #Backup of good files
if [ $? == 0 ] tar -czpPf ${BACKUP_FILE} /boot ${MBR_TMP} ${MBR_FILE}
then if [ $? == 0 ]
echo "Backup written to ${BACKUP_FILE}" then
else echo "Backup written to ${BACKUP_FILE}"
echo "Error writing ${BACKUP_FILE}" >&2 else
die 5 echo "Error writing ${BACKUP_FILE}" >&2
fi die 7
fi
elif [ "${1}" == "check" ] elif [ "${1}" == "check" ]
then then
if $(${HASHER} --check --warn --quiet --strict ${DIGEST_FILE} > ${LOG_FILE}) COUNTER=0
then err=$(dd if=/dev/sda of=${MBR_TMP} bs=1M count=1 status=noxfer 2>&1) || die 8 >&2
if $(${HASHER} --check --warn --quiet --strict ${MBR_FILE} > ${LOG_FILE})
then
echo "MBR ok"
else
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
COUNTER=$((COUNTER + 1))
fi
if $(${HASHER} --check --warn --quiet --strict ${DIGEST_FILE} >> ${LOG_FILE})
then
echo "/boot io"
die 0 die 0
else
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!" else
die 4 echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
fi COUNTER=$((COUNTER + 2))
die $COUNTER
fi
elif [ "${1}" == "recover" ] elif [ "${1}" == "recover" ]
then then
recover recover
else else
echo "Usage: ${0} index|check|recover" >&2 echo "Usage: ${0} index|check|recover" >&2
die 1 die 6
fi fi
die 0 die 0