Message Authentication Codes
Table of Contents
A Message Authentication Code (MAC) is a cryptographic function used to verify the integrity and authenticity of a message. It generates a fixed-size tag from the message and a secret key, appended to the message for the receiver to verify.
Related: Hashing Encryption JSON Web Tokens JWT Attacks Confidentiality Integrity Hashcat Domino
How MACs Work
- A secret key is shared between the sender and receiver
- The sender calculates a MAC value based on the message and the secret key
- The MAC value is appended to the message
- The receiver recalculates the MAC using the same secret key and compares it to the received value — a match confirms the message is both authentic (came from someone with the key) and intact (unmodified in transit)
Critically: a MAC proves the message came from someone who knows the key, not necessarily the intended sender. If the key is weak, guessable, or leaked, anyone can forge valid MACs for arbitrary messages.
Types of MACs
| Type | Basis | Common use |
|---|---|---|
| HMAC | Cryptographic hash function + secret key | JWT (HS256), session cookies |
| CBC-MAC | Cipher block chaining mode of operation | Older/legacy systems |
| CMAC | CBC-MAC variant designed for AES | Modern block-cipher-based MAC |
HMAC is by far the most common variant you'll encounter in practice — it's the mechanism behind JWT's HS256 algorithm and a frequent choice for signing session tokens/cookies directly.
Computing an HMAC
# HMAC-SHA256 of a message with a known key, via openssl echo -n "user=admin&role=user" | openssl dgst -sha256 -hmac "supersecretkey"
import hmac import hashlib message = b"user=admin&role=user" key = b"supersecretkey" tag = hmac.new(key, message, hashlib.sha256).hexdigest() print(tag)
Security Testing — Forging and Cracking MACs
This is the practically relevant side for pentesting: a MAC is only as strong as its secret key and how it's used.
Hardcoded or Weak Secret Keys
If an application signs cookies/tokens with a hardcoded or predictable HMAC secret (found in source code, a config file, or a public repo), any value can be forged with a valid MAC — this is exactly the mechanism behind Domino's cookie forgery: a hardcoded HMAC key discovered in an exposed config allowed forging arbitrary session cookies with valid signatures.
import hmac, hashlib # Once the key is known (leaked, hardcoded, or cracked): forged_message = b"user=admin&role=admin" key = b"leaked_secret_key" forged_tag = hmac.new(key, forged_message, hashlib.sha256).hexdigest() # Send forged_message + forged_tag as the cookie/token — the server # will recompute the same tag and accept it as valid
Brute-Forcing an Unknown Secret
If the algorithm and message are known but the key isn't, and the key is weak enough (short, dictionary word, common pattern), hashcat can brute-force it — same mode used for JWT HS256 secret cracking:
# Mode 16500 covers JWT (HS256) specifically hashcat -m 16500 -a 0 token.txt /usr/share/wordlists/rockyou.txt
See Hashcat for the full mode reference and JWT Attacks for the JWT-specific version of this exact attack.
Length Extension Attacks
Older or naively-constructed MAC schemes (particularly plain
hash(key || message) rather than proper HMAC) can be vulnerable
to length extension — an attacker can append data to a message and
compute a valid new MAC without knowing the key, by exploiting
the internal state of the underlying hash function (MD5, SHA-1,
SHA-256 are all vulnerable in this construction). Proper HMAC's
double-hashing construction specifically defends against this —
one of the reasons HMAC is preferred over naive concatenation.
Best Practices (Defensive)
- Use HMAC over naive
hash(key || message)constructions — immune to length extension attacks - Use a long, random, unpredictable secret key — never hardcoded or derived from guessable values
- Never expose the key in client-side code, public repos, or version control history
- Combine MACs with encryption where confidentiality is also needed — a MAC alone only proves integrity/authenticity, not secrecy
Tips
- On the exam, a hardcoded or leaked HMAC secret is a high-value finding — it typically enables full authentication bypass, not just tampering with one field. Classify it as Insecure Authentication Mechanism if it's a session/auth token, since the authentication mechanism itself is broken regardless of credential strength
- Check exposed source, config files,
.env, or.git/history for hardcoded secrets before assuming brute force is the only path — see Security Misconfiguration for the exposure-hunting methodology - If you find a MAC/signature check but can't find the key, verify whether it's actually being validated server-side at all before spending time cracking it — an unvalidated signature (accept anything) is a faster win than a strong one