Module/Rule.WindowsFeature/Convert/Methods.ps1

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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#region Method Functions
<#
    .SYNOPSIS
        Retreives the WindowsFeature name from the check-content element in the xccdf
 
    .PARAMETER CheckContent
        Specifies the check-content element in the xccdf
#>

function Get-WindowsFeatureName
{
    [CmdletBinding()]
    [OutputType([string])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $CheckContent
    )

    Write-Verbose "[$($MyInvocation.MyCommand.Name)]"
    $windowsFeatureName = @()
    try
    {
        switch ($checkContent)
        {
            {$PSItem -match $regularExpression.WindowsFeatureName}
            {
                <#
                    The regex returns and named capture group called 'featureName'
                    that contains the feature name. $regularExpression.WindowsFeatureName
                    has been updated to incorporate the patterns from
                    $regularExpression.FeatureNameEquals and
                    $regularExpression.FeatureNameSpaceColon. They are both
                    commented out below for now case other STIGs stop parsing correctly.
                    They will be removed as part of Issue #223
                #>

                $null = $PSItem -match $regularExpression.WindowsFeatureName
                $windowsFeatureName += $Matches['featureName']
            }
            {$PSItem -match $regularExpression.FeatureNameSpaceColon}
            {
                $matches = $checkContent | Select-String -Pattern $regularExpression.FeatureNameSpaceColon -AllMatches
                $windowsFeatureName += ( $matches.Matches.Value -replace 'FeatureName\s\:' ).Trim()
            }
            {$PSItem -match $regularExpression.IfTheApplicationExists -and $PSItem -notmatch 'telnet'}
            {
                $matches = $checkContent | Select-String -Pattern $regularExpression.IfTheApplicationExists
                $windowsFeatureName += (($matches.Matches.Value | Select-String -Pattern ([RegularExpression]::TextBetweenQuotes)).Matches.Value -replace '"').Trim()
            }
            {$PSItem -match 'Telnet Client'}
            {
                $windowsFeatureName += 'TelnetClient'
            }
            {$PSItem -match $regularExpression.WebDavPublishingFeature}
            {
                $windowsFeatureName += 'Web-DAV-Publishing'
            }
            {$PSItem -match $regularExpression.SimpleTCP}
            {
                $windowsFeatureName += 'SimpleTCP'
            }
            {$PSItem -match $regularExpression.IISHostableWebCore}
            {
                $windowsFeatureName += 'IIS-HostableWebCore'
            }
            {$PSItem -match $regularExpression.IISWebserver}
            {
                $windowsFeatureName += 'IIS-WebServer'
            }
        }
    }
    catch
    {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] WindowsOptionalFeature : Not Found"
        return $null
    }
    return ($windowsFeatureName -join ',')
}

<#
    .SYNOPSIS
        Retreives the WindowsFeature InstallState from the check-content element in the xccdf
 
    .PARAMETER CheckContent
        Specifies the check-content element in the xccdf
#>

function Get-FeatureInstallState
{
    [CmdletBinding()]
    [OutputType([string])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $CheckContent
    )

    switch ($checkContent)
    {
        <#
            Currently ALL WindowsFeatureRules referenced in any of the STIGs will be uninstalled (Absent)
            so the default is Absent. When a STIG rule states a WindowsFeature is to be installed (Present)
            we can add the logic here.
        #>

        {$PSItem -eq $false}
        {
            return [ensure]::Present
        }
        default
        {
            [ensure]::Absent
        }
    }
}

<#
    .SYNOPSIS
        Test if the check-content contains WindowsFeatures to install/uninstall.
 
    .PARAMETER CheckContent
        Specifies the check-content element in the xccdf
#>

function Test-MultipleWindowsFeatureRule
{
    [CmdletBinding()]
    [OutputType([bool])]
    param
    (
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string]
        $FeatureName
    )

    if ( $FeatureName -match ',')
    {
        return $true
    }
    return $false
}

<#
    .SYNOPSIS
        Consumes a list of mitigation targets seperated by a comma and outputs an array
 
    .PARAMETER FeatureName
        A list of comma seperate WindowsFeature names
#>

function Split-WindowsFeatureRule
{
    [CmdletBinding()]
    [OutputType([array])]
    param
    (
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string]
        $FeatureName
    )

    return ( $FeatureName -split ',' )
}
#endregion