GCP - Artifact Registry Privesc
Reading time: 9 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をサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
Artifact Registry
Artifact Registryに関する詳細情報は、以下を確認してください:
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ライブラリをアップロードする
アップロードするライブラリを作成することから始めます(レジストリから最新バージョンをダウンロードできる場合はこのステップを省略できます):
- プロジェクト構造を設定します:
- ライブラリ用の新しいディレクトリを作成します。例:
hello_world_library
。 - このディレクトリ内に、パッケージ名のディレクトリを作成します。例:
hello_world
。 - パッケージディレクトリ内に
__init__.py
ファイルを作成します。このファイルは空でも、パッケージの初期化を含むこともできます。
bash
mkdir hello_world_library
cd hello_world_library
mkdir hello_world
touch hello_world/__init__.py
- ライブラリコードを書く:
hello_world
ディレクトリ内に、モジュール用の新しいPythonファイルを作成します。例:greet.py
。- "Hello, World!"関数を書きます:
python
# hello_world/greet.py
def say_hello():
return "Hello, World!"
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=[
# ライブラリに必要な依存関係
],
)
さあ、ライブラリをアップロードしましょう:
- パッケージをビルドします:
hello_world_library
ディレクトリのルートから、次のコマンドを実行します:
sh
python3 setup.py sdist bdist_wheel
- 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>
```
**python** ライブラリをダウンロードします:
<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`)
レジストリからアーティファクトを削除します。例えば、dockerイメージ:
<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が作成されると、新しいdockerイメージがプロジェクトのArtifact Registryにプッシュされます。私は新しいイメージでイメージを変更しようとし、現在のイメージ(および`cache`イメージ)を削除しても何も変わらず、Cloud Functionは動作し続けました。したがって、**バケットと同様にレースコンディション攻撃を悪用することが可能かもしれません**が、**保存されたイメージを変更するだけではCloud Functionを妥協することはできません**。
- **App Engine**
App EngineはArtifact Registry内にdockerイメージを作成しますが、**このサービス内でイメージを変更しても**、App Engineインスタンスを削除(新しいものがデプロイされる)しても、**実行されるコードは変更されません**。\
**バケットと同様にレースコンディション攻撃を実行することで、実行されるコードを上書きすることが可能かもしれませんが、これはテストされていません**。
<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)のGitHubリポジトリにPRを提出してハッキングトリックを共有してください。**
</details>
</div>