Skip to content

Instantly share code, notes, and snippets.

@williamclot
Created January 13, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamclot/36087c52c9df9e9fda18a6f2dcb662db to your computer and use it in GitHub Desktop.
Save williamclot/36087c52c9df9e9fda18a6f2dcb662db to your computer and use it in GitHub Desktop.
Convergent Encryption and brute-force attack
# A simple Common Encryption dictionnary attack using OpenSSL
# ------------------------------------------------------------
# William Clot
# 09/01/2019
# Needs the following folders to work:
# - ./dict/ with the dictionnary files
# - ./data/user1...n with the encrypted files of each user using ./ce.sh
# A few styling colors and styles for output text
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
endl=$(tput sgr0)
echo "${yellow} [-] Starting the brute-force attack${endl}"
total_files=0
print_success()
{
echo "${green}$2 has the song $1 in his library${endl}"
# Moving the decryption file to the corresponding user folder
mv test_decryption "data/${2}/${1}"
total_files=`expr $total_files + 1`
}
decryption_test()
{ # Function to test whether we can decrypt the message using a key
message=$1
key=$2
file_name=$3
user_name=$4
if openssl aes-256-cbc -salt -in "$message" -out "test_decryption" -d -k "$key" 2> /dev/null; then
# Decryption did work
print_success $file_name $user_name
fi
}
# Looping through all files in dictionnary
for dict_file in ./dict/*.mp3; do
# Computing hash of file
hash=$(cat $dict_file | openssl dgst -sha256)
file_name=$(echo $dict_file | grep -oh "[a-zA-Z-]*.mp3")
# Looping through each user
for user in ./data/user*; do
user_name=$(echo "$user" | grep -oh "user[0-9]*")
# Looping through each file of user
for encrypted_file in $user/*.enc; do
decryption_test $encrypted_file $hash $file_name $user_name
done
done
done
rm test_decryption
echo "${yellow} Brute-force attack finished: ${total_files} files decrypted ${endl}"
# Convergent Encryption
#----------------------
# William Clot
# 09/01/2019
if [[ $# -eq 0 ]] ; then
echo 'Usage: ./ce.sh file'
exit 0
fi
hash=$(cat $1 | openssl dgst -sha256)
echo "sha256 hash of $1: $hash"
openssl aes-256-cbc -salt -in $1 -out "$1.enc" -k $hash
echo "file $1 has been encrypted using it's hash as key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment