Module/Rule.SqlScriptQuery/Convert/SqlScriptQueryRule.Convert.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
using module .\..\..\Common\Common.psm1
using module .\..\SqlScriptQueryRule.psm1

$exclude = @($MyInvocation.MyCommand.Name,'Template.*.txt')
$supportFileList = Get-ChildItem -Path $PSScriptRoot -Exclude $exclude
foreach ($supportFile in $supportFileList)
{
    Write-Verbose "Loading $($supportFile.FullName)"
    . $supportFile.FullName
}
# Header

    <#
    .SYNOPSIS
        Convert the contents of an xccdf check-content element into a
        SqlScriptQueryRule object
    .DESCRIPTION
        The SqlScriptQueryRule class is used to extract the SQL Server settings
        from the check-content of the xccdf. Once a STIG rule is identified as a
        SQL script query rule, it is passed to the SqlScriptQueryRule class for
        parsing and validation.
    #>

class SqlScriptQueryRuleConvert : SqlScriptQueryRule
{
    <#
        .SYNOPSIS
            Empty constructor for SplitFactory
    #>

    SqlScriptQueryRuleConvert ()
    {
    }

    <#
        .SYNOPSIS
            Converts a xccdf STIG rule element into a Sql Script Query Rule
        .PARAMETER XccdfRule
            The STIG rule to convert
    #>

    SqlScriptQueryRuleConvert ([xml.xmlelement] $XccdfRule) : base ($XccdfRule, $true)
    {
        $ruleType = $this.GetRuleType($this.splitCheckContent)
        $fixText = [SqlScriptQueryRule]::GetFixText($XccdfRule)

        $this.SetGetScript($ruleType)
        $this.SetTestScript($ruleType)
        $this.SetSetScript($ruleType, $fixText)
        $this.SetVariable($ruleType)
        if ($null -ne $this.Variable)
        {
            $this.SetOrganizationValueTestString($ruleType)
        }
        $this.SetDuplicateRule()
        $this.SetDscResource()
    }

    #region Methods

    <#
        .SYNOPSIS
            Extracts the get script from the check-content and sets the value
        .DESCRIPTION
            Gets the get script from the xccdf content and sets the value. If
            the script that is returned is not valid, the parser status is set
            to fail.
        .PARAMETER RuleType
            The type of rule to get the get script for
    #>

    [void] SetGetScript ([string] $RuleType)
    {
        $thisGetScript = & Get-$($RuleType)GetScript -CheckContent $this.SplitCheckContent

        if (-not $this.SetStatus($thisGetScript))
        {
            $this.set_GetScript($thisGetScript)
        }
    }

    <#
        .SYNOPSIS
            Extracts the test script from the check-content and sets the value
        .DESCRIPTION
            Gets the test script from the xccdf content and sets the value. If
            the script that is returned is not valid, the parser status is set
            to fail.
        .PARAMETER RuleType
            The type of rule to get the test script for
    #>

    [void] SetTestScript ($RuleType)
    {
        $thisTestScript = & Get-$($RuleType)TestScript -CheckContent $this.SplitCheckContent

        if (-not $this.SetStatus($thisTestScript))
        {
            $this.set_TestScript($thisTestScript)
        }
    }

    <#
        .SYNOPSIS
            Extracts the set script from the check-content and sets the value
        .DESCRIPTION
            Gets the set script from the xccdf content and sets the value. If
            the script that is returned is not valid, the parser status is set
            to fail.
        .PARAMETER RuleType
            The type of rule to get the set script for
        .PARAMETER FixText
            The set script to run
    #>

    [void] SetSetScript ([string] $RuleType, [string[]] $FixText)
    {
        $checkContent = $this.SplitCheckContent

        $thisSetScript = & Get-$($RuleType)SetScript -FixText $FixText -CheckContent $checkContent

        if (-not $this.SetStatus($thisSetScript))
        {
            $this.set_SetScript($thisSetScript)
        }
    }

    <#
        .SYNOPSIS
            Extracts the variable
        .DESCRIPTION
            Gets the variable string to be used in the SqlScriptQuery resource
        .PARAMETER RuleType
            The type of rule to get the variable string for.
    #>

    [void] SetVariable ([string] $RuleType)
    {
        if (Test-VariableRequired -Rule $this.id)
        {
            $thisVariable = & Get-$($RuleType)Variable
            $this.set_Variable($thisVariable)

            # If a SQlScriptQueryRule has a value in the variable property then it requires an OrgValue
            $this.Set_OrganizationValueRequired($true)
        }
    }

    <#
        .SYNOPSIS
            Extracts the rule type from the check-content and sets the value
        .DESCRIPTION
            Gets the rule type from the xccdf content and sets the value
        .PARAMETER CheckContent
            The rule text from the check-content element in the xccdf
    #>

    [string] GetRuleType ([string[]] $CheckContent)
    {
        $ruleType = Get-SqlRuleType -CheckContent $CheckContent

        return $ruleType
    }

    <#
        .SYNOPSIS
            Set the organizational value test string
        .DESCRIPTION
            Extracts the organizational value from the key and then sets the value
    #>

    [void] SetOrganizationValueTestString ([string] $RuleType)
    {
        $thisOrganizationValueTestString = Get-SqlScriptQueryOrganizationValueTestString -RuleType $RuleType

        if (-not $this.SetStatus($thisOrganizationValueTestString))
        {
            $this.set_OrganizationValueTestString($thisOrganizationValueTestString)
        }

    }

    hidden [void] SetDscResource ()
    {
        if ($null -eq $this.DuplicateOf)
        {
            $this.DscResource = 'SqlScriptQuery'
        }
        else
        {
            $this.DscResource = 'None'
        }
    }

    static [bool] Match ([string] $CheckContent)
    {
        <#
            Provide match criteria to validate that the rule is (or is not) a SQL rule.
            Standard match rules
        #>

        if
        (
            $CheckContent -Match "SELECT" -and
            $CheckContent -Match 'existence.*publicly available.*(").*(")\s*(D|d)atabase' -or
            $CheckContent -Match "(DISTINCT|(D|d)istinct)\s+traceid" -or
            $CheckContent -Match "Verify the SQL Server default 'sa' account name has been changed" -or
            $CheckContent -Match "SQL Server audit setting on the maximum number of files of the trace" -or
            $CheckContent -Match "Obtain the list of roles that are authorized for the SQL Server 'View any database'" -or
            $CheckContent -Match "SQL query to determine SQL Server ownership of all database objects" -or
            $CheckContent -Match "direct access.*server-level" -and
            $CheckContent -NotMatch "'Alter any availability group' permission"
        )
        {
            return $true
        }
        # SQL Server 2016+ matches
        if
        (
            (
                $CheckContent -Match "(\s|\[)principal_id(\s*|\]\s*)\=\s*1" #SysAdminAccount
            ) -or
            (
                $CheckContent -Match "TRACE_CHANGE_GROUP" -or #V-79239,79291,79293,29295
                $CheckContent -Match "DATABASE_OBJECT_OWNERSHIP_CHANGE_GROUP" -or #V-79259,79261,79263,79265,79275,79277
                $CheckContent -Match "SCHEMA_OBJECT_CHANGE_GROUP" -or #V-79267,79269,79279,79281
                $CheckContent -Match "SUCCESSFUL_LOGIN_GROUP" -or #V-79287,79297
                $CheckContent -Match "FAILED_LOGIN_GROUP" -or #V-79289
                $CheckContent -Match "status_desc = 'STARTED'" #V-79141
            )
        )
        {
            return $true
        }
        return $false
    }

    #endregion
}