Module/Rule.Registry/Convert/RegistryRule.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
using module .\..\..\Common\Common.psm1
using module .\..\..\Rule\Rule.psm1
using module .\..\RegistryRule.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 RegistryRule
    .DESCRIPTION
        The RegistryRule class is used to extract the registry settings
        from the check-content of the xccdf. Once a STIG rule is identified a
        registry rule, it is passed to the RegistryRule class for parsing
        and validation.
#>

class RegistryRuleConvert : RegistryRule
{
    <#
        .SYNOPSIS
            Empty constructor for SplitFactory
    #>

    RegistryRuleConvert ()
    {
    }

    <#
        .SYNOPSIS
            Converts a xccdf stig rule element into a Registry Rule
        .PARAMETER XccdfRule
            The STIG rule to convert
    #>

    RegistryRuleConvert ([xml.xmlelement] $XccdfRule) : base ($XccdfRule, $true)
    {
        $fixText = [RegistryRule]::GetFixText($XccdfRule)

        if ($global:stigTitle -match 'Adobe Acrobat Reader')
        {
            $rawString = $fixText
        }
        else
        {
            $rawString = $this.SplitCheckContent
        }

        $this.SetKey($rawString)
        $this.SetValueName($rawString)
        $this.SetValueType($rawString)
        $this.SetDuplicateRule()
        $this.SetDscResource($fixText)

        # Get the trimmed version of the value data line.
        [string] $registryValueData = $this.GetValueData($rawString)

        # If a range is found on the value line, it needs further processing.
        if ($this.TestValueDataStringForRange($registryValueData))
        {
            # Set the OrganizationValueRequired flag to true so that a org level setting will be required.
            $this.SetOrganizationValueRequired()

            # Try to extract a test string from the range text.
            $OrganizationValueTestString = $this.GetOrganizationValueTestString($registryValueData)

            # If a test string was returned, add it.
            if ($null -ne $OrganizationValueTestString)
            {
                $this.set_OrganizationValueTestString($OrganizationValueTestString)
            }
        }
        else
        {
            if ($this.IsDataBlank($registryValueData))
            {
                $this.SetIsNullOrEmpty()
                $registryValueData = ''
            }
            elseif ($this.IsDataEnabledOrDisabled($registryValueData))
            {
                $registryValueData = $this.GetValidEnabledOrDisabled(
                    $this.ValueType, $registryValueData
                )
            }
            elseif ($this.IsDataHexCode($registryValueData))
            {
                $registryValueData = $this.GetIntegerFromHex($registryValueData)
            }
            elseif ($this.IsDataInteger($registryValueData))
            {
                $registryValueData = $this.GetNumberFromString($registryValueData)
            }
            elseif ($this.ValueType -eq 'MultiString')
            {
                if ($registryValueData -match "see below")
                {
                    $registryValueData = $this.GetMultiValueRegistryStringData($this.RawString)
                }
                else
                {
                    $registryValueData = $this.FormatMultiStringRegistryData($registryValueData)
                }
            }
            $this.Set_ValueData($registryValueData)
        }
    }

    #region Methods

    <#
        .SYNOPSIS
            Extracts the registry key from the check-content and sets the value
        .DESCRIPTION
            Gets the registry key from the xccdf content and sets the value. If
            the registry key that is returned is not valid, the parser status is
            set to fail.
    #>

    [void] SetKey ([string[]] $rawString)
    {
        $thisKey = Get-RegistryKey -CheckContent $rawString

        if (-not $this.SetStatus($thisKey))
        {
            $this.set_Key($thisKey)
        }
    }

    <#
        .SYNOPSIS
            Extracts the registry value name from the check-content and sets
            the value
        .DESCRIPTION
            Gets the registry value name from the xccdf content and sets the
            value. If the registry value name that is returned is not valid,
            the parser status is set to fail.
    #>

    [void] SetValueName ([string[]] $rawString)
    {
        $thisValueName = Get-RegistryValueName -CheckContent $rawString

        if (-not $this.SetStatus($thisValueName))
        {
            $this.set_ValueName($thisValueName)
        }
    }

    <#
        .SYNOPSIS
            Extracts the registry value type from the check-content and sets
            the value
        .DESCRIPTION
            Gets the registry value type from the xccdf content and sets the
            value. If the registry value type that is returned is not valid,
            the parser status is set to fail.
    #>

    [void] SetValueType ([string[]] $rawString)
    {
        $thisValueType = Get-RegistryValueType -CheckContent $rawString

        if ($thisValueType -ne "Does Not Exist")
        {
            if (-not $this.SetStatus($thisValueType))
            {
                $this.set_ValueType($thisValueType)
            }
        }
        else
        {
            $this.SetEnsureFlag([Ensure]::Absent)
            $this.ValueType = "None"
        }
    }

    <#
        .SYNOPSIS
            Tests the value data for a range of valid values
        .DESCRIPTION
            Tests the value data string for text that describes a list of valid
            values
        .PARAMETER ValueDataString
            The text to test
    #>

    [bool] TestValueDataStringForRange ([string] $ValueDataString)
    {
        return Test-RegistryValueDataContainsRange -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Extracts the registry value data from the check-content and sets
            the value
        .DESCRIPTION
            Gets the registry value data from the xccdf content and sets the
            value. If the registry value data that is returned is not valid,
            the parser status is set to fail.
    #>


    [string] GetValueData ([string[]] $rawString)
    {
        return Get-RegistryValueData -CheckContent $rawString
    }

    <#
        .SYNOPSIS
            Tests if the value data is supposed to be blank
        .DESCRIPTION
            Some stig settings state that a registry value, if it exists, is set
            to an empty value
        .PARAMETER ValueDataString
            The text to test
    #>

    [bool] IsDataBlank ([string] $ValueDataString)
    {
        return Test-RegistryValueDataIsBlank -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Tests if the value data is an enabled or disabled
        .DESCRIPTION
            Checks if a string contains the literal word Enabled or Disabled
        .PARAMETER ValueDataString
            The text to test
    #>

    [bool] IsDataEnabledOrDisabled ([string] $ValueDataString)
    {
        return Test-RegistryValueDataIsEnabledOrDisabled -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Get the valid version of the enabled or disabled
        .DESCRIPTION
            Get the valid version of the enabled or disabled, based on the the
            value type. A binary enabled, cannot accept the enabled string so
            the valid vaule needs to be returnd.
        .PARAMETER ValueType
            The value tyoe to evaluate
        .PARAMETER ValueData
            The value data to evaluate
    #>

    [string] GetValidEnabledOrDisabled ([string] $ValueType, [string] $ValueData)
    {
        return Get-ValidEnabledOrDisabled -ValueType $ValueType -ValueData $ValueData
    }

    <#
        .SYNOPSIS
            Checks if a string contains a hexadecimal number
        .DESCRIPTION
            Checks if a string contains a hexadecimal number
        .PARAMETER ValueDataString
            The text to test
    #>

    [bool] IsDataHexCode ([string] $ValueDataString)
    {
        return Test-RegistryValueDataIsHexCode -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Returns the integer of a hexadecimal number
        .DESCRIPTION
            Extracts the hex code if it exists, convert to int32 and set the
            output value. This ignores the int that usually accompanies the
            hex value in parentheses.
        .PARAMETER ValueDataString
            The text to test
    #>

    [int] GetIntegerFromHex ([string] $ValueDataString)
    {
        return Get-IntegerFromHex -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Tests if the registry value is an integer
        .DESCRIPTION
            This will match any lines that start with an integer (of any length)
            as the value to be set
        .PARAMETER ValueDataString
            The text to test
    #>

    [bool] IsDataInteger ([string] $ValueDataString)
    {
        return Test-RegistryValueDataIsInteger -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Returns the number from a string
        .DESCRIPTION
            Returns the number from a string
        .PARAMETER ValueDataString
            The text to test
    #>

    [string] GetNumberFromString ([string] $ValueDataString)
    {
        return Get-NumberFromString -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Formats a string value into a multiline string
        .DESCRIPTION
            Formats a string value into a multiline string by spliting it on a
            space or comma space format
        .PARAMETER ValueDataString
            The text to test
    #>

    [string[]] FormatMultiStringRegistryData ([string] $ValueDataString)
    {
        return Format-MultiStringRegistryData -ValueDataString $ValueDataString
    }

    <#
        .SYNOPSIS
            Get the multi-value string data
        .DESCRIPTION
            Get the multi-value string data
        .PARAMETER CheckStrings
            The rule text from the check-content element in the xccdf
    #>

    [string[]] GetMultiValueRegistryStringData ([string[]] $CheckStrings)
    {
        return Get-MultiValueRegistryStringData -CheckStrings $CheckStrings
    }

    <#
        .SYNOPSIS
            Sets the ensure flag to the provided value
        .DESCRIPTION
            Sets the ensure flag to the provided value
        .PARAMETER EnsureFlag
            The value the Ensure flag should be set to
    #>

    [void] SetEnsureFlag ([Ensure] $Ensure)
    {
        $this.Ensure = $Ensure
    }

    # Empty argument method used with Get-HardCodedRuleLogFileEntry
    hidden [void] SetDscResource ()
    {
        $this.DscResource = 'Registry'
    }

    hidden [void] SetDscResource ([string] $FixText)
    {
        if ($null -eq $this.DuplicateOf)
        {
            if ($FixText -match 'Administrative Template' -or $this.key -match "(^hkcu|^HKEY_CURRENT_USER)" -or $this.ValueName -match "RemoteAccessHostFirewallTraversal")
            {
                $this.DscResource = 'RegistryPolicyFile'
            }
            else
            {
                $this.DscResource = 'Registry'
            }
        }
        else
        {
            $this.DscResource = 'None'
        }
    }

    static [bool] Match ([string] $CheckContent)
    {
        if
        (
            (
                $CheckContent -Match "HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER" -and
                $CheckContent -NotMatch "Permission(s|)" -and
                $CheckContent -NotMatch "Sql Server" -and
                $CheckContent -NotMatch "Review the Catalog" -and
                $CheckContent -NotMatch "For 32.bit (production systems|applications)" -and
                $CheckContent -NotMatch 'If the "AllowStrongNameBypass" registry key' -and
                $CheckContent -NotMatch 'DSA Database file' -and
                $CheckContent -NotMatch "N'HKEY_LOCAL_MACHINE'"
            ) -or
            (
                $CheckContent -Match "Windows Registry Editor" -and
                $CheckContent -Match "HKLM|HKCU"
            ) -or
            (
                $CheckContent -Match "HKLM|HKCU" -and
                $CheckContent -Match "REG_DWORD"
            ) -or
            (
                $CheckContent -Match "regedit" -and
                $CheckContent -Match "omnibox"
            )
        )
        {
            return $true
        }
        return $false
    }

    <#
        .SYNOPSIS
            Tests if a rule contains multiple checks
        .DESCRIPTION
            Search the rule text to determine if multiple registry paths are defined
        .PARAMETER CheckContent
            The rule text from the check-content element in the xccdf
    #>

    <#{TODO}#> # HasMultipleRules is implemented inconsistently.
    static [bool] HasMultipleRules ([string] $CheckContent)
    {
        return Test-MultipleRegistryEntries -CheckContent ([Rule]::SplitCheckContent($CheckContent))
    }

    <#
        .SYNOPSIS
            Splits a rule into multiple checks
        .DESCRIPTION
            Once a rule has been found to have multiple checks, the rule needs
            to be split. This method splits registry paths into multiple rules.
            Each split rule id is appended with a dot and letter to keep reporting
            per the ID consistent. An example would be is V-1000 contained 2
            checks, then SplitMultipleRules would return 2 objects with rule ids
            V-1000.a and V-1000.b
        .PARAMETER CheckContent
            The rule text from the check-content element in the xccdf
    #>

    static [string[]] SplitMultipleRules ([string] $CheckContent)
    {
        return (Split-MultipleRegistryEntries -CheckContent ([Rule]::SplitCheckContent($CheckContent)))
    }

    #endregion
}