33.2 Hashing, Salts and Password Storage
A hash function turns any input into a fixed-length fingerprint. Good hashes are one-way, and a tiny change in input gives a completely different output (the "avalanche effect").
| Algorithm | Output | Status |
|---|---|---|
| MD5 | 128-bit | Broken – collisions are practical; never for security |
| SHA-1 | 160-bit | Broken for collisions; being retired |
| SHA-256 / SHA-3 | 256-bit | Safe for integrity and signatures |
| bcrypt, scrypt, Argon2, PBKDF2 | Variable | Designed for passwords – slow on purpose |
Uses: checking downloads (compare the published SHA-256 of the Kali ISO), file integrity (Wazuh FIM, Chapter 31), digital forensics (hash evidence before and after, Chapter 28), and password storage.
Passwords need special care. A plain fast hash like SHA-256 lets Hashcat try billions of guesses per second (Chapter 22). So we use:
- a salt – a random value added per user, so the same password gives different hashes and rainbow tables fail;
- a slow algorithm (bcrypt/Argon2) with a work factor, so each guess is expensive.
sha256sum kali-linux-installer-amd64.iso # compare with the value on kali.org
php -r 'echo password_hash("Pune123", PASSWORD_BCRYPT), PHP_EOL;' # different every run (salt)
openssl passwd -6 -salt Xy12 Pune123 # SHA-512 crypt, like /etc/shadow ($6$)
A HMAC (hash + secret key) proves both integrity and that the sender knew the key – used in API signatures and JWT tokens (HS256).
Ravindra Bagale's Tip
Students store passwords with md5($password) or sha256($password) and ask, "It's a hash, so it's safe, right?" No! A fast hash is easy to crack. In PHP always use password_hash() and password_verify() – you get salt and bcrypt automatically.
Ravindra Bagale's Tip – मराठी
Students md5($password) किंवा sha256($password) ने password save करतात आणि विचारतात "hash आहे ना, मग safe?" नाही! Fast hash crack करायला सोपा. PHP मध्ये नेहमी password_hash() आणि password_verify() वापरा – salt आणि bcrypt आपोआप येतात.
Ravindra Bagale's Tip – हिंदी
Students md5($password) या sha256($password) से password save करते हैं और पूछते हैं "hash है ना, तो safe?" नहीं! Fast hash crack करना आसान है. PHP में हमेशा password_hash() और password_verify() इस्तेमाल करो – salt और bcrypt अपने-आप मिलते हैं.
Lab
In your lab, create two users with the same password and compare their lines in /etc/shadow – note the different salts. Then use john (Chapter 22) on an MD5 hash and a bcrypt hash of the same weak password and compare how long each takes.
Extended teaching notes
Why this lesson matters
Cryptography fails when people invent it. Use standard libraries, modern algorithms (AES-GCM, TLS 1.2+, bcrypt/argon2) and protect keys in a KMS or secrets manager.
Where crypto shows up
[('Passwords', 'bcrypt/argon2/PBKDF2 via the framework hasher — never plain MD5/SHA1.'), ('TLS', "Terminate on ALB/nginx with ACM/Let's Encrypt; redirect HTTP to HTTPS."), ('JWT / API tokens', 'Short lifetime, rotate signing keys, store secrets outside Git.'), ('Disk / S3', 'Provider or customer-managed keys; least privilege on decrypt.')]
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.