One of the most useful tools in a hacker’s toolbox is a password cracker. Passwords are everywhere to protect systems, and sometimes, when there is no way around them, one needs to crack them. In today’s blog post, I am going to present the most popular tool to accomplish this task: John the Ripper.
Warning: The methods shown below should only be used against your own files and systems. Attacking passwords is a crime and, depending on the hacking laws in your country, may land you in jail pretty quickly!
How does password cracking work?
The theory behind password cracking is easy to explain. When you login using a password, you enter a number of characters that should be only known to you. The password is converted to a cryptographic hash, a sequence of characters that is calculated using hash functions. These are one-way algorithms that transform a given message into a unique sequence of characters. Good hash functions have five major properties:
- The same message is always transformed to the same hash.
- Changing the message by one bit significantly changes the resulting hash.
- A hash is quick to calculate (but there are exceptions for special cases).
- It is not possible to generate two different messages with the same hash without major efforts.
- It is not possible to extract a message from a hash.
My favourite analogy to explain hashes is the following:
Imagine you have a secret that you do not want others to know. In order to protect it from others, you write your secret on a plate. Then you take the plate and throw it to the ground, at a perfect angle of 92,5 degrees, at full force!
Now look at the shards! This is your hash. It’s nearly impossible to glue together all the pieces to get your secret. But, how can you prove to others that you know the secret?
Easy. Take another plate, write the secret on it, like you did before, take it and throw it again, at the same angle, with the same force, on a free spot. Now compare: Both broken plates should look the same! Now others can verify that, without needing to know your secret.
Now that you know what a hash is, you should be able to understand that there is no way to break rule 5. Except…you can circumvent it, with a bruteforce attack. A bruteforce attack simply means “Try all possible combinations of characters until the hash of a combination matches the password hash“. As you may know, this can take a very long time (1 second if you are very lucky to several decades), if you do not know anything about the actual passwords. There are different ways to shorten this time:
- Restrict the number of characters
- If you know that the user entered a short password, you can focus the attempts on password lengths of 3-5, for example.
- Restrict the type of characters
- If you know that only numbers were used, you can speed up the attack by using only numbers for attempts.
- The user’s password is a common word
- Most unaware users select very common passwords that are commonly found in dictionaries. Therefore, one can perform a dictionary attack where only words from a dictionary file are attempted. This attack is often quicker and very effective and should be attempted first.
What do I need?
There are different types of password attacks: Online and Offline attacks. When performing an online attack, you directly tamper with a login mechanism. A bruteforce software tries to enter possible combinations of a password to the login service. This can happen by automatically filling out password fields in dialogs or directly interacting with a service. aHydra is a password cracker that does that with different protocols and services.
Offline crackers operate, well…offline, so you can crack a password without requiring an Internet connection. In this case, cracking a password really means cracking a hash, as described above. Local user accounts are commonly breached by this method.
To crack a password, you need one or several of these:
- The password’s hash
- A password cracker
- A dictionary file (optional)
How to get the hash
I will be using two examples to show how the tools work. One thing I often do is crack Zip archive passwords, as my younger self thought it was smart to secure my backups without saving or writing up the passwords. Also, I sometimes have to deal with old Windows or Linux systems, where users forgot the passwords.
Where to get a dictionary file
You can search online. I often use SecLists, which is a huge collection of password lists from different online sources. For this post, if you want to follow along, you can use the “10k-most-common.txt” list.
Cracking a Zip file
Assuming that you successfully installed John the Ripper on your system, I will show you how to use it to extract the password of a Zip archive. For this, you can use your own file, or download the following test archive:
You can see that there is a secret image in the archive, but you can not view the contents without entering the correct password.
John comes with a small utility called zip2john. It extracts the password hash into a format that can be fed to the cracker. The invocation is simple:
~# zip2john secret.zip > secret.hash ~# cat secret.hash secret.zip:$zip2$*0*1*0*4fe8556d0264e165*1f4d*10032*ZFILE*secret.zip*0*3d*35bfa4796a94f8abee5b*$/zip2$:::::secret.zip
Now we can start john and point it to the hash, using the wordfile above:
~# john --format=zip --wordlist=10k-most-common.txt secret.hash
This causes the following output:
Created directory: /root/.john Using default input encoding: UTF-8 Loaded 1 password hash (ZIP, WinZip [PBKDF2-SHA1 128/128 AVX 4x]) Press 'q' or Ctrl-C to abort, almost any other key for status . ok XXXXXX (secret.zip) 1g 0:00:00:02 DONE (2019-03-18 13:26) 0.4098g/s 3708p/s 3708c/s 3708C/s orwell..lonnie Use the "--show" option to display all of the cracked passwords reliably Session completed
In this example, XXXXXX is where the password is shown. I will leave this for you to crack.
Cracking a user password on Linux systems
User passwords on Linux systems are usually stored in protected text files, the shadow file. Once an attacker has access to this file, the password hashes can be extracted and fed to a password cracker like John. You can experiment along with your own system.
Show the contents of the shadow file with
You might get a similar output:
root:*:17541:0:99999:7::: daemon:*:17541:0:99999:7::: bin:*:17541:0:99999:7::: postgres:*:17845:0:99999:7::: messagebus:*:17845:0:99999:7::: sshd:*:17845:0:99999:7::: richard:$6$i70qweVN$JEzB7bmOXqqTi228vFBYM207WhF1WaeAG.yGwoFooyphQeawa1ZndZGMxnmPXOF/SsT26NtU1RAicougdcox//:17973:0:99999
You can see a special sequence of characters for the user richard. This is the account we are going to hack. The procedure is the same as above:
john --wordlist=10k-most-common.txt /etc/shadow
If the process is successful (the password is inside the dictionary), you get the following output:
Warning: detected hash type "sha512crypt", but the string is also recognized as "crypt" Use the "--format=crypt" option to force loading these as that type instead Using default input encoding: UTF-8 Loaded 1 password hash (sha512crypt, crypt(3) $6$ [SHA512 128/128 AVX 2x]) Press 'q' or Ctrl-C to abort, almost any other key for status batman (richard) 1g 0:00:00:00 DONE (2019-03-18 13:56) 6.666g/s 426.6p/s 426.6c/s 426.6C/s password..ashley Use the "--show" option to display all of the cracked passwords reliably Session completed
This post focused on dictionary attacks, as they often contain common passwords, and most people do not select safe random passwords. But john also contains a bruteforce mode that can be used instead of –wordlist, –incremental.