Files
hacktricks-cloud/src/pentesting-cloud/azure-security/az-services/az-container-registry.md
T

9.8 KiB

Az - Container Registry

{{#include ../../../banners/hacktricks-training.md}}

기본 정보

Azure Container Registry (ACR)는 Azure cloud에서 container image를 저장, 관리 및 액세스할 수 있는 안전한 private registry입니다. 여러 Azure services와 원활하게 통합되어 대규모의 자동화된 build 및 deployment workflow를 제공합니다. geo-replication 및 vulnerability scanning과 같은 기능을 통해 ACR은 containerized application에 enterprise-grade security 및 compliance를 보장합니다.

권한

Container Registry에 부여할 수 있는 문서에 따른 다양한 권한은 다음과 같습니다.

  • Access Resource Manager
  • Create/delete registry
  • Push image
  • Pull image
  • Delete image data
  • Change policies
  • Sign images

할당할 수 있는 built-in role도 있으며, custom role을 생성하는 것도 가능합니다.

registry, image, data, policy 및 signing 작업을 관리하기 위한 Azure Container Registry built-in role 권한 매트릭스

인증

Warning

registry name에 uppercase letter가 포함되어 있더라도 login, image push 및 pull에는 항상 lowercase letter를 사용해야 합니다.

ACR에 인증하는 방법은 4가지입니다.

  • Entra ID 사용: ACR에 인증하는 기본 방법입니다. az acr login command를 사용하여 ACR에 인증합니다. 이 command는 credential을 ~/.docker/config.json file에 저장합니다. 또한 cloud shell처럼 docker socket에 액세스할 수 없는 environment에서 이 command를 실행하는 경우, --expose-token flag를 사용하여 ACR 인증에 사용할 token을 가져올 수 있습니다. 그런 다음 인증하려면 다음과 같이 username으로 00000000-0000-0000-0000-000000000000을 사용해야 합니다: docker login myregistry.azurecr.io --username 00000000-0000-0000-0000-000000000000 --password-stdin <<< $TOKEN
  • admin account 사용: admin user는 기본적으로 disabled되어 있지만 enable한 후에는 registry에 대한 full permission을 가진 admin account의 usernamepassword로 registry에 액세스할 수 있습니다. 일부 Azure services가 이를 사용하기 때문에 여전히 지원됩니다. 이 user에 대해 2개의 password가 생성되며 둘 다 유효합니다. az acr update -n <acrName> --admin-enabled true를 사용하여 enable할 수 있습니다. username은 일반적으로 registry name이며 admin이 아닙니다.
  • token 사용: registry에 액세스할 수 있도록 specific scope map (permission)을 가진 token을 생성할 수 있습니다. 그런 다음 token name을 username으로 사용하고 생성된 password 중 하나를 사용하여 docker login -u <registry-name> -p <password> <registry-url>로 registry에 인증할 수 있습니다.
  • Service Principal 사용: service principal을 생성하고 image pull을 위해 **AcrPull**과 같은 role을 할당할 수 있습니다. 그런 다음 SP appId를 username으로 사용하고 생성된 secret을 password로 사용하여 registry에 login할 수 있습니다.

registry에 대한 액세스 권한이 있는 SP를 생성하는 문서의 example script:

#!/bin/bash
ACR_NAME=$containerRegistry
SERVICE_PRINCIPAL_NAME=$servicePrincipal

# Obtain the full registry ID
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query "id" --output tsv)

PASSWORD=$(az ad sp create-for-rbac --name $SERVICE_PRINCIPAL_NAME --scopes $ACR_REGISTRY_ID --role acrpull --query "password" --output tsv)
USER_NAME=$(az ad sp list --display-name $SERVICE_PRINCIPAL_NAME --query "[].appId" --output tsv)

echo "Service principal ID: $USER_NAME"
echo "Service principal password: $PASSWORD"

암호화

Premium SKU만 이미지 및 기타 artifact에 대한 encryption at rest를 지원합니다.

네트워킹

Premium SKUprivate endpoint를 지원합니다. 다른 SKU는 public access만 지원합니다. public endpoint의 형식은 <registry-name>.azurecr.io이고 private endpoint의 형식은 <registry-name>.privatelink.azurecr.io입니다. 따라서 registry 이름은 모든 Azure에서 고유해야 합니다.

Microsoft Defender for Cloud

이를 사용하면 registry의 image를 scan하여 vulnerability를 확인할 수 있습니다.

Soft-delete

soft-delete 기능을 사용하면 지정된 일수 이내에 삭제된 registry를 복구할 수 있습니다. 이 기능은 기본적으로 비활성화되어 있습니다.

Webhooks

registry 내부에 webhook을 생성할 수 있습니다. 이 webhook에는 push 또는 delete action이 수행될 때마다 request가 전송될 URL을 지정해야 합니다. 또한 Webhook은 영향을 받을 repository(image)를 지정하는 scope를 설정할 수 있습니다. 예를 들어 'foo:\*'는 repository 'foo' 아래에서 발생하는 event를 의미합니다.

공격자의 관점에서는 registry에서 action을 수행하기 전에 이를 확인하고, 탐지를 피하기 위해 필요한 경우 일시적으로 제거하는 것이 중요합니다.

Connected registries

이는 기본적으로 한 registry의 image를 다른 registry로 mirror할 수 있도록 하며, 일반적으로 다른 registry는 on-premises에 위치합니다.

두 가지 mode가 있습니다: ReadOnlyReadWrite. 첫 번째 mode에서는 source registry에서 image를 pull하기만 하며, 두 번째 mode에서는 source registry로 image를 push할 수도 있습니다.

client가 Azure에서 registry에 액세스하려면 connected registry가 사용될 때 token이 생성됩니다.

Runs & Tasks

Runs & Tasks를 사용하면 일반적으로 로컬이나 CI/CD pipeline에서 수행해야 하는 container 관련 action을 Azure에서 실행할 수 있습니다. 예를 들어 registry에서 image를 build, push 및 run할 수 있습니다.

container를 build하고 run하는 가장 쉬운 방법은 일반적인 Run을 사용하는 것입니다:

# Build
echo "FROM mcr.microsoft.com/hello-world" > Dockerfile
az acr build --image sample/hello-world:v1 --registry mycontainerregistry008 --file Dockerfile .

# Run
az acr run --registry mycontainerregistry008 --cmd '$Registry/sample/hello-world:v1' /dev/null

하지만 이로 인해 attacker 관점에서는 그다지 흥미롭지 않은 실행이 발생합니다. 해당 실행에는 연결된 managed identity가 없기 때문입니다.

그러나 tasks에는 system 및 user managed identity를 연결할 수 있습니다. 이러한 tasks는 컨테이너에서 권한 상승에 유용합니다. 권한 상승 섹션에서는 tasks를 사용하여 권한을 상승시키는 방법을 확인할 수 있습니다.

Cache

Cache 기능을 사용하면 외부 repository에서 이미지를 다운로드하고 registry에 새 버전을 저장할 수 있습니다. 이를 위해서는 Azure Vault에서 credentials를 선택하여 일부 credentials를 구성해야 합니다.

이는 attacker 관점에서 매우 흥미롭습니다. attacker가 credentials에 액세스할 수 있는 충분한 권한을 가지고 있다면 외부 platform으로 pivot할 수 있기 때문입니다. 또한 외부 repository에서 이미지를 다운로드하고 cache를 구성하는 것은 persistence mechanism으로도 사용될 수 있습니다.

Enumeration

Warning

registry 이름에 대문자가 포함되어 있더라도 해당 registry에 액세스하는 url에서는 소문자만 사용해야 한다는 점이 매우 중요합니다.

# List of all the registries
# Check the network, managed identities, adminUserEnabled, softDeletePolicy, url...
az acr list

# Get the details of a registry
az acr show --name <registry-name>

# List tokens of a registry
az acr token list --registry <registry-name> --resource-group <res-group>

# List repositories in a registry
az acr repository list --name <registry-name> --resource-group <res-group>

# List the tags of a repository
az acr repository show-tags --repository <repository-name> --name <registry-name> --resource-group <res-group>

# List deleted repository tags
## At the time of this writing there isn't yet any command to restore it
az acr repository list-deleted --name <registry-name>

# List tasks
## Check the git URL or the command
az acr task list --registry <registry-name>

# List tasks runs
az acr task list-runs --registry <registry-name>

# List connected registries
az acr connected-registry list --registry <registry-name>

# List cache
az acr cache list --registry <registry-name>

# Get cache details
az acr cache show --name <cache-name> --registry <registry-name>

인증되지 않은 액세스

{{#ref}} ../az-unauthenticated-enum-and-initial-entry/az-container-registry-unauth.md {{#endref}}

Privilege Escalation & Post Exploitation

{{#ref}} ../az-privilege-escalation/az-container-registry-privesc.md {{#endref}}

{{#ref}} ../az-post-exploitation/az-container-registry-post-exploitation.md {{#endref}}

참고 자료

{{#include ../../../banners/hacktricks-training.md}}