Module/Rule.MimeType/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
167
168
169
170
171
172
173
174
175
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#region Method Functions
<#
.SYNOPSIS
    Returns the Extension for the STIG rule.
 
.PARAMETER CheckContent
    An array of the raw string data taken from the STIG setting.
#>

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

    $mimeTypeMatch = $checkContent | Select-String -Pattern $regularExpression.mimeType

    return $mimeTypeMatch.matches.groups.value
}

<#
.SYNOPSIS
    Returns the MimeType for the STIG rule.
 
.PARAMETER CheckContent
    An array of the raw string data taken from the STIG setting.
#>

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

    switch ( $Extension )
    {
        { $PsItem -match '\.exe|\.com' }
        {
            $mimeType = 'application/octet-stream'
        }
        { $PsItem -match '\.dll' }
        {
            $mimeType = 'application/x-msdownload'
        }
        { $PsItem -match '\.bat' }
        {
            $mimeType = 'application/x-bat'
        }
        { $PsItem -match '\.csh' }
        {
            $mimeType = 'application/x-csh'
        }
    }

    if ($null -ne $mimeType)
    {
        Write-Verbose -Message $("[$($MyInvocation.MyCommand.Name)] Found MimeType: {0}" -f $mimeType)

        return $mimeType
    }
    else
    {
        Write-Verbose -Message "[$($MyInvocation.MyCommand.Name)] No MimeType found"
        return $null
    }
}

<#
.SYNOPSIS
    Returns the Extension for the STIG rule.
 
.PARAMETER CheckContent
    An array of the raw string data taken from the STIG setting.
#>

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

    if ($checkContent -match $regularExpression.mimeTypeAbsent)
    {
        Write-Verbose -Message "[$($MyInvocation.MyCommand.Name)] Ensure Absent"
        return "Absent"
    }
    else
    {
        Write-Verbose -Message "[$($MyInvocation.MyCommand.Name)] Ensure not found"
        return $null
    }
}

<#
.SYNOPSIS
    Tests to see if the stig rule needs to be split into multiples.
 
.PARAMETER CheckContent
    An array of the raw string data taken from the STIG setting.
#>

function Test-MultipleMimeTypeRule
{
    [CmdletBinding()]
    [OutputType([bool])]
    param
    (
        [Parameter(Mandatory = $true)]
        [psobject]
        $CheckContent
    )

    $mimeTypes = $checkContent | Where-Object -FilterScript {$PSItem.startswith('.')}

    if ($mimeTypes.Count -gt 1)
    {
        Write-Verbose -message "[$($MyInvocation.MyCommand.Name)] : $true"
        return $true
    }
    else
    {
        Write-Verbose -message "[$($MyInvocation.MyCommand.Name)] : $false"
        return $false
    }
}

<#
.SYNOPSIS
    Splits a STIG setting into multiple rules when necessary.
 
.PARAMETER CheckContent
    An array of the raw string data taken from the STIG setting.
#>

function Split-MultipleMimeTypeRule
{
    [CmdletBinding()]
    [OutputType([object[]])]
    param
    (
        [Parameter(Mandatory = $true)]
        [psobject]
        $CheckContent
    )

    $splitMimeTypeRules = @()

    $mimeTypeMatches = $checkContent | Select-String -Pattern $regularExpression.mimeType

    $mimeTypes  = $mimeTypeMatches.matches.groups.value

    $baseCheckContent = $checkContent| Where-Object -Filterscript {$PSItem -notin $mimeTypes}

    foreach ($mimeType in $mimeTypes)
    {
        $rule = $baseCheckContent + $mimeType
        $splitMimeTypeRules += ($rule -join "`r`n")
    }

    return $splitMimeTypeRules
}
#endregion