#!/bin/bash
#===============================================================================
#
# DIRECTORY:
#   /home/*/.local/share/nautilus/scripts/05a-Windows/
# OR
#   /home/*/.gnome2/nautilus-sctipts/05a-Windows/ (deprecated)
#
# FILE:
#   01-E01-find-installation-and-version
#
# USAGE:
#   Right click on a EWF image (.E01) and
#   choose this nautilus script from the context menu.
#
# OPTIONS:
#   none
#
# DESCRIPTION:
#   Shows/stores partition with windows installation incl. version.
#
# REQUIREMENTS:
#   bash, zenity, awk, sleuthkit, grep and libhivex-bin
#
# BUGS:
#   ---
#
# NOTES:
#   Tested on
#   - Debian 8+
#   - Arch Linux
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COMPANY:
#   (privately)
#
# VERSION:
#   0.9 (beta)
#
# LINK TO THE MOST CURRENT VERSIONS:
#   https://
#
# CREATED:
#   21.03.2020
#
# COPYRIGHT (C):
#   2015-2020 - Patrick Neumann
#
# LICENSE:
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
# WARRANTY:
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# TODO:
#   ----
#
# HISTORY:
#   0.9  - Patrick Neumann - Initial (public) release
#
#===============================================================================

#-------------------------------------------------------------------------------
# Additional supported Distribution(s) (add before Library!).
#-------------------------------------------------------------------------------
SUPPORTED_OSR="arch"

#-------------------------------------------------------------------------------
# Check for library (casualscripter_nautilus-scripts_functions.sh).
#-------------------------------------------------------------------------------
readonly LIBRARY="${0%/*/*}/.casualscripter_nautilus-scripts_functions.sh"
if [ ! -f "${LIBRARY}" ] ; then
  zenity --error \
         --text \
         "ERROR: casualscripter_nautilus-scripts_functions.sh MISSING!"
  exit 1
fi

source "${LIBRARY}"

#-------------------------------------------------------------------------------
# Checks (see library "casualscripter_nautilus-scripts_functions.sh").
#-------------------------------------------------------------------------------
check_dep "${AWK_BIN}" "awk"
check_dep "${FSSTAT_BIN}" "sleuthkit"
check_dep "${GREP_BIN}" "grep"
check_dep "${HIVEXGET_BIN}" "libhivex-bin"
check_dep "${ICAT_BIN}" "sleuthkit"
check_dep "${IFIND_BIN}" "sleuthkit"
check_dep "${MMLS_BIN}" "sleuthkit"

check_ext "${SOURCE}" "[eE]01|dd|DD|raw|RAW|img|IMG"

check_tmp

#-------------------------------------------------------------------------------
# A little bit of configuration before the looping.
#-------------------------------------------------------------------------------
readonly SOFTWARE_HIVE="${TMP}/SOFTWARE.hive"
readonly WINDOWSVERSION="${TMP}/windowsversion.txt"

#-------------------------------------------------------------------------------
# "The" loop...
#-------------------------------------------------------------------------------
if [ ! -f "${WINDOWSVERSION}" ] ; then
  for offset in $( ${MMLS_BIN} -aM "${SOURCE}" \
                   | ${GREP_BIN} --extended-regexp "^[[:digit:]]+:.*" \
                   | ${AWK_BIN} '{ print $3; }' ) ; do

    echo -e -n "Partition (mmls line):\n  " >> "${WINDOWSVERSION}"
    ${MMLS_BIN} -aM "${SOURCE}" \
    | ${AWK_BIN} '$3=="'"${offset}"'" { print $0; }' \
    >> "${WINDOWSVERSION}"

    if ${FSSTAT_BIN} -o "${offset}" "${SOURCE}" > /dev/null 2>&1 ; then
      software="$( ${IFIND_BIN} -o "${offset}" \
                                  -n "/Windows/System32/config/SOFTWARE" \
                                  "${SOURCE}" )"
      if [ "${software}" != "File not found" ] ; then
        if [ ! -f "${SOFTWARE_HIVE}" ] ; then
          if ! ${ICAT_BIN} -o "${offset}" "${SOURCE}" "${software}" > "${SOFTWARE_HIVE}" ; then
            echo "  During the execution of icat an error occurred." >> "${WINDOWSVERSION}"
          fi
        fi

        echo -n "  " >> "${WINDOWSVERSION}"
        ${HIVEXGET_BIN} "${SOFTWARE_HIVE}" \
                          'Microsoft\Windows NT\CurrentVersion' \
                          'ProductName' >> "${WINDOWSVERSION}"
 
        # 32-Bit: "7601.17713.x86fre.win7sp1_gdr.111025-1505"
        # 64-Bit: "7601.18247.amd64fre.win7sp1_gdr.130828-1532"
        if ${HIVEXGET_BIN} "${SOFTWARE_HIVE}" \
                             'Microsoft\Windows NT\CurrentVersion' \
                             'BuildLabEx' \
                             | ${GREP_BIN} --fixed-strings "amd64" > /dev/null 2>&1 ; then
          echo "  64-Bit" >> "${WINDOWSVERSION}"
        else
          echo "  32-Bit" >> "${WINDOWSVERSION}"
        fi

        echo -n "  " >> "${WINDOWSVERSION}"
        ${HIVEXGET_BIN} "${SOFTWARE_HIVE}" \
                          'Microsoft\Windows NT\CurrentVersion' \
                          'CSDVersion' >> "${WINDOWSVERSION}" 2>/dev/null
        echo >> "${WINDOWSVERSION}"
      else
        echo -e "  SOFTWARE hive not found.\n" >> "${WINDOWSVERSION}"
      fi
    else
      echo -e "  ? (The Sleuth Kit does not support the filesystem!)\n" >> "${WINDOWSVERSION}"
    fi
  done | ${ZENITY_BIN} --progress \
                       --title="find_windows_installation_and_version" \
                       --text="Please wait..." \
                       --pulsate
fi

#-------------------------------------------------------------------------------
# Display content of the resultfile "windowsversion.txt".
#-------------------------------------------------------------------------------
display_resultfile "${WINDOWSVERSION}"

exit 0
