Arbitrary escape sequence injection in docker-machine from worker nodes
A malicious CI/CD job can replace the Docker daemon on a worker node so that it returns terminal escape sequences in its version information. When an operator runs docker-machine commands such as ls or version against that worker, the injected sequences can retitle the terminal window, plant text in the prompt to trick the user into running a command, or crash the bastion host.
- Disclosed after 2 months
Hi,
This bug report is specific to docker-machine and its utilization by Gitlab as preferred tool to allow autoscaling across different providers (https://docs.gitlab.com/runner/configuration/autoscale.html). I am not sure if it fits the scope here but wanted to reach out regarding this before I post it publicly in an upcoming blog post about general topic of escape sequence injection as it might still be relevant for you.
I know that in future docker-machine will be replaced by native support but currently this is the preferred tool to allow autoscaling. (https://gitlab.com/groups/gitlab-org/-/epics/2502)
Attack requirements and description:
-
docker-machinereports information like version number in use by worker nodes. -
worker nodes may be running with a docker socket mounted in container to allow CI builds (of course alternatives like kaniko can be used but using docker socket is still a common approach)
-
Using such an approach with docker-machine and docker executor with mounted docker socket basically means that ci/cd jobs have root access to worker nodes.
-
Now a malicious ci/cd job can overwrite locally running docker daemon with a malicious docker daemon which will still be contacted by
docker-machineto retrieve information like version, ip etc. Running commands likedocker-machine lson the bastion host would inject that malicious information in a window on Bastion node.
PoC:
-
Launch a worker instance using docker-machine and any driver.
-
On the worker node, modify docker daemon to report following string in version response:
"\033]2;\033[21t \033[1;32m\033[21t\033[Jroot@host$ \v\033[0m View all output? hit enter to continue: \v\033[A?\033[0\033[2AA\033[0",
This can be done by compiling docker from source and modifying the version information in info.go (https://github.com/moby/moby/blob/23.0/daemon/info.go#L104). Use this docker daemon to overwrite already running docker daemon on the worker node.
- Now run
docker-machine lsordocker-machine version worker_machine_nameand it will:
- change window title by injecting escape sequence using
\033[2; - Inject arbitrary input in user's prompt i.e.
lland try to trick user into hitting enter and executing the input buffer as a command.
- Using a more malicious input, it will also be possible to crash the bastion node completely.
docker-machine is executed with high privilege users and letting it output potentially malicious content can have a high impact not just with escape code injection but also if such an output is used for monitoring or automation scripts assuming that whatever it reports is validated content and not arbitrary information. docker-machine should validate this information especially for things like version
More about escape sequences which can be injected in terminals: https://marc.info/?l=bugtraq&m=104612710031920&w=2
Please review if this fits into the scope here or if it would be alright to publish it as part of a public blog post regarding such attack vectors.
Thanks!
Impact
Escape code injection Denial of Service
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 docker-machine prints daemon-reported values such as the version string to the terminal without stripping or escaping control characters.
Impact
An attacker could manipulate the operator's terminal, trick them into executing planted input, or crash the bastion host; the report notes docker-machine runs with high privileges, which raises the impact.
What the attacker needed
CI job runs on a docker-machine worker; Docker socket mounted into the CI container; Operator runs docker-machine against the worker
How it was found
- Launch a worker instance using docker-machine with any driver.
- On the worker, replace the running Docker daemon with one that returns escape sequences in its version response.
- Run docker-machine ls or docker-machine version against that worker and observe the terminal being manipulated.
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.
A terminal understands special control characters that can change the window title, move the cursor or fake text on screen. Here a worker machine controlled by an attacker sends such characters back as if they were ordinary version text, so the operator's terminal displays attacker-chosen content. The attacker cannot run code directly, but can mislead the operator into typing a command or make the terminal unusable.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.