Azure SQL Database에 대한 취약성 평가를 위한 기준 설정
이 PowerShell 스크립트는 Azure SQL Server의 모든 데이터베이스에 대한 최신 취약성 평가 검사 결과를 기반으로 기준을 설정합니다.
이 샘플에는 Azure PowerShell Az 1.0 이상이 필요합니다. Get-Module -ListAvailable Az
를 실행하여 설치된 버전을 확인합니다.
설치해야 하는 경우 Azure PowerShell 모듈 설치를 참조하세요.
Connect-AzAccount를 실행하여 Azure에 로그인합니다.
Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다.
샘플 스크립트
참고 항목
Azure Az PowerShell 모듈을 사용하여 Azure와 상호 작용하는 것이 좋습니다. 시작하려면 Azure PowerShell 설치를 참조하세요. Az PowerShell 모듈로 마이그레이션하는 방법에 대한 자세한 내용은 Azure PowerShell을 AzureRM에서 Azure로 마이그레이션을 참조하세요.
<#
.SYNOPSIS
This script sets the results of the last successful scan as baseline for each database under the selected Azure SQL Server.
.DESCRIPTION
This script check if the selected Azure SQL Server uses Vulnerability Assessment Express Configuration, iterates through all user databases under a server and sets the latest scan results as a baseline.
#>
$SubscriptionId = "<subscriptionid>" # The Subscription id that the server belongs to.
$ResourceGroupName = "<resource group>" # The Resource Group that the server belongs to.
$ServerName = "<server name>" # The SQL server name that we want to apply the new SQL Vulnerability Assessment policy to (short name, without suffix).
$APIVersion = "2022-05-01-preview"
###### New SQL Vulnerability Assessment Commands ######
#######################################################
function GetExpressConfigurationStatus($SubscriptionId, $ResourceGroupName, $ServerName){
$Uri = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Sql/servers/$ServerName/sqlVulnerabilityAssessments/Default?api-version=" + $APIVersion
SendRestRequest -Method "GET" -Uri $Uri
}
function SetLastScanAsBaselineOnSystemDatabase($SubscriptionId, $ResourceGroupName, $ServerName){
$Uri = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Sql/servers/$ServerName/sqlVulnerabilityAssessments/default/baselines/default?systemDatabaseName=master&api-version=" + $APIVersion
$Body = "{properties: {latestScan: true,results: {}}}"
SendRestRequest -Method "PUT" -Uri $Uri -Body $Body
}
function SetLastScanAsBaselineOnUserDatabase($SubscriptionId, $ResourceGroupName, $ServerName, $DatabaseName){
$Uri = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Sql/servers/$ServerName/databases/$DatabaseName/sqlVulnerabilityAssessments/default/baselines/default?api-version=" + $APIVersion
$Body = "{properties: {latestScan: true,results: {}}}"
SendRestRequest -Method "PUT" -Uri $Uri -Body $Body
}
function SendRestRequest(
[Parameter(Mandatory=$True)]
[string] $Method,
[Parameter(Mandatory=$True)]
[string] $Uri,
[parameter( Mandatory=$false )]
[string] $Body = "DEFAULT")
{
$AccessToken = Get-AzAccessToken
$Token = "Bearer $($AccessToken.Token)"
$headers = @{
'Authorization' = $Token
}
$Params = @{
Method = $Method
Uri = $Uri
Headers = $headers
ContentType = "application/json"
}
if(!($Body -eq "DEFAULT"))
{
$Params = @{
Method = $Method
Uri = $Uri
Body = $Body
Headers = $headers
ContentType = "application/json"
}
}
Invoke-RestMethod @Params
}
#######################################################
# Connect
Connect-AzAccount
Set-AzContext $SubscriptionId
# Check if Express Configuration is enabled
$ECState = (GetExpressConfigurationStatus -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -ServerName $ServerName).properties.State
Write-Host "Express Configuration status: " $ECState
if ($ECState -eq "Enabled")
{
# Get list of databases
$databases = Get-AzSqlDatabase -ResourceGroupName $ResourceGroupName -ServerName $ServerName | where {$_.DatabaseName -ne "master"}
# Set latest scan results as baseline on all user databases
foreach ($database in $Databases)
{
Write-Host "Set baseline on database: '$($database.DatabaseName)'"
SetLastScanAsBaselineOnUserDatabase -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $database.DatabaseName
}
Write-Host "Set baseline on 'master' database"
SetLastScanAsBaselineOnSystemDatabase -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -ServerName $ServerName
}
else
{
Write-Host "The specified server does not have VA Express Configuration enabled therefore bulk baseline operations were not performed."
return
}
다음 단계
Azure PowerShell 모듈에 대한 자세한 내용은 Azure PowerShell 설명서를 참조하세요.