Breaking XML Encryption
This page looks at the attack on XML Encryption in the paper How to break XML encryption [XMLENC-CBC-ATTACK].
In their paper, the authors give a "Toy Example" as a simplified demonstration of how their main attack works. Out of curiousity, we wrote a Python program to implement the attack in the Toy Example. It is surprising how easy it is to attack ciphertexts encrypted in CBC mode if we have an oracle that responds in a certain way to errors.
2020-12-30. In Part 2, we give a Python implementation that carries out a slightly simplified version of the full attack.
Back in the day (early 2000s) the only encryption algorithms specified in the W3C recommendation XML Encryption Syntax and Processing [XMLENC] were Triple DES and AES both using cipher-block chaining (CBC) confidentiality mode. The paper by Tibor Jager and Juraj Somorovsky demonstrated a very practical attack on XML block encryption when used with CBC mode.
The end result was that the XMLENC recommendation was updated to add the AES-GCM encryption algorithm and to strongly recommend against using block encryption in CBC mode. The GCM mode of encryption includes in-built authentication which protects against the described attack.
How it works
CBC-mode is malleable - flipping a bit in the IV flips the same bit in the first block of plaintext. The attack exploits a subtle correlation between the CBC mode of operation and the character encoding of encrypted text, and the response behaviour of a Web service if an XML message cannot be parsed correctly. The W3C recommended padding for block cipher mode adds to the vulnerability by only using the last byte. The Web service acts as an Oracle when sent related ciphertexts and evaluating its response. The attack relies on having a Web server that will happily respond to unlimited queries.
For full details of the attack, see the paper [XMLENC-CBC-ATTACK].
Note there is a typo in line 5 of Algorithm 1: change
Here is our code in Python 3 that implements the Toy Example: break-xmlenc.py.
Part 2. And here is our code that carries out the slightly-simplified real attack: break-xmlenc-2.py.
> pip install cryptosyspkiYou can, of course, substitute your own preferred package to carry out the cryptographic operations in the code. The result should be the same.
"Now is the time for all good men to come to the aid of their par"using AES-128 in CBC mode with the key
0xFEDCBA9876543210FEDCBA9876543210. By design, the message has a length which is an exact multiple of the AES block size (16 bytes) and consists only of 7-bit ASCII text. The ciphertext in this example is
The "Toy Example" in part 3 of the paper describes a simplified attack which assumes a certain oracle is available. The real attack is based on the same idea. The oracle accepts an IV' (a specially chosen one, not necessarily the original) and the first 16-byte block of ciphertext, C. It decrypts the block using the given IV' and the known key to produce a 16-byte plaintext m, and replies as follows:
- 1 if the plaintext m does not contain a NULL (zero) byte
- 0 otherwise; i.e. m does contain a zero byte
The algorithm proceeds by tweaking each byte in turn of IV' and testing the result with the oracle. We make a new IV" by XOR-ing a byte mask with the j-th byte of IV' and querying the oracle with this adjusted IV". If the oracle returns zero, we know we have tweaked that byte to be a zero value, and we can then recover the j-th byte of the plaintext by a simple XOR calculation. We only need to try byte masks from value 1 to 127, since we know the plaintext consists of ASCII characters. One of them will succeed.
In this manner, we can recover each byte of plaintext from the first 16-byte block C. Using the properties of CBC mode, we can repeat this analysis for the second ciphertext block C2 by setting IV' = C1.
Part 2. The slightly-simplified real example gives Python code to implement the attack described in section 4 of the paper. In the paper, the authors describe slightly-simplified algorithms and make some restrictions on the input plaintext (e.g. no "&" characters). We make another simplification in how our XML "parser" works, which does not take away from showing how the overall algorithm works (it just avoids having to set up a full XML parser that works like the old Apache Axis2).
It is assumed that the input only consists of 7-bit ASCII characters. The ASCII character set is partitioned into Type-A and Type-B characters. Type-A characters are all the illegal XML characters (0x00 to 0x1F excluding 0x09, 0x0A and 0x0D) and the characters 0x3C ">" and 0x26 "&" (these are equivalent to the single Type-A 0x00 character in the Toy Example). Type-B characters are all the 128 ASCII characters (0x00 to 0x7F) excluding the Type-A characters.
- [XMLENC] W3C Recommendation, XML Encryption Syntax and Processing Version 1.1, <https://www.w3.org/TR/xmlenc-core1/>, 11 April 2013.
- [XMLENC-CBC-ATTACK] Jager T, Somorovsky J. How to break XML encryption. In: Proceedings of the 18th ACM Conference on Computer and Communications Security, CCS 2011, Chicago, Illinois, USA, October 17-21, 2011. <PDF>.
To contact us or comment on this page, please send us a message.
This page first published 10 December 2020. Last updated 4 January 2021