Hashing algorithms

encryptioncrypto101 hashingcrypto101 hash_cracking crackthehash c4ptur3th3fl4g corridor agentsudoctf basicpentestingjt netsecchallenge mustacchio chillhack cowboyhacker devvortex cozyhosting file_pkzip hash_dcc hash_dcc2 hash_lm hash_md5 hash_nt

A hashing algorithm is a one-way function taking data and generating a hash/digest. They are mainly used to:

  • πŸ”’ Store passwords
  • βœ’οΈ Ensure authenticity (digital signatures)
  • πŸ₯· Ensure integrity (checksum)
  • 🫧 In algorithms such as in hashing tables

Since a hash function is one-way, it's impossible to know the original data. However, we can use a hash function to verify if some data matches a corresponding hash.

Problems in hashing algorithms

  • Hash collision πŸ‘Ž: two different data generate the same hash. Some well-known vulnerable algorithms are SHA1, MD5...

  • Determinism πŸ›£οΈ: a hash function always generates the same hash given the same data. It allows attackers to precompute hashes for well-known passwords, and perform a rainbow table attack. A salt is a parameter (usually, a random string) added to generate different hashes given the same data. Only calling the hash function with the same salt results in the same output.

➑️ They may use brute force with a dictionary of passwords too.

Generate a hash πŸ”‘

πŸ‘‰ There are many language-specific functions, but you can use these commands for testing.

$ echo -n 'password' | <command> | cut -f1 -d' ' > myhash
  • ➑️ Using openssl
# list formats
$ openssl list --digest-commands
md4 md5 sha1 [...]
# generate
$ echo -n "toto" | openssl dgst -md4
  • ➑️compute and check XXX message digest (gnu)
# if you don't use -n, it won't work
$ echo -n 'toto' | sha1sum
$ echo -n 'toto' | md5sum
$ echo -n 'toto' | sha256sum
$ echo -n 'toto' | sha512sum
  • ➑️ Using mkpasswd
# list formats
$ mkpasswd -m help
bcrypt          bcrypt
sha512crypt     SHA-512
sha256crypt     SHA-256
md5crypt        MD5
nt              NT-Hash
# generate
$ mkpasswd -m sha512crypt toto
# or you can use htpasswd
$ htpasswd -bnBC 10 "" toto | tr -d ':\n' # bcrypt
  • ➑️ Online tools

For instance, decrypt.tools (not many algos), hashes.com (many algos), or tunnelsup.

Find the hash algorithm


Let's say we got some hash, and we want to find which algorithm generated this hash, such as MD5, SHA1...

$ echo -n "some hash here" > myhash
  • ➑️ Historically, we used hash-identifier (0.4k⭐). Obsolete (2011).
$ cat myhash | hash-identifier
  • ➑️ hashID (1.2k ⭐). Not updated since 2015.
$ cat myhash | hashid
# sudo apt install name-that-hash
$ nth -t "some hash here"
$ nth -f myhash
  • ➑️ haiti (0.4k ⭐). Similar to Name-that-hash, different results.
# sudo gem install haiti-hash
$ haiti "some hash here"
  • ➑️ Online tools

For instance, hashes.com or md5hashing.net.

  • ➑️ Manually

Some tools may suggest an incorrect format or not support the format you are looking for. You can still try to do it manually.

Some hashes are starting with well-known formats: $id$salt$hash or hash:salt. Some known values for id are: 1 (crypt, cisco, old Linux), 2/2a/2b/2x/2y (bcrypt, web), 5 (sha256crypt), 6 (sha512crypt, modern Linux).

Apache uses $apr1$hash while WordPress uses $P$hash.

Some hashes can be identified by their length.

πŸ‘‰ You can find a lot of examples on hashcat website.

Hash cracking

Hash cracking usually involves a dictionary with a list of potential passwords a.k.a. wordlist (Rainbow tables may be used for older systems).

⚠️ Hash cracking tools may be able to use your GPU to compute results faster. On a virtual machine, they may be less efficient, as the VM itself takes a lot of resources. Consider using your host.

  • ➑️ Online tools

For instance, crackstation (you can download their wordlist!), MD5Hashing, decrypt.tools, hashkiller.io, or hashes.com.

πŸ‘‰ CTFs usually use the wordlist /usr/share/wordlists/rockyou.txt.

Special cracking cases

Some special cases of using john/hashcat along other tools.

Linux shadow hash cracking

password_attacks linprivesc

To crack the whole shadow file, you may use unshadow:

$ # hash format is $id$salt$hashed
$ unshadow /path/to/passwd /path/to/shadow > hashes
$ john hashes --format=sha512crypt --wordlist=wordlist
$ hashcat -m 1800 -a 0 hashes wordlist

Windows password hash cracking

password_attacks hash_dcc hash_dcc2 hash_lm hash_nt

Modern Windows are using the hash format "NT", also referred to as "NTLM", because "LM" was the previous hash format.

$ john myhash --format=nt --wordlist=wordlist
$ john myhash --format=netntlmv2 --wordlist=wordlist
$ hashcat -a 0 -m 1000 myhash # NTLM
$ hashcat -a 0 -m 3000 myhash # LM

There are also Domain Cached Credentials.

$ hashcat -a 0 -m 1100 hash wordlist # password:username
$ hashcat -a 0 -m 2100 hash wordlist # $DCC2$salt#username#hash

GPG passphrase cracking

networksecurityprotocols linuxstrengthtraining encryptioncrypto101

To crack the passphrase of GPG encrypted files, you must convert the file to a crackable file for john:

$ sudo gpg2john file.pgp > myhash
$ john --format=gpg myhash --wordlist=wordlist 

SSH private key - passphrase cracking

password_attacks encryptioncrypto101 basicpentestingjt mustacchio

SSH may have been configured to use a passphrase-protected ssh keys. You must convert the file to a crackable file for john:

$ ssh2john key > myhash
$ john --format=ssh myhash --wordlist=wordlist

Documents - password cracking


You can crack Office documents and PDF documents using:

$ office2john xxx.docx > myhash
$ john myhash --wordlist=wordlist
$ john myhash --show
$ pdf2john xxx.pdf > myhash
$ john myhash --wordlist=wordlist

RAR password cracking

You must convert the RAR file to a crackable file for john:

$ rar2john hello.rar > myhash
$ john myhash --wordlist=wordlist
$ unrar x hello.rar # enter password

7ZIP password cracking


You must convert the RAR file to a crackable file for john/hashcat:

$ 7z2john test.7z > myhash
$ hashcat -m 11600 [...]

ZIP password cracking

password_attacks crackingpasswordswithhashcat agentsudoctf chillhack file_pkzip

You must convert the ZIP file to a crackable file for john:

$ zip2john hello.zip > myhash
$ john myhash --wordlist=wordlist
$ unzip hello.zip # enter password

➑️ There is also fcrackzip (0.4k ⭐)

πŸ‘‰ The same tool can be used with hashcat, but ensure you only keep the hash. See the references, modes 17200-17230.

Wireless password cracking

crackingpasswordswithhashcat wifihacking101

For Wi-Fi password cracking, use hcxtools to create a crackable file:

$ sudo apt-get install hcxtools
$ hcxpcapngtool xxx.cap -o hash
$ hcxpcapngtool xxx.hccapx -o hash
$ hashcat -m 22000 [...]
$ john --format=wpapsk [...]
$ hcxpcaptool -j hash xxx.cap        # alternative?
$ /usr/lib/hashcat-utils/cap2hccapx.bin in.cap out.hccapx
$ hccapx2john out.hccapx > hash      # only for john

OpenSSL Key Cracking (GZIP example)


GZIP (GNU Zip) is a file compression and decompression utility. The result can then be encrypted using tools such as openssl or gpg.

$ gzip id_rsa # => id_rsa.gz
$ openssl enc -aes-256-cbc -salt -in id_rsa.gz -out xxx.gzip -k SomeKey
$ openssl enc -d -aes-256-cbc -in xxx.gzip -out id_rsa.gz -k SomeKey
$ gzip -d id_rsa.gz # If the key is correct, terminate with code 0

I am not aware of any tool to automate this when openssl was used.

Others converters

  • bitlocker2john: bitlocker protected drive, refer to Bitlocker notes
  • keepass2john: keepass files, refer to KeePass notes

Random Notes

File Checksum

The same functions we use to generate hashes may also be used to generate a checksum for a file. Two identical files have the same checksum. MD5 is not reliable, but often used:

$ md5sum /etc/passwd
$ cat /etc/passwd | md5sum
$ Get-FileHash .\file.txt -Algorithm MD5

πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.