CTH_WordExtractor.sh 7.13 KB
Newer Older
Thireus's avatar
Thireus committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#***************************************************************#
#** This file is part of the Crack That Hash project.         **#
#** CTH_WordExtractor.sh v1.2a                                **#
#**                                                           **#
#** -- Let's call this, the padding technique... ;)           **#
#**                                                           **#
#** ********************************************************* **#
#** --------------- Updated: Dec-23-2012 -------------------- **#
#** ********************************************************* **#
#**                                                           **#
#** Author: Pascal CLEMENT aka Thireus <cth@thireus.com>      **#
#**                                                           **#
#** http://blog.thireus.com                                   **#
#**                                                           **#
#** Crack That Hash,                                          **#
#** Copyright © 2012  Thireus.                                **#
#***************************************************************#
#**PLEASE REFER TO THE README FILE FOR ADDITIONAL INFORMATION!**#
#***************************************************************#

######## CHANGELOG BEGIN ########
#  
# v 1.2a
# - Some bug fixes
#
# v 1.2
# - Some bug fixes
#
# v 1.1
# - Changed wordlist generation to all combinations of word size
# - Added directories
# - Added Stats and Occurency limit
#
# v 1.0
# - Initial Release
#
######## CHANGELOG END ########

######## TODO BEGIN ########
# 
# - SESSIONS
# - INPUT CHECK + DEFAULT VALUE
#
######## TODO END ########

#!/bin/bash

######## PARAMETERS BEGIN ########

POT='john.pot'

DIRECTORY_CTH='CTH'
DIRECTORY_TEMP='.cth'

FILENAME_TEMP='temp'
FILENAME_WORDLISTS='CTH_WORDLIST_'
FILENAME_FINAL='CTH_WORDLIST_FINAL_'

EXTENSIONS='dic'
EXTENSIONS_STAT='stat'

PADDING_BEGIN=0
PADDING_END=10

######## PARAMETERS END ########

######## FUNCTIONS BEGIN ########

die () {
    echo >&2 "$@"
    exit 1
}

######## FUNCTIONS END ########

######## GET PARAMETERS BEGIN ########

echo "WELCOME TO CTH_WordExtractor v1.0!"
echo ""

[ "$#" -eq 2 ] || [ "$#" -eq 3 ] || die "Usage: ./CTH_WordExtractor.sh WINDOW_BEGIN WINDOW_END [RESTORE_SESSION]"
echo $1 | grep -E -q '^[0-9]+$' || die "Numeric argument required for WINDOW_BEGIN, $1 provided."
echo $2 | grep -E -q '^[0-9]+$' || die "Numeric argument required for WINDOW_END, $1 provided."
if [ $1 -gt $2 ]; then
	die "WINDOW_BEGIN must be lower than WINDOW_END."
fi
if [ "$#" -eq 3 ]; then
	echo $3 | grep -E -q '^[0-9]+$' || die "Numeric argument required for RESTORE_SESSION, $1 provided."
fi

WINDOW_BEGIN=$1
WINDOW_END=$2

######## GET PARAMETERS END ########

######## REQUIRED HEADERS BEGIN ########

export LC_ALL='C'

POT=$(pwd)/$POT
FILENAME_TEMP=$(pwd)/$DIRECTORY_CTH/$DIRECTORY_TEMP/$FILENAME_TEMP
FILENAME_WORDLISTS=$(pwd)/$DIRECTORY_CTH/$DIRECTORY_TEMP/$FILENAME_WORDLISTS
FILENAME_FINAL=$(pwd)/$DIRECTORY_CTH/$FILENAME_FINAL

mkdir -p $DIRECTORY_CTH/$DIRECTORY_TEMP
cd $DIRECTORY_CTH/$DIRECTORY_TEMP

echo > $FILENAME_TEMP.$EXTENSIONS
rm $FILENAME_TEMP.$EXTENSIONS
echo > $FILENAME_WORDLISTS.$EXTENSIONS
rm $FILENAME_WORDLISTS*.$EXTENSIONS
echo > $FILENAME_FINAL.$EXTENSIONS
rm $FILENAME_FINAL*.$EXTENSIONS
echo > $FILENAME_WORDLISTS.$EXTENSIONS_STAT
rm $FILENAME_WORDLISTS*.$EXTENSIONS_STAT

######## REQUIRED HEADERS END ########

######## GENERATING SESSION BEGIN ########

CTH_RANDOM_SESSION=1

for (( r=1; r<=10000; r++ ))
do
	CTH_RANDOM_SESSION=$(($CTH_RANDOM_SESSION+$[ ( $RANDOM % 10000 )  + r ]))
done

CTH_RANDOM_SESSION=$(($CTH_RANDOM_SESSION % 100000))

# TODO: VERIFY SESSION NOT EXISTS...

PROTECTED=$CTH_RANDOM_SESSION'_CTHprotected'

######## GENERATING SESSION END ########

######## SCRIPT BEGIN ########

echo "SESSION NUMBER: $CTH_RANDOM_SESSION"
echo "IF THIS SCRIPT IS KILLED, PLEASE USE: 
'rm -r $(pwd)/$DIRECTORY_TEMP/*$PROTECTED $(pwd)/*$PROTECTED' TO CLEAN YOUR DIRECTORY!"
echo "TO RESUME A KILLED SESSION, PLEASE USE: 
'./CTH_WordExtractor.sh $WINDOW_BEGIN $WINDOW_END $CTH_RANDOM_SESSION' (feature coming soon)"
echo ""

# Extract john.pot passwords
echo "[-] EXTRACTING $POT in $FILENAME_TEMP.$EXTENSIONS..." && \
cat $POT | cut -d: -f 2- | sort | uniq > $FILENAME_TEMP.$EXTENSIONS.$PROTECTED && \
\
mv $FILENAME_TEMP.$EXTENSIONS.$PROTECTED $FILENAME_TEMP.$EXTENSIONS && \
echo "[X] $FILENAME_TEMP.$EXTENSIONS CREATED!" && \
\
count_i=0 && \
for (( w=$WINDOW_END; w>=$WINDOW_BEGIN; w-- ))
do
# i = window length
	
	# Revert Window
	i=$w
	count_i=$(($count_i+1))
	
	echo "" && \
	echo "[-] EXTRACTING WINDOW $count_i/$(($WINDOW_END-$WINDOW_BEGIN+1)) for $i char(s) words..."
	
	count_j=0 && \
	for (( p=$PADDING_BEGIN; p<=$PADDING_END; p++ ))
	do
	# j = padding

		j=$p
		
		echo "[-] PADDING $count_j/$(($PADDING_END-$PADDING_BEGIN)) for padding of $j char(s)..."
		
		cat $FILENAME_TEMP.$EXTENSIONS | cut -c$((1+$j))-$(($i+$j)) > $FILENAME_WORDLISTS$((1+$j))-$(($i+$j))_$i.$EXTENSIONS.$PROTECTED && \
		\
		mv $FILENAME_WORDLISTS$((1+$j))-$(($i+$j))_$i.$EXTENSIONS.$PROTECTED $FILENAME_WORDLISTS$((1+$j))-$(($i+$j))_$i.$EXTENSIONS && \
		echo "[X] $FILENAME_WORDLISTS$((1+$j))-$(($i+$j))_$i.$EXTENSIONS CREATED!"
		
		count_j=$(($count_j+1))
	 
	done

##NOTE: Unused ' | sort -nr' by awk, thus will not be used here.
	echo "[-] CONCATENATING all $i chars words..." && \
	cat $FILENAME_WORDLISTS*_$i.$EXTENSIONS | grep -E "^.{$i}$" | sort | uniq -c > $FILENAME_WORDLISTS$i-$i.$EXTENSIONS_STAT.$PROTECTED && \
	rm $FILENAME_WORDLISTS*_$i.$EXTENSIONS

done && \
for (( w=$WINDOW_END; w>=$WINDOW_BEGIN; w-- ))
do
# i = window length

	# Revert Window
	i=$w
	count_i=$(($count_i+1))

	echo "" && \
	echo "[-] READING stats for $i chars words..."

	cat $FILENAME_WORDLISTS$i-$i.$EXTENSIONS_STAT.$PROTECTED | awk '{array[$2]=$1; sum+=$1} END { for (i in array) printf "%-15s %-15d %6.2f%%\n", i, array[i], array[i]/sum*100}' | sort -k2,2 -n | tail -n 100

	echo "[!] PLEASE SELECT AN OCCURENCY LIMIT (default 1 occurency):" && \
	read LIMIT_OCCURENCY && \
	echo "[X] You entered: $LIMIT_OCCURENCY"

	echo "[-] BUILDING wordlist. Please wait..."
	# Concat all FILENAME_WORDLISTS for the same window length and higher than LIMIT_OCCURENCY occurencies
	cat $FILENAME_WORDLISTS$i-$i.$EXTENSIONS_STAT.$PROTECTED | awk '{array[$2]=$1; sum+=$1} END { for (i in array) if (array[i] >= '$LIMIT_OCCURENCY') printf "%s\n", i}' > $FILENAME_FINAL$i-$i.$EXTENSIONS.$PROTECTED && \
	rm $FILENAME_WORDLISTS$i-$i.$EXTENSIONS_STAT.$PROTECTED && \
	\
	mv $FILENAME_FINAL$i-$i.$EXTENSIONS.$PROTECTED $FILENAME_FINAL$i-$i.$EXTENSIONS && \
	echo "[X] $i chars words CONCATENATED in $FILENAME_FINAL$i-$i.$EXTENSIONS!"
	
	# Concat FILENAME_WORDLISTS to FILENAME_FINALs
	for (( s=$i; s>=$WINDOW_BEGIN; s-- ))
	do
		for (( t=$WINDOW_END; t>=$i && t!=$s; t-- ))
		do
			cat $FILENAME_FINAL$i-$i.$EXTENSIONS >> $FILENAME_FINAL$s-$t.$EXTENSIONS.$PROTECTED
		done
	done
   
done && \
\
for (( w=$WINDOW_END; w>=$WINDOW_BEGIN; w-- ))
do
	for (( x=$WINDOW_END; x>=$w && x!=$w; x-- ))
	do
		mv $FILENAME_FINAL$w-$x.$EXTENSIONS.$PROTECTED $FILENAME_FINAL$w-$x.$EXTENSIONS && \
		echo "" && \
		echo "[X] $FILENAME_FINAL$w-$x.$EXTENSIONS CREATED!"
	done
done

echo ""
echo "Bye bye =]"

######## SCRIPT END ########