Advanced Password Cracking: Techniques and Tools for Red Teamers and Pentesters
OverView
Whether you’re a penetration tester, red teamer, CTF player, or cybersecurity enthusiast, you’re likely familiar with the concept of password cracking. It is a vital skill for any offensive security practitioner. In the past, passwords were often stored in plaintext or with weak hashing algorithms like MD5. Back then, it was common to dump a database via SQL injection, feed the hashes into Crackstation, and easily recover the plaintext passwords. Remember those days? Well password hashing and encryption have come a long way since then with algorithms and better practices making the process more challenging. During penetration tests or red team engagements it is common that we may find various hashes which may include MD5, NTLM, SHA-256, Bcrypt, etc.
Note: Encoding is the process of converting a data from one format to another usually for storing or transferring data in proper format. It is not for ensuring protection of data. Encryption is the process of converting a plaintext information into ciphertext using an algorithm and a key. This ciphertext can then be decrypted back using the same key. Hashing is the process of converting data into a fixed size characters/values using a hash function which is irreversble meaning it cannot be converted back to original message. The only way is to verify the hash is to compare it to a new hash of the same data.
Cracking Hashes with Wordlists
Two of the most sought after tools when it comes to password cracking is Hashcat and John The Ripper. As with the recent Rockyou2024.txt which is approximately 160 GB, It can significantly enchance the capability of successfully cracking password hashes. Lets take an example during a penetration test we successfully dumped ntlm hash from one of the workstations of a user via same old sekurlsa::logonpasswords
. We can simply use hashcat and try every single combination of password in the worslist. Hashcat will convert every single password candidate into its ntlm format and compare with our given hash value if a match is found the password is cracked. Below -a
is the attack mode and -m
is the hash mode.
1 | ┌──(alex㉿kali)-[~] |
If the hash is unknown hashcat example hashes has a nice table view of hash mode, hash name and example hashes.
Cracking hashes with wordlists and rules
Organizations often use password combinations of {OrganizationName}{Year}
like Microsoft2024
. In my college the WIFI password as well as all the student workstation password was Islington2024
. Rules are ways of manipluating or extending the base words in a wordlist that are common habits for users or organizations. For example the word Islington
may exist in wordlist we can extend this by replacing with uppercase letters, character replacement eg: S
=> $
, a
=> @
, prepending characters like !Islington
or appending characters like Islington2024
.
1 | ┌──(alex㉿kali)-[~] |
Note: Hashcat ships with lots of rule files in the rules directory that we can use.
We can create our own rules. hashcat-rules Lets imagine a scenario we are on a penetration test for a company astrosoft
. From open source intelligence or previous data breach we found that they might be using characters like @
in place of a
, $
in place of s
, 0
in place of o
and year
appended for most of their passwords. We can simply create a rule file as below.
1 | ┌──(alex㉿kali)-[~] |
Above c
is to capitalize the first letter, sa@
means to substitute a with @, so0
means to substitute o with 0 and append 2024
at the end. 36CFEC3D295BDC66AE9DBD059E498C12
this will be the ntlm hash which I will be cracking with some custom made wordlists as below.
1 | ┌──(alex㉿kali)-[~] |
Using hashcat with the custom generated rules we successfully cracked the hash to retrieve plaintext password as A$tr0$0ft2024
.
1 | ┌──(alex㉿kali)-[~] |
Cracking hashes with wordlists and masks
Masks are subset of bruteforce attacks where we know the position of characterset in pasword. Let’s suppose we have ntlm hash of a user 59FC0F884922B4CE376051134C71E22C
, During reconnaissance phase we found that according to company policy the user accounts passwords is 9 characters in total, should begin with an uppercase character followed by 5 lowercase characters followed by 3 digits. We can use masks attacks in hashcat as below. This attack is usually very fast.
1 | ┌──(alex㉿kali)-[~] |
We can also set custom characterset for example: for the hash 07FCF2503320D74A55DE1E53EF835DB0
we know that the password length will be 7 characters last two characters can be either a digit or a special character. We can create our custom characterset like we create a custom charset 1 which resembles either a digit or a special character.
1 | ┌──(alex㉿kali)-[~] |
In above scenario we knew the password length but what if we didnot know the length of password beforehand. We just know that maximum password length is 12, password starts with uppercase letter and may contain digits or special characters at the end. We can create a custom .hcmask
file with different lengths. eg test.hcmask
1 | ┌──(alex㉿kali)-[~] |
Above, We generated an hcmask
file with different lengths upto total length 12. Suppose our ntlm hash is 1501F896CAE1FD4F0BF6B9593E81DEA3
. We know that the maximum password length is 12 or can be less, initial character is uppercase followed by lowercase letters and digit or special character at end. We can use hashcat with above hcmask file.
1 | ┌──(alex㉿kali)-[~] |
We can also define static string in masks suppose we find out the password includes the company name with digits appended but we donot know the length of digits in thise case we can create a mask as below.
1 | ┌──(alex㉿kali)-[~] |
Hashcat Hybrid attacks
The attack mode 6 and 7 are used for combination of wordlists and masks. For example while bruteforcing ntlm hash we need to append ?d?d?d?d
digits at the end of each word in the wordlist using attack mode 6 we can do so as below.
1 | ┌──(alex㉿kali)-[~] |
Similarly if we want to prepend any character, digits or special characters with wordlists we can use attack mode 7 to do so. Here, we are prepending ?d?d?d?d
digits before each word in the wordlist.
1 | ┌──(alex㉿kali)-[/tmp] |
Converting Password-Protected Files to Hashes for Cracking with John
Often we find files such as zip archive, KeePass kdbx files, PDF files, MS office files, ssh private keys, etherium wallet files, etc which are encrypted and required valid password to access them. *2john suite has collection of scripts which can be used to convert these encrypted files into respective hash format which can then be passed to John The Ripper for cracking. John will automatically identity the hash format and attempt to crack it using our supplied wordlist. Below are some examples of converting these encrypted files to hash formats and cracking them using John the Ripper:
- ZIP Files
1
2zip2john protected.zip > ziphash.txt
john --wordlist=wordlist.txt ziphash.txt - KeePass KDBX Files
1
2keepass2john database.kdbx > keepasshash.txt
john --wordlist=wordlist.txt keepasshash.txt - PDF Files
1
2pdf2john.pl protected.pdf > pdfhash.txt
john --wordlist=wordlist.txt pdfhash.txt - MS Office Files
1
2office2john protected.docx > officehash.txt
john --wordlist=wordlist.txt officehash.txt - SSH Private Key
1
2ssh2john id_rsa > sshhash.txt
john --wordlist=wordlist.txt sshhash.txt - Etherium Wallet Files
1
2ethereum2john wallet.json > ethhash.txt
john --wordlist=wordlist.txt ethhash.txt - Mozilla Firefox Master Password
1
2mozilla2john key3.db > mozillahash.txt
john --wordlist=wordlist.txt mozillahash.txt - LUKS Encrypted Partition
1
2luks2john /dev/sdaX > lukshash.txt
john --wordlist=wordlist.txt lukshash.txt