𝗖𝗕𝗖 𝗕𝗶𝘁 𝗙𝗹𝗶𝗽𝗽𝗶𝗻𝗴 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱
Encryption does not mean your data is safe from tampering.
Many developers make this mistake. They think if an attacker cannot read the data, they cannot change it. This is wrong. Cryptography treats secrecy and integrity as two different tasks.
The CBC Bit Flipping attack proves this. An attacker can change your data without knowing your secret key.
Here is how it works:
AES does not encrypt data in one large piece. It breaks data into 16-byte blocks. In CBC mode, these blocks chain together. The encrypted output of the first block mixes into the plaintext of the second block.
This chain creates a weakness during decryption. To get the original text of Block 2, the server decrypts it and combines it with the ciphertext from Block 1.
An attacker can exploit this:
- The attacker intercepts your encrypted traffic.
- They do not know the key, so the data looks like gibberish.
- They change specific bits in the ciphertext of Block 1.
- They send this altered data to your server.
- When the server decrypts it, the change in Block 1 shifts the math for Block 2.
The attacker does not need to read the message. They just flip bits to change the final result.
Consider an old web app using encrypted cookies for sessions. A cookie might hold: userid=994;role=user;
An attacker intercepts this cookie and flips bits in the ciphertext. They send many requests until the server accepts one. Because the server only checks if the data decrypts, it processes the modified text. Suddenly, the decrypted string reads: userid=994;role=admi;
The attacker now has admin access. They never read the key or the original cookie.
The mistake is assuming encryption guarantees integrity.
- Confidentiality stops people from reading data.
- Integrity stops people from changing data.
To fix this, use Authenticated Encryption like AES-GCM. It creates a cryptographic tag. This tag acts like a seal. If an attacker changes even one bit, the seal breaks. The server rejects the data immediately.
If you must use CBC, use an Encrypt-then-MAC architecture. Create an authentication code for the ciphertext and verify it before you start decryption.
Secret data is not always trustworthy data. Always prove your data is unchanged before you use it to make decisions.
Spiegazione del CBC Bit-Flipping: Perché la sola crittografia non garantisce l'integrità
In crittografia, spesso si fa confusione tra riservatezza (confidentiality) e integrità (integrity). Sebbene la crittografia sia eccellente per nascondere i dati agli occhi di chi non è autorizzato, non impedisce necessariamente a un malintenzionato di modificarli.
In questo articolo, esploreremo un attacco specifico chiamato CBC Bit-Flipping, che dimostra come sia possibile manipolare il testo in chiaro (plaintext) modificando il testo cifrato (ciphertext), anche senza conoscere la chiave di cifratura.
Cos'è il CBC (Cipher Block Chaining)?
Il modo di cifratura Cipher Block Chaining (CBC) è un metodo in cui ogni blocco di testo in chiaro viene combinato (tramite l'operazione XOR) con il blocco di testo cifrato precedente prima di essere cifrato.
Il processo di cifratura può essere riassunto così:
- Il primo blocco di testo in chiaro viene combinato con un Vettore di Inizializzazione (IV) tramite XOR.
- Il risultato viene cifrato con la chiave.
- Per i blocchi successivi, il blocco di testo in chiaro corrente viene combinato con il blocco di testo cifrato precedente tramite XOR prima di essere cifrato.
La formula per la cifratura è: $C_i = E_k(P_i \oplus C_{i-1})$
E la formula per la decifratura è: $P_i = D_k(C_i) \oplus C_{i-1}$
Dove:
- $P_i$ è il blocco di testo in chiaro.
- $C_i$ è il blocco di testo cifrato.
- $E_k$ è la funzione di cifratura con chiave $k$.
- $D_k$ è la funzione di decifratura con chiave $k$.
- $C_{i-1}$ è il blocco di testo cifrato precedente (o l'IV per il primo blocco).
La Vulnerabilità: L'Attacco Bit-Flipping
La vulnerabilità risiede nella formula di decifratura: $P_i = D_k(C_i) \oplus C_{i-1}$
Notate che il blocco di testo in chiaro $P_i$ dipende direttamente dal blocco di testo cifrato precedente $C_{i-1}$ attraverso un'operazione XOR.
Se un attaccante modifica un bit nel blocco $C_{i-1}$, quel cambiamento si rifletterà direttamente nello stesso bit del blocco di testo in chiaro $P_i$ durante la decifratura.
Sebbene la modifica di $C_{i-1}$ corrompa completamente il blocco $P_{i-1}$ (rendendolo indecifrabile), l'attaccante può prevedere esattamente come la modifica di $C_{i-1}$ influenzerà $P_i$.
Esempio Pratico
Immaginiamo che un'applicazione utilizzi la cifratura CBC per memorizzare un cookie di sessione che contiene i permessi dell'utente.
Testo in chiaro originale ($P$):
user=guest;admin=false
Supponiamo che questo testo sia diviso in blocchi. Il blocco che contiene admin=false è il secondo blocco ($P_2$).
Testo cifrato ($C$):
[Blocco C1] [Blocco C2]
Un attaccante vuole cambiare admin=false in admin=true.
L'attaccante non può decifrare $C_2$ per vedere cosa c'è dentro, ma sa che: $P_2 = D_k(C_2) \oplus C_1$
Se l'attaccante modifica $C_1$ in modo che $C_1' = C_1 \oplus \Delta$, dove $\Delta$ è la differenza tra "false" e "true", allora il nuovo testo in chiaro $P_2'$ sarà: $P_2' = D_k(C_2) \oplus (C_1 \oplus \Delta)$ $P_2' = (D_k(C_2) \oplus C_1) \oplus \Delta$ $P_2' = P_2 \oplus \Delta$
In questo modo, l'attaccante può manipolare il valore di admin senza mai conoscere la chiave di cifratura.
Conseguenze
L'attacco bit-flipping può portare a:
- Escalation dei privilegi: Cambiare i ruoli utente.
- Manipolazione dei dati: Modificare importi monetari, ID utente o altri parametri critici.
- Bypass di controlli di sicurezza: Alterare flag di autenticazione.
Come Mitigare il Rischio
La lezione fondamentale è che la cifratura da sola non garantisce l'integrità. Per proteggersi, è necessario utilizzare meccanismi che verifichino se il testo cifrato è stato alterato.
1. Utilizzare un MAC (Message Authentication Code)
Un approccio comune è l'utilizzo dell'Encrypt-then-MAC. Si cifra il messaggio e poi si calcola un MAC (come l'HMAC) sul testo cifrato risultante. Durante la decifratura, si verifica prima il MAC; se non corrisponde, il messaggio viene scartato prima ancora di tentare la decifratura.
2. Utilizzare la Crittografia Autenticata (AEAD)
La soluzione moderna e raccomandata è l'utilizzo di algoritmi di Authenticated Encryption with Associated Data (AEAD), come AES-GCM (Galois/Counter Mode) o ChaCha20-Poly1305.
Questi algoritmi integrano la riservatezza e l'integrità in un unico passaggio. Se anche un solo bit del testo cifrato viene modificato, la decifratura fallirà automaticamente, impedendo l'attacco bit-flipping.
Conclusione
Quando progettate sistemi di sicurezza, non assumete mai che la cifratura protegga i dati dalle modifiche. Assicuratevi sempre di implementare controlli di integrità per garantire che i dati ricevuti siano esattamente quelli inviati.
Se hai trovato utile questo articolo, segui per altri approfondimenti sulla sicurezza informatica!