GCP - Artifact Registry Privesc

Reading time: 5 minutes

tip

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE) Azure 해킹 배우기 및 연습하기: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기

Artifact Registry

Artifact Registry에 대한 자세한 정보는 다음을 확인하세요:

GCP - Artifact Registry Enum

artifactregistry.repositories.uploadArtifacts

이 권한을 통해 공격자는 Docker 이미지와 같은 악성 코드를 포함한 아티팩트의 새 버전을 업로드할 수 있습니다:

bash
# Configure docker to use gcloud to authenticate with Artifact Registry
gcloud auth configure-docker <location>-docker.pkg.dev

# tag the image to upload it
docker tag <local-img-name>:<local-tag> <location>-docker.pkg.dev/<proj-name>/<repo-name>/<img-name>:<tag>

# Upload it
docker push <location>-docker.pkg.dev/<proj-name>/<repo-name>/<img-name>:<tag>

caution

같은 이름과 태그를 가진 새로운 악성 docker 이미지를 업로드하는 것이 가능하다는 것이 확인되었으므로, 기존 이미지는 태그를 잃게 되고 다음에 해당 태그로 이미지를 다운로드하면 악성 이미지가 다운로드됩니다.

Python 라이브러리 업로드

업로드할 라이브러리를 생성하는 것부터 시작하세요 (레지스트리에서 최신 버전을 다운로드할 수 있다면 이 단계를 건너뛸 수 있습니다):

  1. 프로젝트 구조 설정:
  • 라이브러리를 위한 새 디렉토리를 생성합니다, 예: hello_world_library.
  • 이 디렉토리 안에 패키지 이름으로 또 다른 디렉토리를 생성합니다, 예: hello_world.
  • 패키지 디렉토리 안에 __init__.py 파일을 생성합니다. 이 파일은 비어있거나 패키지 초기화를 포함할 수 있습니다.
bash
mkdir hello_world_library
cd hello_world_library
mkdir hello_world
touch hello_world/__init__.py
  1. 라이브러리 코드를 작성합니다:
  • hello_world 디렉토리 안에 모듈을 위한 새로운 Python 파일을 생성합니다, 예: greet.py.
  • "Hello, World!" 함수를 작성합니다:
python
# hello_world/greet.py
def say_hello():
return "Hello, World!"
  1. setup.py 파일 생성:
  • hello_world_library 디렉토리의 루트에 setup.py 파일을 생성합니다.
  • 이 파일은 라이브러리에 대한 메타데이터를 포함하고 Python에 설치 방법을 알려줍니다.
python
# setup.py
from setuptools import setup, find_packages

setup(
name='hello_world',
version='0.1',
packages=find_packages(),
install_requires=[
# 라이브러리에 필요한 의존성
],
)

이제 라이브러리를 업로드합시다:

  1. 패키지 빌드:
  • hello_world_library 디렉토리의 루트에서 다음을 실행합니다:
sh
python3 setup.py sdist bdist_wheel
  1. twine에 대한 인증 구성 (패키지를 업로드하는 데 사용됨):
  • twine이 설치되어 있는지 확인합니다 (pip install twine).
  • gcloud를 사용하여 자격 증명을 구성합니다:
`
```
twine upload --username 'oauth2accesstoken' --password "$(gcloud auth print-access-token)" --repository-url https://<location>-python.pkg.dev/<project-id>/<repo-name>/ dist/*
```
```
3. **빌드를 정리하다**
<div class="codeblock_filename_container"><span class="codeblock_filename_inner hljs">bash</span></div>

```bash
rm -rf dist build hello_world.egg-info
```
</details>

<div class="mdbook-alerts mdbook-alerts-caution">
<p class="mdbook-alerts-title">
  <span class="mdbook-alerts-icon"></span>
  caution
</p>


이미 존재하는 것과 동일한 버전의 python 라이브러리를 업로드하는 것은 불가능하지만, **더 높은 버전**을 업로드하거나 (작동하는 경우 버전 끝에 **`.0` 추가** - 그러나 python에서는 아님), 또는 **마지막 버전을 삭제하고 새로운 버전을 업로드할 수 있습니다** (필요한 `artifactregistry.versions.delete`):**

```sh
gcloud artifacts versions delete <version> --repository=<repo-name> --location=<location> --package=<lib-name>
```

</div>


### `artifactregistry.repositories.downloadArtifacts`

이 권한으로 **아티팩트**를 **다운로드**하고 **민감한 정보** 및 **취약점**을 검색할 수 있습니다.

**Docker** 이미지를 다운로드:
<div class="codeblock_filename_container"><span class="codeblock_filename_inner hljs">sh</span></div>

```sh
# Configure docker to use gcloud to authenticate with Artifact Registry
gcloud auth configure-docker <location>-docker.pkg.dev

# Dowload image
docker pull <location>-docker.pkg.dev/<proj-name>/<repo-name>/<img-name>:<tag>
```
**파이썬** 라이브러리 다운로드:
<div class="codeblock_filename_container"><span class="codeblock_filename_inner hljs">bash</span></div>

```bash
pip install <lib-name> --index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@<location>-python.pkg.dev/<project-id>/<repo-name>/simple/" --trusted-host <location>-python.pkg.dev --no-cache-dir
```
- 원격 레지스트리와 표준 레지스트리가 가상 레지스트리에서 혼합되고 패키지가 두 곳에 모두 존재하는 경우 어떻게 됩니까? 이 페이지를 확인하세요:

<a class="content_ref" href="../gcp-persistence/gcp-artifact-registry-persistence.md"><span class="content_ref_label">GCP - Artifact Registry Persistence</span></a>

### `artifactregistry.tags.delete`, `artifactregistry.versions.delete`, `artifactregistry.packages.delete`, (`artifactregistry.repositories.get`, `artifactregistry.tags.get`, `artifactregistry.tags.list`)

레지스트리에서 아티팩트를 삭제합니다. 예: 도커 이미지:
<div class="codeblock_filename_container"><span class="codeblock_filename_inner hljs">bash</span></div>

```bash
# Delete a docker image
gcloud artifacts docker images delete <location>-docker.pkg.dev/<proj-name>/<repo-name>/<img-name>:<tag>
```
### `artifactregistry.repositories.delete`

전체 리포지토리를 삭제합니다(내용이 있더라도):
```
gcloud artifacts repositories delete <repo-name> --location=<location>
```
### `artifactregistry.repositories.setIamPolicy`

이 권한을 가진 공격자는 이전에 언급된 일부 리포지토리 공격을 수행할 수 있는 권한을 스스로 부여할 수 있습니다.

### Artifact Registry 읽기 및 쓰기를 통한 다른 서비스로의 피벗

- **Cloud Functions**

Cloud Function이 생성될 때 새로운 도커 이미지가 프로젝트의 Artifact Registry에 푸시됩니다. 나는 새로운 이미지로 이미지를 수정하려고 시도했으며, 현재 이미지를 삭제하고(`cache` 이미지 포함) 아무것도 변경되지 않았습니다. 클라우드 함수는 계속 작동합니다. 따라서, 아마도 **버킷과 같은 Race Condition 공격을 악용하여 실행될 도커 컨테이너를 변경할 수 있을지도 모릅니다**. 그러나 **저장된 이미지를 수정하는 것만으로는 Cloud Function을 손상시킬 수 없습니다**.

- **App Engine**

App Engine이 Artifact Registry 내에서 도커 이미지를 생성하더라도, **이 서비스 내에서 이미지를 수정하고 App Engine 인스턴스를 제거(새 인스턴스가 배포됨)하더라도** **실행되는 코드는 변경되지 않습니다**.\
버킷과 같은 **Race Condition 공격을 수행하면 실행되는 코드를 덮어쓸 수 있을지도 모르지만**, 이는 테스트되지 않았습니다.

<div class="mdbook-alerts mdbook-alerts-tip">
<p class="mdbook-alerts-title">
  <span class="mdbook-alerts-icon"></span>
  tip
</p>


AWS 해킹 배우기 및 연습하기:<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
GCP 해킹 배우기 및 연습하기: <img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
Azure 해킹 배우기 및 연습하기: <img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://training.hacktricks.xyz/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">

<details>

<summary>HackTricks 지원하기</summary>

- [**구독 계획**](https://github.com/sponsors/carlospolop) 확인하기!
- **💬 [**Discord 그룹**](https://discord.gg/hRep4RUj7f) 또는 [**텔레그램 그룹**](https://t.me/peass)에 참여하거나 **Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**를 팔로우하세요.**
- **[**HackTricks**](https://github.com/carlospolop/hacktricks) 및 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.**

</details>

</div>