Module/Rule/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
#region Method Functions

<#
    .SYNOPSIS
        Checks for Html encoded char
 
    .PARAMETER CheckString
        The string to convert.
#>

function Test-HtmlEncoding
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $CheckString
    )

    if ( $CheckString -match '&\w+;' )
    {
        return $true
    }
    else
    {
        return $false
    }
}

<#
    .SYNOPSIS
        Converts Html encoded strings back in the ascii char
 
    .PARAMETER CheckString
        The string to convert.
#>

function ConvertFrom-HtmlEncoding
{
    [OutputType([String])]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $CheckString
    )

    return [System.Web.HttpUtility]::HtmlDecode( $CheckString )
}

<#
    .SYNOPSIS
        Tests the results of ConvertTo-*Rule functions for duplicates. The DNS STIG has multiple duplicates but we only
        need to account for them once. If a duplicate is detected we will convert that rule to a document rule.
 
    .PARAMETER ReffernceObject
        The list of Stigs objects to compare to.
 
    .PARAMETER DifferenceObject
        The newly created object to verify is not a duplicate.
 
    .NOTES
        General notes
#>

function Test-DuplicateRule
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [AllowNull()]
        [object]
        $ReferenceObject,

        [Parameter(Mandatory = $true)]
        [object]
        $DifferenceObject
    )

    $ruleType = $DifferenceObject.GetType().Name
    $ruleType = $ruleType.Replace("Convert", "")
    $baseRule = [Rule]::New()

    $referenceProperties = ($baseRule | Get-Member -MemberType Property).Name
    $differenceProperties = ($DifferenceObject | Get-Member -MemberType Property).Name

    $propertyList = (Compare-Object -ReferenceObject $referenceProperties -DifferenceObject $differenceProperties).InputObject
    $referenceRules = $ReferenceObject | Where-Object -FilterScript {$PsItem.GetType().Name -eq $ruletype}

    foreach ($rule in $referenceRules)
    {
        $results = @()

        foreach ($propertyName in $PropertyList)
        {
            $results += $rule.$propertyName -eq $DifferenceObject.$propertyName
        }

        if ($results -notcontains $false)
        {
            return $rule.id
        }
    }

    # If the code made it this far a duplicate does not exist and we return $null
    return $null
}

<#
    .SYNOPSIS
        Looks in $global:stigSettings for existing rules
 
    .NOTES
        Some rules in the STIG enforce multiple settings. This function will test for
        this scenario to so we can act upon it later.
#>

function Test-ExistingRule
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param
    (
        [Parameter()]
        [object]
        $RuleCollection,

        [Parameter()]
        [object]
        $NewRule
    )

    $IdExist = $RuleCollection | Where-Object {$PSItem.Id -eq $NewRule.Id}

    return $IdExist.id -eq $NewRule.id
}
#endregion