SlideShare a Scribd company logo
Block Cipher Mode
by oalieno
⽬錄
• Block Cipher
• Block Cipher Mode
• ECB Mode
• CBC Mode
• CTR Mode
• GCM Mode
• Attack Scenarios
• ECB Mode / Cut & Paste
• ECB Mode / Prepend Oracle Attack
• CBC Mode / Bit-Flipping Attack
• CBC Mode / Padding Oracle Attack
• GCM Mode / Forbidden Attack
Block Cipher
Block Cipher
加密
16 bytes 明⽂
16 bytes 密⽂
密鑰
• 輸入輸出長度固定
• 以 AES 為例
• 輸入明⽂ 16 bytes
• 輸出密⽂ 16 bytes
• 密鑰長度有 16, 24, 32 bytes 三種
最常⾒
Block Cipher
我要加密的明⽂不是 16 bytes 怎麼辦 ?
切成很多個 16 bytes
加密
16 bytes 明⽂
16 bytes 密⽂
密鑰 加密
14 bytes 明⽂ + 2 bytes 填充 ( padding )
16 bytes 密⽂
密鑰
30 bytes 明⽂
Block Cipher
Block Cipher Mode
Block Cipher Mode
• AES 是 Block Cipher,輸入輸出長度固定
• 所以要加密任意長度的明⽂,需要⼀些額外加⼯
• ⼀些常⾒的加⼯模式:ECB, CBC, CFB, OFB, CTR...
• 有 AEAD 的模式:CCM, GCM, OCB…
Authenticated Encryption with Associated Data
Authenticated Encryption with Associated Data
• Authenticated Encryption ( AE )
• 會多計算⼀個 MAC ( Message Authentication Code )
• Associated Data ( AD )
• 可以連同⼀些沒加密的資料⼀起做 Authentication
• 比如⼀些不需要加密的 Header Information
ECB Mode
ECB Mode Encryption
Encrypt
Plaintext
Ciphertext
Key Encrypt
Plaintext
Ciphertext
Key Encrypt
Plaintext
Ciphertext
Key
ECB Mode Decryption
Decrypt
Ciphertext
Plaintext
Key Decrypt
Ciphertext
Plaintext
Key Decrypt
Ciphertext
Plaintext
Key
ECB Modehttps://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
ECB 模式的缺點:相同的明⽂區塊會加密出相同的密⽂區塊
CBC Mode
CBC Mode Encryption
Encrypt
Plaintext
Ciphertext
Key
IV ⊕
Encrypt
Plaintext
Ciphertext
Key
⊕
CBC Mode Decryption
Decrypt
Ciphertext
Plaintext
Key
IV ⊕
Decrypt
Ciphertext
Plaintext
Key
⊕
CBC Mode
• 每塊密⽂都依賴前⾯所有的明⽂
• 相同的區塊明⽂會加密出不同的區塊密⽂
• 有⼀個初始化向量 ( Initial Vector ),簡稱 IV
CTR Mode
CTR Mode Encryption
⊕
Encrypt
a59c…0000
Ciphertext
Key
Plaintext ⊕
Encrypt
a59c…0001
Ciphertext
Key
Plaintext
Counter
CTR Mode Decryption
⊕
Encrypt
a59c…0000
Plaintext
Key
Ciphertext ⊕
Encrypt
a59c…0001
Plaintext
Key
Ciphertext
Counter
CTR Mode
• 利⽤ AES 去產⽣ xor key 然後做 xor cipher
• 加密和解密都是⽤ AES Encryption,因為要產⽣相同的 xor key
• Counter 會初始⼀個隨機數字,每次加⼀
• Block 之間沒有互相依賴,可平⾏運算
GCM Mode
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
⊕
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
⊕
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
CTR Mode
=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
⊕
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth TagAuthentication
=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
⊕
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
• len 是 bitlen
• [10]64 → 000000000000000a
不是 16 bytes 的倍數會在後⾯補 x00
=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
如果 len(IV) ≠ 96 IV [len(IV)]6400
⊕
MultHMultH MultH
⊕
IV
J0
如果 len(IV) = 96 IV 00000001 J0=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
⊕
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
Ek
0
H
H = Ek(0)
=
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
• 低位 32 bits 加⼀ modulo 232
• 剩下的⾼位不動
inc32
GCM Mode Encryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
MultH
• MultH 是在 GF(2128) 下乘上 H
• GF(2128) 的 reduction modulus 是 x128 + x7 + x2 + x + 1
• 從 bytes 轉成 GF(2128) 下的元素
• u is the variable of the polynomial
• x0x1…x127 → x0 + x1u + … + x127u127
• 'x00…x00x01x02' → x119+x126
Galois Field
• Galois Field ( GF ) 或叫 Finite Field
• GF(2128) 裡⾯的元素可以表⽰成⼀個 degree = 127 的多項式
• 加法和乘法就是做多項式的加法和除法 modulo ⼀個 degree = 128 的 irreducible polynomial
• GCM Mode ⽤的 GF(2128) 是 modulo x128 + x7 + x2 + x + 1
GCM Mode Decryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
⊕
=
GCM Mode Decryptionhttps://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf
a59c…0002a59c…0001 a59c…0003
J0
inc32 inc32
IV
Ek
⊕Plaintext
Ciphertext
⊕
MultH
Ek Ek
Ciphertext
Plaintext
MultH
⊕
MultH
[len(A)]64 || [len(C)]64
MultH
⊕
Auth Data
⊕
Auth Tag
⊕
這邊和加密時顛倒
其他都⼀樣
=
Attack Scenarios
ECB Mode / Cut & Paste
ECB Mode / Cut & Paste
Encrypt
user:hi;money:10
A
Key Encrypt
;id=015647659901
B
Key Encrypt
5874573959304852
C
Key
ECB Mode / Cut & Paste
Decrypt
A
user:hi;money:10
Key Decrypt
C
5874573959304852
Key Decrypt
C
5874573959304852
Key
錢錢變多了~
ECB Mode / Prepend Oracle Attack
The Oracle
• 使⽤ AES ECB Mode
• 伺服器將我們的明⽂ prepend 在 flag 前⾯作加密
def oracle(plain):
aes = AES.new(KEY, AES.MODE_ECB)
return aes.encrypt(plain + flag)
找 flag[0]
Encrypt
AAAAAAAAAAAAAAAC
Ciphertext
Key Encrypt
TF{FLAG}…
Ciphertext
Key
先塞 15 個垃圾
讓 flag 的第⼀個字元掉進來
塞 15 個⼀樣的垃圾 +
爆搜最後⼀個 byte
找 flag[0]
Encrypt
AAAAAAAAAAAAAAA00
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
找 flag[0]
Encrypt
AAAAAAAAAAAAAAA01
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
塞 15 個⼀樣的垃圾 +
爆搜最後⼀個 byte
找 flag[0]
Encrypt
AAAAAAAAAAAAAAAC
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
找到了 flag 的第⼀個 byte
找 flag[1]
Encrypt
AAAAAAAAAAAAAACT
Ciphertext
Key Encrypt
F{FLAG}…
Ciphertext
Key
先塞 14 個垃圾
讓 flag 的前兩個字元掉進來
找 flag[1]
Encrypt
AAAAAAAAAAAAAAC00
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
塞 14 個⼀樣的垃圾 +
已知的 flag 第⼀個 byte +
爆搜最後⼀個 byte
找 flag[1]
Encrypt
AAAAAAAAAAAAAAC01
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
塞 14 個⼀樣的垃圾 +
已知的 flag 第⼀個 byte +
爆搜最後⼀個 byte
找 flag[1]
Encrypt
AAAAAAAAAAAAAACT
Ciphertext
Key Encrypt
CTF{FLAG}…
Ciphertext
Key
找到了 flag 的第⼆個 byte
Finally
• 這樣⼀直做下去就能解密出 flag 的所有字元了
• 最多需要做 len(flag) * 257 次
CTF Challenges
• UTCTF 2020 - Random ECB
• TUCTF 2018 - AESential Lesson
• cryptohack.org - ECB Oracle
CBC Mode / Bit-Flipping Attack
CBC Mode / Bit-Flipping Attack
Decrypt
Ciphertext
…b5
Key
…49 ⊕
Decrypt
Ciphertext
Plaintext
Key
⊕
⊕ 1
⊕ 1
CBC Mode / Bit-Flipping Attack
Decrypt
Ciphertext
…b4
Key
…48 ⊕
Decrypt
Ciphertext
Plaintext
Key
⊕
CBC Mode / Bit-Flipping Attack
Decrypt
…04
…91
Key
IV ⊕
Decrypt
Ciphertext
…e3
Key
⊕
⊕ 1
⊕ 1
CBC Mode / Bit-Flipping Attack
Decrypt
…03
…f3
Key
IV ⊕
Decrypt
Ciphertext
…e2
Key
⊕
CBC Mode / Bit-Flipping Attack
Decrypt
…04
…91
Key
IV ⊕
Decrypt
Ciphertext
…e3
Key
⊕
⊕ e3 ⊕ 66
⊕ e3 ⊕ 66
CBC Mode / Bit-Flipping Attack
Decrypt
…81
…1c
Key
IV ⊕
Decrypt
Ciphertext
…66
Key
⊕
將解密的明⽂改成我們指定的 0x66
CBC Mode / Bit-Flipping Attack
• 透過修改 IV 和 Ciphertext 來控制 Plaintext
• 其他 CFB, OFB, CTR 也存在相同的攻擊⽅式
CTF Challenges
• AIS3 preexam 2018 - EFAIL
• AIS3 preexam 2018 - BLIND
• AceBear CTF - CNVService
• Teaser Dragon CTF 2018 - AES-128-TSB
CBC Mode / Padding Oracle Attack
The Oracle
• 使⽤ AES CBC Mode 配上 PKCS#7 Padding Scheme
• 伺服器能幫我們解密訊息
• 如果解密出來的訊息 Padding 錯誤會噴錯
def unpad(data):
if not all([x == data[-1] for x in data[-data[-1]:]]):
raise ValueError
return data[:-data[-1]]
def oracle(cipher):
aes = AES.new(KEY, AES.MODE_CBC)
try:
plain = unpad(aes.decrypt(cipher))
except ValueError:
return False
return True
PKCS#7
PKCS#7 : Cryptographic Message Syntax
https://tools.ietf.org/html/rfc2315
後⾯要填充 6 個 bytes
34 83 E6 2F 20 0A 33 AC 49 41 06 06 06 06 06 06
明⽂
• 這個標準裡⾯定義了⼀種 Padding 的格式
• 要填充 5 個 bytes 就填充 5 個 0x05
• 要填充 2 個 bytes 就填充 2 個 0x02
Padding 錯誤
• 怎麼樣會 Padding 錯誤?
• 抓最後⼀個 byte 就可以知道 Padding 長度
都要等於最後⼀個 byte
34 83 E6 2F 20 0A 33 AC 49 41 06 ab 06 06 06 06
Python Implementation
def pad(data):
p = 16 - len(data) % 16
return data + bytes([p]) * p
def unpad(data):
if not all([x == data[-1] for x in data[-data[-1]:]]):
raise ValueError
return data[:-data[-1]]
解 plaintext[-1]
Padding Oracle Attack
…47
Decrypt
Ciphertext
…69
Key
⊕
未知的明⽂
已知的密⽂
Padding Oracle Attack
…00
Decrypt
Ciphertext
…2e
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試最後⼀個 byte
Padding Oracle Attack
…01
Decrypt
Ciphertext
…2f
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試最後⼀個 byte
Padding Oracle Attack
…02
Decrypt
Ciphertext
…2c
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試最後⼀個 byte
Padding Oracle Attack
…2f
Decrypt
Ciphertext
…01
Key
⊕
推論 ( 很⼤機率 )
Padding 正確
暴⼒嘗試最後⼀個 byte
2f47 ⊕⊕69 =
明⽂ 原本的密⽂
解 plaintext[-2]
Padding Oracle Attack
…7d47
Decrypt
Ciphertext
…6869
Key
⊕
部分未知的明⽂
已知的密⽂
Padding Oracle Attack
…002c
Decrypt
Ciphertext
…1502
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試倒數第⼆個 byte
47 69 02⊕ ⊕
2c=
設定明⽂最後⼀個 byte 為 02
Padding Oracle Attack
…012c
Decrypt
Ciphertext
…1402
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試倒數第⼆個 byte
Padding Oracle Attack
…022c
Decrypt
Ciphertext
…1702
Key
⊕
不知道 只知道
Padding 錯誤
暴⼒嘗試倒數第⼆個 byte
Padding Oracle Attack
…172c
Decrypt
Ciphertext
…0202
Key
⊕
推論
Padding 正確177d ⊕⊕68 =
明⽂ 原本的密⽂
暴⼒嘗試倒數第⼆個 byte
Summary
• 總共有三層迴圈
• 猜 0 - 255 直到猜出⼀個 byte
• ⼀次解出⼀個 byte 直到解完⼀個 block
• ⼀次解出⼀個 block 直到解完所有 blocks
• 解出⼀個 block 最多需要 4096 次嘗試
CTF Challenges
原汁原味 padding oracle attack :
• CSAW CTF 2016 Quals - Neo
• HITCON CTF 2016 Quals - Hackpad
• BAMBOOFOX CTF 2018 - mini-padding
padding 相關攻擊技巧 :
• HITCON CTF 2017 Quals - Secret Server
• HITCON CTF 2017 Quals - Secret Server Revenge
• BAMBOOFOX CTF 2018 - baby-lea-revenge
• BAMBOOFOX CTF 2018 - baby-lea-impossible
GCM Mode / Forbidden Attack
GCM Mode / Forbidden Attack
• ⼜叫 Nonce Repeating Attack
• 也就是當 IV 重複⽤的情況
• 假設可以拿到任意明⽂加密的結果
• 這個攻擊可以做到偽造簽章
GCM Mode / Forbidden Attack
• T = AH4 + C1H3 + C2H2 + LH + Ek(J0)
Tag
Associated Data
Ciphertext
[len(A)]64 || [len(C)]64
• 在 Authentication 中做的 xor 就是 GF(2128) 中的加法
• Tag 可以表⽰成在 GF(2128) 中的式⼦如下
未知的只有藍⾊的部分
GCM Mode / Forbidden Attack
• T1 = A1H4 + C11H3 + C12H2 + L1H + Ek(J0)
• T2 = A2H4 + C21H3 + C22H2 + L2H + Ek(J0)
• T1 - T2 = (A1 - A2)H4 + (C11 - C21)H3 + (C12 - C22)H2 + (L1 - L2)H
• 做兩次加密拿到兩個 Tag
• 兩個相減後只剩 H 未知,解⽅程式求根可得 H
• 有 H 帶回原式可得 Ek(J0) 就可以偽造 Tag 了
CTF Challenges
• UTCTF 2020 - Galois
• VolgaCTF 2018 Quals - Forbidden

More Related Content

PDF
Pwning in c++ (basic)
PDF
How NOT to Write a Microbenchmark
PDF
The Hand That Strikes, Also Blocks
PDF
VerilatorとSystemC
PDF
Histogram-in-Parallel-universe-of-MySQL-and-MariaDB
PDF
SQL injection: Not Only AND 1=1 (updated)
PDF
MySQL Replication Performance Tuning for Fun and Profit!
PDF
PostgreSQL - C言語によるユーザ定義関数の作り方
Pwning in c++ (basic)
How NOT to Write a Microbenchmark
The Hand That Strikes, Also Blocks
VerilatorとSystemC
Histogram-in-Parallel-universe-of-MySQL-and-MariaDB
SQL injection: Not Only AND 1=1 (updated)
MySQL Replication Performance Tuning for Fun and Profit!
PostgreSQL - C言語によるユーザ定義関数の作り方

What's hot (20)

PDF
階層的決定性ウォレットを理解しよう
PDF
MySQL 8.0.16 New Features Summary
PPT
[DEFCON 16] Bypassing pre-boot authentication passwords by instrumenting the...
PDF
Local File Inclusion to Remote Code Execution
PDF
Binary exploitation - AIS3
PDF
Why MySQL Replication Fails, and How to Get it Back
DOCX
Web-servers & Application Hacking
PDF
汎用性と高速性を目指したペアリング暗号ライブラリ mcl
PDF
SAST vs. DAST: What’s the Best Method For Application Security Testing?
PDF
MySQL Scalability and Reliability for Replicated Environment
PDF
Deep Exploit@Black Hat Europe 2018 Arsenal
PDF
Open Source 101 2022 - MySQL Indexes and Histograms
PDF
An introduction to Rust: the modern programming language to develop safe and ...
ODP
Rust Primer
PDF
C/C++プログラマのための開発ツール
PPTX
Introduction to Rust language programming
PDF
[cb22] Lets Dance in the Cache Destabilizing Hash Table on Microsoft IIS by O...
PPT
Pentest Application With GraphQL | Null Bangalore Meetup
PDF
TDOH x 台科 pwn課程
PPTX
ホモトピー型理論入門
 
階層的決定性ウォレットを理解しよう
MySQL 8.0.16 New Features Summary
[DEFCON 16] Bypassing pre-boot authentication passwords by instrumenting the...
Local File Inclusion to Remote Code Execution
Binary exploitation - AIS3
Why MySQL Replication Fails, and How to Get it Back
Web-servers & Application Hacking
汎用性と高速性を目指したペアリング暗号ライブラリ mcl
SAST vs. DAST: What’s the Best Method For Application Security Testing?
MySQL Scalability and Reliability for Replicated Environment
Deep Exploit@Black Hat Europe 2018 Arsenal
Open Source 101 2022 - MySQL Indexes and Histograms
An introduction to Rust: the modern programming language to develop safe and ...
Rust Primer
C/C++プログラマのための開発ツール
Introduction to Rust language programming
[cb22] Lets Dance in the Cache Destabilizing Hash Table on Microsoft IIS by O...
Pentest Application With GraphQL | Null Bangalore Meetup
TDOH x 台科 pwn課程
ホモトピー型理論入門
 
Ad

Similar to [Crypto Course] Block Cipher Mode (9)

PDF
應用密碼學入門 - HITCON CMT 2018
ODP
密碼學漏洞與他們的產地 Crypto fail and where to find them
PPTX
流行应用的加密算法实现缺陷与利用
PPTX
🔐 密碼學入門|從理論到實作,掌握資訊安全核心技術!introduction to cryptology.pptx
PDF
密码学 & DRM & sgx
PPT
Internet Security
PPT
網路安全
PPT
網路安全
PPT
電腦網路 網路安全
應用密碼學入門 - HITCON CMT 2018
密碼學漏洞與他們的產地 Crypto fail and where to find them
流行应用的加密算法实现缺陷与利用
🔐 密碼學入門|從理論到實作,掌握資訊安全核心技術!introduction to cryptology.pptx
密码学 & DRM & sgx
Internet Security
網路安全
網路安全
電腦網路 網路安全
Ad

More from WEI CHIEH CHAO (7)

PDF
[Crypto Course] LFSR
PDF
[Crypto Course] Hash
PDF
[Crypto Course] Bleichenbacher RSA Signature Forgery
PDF
[Crypto Course] RSA
PDF
[Crypto Course] Blockchain Security
PDF
Rootkit 101
PPTX
滲透測試基本技巧與經驗分享
[Crypto Course] LFSR
[Crypto Course] Hash
[Crypto Course] Bleichenbacher RSA Signature Forgery
[Crypto Course] RSA
[Crypto Course] Blockchain Security
Rootkit 101
滲透測試基本技巧與經驗分享

[Crypto Course] Block Cipher Mode