hi and welcome back to hacker 101 this is the second in a series of three sessions on cryptography if you haven't watched the first yet I recommend going to check that out first in this session we're going to be discussing some interesting practical attacks against crypto in the real world first let's talk about stream cipher reuse if we look back at stream ciphers we see that they generate a stream of pseudo-random bytes which are exerted with the plaintext to encrypt data anytime we cede the PRNG with a given key it generates the exact same sequence
of bytes this is why decryption is possible but consider what happens when you encrypt two pieces of data with the same key stream each byte of the two plaintext sequences get ex-ored with the same byte from the key stream because of this fact x-raying the two cipher text with each other cancels out the key stream entirely you end up with the XOR of the two plaintext blobs instead now this bug doesn't affect modern stream ciphers like estream which take a nonce or a single-use value like an IV and CBC mode along with the key however
each stream is practically never used rc4 is by far the most commonly used stream cipher and this is absolutely affects rc4 the only mitigation that really helps with this is to XOR the rc4 key with a nonce prior to use it's not perfect but it does work jumping over to block ciphers ECB block reordering is one of the nasty bugs you'll run into and it's surprisingly common because each one of the blocks is encrypted separately there is actually nothing stopping us from arbitrarily reordering those blocks they're still completely valid in any order or removed entirely
so consider a cookie that contains an admin flag and a username encrypted with DES and ECB mode because des uses eight byte blocks this means we'd encrypt three blocks which you can see on the screen there's a single byte of padding which we'll talk about later in the session if we control our username there's a big attack we can perform here we don't need to be able to encrypt or do the data on our own which necessitates knowing the key but we can use block reordering to make ourselves an admin if our username is padding
admin equals one semicolon we get a block that contains just admin equals one semicolon a perfect replacement for the normal admin equals zero first block all we have to do is take that last block and use it to replace the first block now we have a valid admin cookie with the username padding this attack is trivial in most cases and absolutely inherent to ECB mode likewise if we have the ability to decrypt any ECB data we can use this to decrypt any other ECB data that happens to use the same key for instance we could
set our username to padding again then place any ECB block immediately after this if the username is shown in the web interface it'll show us the decrypted block essentially if a key is used for more than one thing in an application it's probably vulnerable to attacks like these these flaws and others will talk about rely on the fact that while we may not be able to see the data or even guess the original content we can tamper with the cipher text and the server will decrypt it the solution of this is really simple encrypt your
data then apply a Mac to that note that I said apply a Mac to the cipher text this is something that many crypto protocols get wrong but so many things can go badly if you Mac and then encrypt always always encrypt-then-mac now before we get too much further we need to talk about one fundamental concept padding given that data encrypted with a block cipher won't always end on a block boundary we need some way of padding the data to align it properly because of this we always apply a padding scheme even when we do end
on a block boundary that way we always know what is padding and what is legitimate data the most common padding scheme in the real world is pkcs7 and it's quite simple if you need one byte of padding you end with a single byte with a value of 1 if you need to two bytes with a value of 2 etc on this slide you can see a couple of samples pause and make sure you understand the scheme before you move on now padding has to be validated during decryption this serves as a basic corruption detection mechanism
and because it needs to be stripped off before use in pkcs7 you can simply look at the last byte of the last block to see how many padding bytes there are then walk backwards to make sure they're all there and have the right value padding Oracle's can come into play if you have data encrypted in CBC mode and its padded with pkcs7 if the server behaves differently when decrypting improperly padded data versus properly padded data this is an Oracle you can determine whether or not it's padded properly this may seem like a silly thing to
think about but it gets fun if you recall from last session I mentioned that flipping a bit in one CBC ciphertext block will flip a bit in the resulting plain text of the following block due to that a padding Oracle can actually let us use the server to fully decrypt the data without having to know the key what we'll do is start with the last byte of the second-to-last block modifications to that byte will affect the very last byte our goal here is to determine what byte of cipher text in block n minus-1 will cause
the plaintext of block n to be one with an XOR together once we know this we know that the plain text of that byte in block n is ciphertext of n minus 1 XOR with one if we have the data hacker 101 and Pat it out to eight byte blocks and encrypt it we'll get some ciphertext blob to decrypt the second block you decrypt it with your cipher then XOR with the ciphertext of the first block that gets you the original plaintext with the example we know that when you decrypt the block feed face four
five six seven eight nine a B you have to get something where the last by XOR Oh X 23 the last byte of the previous block is equal to seven our known padding value any other value will cause a padding error except one if the last byte is one that's a valid single byte of padding so if we try all 255 other possible values we'll know that the other valid one will be the value one once we know this value we simply XOR that byte with three to change the final padding from one to two
and repeat the same thing for the previous byte one value will be valid and that value we will know to be two by performing this across all the bytes in a block then walking back from there along the blocks we can find the plaintext value of every single byte in this data I cut out a few details for simplicity but this is the basic process Google for padding Oracle attack and you'll find more in-depth examples and code to execute this attack as before macking your data after encryption and validating that Mac before decryption will solve
this problem however many crypto protocols do not do this and cannot be changed for compatibility reasons if you can't do this it's critical that you do not differentiate between errors it should be a success or a failure users shouldn't know why anything crypto wise is failing moving along hash extension attacks are an interesting case with merkel guard hashes md5 sha-1 and other non truncated hashes the digest that you get out is really just the internal state of the algorithm this leads to an interesting bug simplifying slightly hashes work like this initialize the hash makes each
block of data into the hash state pad the hash output the internal state what that means is that you can actually add new data trivially put the existing digest into the internal state of the hash algorithm add new data like you would normally then Pat it accordingly now why is this important let's say you have a hash of a secret key plus some data with length extension you can add any arbitrary data after the original data without having to know the secret key because they're padding between the old and new data though since it's added
by the algorithm you do need to figure out what that padding is then you add the hash padding into the original data that way the hash still matches when everything is checked out the process of finding the padding depends on the algorithm here you can see some links to resources on exactly that in terms of mitigation this is one of the least direct ones which is just say you can't actually avoid hash extension attacks if you're using a vulnerable algorithm period what you can do is use constructions sidestep this issue think back to H max
these use the double hash structure so you'd have to be able to extend both the outer and inner hashes which is impossible anytime you're hashing a secret with user data you need to use an H map in the next and final of installment of this series we're going to be going through some final thoughts and some tips and tricks for crypto and practice as always thanks for watching and happy breaking