<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Explore Security &#187; Cryptography</title>
	<atom:link href="https://www.exploresecurity.com/category/cryptography/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.exploresecurity.com</link>
	<description>IT security tools, techniques and commentary</description>
	<lastBuildDate>Wed, 15 Jun 2022 09:21:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>Padding Oracle Decryption Attack</title>
		<link>https://www.exploresecurity.com/padding-oracle-decryption-attack/</link>
		<comments>https://www.exploresecurity.com/padding-oracle-decryption-attack/#comments</comments>
		<pubDate>Thu, 14 Nov 2013 23:30:18 +0000</pubDate>
		<dc:creator>Jerome</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[block cipher]]></category>
		<category><![CDATA[padding oracle]]></category>

		<guid isPermaLink="false">http://www.exploresecurity.com/?p=138</guid>
		<description><![CDATA[This posting describes the process of decrypting ciphertext produced by a block cipher using a “padding oracle”. This idea – especially relating to the ASP.NET bug MS10-070 – has been documented before but I’m going to lay it out step-by-step with diagrams. I&#8217;m no cryptographer but I do find it interesting and walking it through [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>This posting describes the process of decrypting ciphertext produced by a block cipher using a “padding oracle”. This idea – especially relating to the ASP.NET bug MS10-070 – has been documented before but I’m going to lay it out step-by-step with diagrams. I&#8217;m no cryptographer but I do find it interesting and walking it through in a way that makes sense to me will be useful for another day when I&#8217;ve forgotten it all! Hopefully it will be useful to someone else too. Note that I&#8217;m taking a general case here and the technique described below does not apply <em>exactly</em> to all of the cases when a padding oracle can be exploited. Maybe that will be another posting one day. That said, let’s get on with it.<span id="more-138"></span></p>
<p><strong>Block Ciphers and Padding</strong></p>
<p>The thing about a block cipher is that it requires the input to be a whole number of blocks with each block being a fixed size. When a plaintext message isn’t exactly divisible by the block size, the last block needs some padding to make it valid. The last byte of the last block tells you how many bytes of padding there are and each padding byte has that same value.</p>
<p><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide1.png"><img class=" wp-image-139  aligncenter" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide1.png" width="576" height="432" /></a>In the diagram above we have a plaintext message broken up into blocks of 8 bytes – i.e. the cipher has a block size of 8 bytes. The last two blocks are set out in the table with two example rows. In the first example (Ex. 1) the message fits into three blocks with the third block ending with two bytes of padding (in orange). So each padding byte has a value of 2. In the second example (Ex.2) the message is exactly 24 bytes long (three blocks) and therefore another block is required for padding. Since the entire fourth block just contains padding, each of its bytes has a value of 8.</p>
<p><strong>Encryption using CBC</strong></p>
<p>When a block cipher is used in Cipher-Block-Chaining (CBC) mode, before a plaintext block is encrypted it is first XORed with the ciphertext of the previous block (as shown below). This creates an interdependence between the ciphertext blocks that cryptographers just love.</p>
<p><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide2.png"><img class=" wp-image-141  aligncenter" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide2.png" width="576" height="432" /></a>The “truth table” for XOR is also shown: when both inputs are identical, XOR returns a zero; when both inputs are different, XOR returns a one. But what about the first block when there is no previous block? In this case, an initialisation vector (IV) can be used – it’s simply a random value (well, it doesn&#8217;t have to be random but it should be), exactly the same size as the block size. The IV ensures that the same plaintext message won’t produce the same ciphertext. Of course, for the message to be decrypted in full successfully, the IV must be known to the recipient somehow. If the recipient doesn&#8217;t know it by prior arrangement, it is simply sent along with the ciphertext message.</p>
<p><strong>CBC Decryption and the Padding Oracle<br />
</strong></p>
<p>The decryption process is shown below:</p>
<p style="text-align: left;"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide3.png"><img class="aligncenter  wp-image-144" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide3.png" width="576" height="432" /></a>When decrypted, the last block’s padding is checked so that the end of the plaintext is known. The last byte must be some number between 1 and the block size inclusive &#8211; and all the padding bytes must contain that same number. If not, something has gone wrong. In crypto circles an “oracle” is a <del>database</del> black box that leaks information so a “padding oracle” is an oracle that tells you whether or not the padding of a plaintext message is correct.</p>
<p style="text-align: left;"><strong>Using the Padding Oracle for Decryption</strong></p>
<p>Let’s start our attack with the first block (you could also start with last block).</p>
<p style="text-align: left;"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide4.png"><img class="aligncenter  wp-image-145" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide4.png" width="576" height="432" /></a>We don’t know the Plaintext or the Key or the Intermediate bytes (that is, the bytes that drop out of the cipher, which then require XORing to produce the plaintext). But we do have control over the IV and the ciphertext that we send for decryption. If we send this single block as it stands (i.e. just the IV and the first block of ciphertext) to our “padding oracle”, we get a padding error returned as the last plaintext byte is not valid. There can’t be 22 bytes of padding in an 8-byte block cipher!</p>
<p>So how do we start guessing the plaintext? The attack starts by targeting the last plaintext byte (which we’ll denote as P<sub>8</sub>, the 8th plaintext byte) of the first block. Since we&#8217;re only sending this first block for decryption, the aim to is produce a P<sub>8</sub> of 1, which is a valid padding byte.</p>
<p style="text-align: left;"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide5.png"><img class="aligncenter  wp-image-146" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide5.png" width="576" height="432" /></a>Remember that P<sub>8</sub> = INT<sub>8</sub> XOR IV<sub>8</sub> and currently that’s CD XOR EF = 22. Let’s guess that P<sub>8</sub> = G (G for guess). We’ll now change the IV we send so that the last byte IV<sub>8</sub> = IV<sub>8</sub> XOR G XOR 1. The result is that the padding oracle won’t calculate the real P<sub>8</sub> but P<sub>8</sub> XOR G XOR 1. This is because we&#8217;ve effectively XORed G XOR 1 to both sides of P<sub>8</sub> = INT<sub>8</sub> XOR IV<sub>8</sub>. When the guess is correct and G = P<sub>8</sub> the padding oracle calculates P<sub>8</sub> XOR P<sub>8</sub> XOR 1. Anything XORed with itself is 0 and anything XORed with 0 is unchanged (look back at the truth table) so P<sub>8</sub> XOR P<sub>8</sub> XOR 1 = 0 XOR 1 = 1. Thus when we guess correctly, the last byte will be a valid padding byte.</p>
<p>Let’s see what happens when we guess correctly in the above example. When we make a guess of G = 22, which really is the last byte of plaintext, we change IV<sub>8</sub> to be EF XOR 22 XOR 1 = CC. When this is decrypted P<sub>8</sub> = CD XOR CC = 1 and the padding oracle doesn’t complain. Of course the message is most likely gibberish as we’ve truncated the ciphertext to just the first seven bytes, and this might produce a different error, but the point is it’s not a padding error. When we guess wrong, we get a padding error; when we guess right, we get either no padding error or a different error message.</p>
<p><strong>The Next Byte</strong></p>
<p>Since the byte we’re targeting has 256 possible values, it will take no more than 256 guesses to figure out what the value of P<sub>8</sub> is (of course, you could infer it after 255 failed guesses but it&#8217;s always nice to get positive confirmation!). We can better this if we know something about the plaintext byte, such as that it&#8217;s a printable character, in which case we can moderate our guesses. Once we know P<sub>8</sub>, we target the 7th byte in the same way – except that this time we want the final two bytes to be 2.</p>
<p style="text-align: left;"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide6.png"><img class="aligncenter  wp-image-147" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide6.png" width="576" height="432" /></a>First we fix IV<sub>8</sub> so that P<sub>8</sub> will always evaluate to 2. In the example above, we found that P<sub>8</sub> = 22 and we know that IV<sub>8</sub> = EF, thus INT<sub>8</sub> = 22 XOR EF = CD. To make P<sub>8</sub> = 2 we set IV<sub>8</sub> = CD XOR 2 = CF. Now we change the IV so that the 7th byte (IV<sub>7</sub>) = IV<sub>7</sub> XOR G XOR 2 where G is the guess for P<sub>7</sub>. When the guess is correct we’ll make P<sub>7</sub> = 2 and the overall padding for the block will now be correct, as shown above. The correct guess of 11 creates an IV<sub>7</sub> byte = CD XOR 11 XOR 2 = DE (where CD was the original value of IV<sub>7</sub>). When XORed with the corresponding intermediate byte INT<sub>7</sub> we get P<sub>7</sub> = DC XOR DE = 2, as required. Repeating this process will decrypt the first block of plaintext.</p>
<p style="text-align: left;"><strong>The Next Block</strong></p>
<p>So what about the other blocks? To find the first plaintext block we manipulated the IV but the second block of plaintext doesn&#8217;t depend on the IV but on the first block of ciphertext, over which we also have control.</p>
<p><a href="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide7.png"><img class="aligncenter size-full wp-image-149" alt="" src="http://www.exploresecurity.com/wp-content/uploads/2013/11/Slide7.png" width="960" height="720" /></a>To progress, then, we feed the padding oracle with the IV and the first two ciphertext blocks, and we simply use the same trick as before – but instead of manipulating the IV, we edit the first block of ciphertext. In the diagram above we’re targeting the last byte of block 2 by manipulating the last byte of ciphertext block 1. Again, what we’re aiming for at this stage is for the plaintext of the last byte of block 2 to evaluate to 1. When this happens the plaintext passes the padding check, but to achieve this we have corrupted the last byte of plaintext for block 1 (and the message is still truncated, remember). Again, though, it’s the absence of the padding error that we care about.</p>
<p>So that&#8217;s the general idea of how ciphertext encrypted under a block cipher can be decrypted when a padding oracle exists &#8211; and it all boils down to simple information leakage.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.exploresecurity.com/padding-oracle-decryption-attack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
