#!/bin/bash

#---------------------------------------------------------------------
# Bash script that loops through all the files of a folder
# and extract the strings.
# Created by Alexandre Colucci on 16.01.2017
# https://blog.timac.org/2017/0124-deobfuscating-libmobilegestalt-keys
#---------------------------------------------------------------------


#---------------------------------------------------------------------
# Force expand a wildcard pattern into the list of matching pathnames
#---------------------------------------------------------------------
shopt -s nullglob

#---------------------------------------------------------------------
# Function to print the usage
#---------------------------------------------------------------------
printUsage ()
{
	echo "Usage: ExtractStrings.sh IN_FOLDER_PATH OUT_FILE"
	echo "IN_FOLDER_PATH: Folder to extract strings from"
	echo "OUT_FILE: All the strings found will be saved to this text file"
	echo ""
	echo "Examples:"
	echo "  ExtractStrings.sh /System/Library /tmp/output.txt"
	echo "  ExtractStrings.sh /System /tmp/output.txt"
	echo "  ExtractStrings.sh / /tmp/output.txt"
	echo ""
	echo "Note: run as root in order to avoid permission issues."
	echo ""
}

#---------------------------------------------------------------------
# Check if the script was called with the expected usage
#---------------------------------------------------------------------
PARAMETER_NUMBER=$#
PARAMETER_REQUIRED=2
if [ $PARAMETER_NUMBER != $PARAMETER_REQUIRED ];
then
	printUsage
	exit 1
fi


#---------------------------------------------------------------------
# Get the folder path
#---------------------------------------------------------------------
PATH_TO_CHECK=$1
OUTPUT_TXT_FILE=$2

echo ""
echo "Start time:"
date
echo ""
echo "Extract strings from ${PATH_TO_CHECK}"
echo "Save strings to ${OUTPUT_TXT_FILE}"


#---------------------------------------------------------------------
# Find all the files in all subdirectories and extract the strings
#---------------------------------------------------------------------

find ${PATH_TO_CHECK} -type f -exec strings {} \; >> "${OUTPUT_TXT_FILE}"


#---------------------------------------------------------------------
# Finalizing
#---------------------------------------------------------------------
echo ""
echo "Completed at:"
date
echo ""