For this lab we’ll be using GPG, OpenSSL to demonstrate symmetric and asymmetric encryption/decryption and MD5, SHA1 to demonstrate hash functions.
Virtual Machine Needed: Kali
Before starting the lab here are some definitions:
In all symmetric crypto algorithms (also called Secret Key encryption) a secret key is used for both encrypt plaintext and decrypt the ciphertext. The secret key can be a number, a word, or a string of random letter, once applied to a given data (message, text).
We can distinguish two kinds of symmetric cryptographic algorithms: Classic and Modern.
- Some of the classic algorithms are: Cesare, Vigenere, and XOR
- Some of the modern algorithms are: RC4, Data Encryption Standard (DES) and Advanced Encryption Standard (AES), CAST5
The process can be schematized (whatever the algorithm used is) as follow:
When it comes to asymmetric crypto algorithms, two keys are used — one called public key, and the other is called private key. What the public key can encrypt is only decrypted using the private key. In general, a message is encrypted with a public key and only person with the private key can decrypt it.
The high-level process is as follow:
The difference between encryption either symmetric or asymmetric and hashing is the fact that hash functions are not reversible, no private key, passphrase or password is used, and almost all hashing function produce a fixed length output from a given arbitrary length input. The main point of difference is the one-way mapping between the input and the output. It’s theoretically impossible to get a plaintext from a hashed value. The two most commonly used hash algorithms are MD5 and SHA1 with the respectively fixed length output of 32 characters and 40 characters.
Due to its “predictive” nature and to avoid dictionary attacks against hashed passwords or passphrases by using pre-calculated hashes for every value, a salt is random data that is used as an additional input to a one-way function that “hashes” a password or passphrase. In a typical circumstance, a password is concatenated with a salt value before hashing them. The salt in the schemas below is 3ab9
And by contrast to all of this, encoding is not a cryptographic concept; it’s simply putting a sequence of characters into a given format for transmission or storage purpose. UTF, ASCII, UNICODE, and Base64 are examples of encoding systems.
Encryption using Symmetric Key
Create text file on your desktop folder using the command:
root@attackserver:~# cat > /root/Desktop/sample.txt
This is a sample text.
And use Ctrl+D to save an exit:
Execute the following command to encrypt the file sample.txt using a symmetric key. The first time when GPG is run, a .gnupg folder is created. You will be asked to enter a passphrase twice. The passphrase used is “infosec”:
root@attackserver:~# gpg -c /root/Desktop/sample.txt
The option “-c” indicated the GPG to use symmetric keys.
An encrypted file is now created in the same location as the plaintext file with the name sample.txt.gpg to see difference in file, open the plain text using the command cat /root/Desktop/sample.txt then open the encrypted file using the command cat /root/Desktop/sample.txt.gpg:
The encrypted file is unreadable, and GPG 2.0 uses by default CAST5 encryption.
Decryption using Symmetric Key
To decrypt the previously encrypted file run the following command:
gpg -o /root/Desktop/sample_decrypted.txt /root/Desktop/sample.txt.gpg
You will be prompted to enter the passphrase used to encrypt. Once you enter that correctly, “sample_decrypted.txt” file will be created on your Desktop folder:
Use the command cat /root/Desktop/sample_decrypted.txt to view its content:
As explained in the introduction, we need first to generate a public/private key pair to use asymmetric encryption. For this purpose, we will use RSA with OpenSSL to generate a private key (infosec_private_key.pem) with the size 1024 bytes. To generate a private key execute this command:
openssl genrsa -out /root/Desktop/infosec_private_key.pem 1024
The private key is now stored as infosec_private_key.pem on the desktop folder. Next step is to derivate the public key from our generated private key using the following command:
openssl rsa -in /root/Desktop/infosec_private_key.pem -out /root/Desktop/infosec_public_key.pem -outform PEM –pubout
At this point now on the Desktop folder, we have both public and private keys.
To encrypt the previously created sample.txt file, from the terminal window execute the following command:
openssl rsautl -encrypt -inkey /root/Desktop/infosec _public_key.pem -pubin -in /root/Desktop/sample.txt -out /root/Desktop/asymmetric_encrypt.dat
Try to open the asymmetric_encrypt.dat file using cat command:
As seen, asymmetric_encrypt.dat is unreadable and must be decrypted only using the private key we previously generated.
To decrypt the file, from the terminal window execute the following command:
openssl rsautl -decrypt -inkey /root/Desktop/infosec_private_key.pem -in /root/Desktop/asymmetric_encrypt.dat -out /root/Desktop/asymmetric_decrypt.txt
Now try to view the file using cat command:
To get an MD5 hash of a plaintext phrase using your terminal, execute the following command: echo -n “This is a sample text” | md5sum
The value 6029f28561014cd2fccef51253be6dbb (which is 32 characters long) is the MD5 hash equivalent of “This is a sample text” if you input a longer phrase as “This is a sample text, crypto is awesome.”
The output is obviously different, but it remains 32 characters long.
You can try to hash the same phrases using SHA1 algorithm by running the command: echo -n “This is a sample text” | sha1sum
Md5sum and sha1sum can be used to verify file integrity, so to have the md5 and sha1 hash values of the previously sample.txt file we can execute the commands:
echo –n /root/Desktop/sample.txt | md5sum
echo –n /root/Desktop/sample.txt | sha1sum
Even changing a single letter or switching from upper to lower case a single character will produce a different hash. As you can see from SHA2:
echo -n “This is a sample text” | sha256sum
Ethical Hacking Training – Resources (InfoSec)
Now for generating a salted SHA2 hash using the password ‘infosec’ and the salt ‘infosecSalt” we can use the command bellow:
mkpasswd -m sha-256 -S infosecSalt -s <<< infosec
You can type the command mkpasswd -m help to see supported hash functions:
Base64 is one of the most used binary-to-text encoding system that represents binary data in an ASCII string format by translating it into a radix-64 (https://en.wikipedia.org/wiki/Radix) representation.
The sample “This is a sample text” is represented as a byte sequence of 8-bit-padded ASCII characters encoded in MIME’s Base64 scheme (https://en.wikipedia.org/wiki/MIME) as follows:
The command used is:
echo -n This is a sample text | base64
To decode a base64 byte sequence, use the following command:
echo -n VGhpcyBpcyBhIHNhbXBsZSB0ZXh0| base64 –d
No quotation marks are used.