#!/bin/bash

################################################################################
#Script Description: decode vbcharacter codes from within a file.
#Script Name: vbfiledecoder.sh
#Script Version: 1.0
#Script Author: Sheldon Johnson
#Script Sponsor: Selex-ES
#Script Release Date: 13/05/2015
#
#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.
#
#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/>.
################################################################################

################################################################################
#Background
#I previously made a vbdecoder which just worked on the strings... 
#that was useful but way too slow.
#anyway, this has been tested against this:
#http://blog.dynamoo.com/2015/05/malware-spam-need-your.html
################################################################################

################################################################################
#Notes
#Will fill this in tomorrow. I am hungry and want to go home.
################################################################################

#create a big array of all the possible values

SYMBOL[${#SYMBOL[@]}]=' '
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='[Backspace]'
SYMBOL[${#SYMBOL[@]}]='[Tab]'
SYMBOL[${#SYMBOL[@]}]='[Line Feed Return]'
SYMBOL[${#SYMBOL[@]}]='[Shift+Enter]'
SYMBOL[${#SYMBOL[@]}]='[Page Break]'
SYMBOL[${#SYMBOL[@]}]='[Carriage Return]'
SYMBOL[${#SYMBOL[@]}]='[Column Break]'
SYMBOL[${#SYMBOL[@]}]='[Shift In]'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='�'
SYMBOL[${#SYMBOL[@]}]='[Space]'
SYMBOL[${#SYMBOL[@]}]='!'
SYMBOL[${#SYMBOL[@]}]='"'
SYMBOL[${#SYMBOL[@]}]='#'
SYMBOL[${#SYMBOL[@]}]='$'
SYMBOL[${#SYMBOL[@]}]='%'
SYMBOL[${#SYMBOL[@]}]='&'
SYMBOL[${#SYMBOL[@]}]="'"
SYMBOL[${#SYMBOL[@]}]='('
SYMBOL[${#SYMBOL[@]}]=')'
SYMBOL[${#SYMBOL[@]}]='*'
SYMBOL[${#SYMBOL[@]}]='+'
SYMBOL[${#SYMBOL[@]}]=','
SYMBOL[${#SYMBOL[@]}]='-'
SYMBOL[${#SYMBOL[@]}]='.'
SYMBOL[${#SYMBOL[@]}]='/'
SYMBOL[${#SYMBOL[@]}]='0'
SYMBOL[${#SYMBOL[@]}]='1'
SYMBOL[${#SYMBOL[@]}]='2'
SYMBOL[${#SYMBOL[@]}]='3'
SYMBOL[${#SYMBOL[@]}]='4'
SYMBOL[${#SYMBOL[@]}]='5'
SYMBOL[${#SYMBOL[@]}]='6'
SYMBOL[${#SYMBOL[@]}]='7'
SYMBOL[${#SYMBOL[@]}]='8'
SYMBOL[${#SYMBOL[@]}]='9'
SYMBOL[${#SYMBOL[@]}]=':'
SYMBOL[${#SYMBOL[@]}]=';'
SYMBOL[${#SYMBOL[@]}]='<'
SYMBOL[${#SYMBOL[@]}]='='
SYMBOL[${#SYMBOL[@]}]='>'
SYMBOL[${#SYMBOL[@]}]='?'
SYMBOL[${#SYMBOL[@]}]='@'
SYMBOL[${#SYMBOL[@]}]='A'
SYMBOL[${#SYMBOL[@]}]='B'
SYMBOL[${#SYMBOL[@]}]='C'
SYMBOL[${#SYMBOL[@]}]='D'
SYMBOL[${#SYMBOL[@]}]='E'
SYMBOL[${#SYMBOL[@]}]='F'
SYMBOL[${#SYMBOL[@]}]='G'
SYMBOL[${#SYMBOL[@]}]='H'
SYMBOL[${#SYMBOL[@]}]='I'
SYMBOL[${#SYMBOL[@]}]='J'
SYMBOL[${#SYMBOL[@]}]='K'
SYMBOL[${#SYMBOL[@]}]='L'
SYMBOL[${#SYMBOL[@]}]='M'
SYMBOL[${#SYMBOL[@]}]='N'
SYMBOL[${#SYMBOL[@]}]='O'
SYMBOL[${#SYMBOL[@]}]='P'
SYMBOL[${#SYMBOL[@]}]='Q'
SYMBOL[${#SYMBOL[@]}]='R'
SYMBOL[${#SYMBOL[@]}]='S'
SYMBOL[${#SYMBOL[@]}]='T'
SYMBOL[${#SYMBOL[@]}]='U'
SYMBOL[${#SYMBOL[@]}]='V'
SYMBOL[${#SYMBOL[@]}]='W'
SYMBOL[${#SYMBOL[@]}]='X'
SYMBOL[${#SYMBOL[@]}]='Y'
SYMBOL[${#SYMBOL[@]}]='Z'
SYMBOL[${#SYMBOL[@]}]='['
SYMBOL[${#SYMBOL[@]}]='\\' #have to cheat on this one or cry when you get to sed.
SYMBOL[${#SYMBOL[@]}]=']'
SYMBOL[${#SYMBOL[@]}]='^'
SYMBOL[${#SYMBOL[@]}]='_'
SYMBOL[${#SYMBOL[@]}]='`'
SYMBOL[${#SYMBOL[@]}]='a'
SYMBOL[${#SYMBOL[@]}]='b'
SYMBOL[${#SYMBOL[@]}]='c'
SYMBOL[${#SYMBOL[@]}]='d'
SYMBOL[${#SYMBOL[@]}]='e'
SYMBOL[${#SYMBOL[@]}]='f'
SYMBOL[${#SYMBOL[@]}]='g'
SYMBOL[${#SYMB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}]='’'
SYMBOL[${#SYMBOL[@]}]='“'
SYMBOL[${#SYMBOL[@]}]='”'
SYMBOL[${#SYMBOL[@]}]='•'
SYMBOL[${#SYMBOL[@]}]='–'
SYMBOL[${#SYMBOL[@]}]='—'
SYMBOL[${#SYMBOL[@]}]='˜'
SYMBOL[${#SYMBOL[@]}]='™'
SYMBOL[${#SYMBOL[@]}]='š'
SYMBOL[${#SYMBOL[@]}]='›'
SYMBOL[${#SYMBOL[@]}]='œ'
SYMBOL[${#SYMBOL[@]}]=''
SYMBOL[${#SYMBOL[@]}]='ž'
SYMBOL[${#SYMBOL[@]}]='Ÿ'
SYMBOL[${#SYMBOL[@]}]=' '
SYMBOL[${#SYMBOL[@]}]='¡'
SYMBOL[${#SYMBOL[@]}]='¢'
SYMBOL[${#SYMBOL[@]}]='£'
SYMBOL[${#SYMBOL[@]}]='¤'
SYMBOL[${#SYMBOL[@]}]='¥'
SYMBOL[${#SYMBOL[@]}]='¦'
SYMBOL[${#SYMBOL[@]}]='§'
SYMBOL[${#SYMBOL[@]}]='¨'
SYMBOL[${#SYMBOL[@]}]='©'
SYMBOL[${#SYMBOL[@]}]='ª'
SYMBOL[${#SYMBOL[@]}]='«'
SYMBOL[${#SYMBOL[@]}]='¬'
SYMBOL[${#SYMBOL[@]}]=''
SYMBOL[${#SYMBOL[@]}]='®'
SYMBOL[${#SYMBOL[@]}]='¯'
SYMBOL[${#SYMBOL[@]}]='°'
SYMBOL[${#SYMBOL[@]}]='±'
SYMBOL[${#SYMBOL[@]}]='²'
SYMBOL[${#SYMBOL[@]}]='³'
SYMBOL[${#SYMBOL[@]}]='´'
SYMBOL[${#SYMBOL[@]}]='µ'
SYMBOL[${#SYMBOL[@]}]='¶'
SYMBOL[${#SYMBOL[@]}]='·'
SYMBOL[${#SYMBOL[@]}]='¸'
SYMBOL[${#SYMBOL[@]}]='¹'
SYMBOL[${#SYMBOL[@]}]='º'
SYMBOL[${#SYMBOL[@]}]='»'
SYMBOL[${#SYMBOL[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YMBOL[${#SYMBOL[@]}]='å'
SYMBOL[${#SYMBOL[@]}]='æ'
SYMBOL[${#SYMBOL[@]}]='ç'
SYMBOL[${#SYMBOL[@]}]='è'
SYMBOL[${#SYMBOL[@]}]='é'
SYMBOL[${#SYMBOL[@]}]='ê'
SYMBOL[${#SYMBOL[@]}]='ë'
SYMBOL[${#SYMBOL[@]}]='ì'
SYMBOL[${#SYMBOL[@]}]='í'
SYMBOL[${#SYMBOL[@]}]='î'
SYMBOL[${#SYMBOL[@]}]='ï'
SYMBOL[${#SYMBOL[@]}]='ð'
SYMBOL[${#SYMBOL[@]}]='ñ'
SYMBOL[${#SYMBOL[@]}]='ò'
SYMBOL[${#SYMBOL[@]}]='ó'
SYMBOL[${#SYMBOL[@]}]='ô'
SYMBOL[${#SYMBOL[@]}]='õ'
SYMBOL[${#SYMBOL[@]}]='ö'
SYMBOL[${#SYMBOL[@]}]='÷'
SYMBOL[${#SYMBOL[@]}]='ø'
SYMBOL[${#SYMBOL[@]}]='ù'
SYMBOL[${#SYMBOL[@]}]='ú'
SYMBOL[${#SYMBOL[@]}]='û'
SYMBOL[${#SYMBOL[@]}]='ü'
SYMBOL[${#SYMBOL[@]}]='ý'
SYMBOL[${#SYMBOL[@]}]='þ'
SYMBOL[${#SYMBOL[@]}]='ÿ'

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

IFS=$'\n'
ORIGINALFILE=$1
DECODEDOUTPUT=$ORIGINALFILE".decoded"
unset IFS

clear

echo "vbfiledecoder.sh Copyright (C) 2015  Sheldon Johnson"
echo "This program comes with ABSOLUTELY NO WARRANTY."
echo "This is free software, and you are welcome to redistribute it"
echo "under certain conditions"
echo -e "\npress any key to accept these terms and continue."
read

clear

ls $ORIGINALFILE &> /dev/null 
FILEEXISTS=$?
if [[ $# == 1 ]] && [[ $FILEEXISTS == 0 ]]; then
	cp $ORIGINALFILE $DECODEDOUTPUT
	x=$(grep -o \ \*[ChrW]\\+\$\*\([0-9]\\\{1,3\\\}\)\ \*\&\* $DECODEDOUTPUT)
	IFS=$'\n'
	for i in $(grep -o \ \*[ChrW]\\+\$\*\([0-9]\\\{1,3\\\}\)\ \*\&\* $DECODEDOUTPUT); do 
		y=$(echo $i | grep -o [0-9]\\\{1,3\\\});
		sed -i "s|$i|${SYMBOL[$y]}|g" $DECODEDOUTPUT
	done
	
	echo -e $GREEN"Decoding complete!"$NC"\n"
	echo "Output saved to: $DECODEDOUTPUT"
	echo "would you like to view the output now (in less)? Y/N"
	read INPUT
	
	if [[ $INPUT == "y" || $INPUT == "Y" ]]; then
		less $DECODEDOUTPUT
	fi
	unset IFS
else
	echo "Usage: vbdecoder.sh 'File_To_Decode'"
	echo "Remember: if your filename has spaces to place quotes around it."
	if [[ $# == 0 ]]; then
		echo -e $RED"You must provide a file to decode"$NC
	elif [[ $# > 1 ]]; then
		echo -e $RED"You must only provide 1 file at a time"$NC
	elif [[ $FILEEXISTS != 0 ]]; then
		echo -e $RED"The file you provided does not exist."$NC
	fi
fi
