-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add vulnerability assessment cmdlets for IAAS in Az.Security #14607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
03adaa1
Add sql vulnerability assessment model
08a150a
temp
00e5561
Add Sql Vulnerability asessment model + Cmdlets implementation
192eb51
add Documentation files
e1ceff2
Fix design review comments
b6dfc4e
Add test scenario
0c47d96
Add Tests
31997c3
Add vm domail label to creating vm in test
48e1e4f
adjust the reference for monitoringSolutions
ef7b9b8
Change test category to live only.
900e505
Remove unnecessary dependencies
2aa5850
sync repo and update versions
ba352ab
update changelog.md
bc2303e
Fix PR comments
74101f9
Modify mark down files and add examples
8108269
Rename baseline cmdlets
208ed4d
rename files for baseline cmdlets
a601aed
Add Examples for set baseline cmdlet + some final adjustments to the …
3646385
Fixed PR comments.
a609c9f
Fix some static analysis issues
d1f3265
Add module name to cmdlets to distinguish them from PAAS cmdlets.
5ce43d4
Rename files.
8ee87ed
Add should process + rename help files.
3fd3542
update documentation.
bf2b30f
fix rename issues from tests.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/Security/Security.Test/ScenarioTests/SqlVulnerabilityAssessmentTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using Microsoft.Azure.Commands.ScenarioTest; | ||
| using Microsoft.Azure.ServiceManagement.Common.Models; | ||
| using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.Commands.Security.Test.ScenarioTests | ||
| { | ||
| public class SqlVulnerabilityAssessmentTests | ||
| { | ||
| private readonly XunitTracingInterceptor _logger; | ||
|
|
||
| public SqlVulnerabilityAssessmentTests(Xunit.Abstractions.ITestOutputHelper output) | ||
| { | ||
| _logger = new XunitTracingInterceptor(output); | ||
| XunitTracingInterceptor.AddToContext(_logger); | ||
| TestExecutionHelpers.SetUpSessionAndProfile(); | ||
| } | ||
|
|
||
| /* | ||
| * This test is live only because it cannot be recorded. | ||
| * New-AzMonitorLogAnalyticsSolution cmdlet depends on Az.MonitoringSolution | ||
| * which is a generated module and is not currently supported by the testing framework | ||
| */ | ||
| [Fact] | ||
| [Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
| public void TestAzSecuritySecuritySqlVulnerabilityAssessment() | ||
| { | ||
| TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzSecuritySecuritySqlVulnerabilityAssessment"); | ||
| } | ||
| } | ||
| } | ||
197 changes: 197 additions & 0 deletions
197
src/Security/Security.Test/ScenarioTests/SqlVulnerabilityAssessmentTests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| # ---------------------------------------------------------------------------------- | ||
| # | ||
| # Copyright Microsoft Corporation | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ---------------------------------------------------------------------------------- | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Tests end to end scenario for SQL vulnerability assessment on SQL VM. | ||
| This test is live only because it cannot be recorded. | ||
| New-AzMonitorLogAnalyticsSolution cmdlet depends on Az.MonitoringSolution | ||
| which is a generated module and is not currently supported by the testing framework | ||
| #> | ||
| function Test-AzSecuritySecuritySqlVulnerabilityAssessment | ||
BethanyZhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| # Setup | ||
| $testPrefix = "pssqlva" | ||
| $testParams = Get-SecuritySqlVulnerabilityAssessmentTestEnvironmentParameters $testPrefix | ||
| $vmResourceId = "/subscriptions/" + $testParams.subscriptionId + "/resourceGroups/" + $testParams.rgName + "/providers/Microsoft.Compute/VirtualMachines/" + $testParams.sqlVmNamePrefix | ||
| Create-TestEnvironmentWithParams $testParams | ||
|
|
||
| $vaScanRecord = Get-AzSecuritySqlVulnerabilityAssessmentScanRecord -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -ScanId latest | ||
| $resultsOnMaster = Get-AzSecuritySqlVulnerabilityAssessmentScanResult -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master | ||
| $resultsWithFindingsOnMaster = $resultsOnMaster | where { $_.Status -eq "Finding" } | ||
|
|
||
| Assert-True { $resultsOnMaster.Count -eq $vaScanRecord.TotalRulesCount } | ||
| Assert-True { $resultsWithFindingsOnMaster.Count -eq $vaScanRecord.TotalFailedRulesCount } | ||
|
|
||
| $finding = $resultsWithFindingsOnMaster | select -first 1 | ||
| $ruleResult = Get-AzSecuritySqlVulnerabilityAssessmentScanResult -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -ScanId $vaScanRecord.Name -RuleId $finding.Name | ||
|
|
||
| Assert-True { $finding.Name -eq $ruleResult.Name } | ||
|
|
||
| # check add baseline with latest. | ||
| Add-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $finding.Name | ||
|
|
||
| $baseline = Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $finding.Name | ||
|
|
||
| Assert-NotNull $baseline | ||
|
|
||
| $baseline | Remove-AzSecuritySqlVulnerabilityAssessmentBaseline -Force | ||
|
|
||
| Assert-Throws { Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $finding.Name } | ||
|
|
||
| # check Add baseline with result | ||
| Add-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $finding.Name -Baseline $finding.QueryResults | ||
|
|
||
| $baseline = Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $finding.Name | ||
| Assert-NotNull $baseline | ||
|
|
||
| $baseline | Remove-AzSecuritySqlVulnerabilityAssessmentBaseline -Force | ||
|
|
||
| ### Check piping | ||
|
|
||
| $resultsOnMsdb = Get-AzSecuritySqlVulnerabilityAssessmentScanResult -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database msdb | ||
| $resultsWithFindingsOnMsdb = $resultsOnMsdb | where { $_.Status -eq "Finding" } | ||
| $rulesNamesOnMsdb = $resultsWithFindingsOnMsdb | select -ExpandProperty Name | ||
| $rulesNamesOnMaster = $resultsWithFindingsOnMaster | select -ExpandProperty Name | ||
|
|
||
| # get rules intersection between master and msdb | ||
| $ruleWithFindingsOnBothDbs = $rulesNamesOnMaster | ? {$rulesNamesOnMsdb -contains $_} | ||
|
|
||
| if ($ruleWithFindingsOnBothDbs.Count -gt 0) | ||
| { | ||
| # add baseline on master | ||
| Add-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $ruleWithFindingsOnBothDbs[0] | ||
|
|
||
| # bypass it to msdb | ||
| Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -RuleId $ruleWithFindingsOnBothDbs[0] ` | ||
| | Add-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database msdb | ||
|
|
||
| $baseline = Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database msdb -RuleId $ruleWithFindingsOnBothDbs[0] | ||
| Assert-NotNull $baseline | ||
| } | ||
|
|
||
| # Set all latest results as Baseline | ||
| Set-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -Force | ||
| $vaScanRecord = Get-AzSecuritySqlVulnerabilityAssessmentScanRecord -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -ScanId latest | ||
|
|
||
| Assert-True { $vaScanRecord.State -eq "Passed"} | ||
| Assert-True { $vaScanRecord.TotalFailedRulesCount -eq 0 } | ||
|
|
||
| Get-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master | Remove-AzSecuritySqlVulnerabilityAssessmentBaseline -Force | ||
|
|
||
| $baselineSet = @{} | ||
| $resultsWithFindingsOnMaster | select -skip 3 | ForEach-Object { $baselineSet.Add($_.RuleId, $_.QueryResults)} | ||
|
|
||
| Set-AzSecuritySqlVulnerabilityAssessmentBaseline -ResourceId $vmResourceId -WorkspaceId $testParams.workspaceId -Server $testParams.sqlServerName -Database master -BaselineSet $baselineSet -Force | ||
|
|
||
| Delete-TestEnvironments ($testParams) | ||
| } | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Gets the values of the parameters used at the tests | ||
| #> | ||
| function Get-SecuritySqlVulnerabilityAssessmentTestEnvironmentParameters ($testPrefix) | ||
| { | ||
| $location = Get-Location "Microsoft.Compute" "virtualMachines" "East Us 2 Euap"; | ||
| $sqlVmName = getAssetName ($testPrefix +'vm'); | ||
|
|
||
| return @{ subscriptionId = (Get-AzContext).Subscription.Id; | ||
| rgName = getAssetName ($testPrefix); | ||
| sqlVmNamePrefix = $sqlVmName; | ||
| sqlVmDomain_prefix = 'domainvm'; | ||
| sqlVmMaxLength = 15; | ||
| sqlVmUserName = 'testuser'; | ||
| sqlVmPassword = Generate-RandomVmPassword; | ||
| sqlServerImage = 'MicrosoftSQLServer:SQL2017-WS2016:Enterprise:latest'; | ||
| sqlServerVmSize = 'Standard_DS2_v2'; | ||
| sqlServerName = "MSSQLSERVER" | ||
| operationalInsightsWorkspaceName = getAssetName ($testPrefix +"psWorkspace"); | ||
| workspaceId = ""; | ||
| location = location; | ||
| vmLocation = $location.Replace(' ', ''); | ||
| vmDomainNameLabel = (getAssetName ($sqlVmName +'-')).ToLower(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Creates the basic test environment needed to perform the sql vulnerability assessment tests - resource group, VM, workspace,... etc | ||
| #> | ||
| function Create-TestEnvironmentWithParams ($testParams) | ||
| { | ||
| # Create a new resource group. | ||
| New-AzResourceGroup -Name $testParams.rgName -Location $testParams.location | ||
|
|
||
| $passWord = ConvertTo-SecureString -String $testParams.sqlVmPassword -AsPlainText -Force | ||
| $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $testParams.sqlVmUserName, $passWord | ||
|
|
||
| # Create the sql virtual machine. | ||
| Write-Host "ResourceGroupName " + $testParams.rgName + " -Location " + $testParams.location " -Size "+ $testParams.sqlServerVmSize + " -Image " + $testParams.sqlServerImage + " -Credential " + $cred " -Name " + $testParams.sqlVmNamePrefix | ||
|
|
||
| New-AzVm -ResourceGroupName $testParams.rgName -Location $testParams.vmLocation -Image $testParams.sqlServerImage -Credential $cred -Name $testParams.sqlVmNamePrefix -DomainNameLabel $testParams.vmDomainNameLabel | ||
|
|
||
| # Create the log analytics worskspace | ||
| $workspace = New-AzOperationalInsightsWorkspace -Location $testParams.location -Name $testParams.operationalInsightsWorkspaceName -ResourceGroupName $testParams.rgName | ||
| New-AzMonitorLogAnalyticsSolution -Type SQLVulnerabilityAssessment -ResourceGroupName $testParams.rgName -Location $testParams.location -WorkspaceResourceId $workspace.ResourceId | ||
|
|
||
| # Install microsoft Monitoring agent on the VM | ||
| $workspaceKeys = Get-AzOperationalInsightsWorkspaceSharedKey -Name $testParams.operationalInsightsWorkspaceName -ResourceGroupName $testParams.rgName | ||
| $publicSettings = @{"workspaceId" = $workspace.CustomerId} | ||
| $protectedSettings = @{"workspaceKey" = $workspaceKeys.PrimarySharedKey} | ||
|
|
||
| $testParams.workspaceId = $workspace.CustomerId; | ||
|
|
||
| Set-AzVMExtension -ExtensionName "MicrosoftMonitoringAgent" ` | ||
| -ResourceGroupName $testParams.rgName ` | ||
| -VMName $testParams.sqlVmNamePrefix` | ||
| -Publisher "Microsoft.EnterpriseCloud.Monitoring" ` | ||
| -ExtensionType "MicrosoftMonitoringAgent" ` | ||
| -TypeHandlerVersion 1.0 ` | ||
| -Settings $publicSettings ` | ||
| -ProtectedSettings $protectedSettings ` | ||
| -Location $testParams.vmLocation | ||
|
|
||
| # Update the registery and restart the Monitoring agent to force a scan. | ||
| Invoke-AzVMRunCommand -ResourceGroupName $testParams.rgName -Name $testParams.sqlVmNamePrefix -CommandId 'RunPowerShellScript' -ScriptPath 'SecuritySqlVulnerabilityAssessmentTestResources\SetUpVm.ps1' | ||
|
|
||
| Start-Sleep -Seconds 180 | ||
| } | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Deletes the Test enviroment once the test passes. | ||
| #> | ||
| function Delete-TestEnvironments ($testParams) | ||
| { | ||
| New-AzResourceGroup -Name $testParams.rgName | ||
| } | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Generates a random password for vm that satisfies these conditions: | ||
| - At least 15 character long | ||
| - Contains at at least one number | ||
| - Contains at least one lower case letter | ||
| - Contains at least one upper case letter | ||
| #> | ||
| function Generate-RandomVmPassword() | ||
| { | ||
| $password = ("abcdefghijklmnopqrstuvwxyz".tochararray() | sort {Get-Random})[0..4] -join '' | ||
| $password += ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".tochararray() | sort {Get-Random})[0..7] -join '' | ||
| $password += ("0123456789".tochararray() | sort {Get-Random})[0..4] -join '' | ||
|
|
||
| return ($password.tochararray() | sort {Get-Random}) -join '' | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Security/Security.Test/SqlVulnerabilityAssessmentTestResources/SetUpVM.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| New-Item -ItemType Directory -Force -Path C:\\Users\\testuser\\Desktop\\Va_Logs\ | ||
| New-Item -ItemType Directory -Force -Path C:\\Users\\testuser\\Desktop\\Setup_Logs\ | ||
| New-Item -Path HKLM:\\Software\\Microsoft\\AzureOperationalInsights\ | ||
| Set-ItemProperty -Path HKLM:\\Software\\Microsoft\\AzureOperationalInsights -Name SqlVulnerabilityAssessment_LogDirectoryPath -Value C:\\Users\\testuser\\Desktop\\Va_Logs\ | ||
| Set-ItemProperty -Path HKLM:\\Software\\Microsoft\\AzureOperationalInsights -Name SqlVulnerabilityAssessment_BypassHashCheck -Value true\ | ||
| Set-ItemProperty -Path HKLM:\\Software\\Microsoft\\AzureOperationalInsights -Name SqlVulnerabilityAssessment_TestMachine -Value true | ||
|
|
||
| Start-Sleep -Seconds 60 | ||
| Restart-Service HealthService |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.