[TryHackMe] Cryptography — John the Ripper

Tanseejou
15 min readNov 15, 2022

--

John the Ripper is one of the most well-known, powerful, and versatile hashes cracking tools.

Hello! Welcome back to my TryHackMe walkthrough write-up! In the last post, we had discuss the Crypto101 room together and has been introduced hashes cracking tool — John the Ripper. Therefore, we will explore John the Ripper tool in this room.

The link to this room is https://tryhackme.com/room/johntheripper0

Some brief introduction about Hash

Hashing is converting a given piece of data of any length into a non-reversible fixed-size bits value via a hashing algorithm. Meaning, no matter what size the input is given, the output of hashing will be end up in a fixed size.

For example, both “polo” (4 characters) and “polomints” (9 characters) after run it through the same MD5 algorithm, the output will be a standard 32 character MD5 hash.
“polo” → b53759f3ce692de7aff1b5779d3964da
“polomints” → 584b6e4f4586e136bc280f27f9c64f3b

Even though hashing is non-reversible, however, this doesn’t mean that cracking the hashes is impossible. For example in this scenario:
we have a hashed version of a password [A] , and we know the hashing algorithm. We can use that hashing algorithm to hash a large number of words (dictionary). Then, compare these hashes to the one [A] we are trying to crack, to see if any of them match. If they do, meaning that we have successfully crack the password. This scenario is called Dictionary Attack.

John the Ripper [John] is the tools that allows us to conduct fast brute-force attacks on a large array of different hash types.

To install John on Parrot / Kali

To check if the machine have John installed or no: john
and if it has, we will see the version number, as shown below:

else, we can use sudo apt install john to install it.

Question 2.1
What is the most popular extended version of John the Ripper?
➤ Jumbo John

Wordlists

The wordlist that we can used to hash and compare for can be found at /usr/share/wordlists directory on Parrot / Kali Linux. Most common password wordlist used is rockyou.txt wordlist.

To unzip the rockyou.txt wordlist from .gz format in Kali Linux,
sudo gzip -d /usr/share/wordlists/rockyou.txt.gz .

Question 3.1
What website was the rockyou.txt wordlist created from a breach on?
➤ rockyou.com

Cracking Basic Hashes

Basis syntax of John command is:
john [options] [path to file]
john ⇒ invoke the John the Ripper program
[path to file] ⇒ the file containing the hash we’re trying to crack (if the file is in the same directory, then we only need to specific the filename)

Automatic Cracking

John has built-in feature to detect what type of hash it’s being given, and then select the appropriate rules and formats to crack it for us. However, sometime is can be unreliable. In the case that we are not able to identify the hash type, then this is a good option. The syntax is:
john --wordlist=[path to wordlist] [path to file]
--wordlist ⇒ specifies using wordlist mode
[path to wordlist] ⇒ the path to the wordlist that we are using
eg: john --wordlist=/usr/share/wordlists/rockyou.txt hashToCrack.txt

Identifying Hashes

Since John’s automatically identifying hash sometime is not reliable, we can use other tools to identify the hash and then set John to use the specific hash format.
Below are some tools for hash identifier:
1. hashes.com
2. hash-identifier → a python tool.
To use it, we can pull the python file from Gitlab using:wget https://gitlab.com/kalilinux/packages/hash-identifier/-/raw/kali/master/hash-id.py
Then, launch it with python3 hash-id.py and enter the hash we’re trying to identify.

Format-Specifying Cracking

Once we have identified the type of the hash, we can use John to crack it by specifying the hash type with the following syntax:
john --format=[format] --wordlist=[path to wordlist] [path to file]
--format ⇒ this is the flag to tell john that we’re giving it a hash of a specific format, and use the following format to crack it.
[format] ⇒ the format that the hash is in
eg: john --format=raw-md-5 --wordlist=/usr/share/wordlists/rockyou.txt hashToCrack.txt

:: Note ::
When you are telling John to use formats, if you’re dealing with a standard hash type, e.g. md5 as in the example above, you have to prefix it withraw- to tell john you're just dealing with a standard hash type, though this doesn't always apply. To check if you need to add the prefix or not, you can list all of John's formats using john --list=formats and either check manually, or grep for your hash type using something like
john --list=formats | grep -iF "md5"

Question 4
i.What type of hash is hash1.txt?
ii.What is the cracked value of hash1.txt?

Method 1:
➤ We can use online tool Hashes.com to find out the type of hash and crack it.

Method 2:
➤ Use the hash-identifier tool
➤ pull the python file from Gitlab: wget https://gitlab.com/kalilinux/packages/hash-identifier/-/raw/kali/master/hash-id.py
➤ launch it with python3 hash-id.py
➤ then enter the hash value.

To crack it using Ripper the John tool:
➤ echo the hash to a file first
echo “2e728dd31fb5949bc39cac5a9f066498” > text1.txt
➤ Then, specify the hash type in the John command,
john --format=raw-md5 --wordlist=/usr/share/wordlists/rockyou.txt /root/text1.txt [remember to write raw- ]
➤The value of the hash is return.

iii. What type of hash is hash2.txt?
iv. What is the cracked value of hash2.txt?
Hashes.com only able to provide the hash type, thus, we need to use John to crack the hash after get the hash type.
➤ specify the hash type in the John command,
john --format=raw-SHA1 --wordlist=/usr/share/wordlists/rockyou.txt /root/text2.txt

v. What type of hash is hash3.txt?
vi. What is the cracked value of hash3.txt?
➤ get the hash type
➤ specify the hash type in the John command,
john --format=raw-SHA256 --wordlist=/usr/share/wordlists/rockyou.txt /root/text3.txt

vii. What type of hash is hash4.txt?
viii. What is the cracked value of hash4.txt?
➤ get the hash type
➤ specify the hash type in the John command,
john --format=Whirlpool --wordlist=/usr/share/wordlists/rockyou.txt /root/text4.txt

Cracking Windows Authentication Hashes

Authentication hashes are the hashed versions of passwords that are stored by OS, it is sometimes possible to crack them using the brute-force methods that we’re using.

NTHash / NTLM
NThash/NTLM is the hash format that modern Windows OS machines will store user and service passwords in. We can acquire NTHash/NTML hashes by dumping the SAM database on a Windows machine, by using a tool like Mimikatz or from the Active Directory database: NTDS.dit. We may not have to crack the hash to continue privilege escalation as we can often conduct a “pass the hash” attack instead, but sometimes hash cracking is a viable option if there is a weak password policy.

Question 5
i. What do we need to set the “format” flag to, in order to crack this?
➤ Using online tool — Hashes.com to get the hash type. The NTLM value for the format is NT

ii. What is the cracked value of this password?
➤ echo the hash into a text file
➤ use John to crack the password
john --format=raw-md-5 --wordlist=/usr/share/wordlists/rockyou.txt hashToCrack.txt

Cracking Hashes from /etc/shadow

The /etc/shadow file is on the Linux machine where password hashes are stored. It also stored other information such as the date of the last password change and password expiration information. It contains one entry per line for each user or user account of the system.

This file is usually only accessible by the root user. Thus, in order to get our hands on the hashes, we must have sufficient privileges. However, if we do have the sufficient privileges, there is a chance that we will be able to crack some of the hashes.

Unshadowing

John can be very particular about the formats, for this reason, in order to crack /etc/shadow passwords, we must combine it with the /etc/passwd file in order for John to understand that data it’s being given. To do this, we use a tool built into the John suite of tools — unshadow. The basic syntax of unshadow is :
unshadow [path to passwd] [path to shadow]
unshadow ⇒ invokes the unshadow tool
[path to passwd] ⇒ the file that contain the copy of the /etc/passwd file we’ve taken from the target machine.
[path to shadow] ⇒ the file that contain the copy of the /etc/shadow file we’ve taken from the target machine.
eg: unshadow local_passwd local_shadow > unshadowed.txt

when using unshadow, we can either use the entire /etc/passwd and /etc/shadow file; or we can use the relevant line from each file, eg:

FILE 1 — local_passwd
Contains the /etc/passwd line for the root user: root:x:0:0::/root:/bin/bash

FILE 2 — local_shadow
Contains the /etc/shadow line for the root user: root:$6$2nwjN454g.dv4HN/$m9Z/r2xVfweYVkrr.v5Ft8Ws3/YYksfNwq96UL1FX0OJjY1L6l.DS3KEVsZ9rOVLB/ldTeEL/OIhJZ4GMFMGA0:18576::::::

Cracking

Now we can feed the output file (unshadowed.txt) to John. We should not need to specify a mode here as we have made the input specifically for John, however, in some cases, we will need to specify the format as we have done previously using: --format=sha512crypt

john --wordlist=/usr/share/wordlists/rockyou.txt --format=sha512crypt unshadowed.txt

Question 6
i. What is the root password?
➤ copy the provided /etc/passwd line and /etc/shadow line in “etchashes.txt” into different file.

➤ unshadow the two files
unshadow /root/local_passwd /root/local_shadow > unshadowed.txt

➤ then, use John to crack the unshadowed.txt file. However, we get the “No password hashes loaded” response back.

➤ after several check, I found out that the /etc/shadow value I echo into local_shadow file is not correct. This is because the /etc/shadow value contain dollar [ $ ] sign, and Kali Linux treat the dollar [ $ ] sign as the variable. Thus, I nano to edit the content of local_shadow file with the correct value.

➤ unshadow both file again. Then try to crack it using John, and the flag return to us!

Single Crack Mode

John has another mode — Single Crack Mode. In this mode, John use only the information provided in the username, to try and work out possible passwords heuristically, by slightly changing the letters and numbers contained within the username.

Word Mangling

example: username:Markus, then the possible passwords could be:
- Markus1, Markus2, Markus3 …
- MArkus, MARkus, MARKus…
- Markus!, Markus$, Markus*…

John building its own dictionary based on the information that is has been given, and uses “mangling rules” to generate a wordlist based off what relevant factors for the target we’re trying to crack. ⇒ This is exploiting how poor passwords can be based off information about the username, or the service they’re logging into.

GECOS

John’s implementation of word mangling also features compatibility with the Gecos fields of the UNIX operating system, and other UNIX-like operating systems such as Linux . So what are Gecos? Remember in the last task where we were looking at the entries of both /etc/shadow and /etc/passwd? Well if you look closely You can see that each field is seperated by a colon [ : ]. Each one of the fields that these records are split into are called Gecos fields. John can take information stored in those records, such as full name and home directory name to add in to the wordlist it generates when cracking /etc/shadow hashes with single crack mode.

Using Single Crack Mode

The syntax for single crack mode: eg username: Mike
john --single --format=[format] [path to file]
--single ⇒ let John know that we want to use single hash cracking mode
eg: john --single --format=raw-sha256 hashes.txt

When we’re using the single cracking mode, we need to change the file format by prepending the hash with username that the hash belongs to, so that John understand what data to create a wordlist from, eg:
From: 1efee03cdcb96d90ad48ccc7b8666033
Prepending username: mike:1efee03cdcb96d90ad48ccc7b8666033

Question 7
i. What is Joker’s password?
➤ Prepending the hash value with the username, and save it into a file

➤ Find out the hash type

➤ Then, use John Single Cracking Mode to crack the password (noticed that we no need to provide wordlists for John)

Custom Rules

We can set our own rules in which John will use to dynamically create password wordlists. This is especially useful when we know more information about the password structure of whatever our target is.

Common Custom Rules

Most of the organizations will required a certain level of password complexity (combination of capital letter, number, symbols) to try and combat dictionary attacks. eg: if the user create a password polopassword, and he will get a complain like the password need to contain capital letter, symbol, number. Thus, in most case, many users will change the password to something like: Polopassword1! , to meet the requirement.

This kind of pattern [capital letter — number — followed by symbol at the end] is a memorable pattern that people will use and reuse when they create password. This pattern can let us exploit password complexity predictability. Attacker can exploit the fact that the likely position of these added elements to create dynamic passwords from the wordlists.

To create Custom Rules

Custom rules are defined in the john.conf file, usually located in /etc/john/john.conf (on the THM attackbox, it is locate at /opt/john/john.conf).

To get the full view of the types of modifier we can use, and examples of rule implementation, we can check here.

[List.Rules:THMRules] ⇒ used to define the name of our rule, and will use this to call our custom rules as a John argument.
we can use a regex style pattern match to define where in the word will be modified.
Az ⇒ takes the word and append it with the characters we define
A0 ⇒ takes the word and prepends it with the characters we define
c ⇒ capitalizes the character positionally

Then, define what characters should be appended, prepended or otherwise included, we do this by adding character sets in square bracket [ ] in the order they should be used. These directly follow the modifier patterns inside of double quotes “ ”
[0-9] ⇒ will include numbers 0-9
[0] ⇒ will include only the number 0
[A-z] ⇒ will include both upper and lowercase
[A-Z] ⇒ will include only uppercase letters
[a-z] ⇒ will include only lowercase letters
[a] ⇒ will include only a
[!£$%@] ⇒ will include the symbols !£$%@

Putting all these together in order to generate a wordlist from the rules that would match the example password Polopassword1! (assuming the word polopassword was in our wordlist) we would create a rule entry that looks like this:
[List.Rules:PoloPassword] cAz"[0-9] [!£$%@]"

This mean::
capitalize the first letter ⇒ c
append to the end of the word ⇒ Az
a number in range 0-9 ⇒ [0-9]
followed by a symbol that is one of ⇒ [!£$%@]

Using Custom Rules

We would then call this custom rule as a John argument using the --rule=[rule] flag eg:
john --wordlist=[path to wordlist] --rule=PoloPassword [path to file]

Question 8
i. What do custom rules allow us to exploit?
➤ Password complexity predictability

ii. What rule would we use to add all capital letters to the end of the word?
➤ Az”[A-Z]”

iii. What flag would we use to call a custom rule called “THMRules”
➤--rule=THMRules

Cracking Password Protected Zip Files

We can use John to crack the password on password protected Zip files. We are using a separate part of the John suite tool to convert the zip file into a format that John will understand.

Zip2John

We’re going to use zip2john tool to convert the zip file into a hash format that John is able to understand, and hopefully crack it.
zip2john [options] [zip file] > [output file]
[options] ⇒ allows us to pass specific checksum options to zip2john (should not often be necessary)
[zip file] The path to the zip file you wish to get the hash of
> ⇒ this is the output director
[output file] ⇒ this is the file that will store the output form
eg: zip2john zipfile.zip > zip_hash.txt

Cracking

Then, take the output file from zip2john, and feed it directly into John as we have made the input specifically for it.
john --wordlist=/usr/share/wordlists/rockyou.txt zip_hash.txt

Question 9
i. What is the password for the secure.zip file?
➤ using zip2john tool to convert the zip file into a hash format that is understand by John

sudo gzip -d /usr/share/wordlists/rockyou.txt.gz to upzip rockyou.txt.gz in Kali Linux
➤ then, crack the hash using John, and the password is returned in the response.

ii. What is the contents of the flag inside the zip file?
➤ After getting the password, keyin in the prompt box when opening the file.

Cracking a Password Protected RAR Archive

rar archives are compressed files created by the Winrar archive manager. Just like the zip files they compress a wide variety of folders and files.

Rar2John

We are going to use rar2john tool to convert the rar file into a hash format that John will be able to understand. The syntax is:
rar2john [rar file] > [output file]
rar2john ⇒ invokes the rar2john tool
[rar file] ⇒ the path to the rar file you wish to get the hash of
> ⇒ this it the output director
[output file] ⇒ this is the file that will store the output from
eg: rar2john rarfile.rar > rar_hash.txt

Cracking

The syntax to crack the output rar file:
john --wordlist=/usr/share/wordlists/rockyou.txt rar_hash.txt

Question 10
i. What is the password for the secure.rar file?
ii. What is the contents of the flag inside the zip file?
➤ using rar2john tool to convert the rar file into a hash format that is understand by John.

➤ Then, crack the hash using John, and the password is returned in the response. Key in the password to open the rar file.

Cracking SSH Keys with John

Using John to crack the SSH private key password of id_rsa files. Unless configured otherwise, we authenticate our SSH login using a password. However, we can configure key-based authentication, which lets us use our private key, id_rsa, as an authentication key to login to a remote machine over SSH. However, doing so will often required a password — here we will be using John to crack this password to allow authentication over SSH using the key.

SSH2John

ssh2john converts the id_rsa private key that we use to login to the SSH session into hash format that john can work with. [if do not have ssh2john installed, can use ssh2john.py that locate in the /opt/john/ssh2john.py, and need to replace command ssh2john with python3 /opt/john/ssh2john.py or on Kali, python /usr/share/john/ssh2john.py]

ssh2john [id_rsa private key file] > [output file]
ssh2john ⇒ invokes the ssh2john tool
[id_rsa private key file] ⇒ the path to the id_rsa file
[output file] ⇒ this is the file that will store the output
eg: ssh2john id_rsa > id_rsa_hash.txt

Cracking

john --wordlist=/usr/share/wordlists/rockyou.txt id_rsa_hash.txt

Question 11
i. What is the SSH private key password?
➤ using ssh2john tool to convert the file into a hash format that is understand by John. Then, crack the password using John.

Pheww.. we have come to end of this room. Thanks for reading until here! This room has definitely providing a good introduction to John the Ripper tool. Besides, I have a better understanding by completing the tasks provided. Therefore, it is so worth to subscribe and access this room!

In the next post, we will continue explore the next part of cryptography, which is Encryption — Crypto101 room. See you =)

--

--

Tanseejou

cybersecurity enthusiast | cybersecurity researcher