GCP - Non-svc Persistance

Support HackTricks

어떤 방식으로든 GCP 자격 증명이나 GCP 환경에서 실행 중인 머신을 손상시킨 경우 유용한 기술입니다.

Token Hijacking

Authenticated User Tokens

사용자의 현재 토큰을 얻으려면 다음을 실행할 수 있습니다:

sqlite3 $HOME/.config/gcloud/access_tokens.db "select access_token from access_tokens where account_id='<email>';"

이 페이지에서 gcloud를 사용하여 이 토큰을 직접 사용하는 방법을 확인하세요:

새로운 액세스 토큰을 생성하는 세부 정보를 얻으려면 다음을 실행하세요:

sqlite3 $HOME/.config/gcloud/credentials.db "select value from credentials where account_id='<email>';"

$HOME/.config/gcloud/application_default_credentials.json 및 **$HOME/.config/gcloud/legacy_credentials/*/adc.json**에서 refresh token을 찾을 수도 있습니다.

refresh token, client ID, 및 client secret을 사용하여 새로 갱신된 access token을 얻으려면 다음을 실행하십시오:

curl -s --data client_id=<client_id> --data client_secret=<client_secret> --data grant_type=refresh_token --data refresh_token=<refresh_token> --data scope="https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/accounts.reauth" https://www.googleapis.com/oauth2/v4/token

리프레시 토큰의 유효성은 Admin > Security > Google Cloud session control에서 관리할 수 있으며, 기본적으로 16시간으로 설정되어 있지만 만료되지 않도록 설정할 수도 있습니다:

Auth flow

gcloud auth login과 같은 것을 사용할 때 인증 흐름은 브라우저에서 프롬프트를 열고 모든 범위를 수락한 후 브라우저가 도구에 의해 열려 있는 http 포트로 다음과 같은 요청을 보낼 것입니다:

/?state=EN5AK1GxwrEKgKog9ANBm0qDwWByYO&code=4/0AeaYSHCllDzZCAt2IlNWjMHqr4XKOuNuhOL-TM541gv-F6WOUsbwXiUgMYvo4Fg0NGzV9A&scope=email%20openid%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/cloud-platform%20https://www.googleapis.com/auth/appengine.admin%20https://www.googleapis.com/auth/sqlservice.login%20https://www.googleapis.com/auth/compute%20https://www.googleapis.com/auth/accounts.reauth&authuser=0&prompt=consent HTTP/1.1

그런 다음, gcloud는 일부 하드코딩된 client_id (32555940559.apps.googleusercontent.com) 및 client_secret (ZmssLNjJy2998hD4CTg2ejr2)와 함께 상태 및 코드를 사용하여 최종 리프레시 토큰 데이터를 얻습니다.

localhost와의 통신이 HTTP로 이루어지므로 데이터를 가로채서 리프레시 토큰을 얻을 수 있지만, 이 데이터는 한 번만 유효하므로 쓸모가 없습니다. 파일에서 리프레시 토큰을 읽는 것이 더 쉽습니다.

OAuth Scopes

모든 Google 스코프는 https://developers.google.com/identity/protocols/oauth2/scopes에서 찾을 수 있으며, 다음 명령을 실행하여 얻을 수 있습니다:

curl "https://developers.google.com/identity/protocols/oauth2/scopes" | grep -oE 'https://www.googleapis.com/auth/[a-zA-A/\-\._]*' | sort -u

이 스크립트를 사용하여 **gcloud**가 인증에 사용하는 애플리케이션이 지원할 수 있는 범위를 확인할 수 있습니다:

curl "https://developers.google.com/identity/protocols/oauth2/scopes" | grep -oE 'https://www.googleapis.com/auth/[a-zA-Z/\._\-]*' | sort -u | while read -r scope; do
echo -ne "Testing $scope         \r"
if ! curl -v "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=32555940559.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+$scope+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fsqlservice.login+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=AjvFqBW5XNIw3VADagy5pvUSPraLQu&access_type=offline&code_challenge=IOk5F08WLn5xYPGRAHP9CTGHbLFDUElsP551ni2leN4&code_challenge_method=S256" 2>&1 | grep -q "error"; then
echo ""
echo $scope
fi
done

실행 후 이 앱이 다음 범위를 지원하는지 확인했습니다:

https://www.googleapis.com/auth/appengine.admin
https://www.googleapis.com/auth/bigquery
https://www.googleapis.com/auth/cloud-platform
https://www.googleapis.com/auth/compute
https://www.googleapis.com/auth/devstorage.full_control
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/userinfo.email

이 앱이 drive 범위를 지원하는 것을 보면 흥미롭습니다. 이는 공격자가 사용자가 이 범위로 토큰을 생성하도록 강제할 수 있다면 GCP에서 Workspace로 권한을 상승시킬 수 있게 합니다.

여기서 이것을 악용하는 방법을 확인하세요.

Service Accounts

인증된 사용자와 마찬가지로, 서비스 계정의 개인 키 파일을 탈취하면 원하는 만큼 액세스할 수 있습니다. 그러나 서비스 계정의 OAuth 토큰을 탈취하면 더 흥미로울 수 있습니다. 기본적으로 이러한 토큰은 한 시간 동안만 유효하지만, 피해자가 개인 API 키를 삭제하더라도 OAuth 토큰은 만료될 때까지 유효합니다.

Metadata

분명히, GCP 환경에서 실행 중인 머신 내부에 있는 한, 메타데이터 엔드포인트에 접속하여 해당 머신에 연결된 서비스 계정에 액세스할 수 있습니다 (이 엔드포인트에서 액세스할 수 있는 OAuth 토큰은 보통 범위에 의해 제한됩니다).

Remediations

이러한 기술에 대한 몇 가지 완화 방법은 https://www.netskope.com/blog/gcp-oauth-token-hijacking-in-google-cloud-part-2에서 설명되어 있습니다.

References

Support HackTricks

Last updated