Functions/Set-GraphSecurityAlert.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#
.Synopsis
   Sets the status of alerts in Microsoft Graph Security.
 
.DESCRIPTION
   Sets the status of alerts in Microsoft Graph Security.
 
   There are multiple parameter:
 
   assignedTo: Used for setting the name of the analyst the alert is assigned to for triage, investigation, or remediation.
   closed: Used to close the alert [default is no]
   closedDateTime: Time at which the alert was closed. [default is current date and time]
   comments: Analyst comments on the alert.
   feedback: Analyst feedback on the alert. Possible values are: unknown, truePositive, falsePositive, benignPositive.
   status: Alert lifecycle status (stage). Possible values are: unknown, newAlert, inProgress, resolved.
   tags: User-definable labels that can be applied to an alert and can serve as filter conditions.
 
   An alert id is always required to be specified either explicity or implicitly from the pipeline.
 
.EXAMPLE
   Set-GraphSecurityAlert -id D0ED0BD3-AB24-3E05-A4D3-171280CA3CB9 -Status resolved -Feedback truePositive
 
    This marks a single specified alert as 'resolved' and as a 'truePositive'.
 
.EXAMPLE
   Get-GraphSecurityAlert -id D0ED0BD3-AB24-3E05-A4D3-171280CA3CB9 | Set-GraphSecurityAlert -Status inProgress -Assignedto joe@contoso.com
 
    This will set the status of the specified alert as "inProgress" and who is working it "joe@contoso.com".
 
.FUNCTIONALITY
   Set-GraphSecurityAlert is intended to function as a mechanism for setting the status of alerts using Microsoft Graph Security.
#>


function Set-GraphSecurityAlert {

    [CmdletBinding()]

    Param
    (

        # Specifies the alert id
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$id,

        #Specifies the API Version
        [Parameter(Mandatory = $false)]
        [ValidateSet("v1", "Beta")]
        [string]$Version = "v1",

        #Sets the owner of the alert
        [Parameter(Mandatory = $false)]
        [ValidatePattern("[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*")]
        [string]$assignedTo,

        #sets the alert to closed
        [Parameter(Mandatory = $false)]
        [switch]$Closed,

        #sets the alert to open
        [Parameter(Mandatory = $false)]
        [switch]$Open,

        #Sets the close time
        [Parameter(Mandatory = $false)]
        [datetime]$closedDateTime,

        #Sets any comments
        [Parameter(Mandatory = $false)]
        [string]$comments,

        #Sets the Feedback; 0,1,2,3
        [Parameter(Mandatory = $false)]
        [ValidateSet("unknown", "truePositive", "falsePositive", "benignPositive")]
        [string]$feedback,

        #Sets the Feedback; 0,1,2,3
        [Parameter(Mandatory = $false)]
        [ValidateSet("unknown", "newAlert", "inProgress", "resolved")]
        [string]$Status,

        #Sets any tags
        [Parameter(Mandatory = $false)]
        [string]$Tags

    )

    Begin {

        Try {Test-GraphSecurityAuthToken }
        Catch { Throw $_ }

        If ($Closed -and $Open) {
            Write-Error "You cannot specify open and close parameters at the same time"
            exit
        }

    }

    Process {
        $Resource = "security/alerts/$id"

        if ($Version -eq "Beta") {

            $uri = "https://graph.microsoft.com/beta/$($resource)"

        }

        Else {

            $uri = "https://graph.microsoft.com/$Version.0/$($resource)"

        }

        $alert = Invoke-RestMethod -Uri $uri -Headers $GraphSecurityAuthHeader -Method Get

        $provider = $Alert.vendorInformation.provider

        $Vendor = $Alert.vendorInformation.vendor

        #need to build the body https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/alert_update
        $baseBody = @"
{
    "vendorInformation": {
        "provider": "$provider",
        "vendor": "$Vendor"
        }
}
"@

        $objBody = ConvertFrom-Json $baseBody

        if ($assignedTo) { $objBody | Add-Member -Type NoteProperty -Name 'assignedTo' -Value "$assignedTo" }

        if ($Closed) {

            $DateTime = (Get-Date -UFormat '+%Y-%m-%dT%H:%M:%SZ')

            $objBody | Add-Member -Type NoteProperty -Name 'closedDateTime' -Value "$DateTime"

        }

        if ($closedDateTime) {

            $closedDateTime = (Get-Date -Date $closedDateTime -UFormat '+%Y-%m-%dT%H:%M:%SZ')

            $objBody | Add-Member -Type NoteProperty -Name 'closedDateTime' -Value "$closedDateTime"

        }

        if ($Open) { $objBody | Add-Member -Type NoteProperty -Name 'closedDateTime' -Value $null }

        if ($comments) { $objBody | Add-Member -Type NoteProperty -Name 'comments' -Value @("$comments") }

        if ($feedback) { $objBody | Add-Member -Type NoteProperty -Name 'feedback' -Value "$feedback" }

        if ($status) { $objBody | Add-Member -Type NoteProperty -Name 'status' -Value "$status" }

        if ($tags) { $objBody | Add-Member -Type NoteProperty -Name 'tags' -Value @("$tags") }

        $Body = ConvertTo-Json $objBody -Depth 5

        try {

            Invoke-RestMethod -Uri $uri -Headers $GraphSecurityAuthHeader -Method Patch -Body $Body

        }

        catch {

            $ex = $_.Exception

            $errorResponse = $ex.Response.GetResponseStream()

            $reader = New-Object System.IO.StreamReader($errorResponse)

            $reader.BaseStream.Position = 0

            $reader.DiscardBufferedData()

            $responseBody = $reader.ReadToEnd();

            Write-Verbose "Response content:`n$responseBody"

            Write-Error "Request to $Uri failed with HTTP Status $($ex.Response.StatusCode) $($ex.Response.StatusDescription)"



            break

        }

    }

    End {

        #Do Nothing
    }

}