AES is a block cipher (*) like Triple-DES, Blowfish... (*) from now on we’ll say AES for AES-128. it doesn’t really matter, just makes the key smaller☺
A block cipher ● takes a block of data ○ of fixed size (=“block size”) ■ 16 bytes for AES, 8 for Blowfish/DES3... ○ padded if smaller than blocksize ● a key ● returns a ‘scrambled’ block of data ● security criteria: ○ invertible (permutation).. ○ but only if the key is known ● behaves as a 'random permutation' (aka 'ideal cipher')
AES encryption 1/3 Parameters Results k:'MySecretKey12345' ┐◄n╩i▐☼←∞└╞∙iû╨► block:'a block of text.' (BF 11 6E CA 69 DE 0F 1B EC C0 C6 F9 69 96 D0 10)
AES encryption 3/3 Parameters Results k:'MySecretKey12345' wε╩▬▄y&↕ú@αùαφ♣O block:'a block of text!' (77 EE CA 16 DC 79 26 12 A3 40 E0 97 E0 ED 05 4F)
with a tiny change in the key or input block, the output block is completely different
we can’t control the output (the differences are unpredictable)
Reverse operation ● get the original block with the reverse operation and the same key ● encrypt then decrypt In some ciphers (such as NOEKEON*), encryption and decryption are almost identical. *http://gro.noekeon.org/
Jargon plaintext = readable, not encrypted (in theory) a plaintext block is encrypted into ciphertext block a ciphertext block is decrypted into a plaintext block
Encryption and decryption 1/3 Encrypting “a block of text.” with key = “MySecretKey12345” with AES gives “┐◄n╩i▐☼←∞└╞∙iû╨►” (BF 11 6E CA 69 DE 0F 1B EC C0 C6 F9 69 96 D0 10)
Encryption and decryption 2/3 Decrypting the result (“┐◄n╩i▐☼←∞└╞∙iû╨►”) with the same key (“MySecretKey12345”) gives back “a block of text.”
Encryption and decryption 3/3 but decrypting the same block again with a slightly different key “MySecretKey12346” gives “π╔6I►♣♫Σ♣╝╤→√çφ╡” (E3 C9 36 49 10 05 0E E4 05 BC D1 1A FB 87 ED B5)
File formats 101 ● most files on your system use a standard format. ● some for executables (ran by the OS) ○ very complex - depend on the OS ● some for documents (open by Office, your browser…) ○ “less” complex - depend on the specs only
File formats signatures (& headers) usually start with a magic signature ● a fixed byte sequence ○ PNG \x89 PNG\r\n\x1a\n ○ PDF %PDF-1.x ○ FLV FLV ○ JPG \xFF \xD8 ● enforced at offset 0
Why using a magic signature? ● quick identification ● the file is invalid if the signature is missing Collisions? ● very rare: ○ 0xCAFEBABE: universal Mach-O and JAVA Class ■ recent Mach-O = 0xFEEDFACE / 0xFEEDFACF
Typical data structure formats are made of chunks ● chunks have different names ○ “chunk”, “segment”, “atom” ● structure (type length value) 1. a type identifier ○ “marker”, “type”, “id” 2. (typically) their length 3. the chunk data itself 4. (sometimes) data’s checksum
Why using a chunk-structure? ● newer chunk types can be ignored for ‘forward compatibility” ● tools can use custom chunks to store extra info while staying standard
Chunks example (simplified) A valid file: 1. magic signature 2. chunks a. header b. comment c. thumbnail d. data e. end some chunks are critical, some aren’t (=ancillary)
Appended data most file formats tolerates any data of any length after the end marker valid file + random data ⇒ still valid Few formats reject any appended data: ● Java CLASS, Java Archive
A valid binary file to summarize: to be valid, a binary file requires: 1. a valid header ○ including a valid magic 2. a valid chunk structure ○ an end chunk and may be followed by any data if tolerated
Let’s go back to the challenge (at last)
Encrypt a valid JPG into a valid JPG (and if possible, any other standard format)
First analysis since a block cipher’s output is ‘random’, encrypting a valid JPG into a valid JPG seems impossible: both files can’t even have valid signatures and structures we would have to control the output of AES (!)
Block cipher modes 101 how block ciphers are applied to files
Encrypting data bigger than a block how does one apply encryption on a file? ● if the key and plaintext are the same → the ciphertext is the same
Electronic CodeBook mode if we just apply the cipher on each block, identical blocks will give identical output → big weakness
that doesn’t look terribly encrypted, does it ?
Good job, guys!
Block cipher modes of operation various modes can be used to operate block ciphers on files: ● chaining each block’s encryption to propagate differences from the start to the end of the file, killing repetitive patterns http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation for this, auxiliary input may be needed, such as either: ● unpredictable IV (CBC) ● unique nonce (CTR)
Initialization Vector 101 Several modes (CBC, OFB, CFB,...) introduce an extra parameter IV that we can abitrarily choose (in practice, it should be unpredictable)
C1 = Enc(P1 ^ IV)
CBC observations no matter the key or block cipher, for a given P1 and C1, we can craft a IV so that: a file starting with P1 will be encrypted into a file starting with C1 with IV = Dec(C1) xor P1
Example With key: my_own_key_12345 IV: 0f 0d ec 1c 96 4c 5f 1e 84 19 4a 38 81 ef b7 f6 "%PDF-1.5\n1 0 obj" encrypts as "89 PNG 0d 0a 1a 0a 00 00 00 0d IHDR"
Current status ● we control the first block :) ● the following blocks will look random :(
decrypting plaintext (ciphers don’t analyze your input)
Encryption & decryption they are just 2 reverse operations ● they both: ○ take any input ○ give the resulting output ● the reverse operation gives back the original block ○ (if the key is the same)
Example (1/2) key = "MySecretKey12345" p = "a block of text." decrypt(AES, key, p) = “ä/ë-╦7 ↓h│☻⌂µ[←Ñ” (84 2F 89 2D CB 37 00 19 68 B3 02 7F E6 5B 1B A5) it doesn’t really make sense to ‘decrypt’ plaintext… but it doesn’t matter for the cipher, so...
Example (2/2) indeed, with: key = "MySecretKey12345" c = “ä/ë-╦7 ↓h│☻⌂µ[←Ñ” encrypt(AES, key, c) = "a block of text."
you can decrypt plaintext: it gives you back your plaintext after re-encryption (ie, you can control some AES encryption output)
let’s add plaintext to our encrypted file!
Consequences since adding junk at the end of our valid file still makes it valid, we add decrypted plaintext, that will encrypt to what we want
Current status 1. we control the first block 2. we control some appended data how do we control the encrypted data from the source file that is in-between?
we don’t we politely ask the file format to ignore it (by surrounding this data in an extra chunk)
Our current challenge within a block, get a valid 1. header 2. chunk start this is specific to each target format
block size our goal
PDF Portable Document Format
PDF in a nutshell ● magic signature: %PDF-1.X ● PDF are made of objects ● stream objects can contain any data
Required space for our block AES has a block size of 16 bytes a standard PDF header + stream object start takes >30 bytes!
Let’s shrink the header 1. truncate the signature %PDF-\0 2. remove the object number 0 0 obj 3. remove the parameter dictionary <<>> et voilà, exactly 16 bytes! %PDF-\0obj\nstream
PDF laxism FTW PDF doesn’t care if 2 signatures are present → we can close the stream at any point with: endstream endobj and resume our original PDF file happily
Steps to encrypt as PDF 1. we choose our key, source and target contents 2. our first cipher block: %PDF-\0obj\nstream 3. determine IV from plaintext & cipher blocks 4. encrypt source file 5. append object termination 6. append target file 7. decrypt final file 8. et voilà, the final file will encrypt as expected!
PoC @ corkami
JPG Joint Photographic Experts Group (image)
JPG in a nutshell ● magic signature: FF D8 (only 2 bytes) ● chunk’s structure: <id:2> <length:2> <data:?> ● comment chunk ID: FF FE → only 6 bytes are required!
Steps to encrypt as JPG 1. get original size, padded to 16 2. 1st cipher block = FF D8 FF FE <source size:2> <padding> 3. generate IV from plaintext & cipher blocks 4. AES-CBC encrypt source file 5. append target file minus signature 6. decrypt final file
Encrypt as PNG 1. get original file size 2. generate cipher block 3. compute the IV 4. encrypt original data 5. get encrypted(original data) checksum 6. append checksum and target data ○ target data = target file - signature 7. decrypt file
PoC PNG PoC
FLV Flash Video
Flash Video 1. magic = “FLV” 2. followed by 2 bytes parameters 3. then size(chunk) on 4 bytes ⇒ we can arbitrarily increase it and put our next chunk where we want no checksum or trick
→ an FLV PoC (key = “a man will crawl”)
How can we call that trick?
Reminder ● this is not specific to AES ● this is not specific to CBC required conditions ● control the first cipherblock ● the source format tolerates appended data ● header+chunk declaration fits in “blocksize” ○ the source size fits in the specified size encoding (short, long…)
Bonus as a consequence ● the same file can encrypt or decrypt to ○ various files ○ of different formats ○ with different ciphers ○ and different modes if you can craft a header (see GynCryption)
a step by step walkthrough AES(ZIP) = PNG
Let’s encrypt this (ZIP)
Into this (PNG)
Preliminary ● ZIP tolerates appended data, so does PNG ● our source file is 128 bytes ● AES works with 16 bytes blocks → one block of 16 bytes of value 0x10 will be padded (not strictly required here, but that's the standard PKCS7 padding)
P1 the first block of the source file is: .P .K 03 04 0A 00 00 00 00 00 11 AA 7F 44 A3 1C
Target format 1/2 the target format is a PNG: ● the encrypted file must start with the PNG signature: 89 .P .N .G \r \n 1A \n (8 bytes) ● followed by chunk length ○ our source file is 144 bytes (with padding) ○ already 16 bytes are covered by first block ○ so our dummy block will be 128 bytes long ○ encoded 00 00 00 80, as PNG is little endian
Target format 2/2 ● followed by chunk type ○ 4 letters, non-critical if starting with lowercase ■ we could use the standard ‘tEXt’ comment chunk ■ or just our own, ‘aaaa’ or whatever so our target’s first cipherblock will be: 89 .P .N .G \r \n 1A \n 00 00 00 80 61 61 61 61 SIG ------------------- LENGTH ---- TYPE ------
Decrypting C1 ● the key we’ll use is: MySecretKey01234 ● our C1 is: 89 .P .N .G \r \n 1A \n 00 00 00 80 61 61 61 61 ● with this key, C1 decrypts as: ee 1b 01 b2 5a a5 bd a8 3a 9e 35 44 2f 5f 23 35
Crafting the IV ● P1 is: .P .K 03 04 0A 00 00 00 00 00 11 AA 7F 44 A3 1C ● our decrypted C1 is: 89 .P .N .G \r \n 1A \n 00 00 00 80 61 61 61 61 ● by xoring them, we get the IV: be 50 02 b6 50 a5 bd a8 3a 9e 24 ee 50 1b 80 29 now, our key and IV are determined. we just need to combine both file’s content.
Making the final file 1. encrypt our padded source file 2. determine the CRC of our dummy chunk once encrypted (even if it will be surrounded by ‘plaintext’): ○ 6487910E in our case 3. append this CRC to finish the chunk 4. append all the chunks (whole file minus the SIG) of the target file. → our file is now a valid PNG
It works, but... both files aren’t standard appended data is a giveaway
A smarter appended data since we have to handle the file format
To prevent obvious appended data ● hide ‘external’ data just after the source data ○ provided the extra data is ignored ● combine encryption/decryption block
Appended data at file level: ● original file ● appended data
Appended data on known format if we know the structure, this gives: ● original file ○ header ○ format-specific data ○ footer ● appended data
Append data in the format right after the original dat ● original file ○ header ○ format-specific data ■ appended data ○ footer
appending data at file format level
Combining blocks since blocks encryption/decryption only depends on previous blocks & parameters 1. append data 2. perform operation on the whole block ○ alternate encryption and decryption 3. repeat
this is our firs ■)²0░üîä╬`¥√usH; ⇒ t block îô$úqΘ↕Å£│íΓª◄•| !≡╩b1è>!╢╬^ºlß¬Φ this is our encr ☺↑☼GJ♪R┴◄a7é┤╚0v ypted block - le ⇐ ≡µΣ=↓v≡÷v◘;▬♀▬¥. t's make it long /æªó╜2 :∩h↑ú∟áéÑ er... our 2nd non encr ½! |┼ñV₧îöHoCÖΘp ⇒ ypted block ë∟Θ╜╢¼æá.╛ÄP▲τ°√ ⇐ è─9¥ ΦO7µ→↔P÷╚ê▓ our final encryp 9┬ñ┘§s@7╓b☼#¬¡▀√ ted block chaining encrypted & decrypted block key = "alsmotrandomkey!" IV = "Initialization.."
a more complex layout → the ‘start’ file is a standard PNG
a PNG encrypted in a standard PNG
a note on ZIP it’s not as permissive as we usually think
ZIP file, in practice ● the signature is not enforced at offset 0 ⇒ ZIP data is usually remembered as ‘valid anywhere’ in the file. That’s wrong: ZIP is different from modern standards, but it doesn’t work ‘anywhere’
ZIP is parsed backward
✓/✗ Tools don’t accept too much appended data size
✓ duplicating the End of Central Directory increases compatibility
Increase ZIP compatibility Duplicate EoCD after appended data (cheap internal appended data) ⇒ tools will parse the ZIP correctly ⇒ AES(PNG) = APK
GynCryption as suggested by Gynvael Coldwind ● JPG only requires 4 bytes ⇒ use ECB and bruteforce the key recompress the JPG if the chunk size is too big ○ the chunk size is ‘random’ but stored on 2 bytes ○ same dimensions ⇒ same 1st block
Steps 1. get P1 2. bruteforce key until C1 starts with FF D8 FF FE (required ~18M iterations for me) 3. shrink S if bigger than chunk’s size 4. pad S until the right offset 5. encrypt S 6. append T ○ minus its signature 7. decrypt
Conclusion ● a funny trick ○ a bit of crypto magic, a bit of binary magic ○ having fun with usually scary topics ● steganographic application ● a reminder that: ○ crypto is not always ‘random’ ○ binary manipulation doesn’t require full understanding possible applications: ● protocols: JWE, OCSP...