Skip to main content
  1. All Posts/

nextcloud-tools

Tools PHP

nextcloud-tools

The nextcloud-tools have been developed by SysEleven to debug the encryption and signature methods executed by the default encryption module of Nextcloud. The provided scripts may be helpful for other Nextcloud users and developers to debug problems or restore files.

Rescue Tooling

Rescue tooling is located in the ./rescue/ subfolder.

decrypt-all-files.php

This script can save your precious files in cases where you encrypted them with the Nextcloud Server Side Encryption and still have access to the data directory and the Nextcloud configuration file (“config/config.php”). This script is able to decrypt locally stored files within the data directory. It supports master-key encrypted files, user-key encrypted files and can also use a rescue key (if enabled) and the public sharing key if files had been publicly shared.
Update 2022-12-28: The decrypt-all-files.php script now supports the new binary encoding that was introduced with the Nextcloud 25 release. Furthermore, the code has been reworked and smaller improvements have been added.
Update 2022-07-14: The decrypt-all-files.php script now includes a PHP-only implementation of RC4 so that files can be decrypted even when the legacy support of OpenSSL v3 is not enabled. You can enable the OpenSSL v3 legacy support by adding the following configuration to the end of your openssl.cnf file that MartB has provided:

[provider_sect]
default = default_sect
legacy  = legacy_sect

[default_sect]
activate = 1

[legacy_sect]
activate = 1

Update 2022-07-14: @fastlorenzo has provided a patch so that the decrypt-all-files.php script now supports even older encrypted files.
Update 2021-07-05: The decrypt-all-files.php script now has improved support for external storages as well as the updated encrypted JSON key format that is introduced with the Nextcloud 21 release. It also supports the decryption of single files and a failed encryption can be resumed by starting the script again.
Update 2020-08-29: The decrypt-all-files.php script now has basic support for external storages as well as the encrypted JSON key format that is introduced with the Nextcloud 20 release.

Configuration

Nextcloud Definitions

The Nextcloud definitions are values that you have to copy from the Nextcloud configuration file ("config/config.php"). The names of the values are equal to the ones found in the Nextcloud configuration file.

  • DATADIRECTORY – this is the location of the data directory of your Nextcloud instance, if you copied or moved your data directory then you have to set this value accordingly, this directory has to exist and contain the typical file structure of Nextcloud
  • INSTANCEID – this is a value from the Nextcloud configuration file, there does not seem to be another way to retrieve this value
  • SECRET – this is a value from the Nextcloud configuration file, there does not seem to be another way to retrieve this value
Custom Definitions

The custom definitions define how the decrypt-all-files.php script works internally. These are the supported configuration values:

  • RECOVERY_PASSWORD – this is the password for the recovery key, you can set this value if you activated the recovery feature of your Nextcloud instance, leave this value empty if you did not acticate the recovery feature of your Nextcloud instance
  • USER_PASSWORDS – these are the passwords for the user keys, you have to set these values if you disabled the master key encryption of your Nextcloud instance, you do not have to set these values if you did not disable the master key encryption of your Nextcloud instance, each value represents a (username, password) pair and you can set as many pairs as necessary
  • EXTERNAL_STORAGES – these are the mount paths of external folders, you have to set these values if you used external storages within your Nextcloud instance, each value represents an (external storage, mount path) pair and you can set as many pairs as necessary, the external storage name has to be written as found in the DATADIRECTORY/files_encryption/keys/files/ folder, if the external storage belongs to a specific user then the name has to contain the username followed by a slash followed by the external storage name as found in the DATADIRECTORY/$username/files_encryption/keys/files/ folder, the external storage has to be mounted by yourself and the corresponding mount path has to be set
  • SUPPORT_MISSING_HEADERS – this is a value that tells the script if you have encrypted files without headers, this configuration is only needed if you have data from a VERY old OwnCloud/Nextcloud instance, you probably should not set this value as it will break unencrypted files that may live alongside your encrypted files

Execution

To execute the script you have to call it in the following way:

./decrypt-all-files.php <targetdir> [<sourcedir>|<sourcefile>]*
  • <targetdir> – this is the target directory where the decrypted files get stored, the target directory has to already exist and should be empty as already-existing files will be skipped, make sure that there is enough space to store all decrypted files in the target directory
  • <sourcedir> – this is the name of the source folder which shall be decrypted, the name of the source folder has to be either absolute or relative to the `DATADIRECTORY, if this parameter is not provided then all files in the data directory will be decrypted
  • <sourcefile> – this is the name of the source file which shall be decrypted, the name of the source file has to be either absolute or relative to the DATADIRECTORY, if this parameter is not provided then all files in the data directory will be decrypted

The execution may take a lot of time, depending on the power of your computer and on the number and size of your files. Make sure that the script is able to run without interruption. As of now it does not have a resume feature. On servers you can achieve this by starting the script within a screen session.
Also, the script currently does not support the decryption of files in the trashbin that have been deleted from external storage as Nextcloud creates zero byte files when deleting such a file instead of copying over its actual content.

Debug Tooling

The debug tooling only supports older versions of the Nextcloud Server Side Encryption. Use the rescue tooling instead which is kept up-to-date.
Debug tooling is located in the ./debug/ subfolder.

check-signature.php

The check-signature.php script contains a re-implementation of Nextcloud’s signature checking process. It supports different types of private keys – including master keys, public sharing keys, recovery keys and user keys. Furthermore, it supports different types of files – including regular files, version files, trashed files and trashed version files.
Update: Nextcloud now finally supports the correction of version information through the ./occ encryption:fix-encrypted-version command which has been ported over from OwnCloud. Using the built-in command will be easier to use to fix broken file signatures.

Preparation

As the check-signature.php script does not implement database accesses, the necessary Nextcloud database tables have to be provided in the form of well-structured CSV files. These files can be exported directly from the database.

MariaDB/MySQL

To export the necessary CSV files from MariaDB/MySQL you have to connect to the correct database: sudo mysql -D <dbname>
Then you can execute the export:

SELECT storage, path, encrypted FROM oc_filecache INTO OUTFILE '/var/lib/mysql-files/filecache.csv' FIELDS ESCAPED BY '' TERMINATED BY ',' LINES TERMINATED BY 'n';

SELECT numeric_id, id FROM oc_storages INTO OUTFILE '/var/lib/mysql-files/storages.csv' FIELDS ESCAPED BY '' TERMINATED BY ',' LINES TERMINATED BY 'n';

QUIT;

You finally have to move the CSV files to their target location:

sudo mv /var/lib/mysql-files/filecache.csv /tmp/

sudo mv /var/lib/mysql-files/storages.csv /tmp/
PostgreSQL

To export the necessary CSV files from…