#!/bin/bash
#f**k the offset
#Daniele Murrau (http://brainstretching.blogspot.com)

if [ $# -lt 3 ]; then
         {
         echo "Usage: $0 image-file starting-block ending-block"
         exit
         }
fi
FILE=$1
SBLOCK=$2
EBLOCK=$3
LOG=offset.log
echo -e "Started at: $(date) \n\n" >> $LOG
echo "Image: $FILE" >> $LOG
echo -e "Searching in the block range: $SBLOCK-$EBLOCK\n\n" >> $LOG
for ((i = $SBLOCK; i <= $EBLOCK; i++)); do
         typeset -i offset
         offset=$i*512
         echo "Trying offset: $offset"
         mount -t auto -o ro,loop,nodev,noexec,noatime,offset=$offset $FILE pen 2>/dev/null
         a=${?}
         if [ "$a" -eq 0 ]; then
                 {
                 echo "Error code:" $? "Success! Mounted at offset: " $offset
                 echo "Error code:" $? "Success! Mounted at offset: " $offset >> $LOG
                 losetup -a >> $LOG
                 umount pen
                 losetup -d /dev/loop0 2>/dev/null
                 echo -e "\nGoing to search next offset..\n" >> $LOG
                 }
         else
                 {
                 losetup -d /dev/loop0 2>/dev/null
                 }
         fi
done
echo -e "\n\nFinished at: $(date) \n">> $LOG

