Public/CloudWatch/Read-ATCWLFlowLog.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
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
function Read-ATCWLFlowLog
{
<#
    .SYNOPSIS
        Read a flow log into a list of PowerShell custom objects

    .DESCRIPTION
        Read a flow log into a list of PowerShell custom objects and emits this as the result of the script.
        The various fields of the flow log are parsed out and can be accessed as properties of the returned
        object simplifying sorting and searching of the log events.

        You can pipe the output to Out-GridView to view quickly or Export-Csv for further analysis in Excel.

    .PARAMETER LogGroupName
        The name of the log group.

    .PARAMETER LogStreamName
        The name of the log stream.

    .PARAMETER Interleaved
        If the value is true, the operation makes a best effort to provide responses that contain events from multiple log streams within the log group, interleaved in a single response.
        If the value is false, all the matched log events in the first log stream are searched first, then those in the next log stream, and so on. The default is false.

    .PARAMETER StartTime
        The start of the time range. Events with a time stamp equal to this time or later than this time are included. Events with a time stamp earlier than this time are not included.

    .PARAMETER EndTime
        The end of the time range. Events with a time stamp equal to or later than this time are not included.

    .PARAMETER Last
        Sets the time range to the last X minutes from now.

    .PARAMETER FilterPattern
        Applies a server-side filter to the results. This filters the results before they are returned by AWS. If not provided, all the events are matched.
        See Related Links for a link to filter syntax.

    .EXAMPLE
        Read-FlowLog.ps1 -Profile myprofile -LogStreamName eni-00000000000000000-all -Last 30
        Read all events for the last 30 minutes.

    .EXAMPLE
        Read-FlowLog.ps1 -Profile myprofile -LogStreamName eni-00000000000000000-all,eni-00000000000000001-all -Last 30 -Interleaved
        Read all events for the last 30 minutes for two interfaces and interleave the results.

    .EXAMPLE
        Read-FlowLog.ps1 -Profile myprofile -LogStreamName eni-00000000000000000-all -StartTime ([DateTime]::UtcNow.AddHours(-1)) -EndTime ([DateTime]::UtcNow.AddMinutes(-30)) | Where-Object { $_.DestPort -eq 80 }
        Read all events for the given range and filter client-side (slow, uses more memory)

    .EXAMPLE
        Read-FlowLog.ps1 -Profile myprofile -LogStreamName eni-00000000000000000-all -StartTime ([DateTime]::UtcNow.AddHours(-1)) -EndTime ([DateTime]::UtcNow.AddMinutes(-30)) -FilterPattern "[Version,AccountId,InterfaceId,SourceAddress,DestAddress,SourcePort,DestPort=80,...]"
        Read all events for the given range and filter server side (fast, but tricky syntax)

    .NOTES
        IAM permissions required to run this command
        - logs:FilterLogEvents

        Each object has the following fields

        | Property | Description|
        |---------------|---------------------------------------------------------------------------------------------------------------------|
        | Version | The VPC Flow Logs version.|
        | AccountId | The AWS account ID for the flow log.|
        | InterfaceId | The ID of the network interface for which the traffic is recorded.|
        | SourceAddress | The source IPv4 or IPv6 address.|
        | DestAddress | The destination IPv4 or IPv6 address|
        | SourcePort | The source port of the traffic.|
        | DestPort | The destination port of the traffic.|
        | Protocol | The IANA protocol number of the traffic.|
        | Packets | The number of packets transferred during the capture window.|
        | Bytes | The number of bytes transferred during the capture window.|
        | StartTime | The time, as a local DateTime, of the start of the capture window.|
        | EndTime | The time, as a local DateTime, of the end of the capture window.|
        | Action | The action associated with the traffic: ACCEPT or REJECT|
        | Status | The logging status of the flow log: OK, NODATA or SKIPDATA|

        Notes
        - IPv4 addresses for network interfaces are always their private IPv4 address.

    .INPUTS
        None

    .OUTPUTS
        [object]
        List of parsed flow log entries. See Notes for a description of the fields.

    .LINK
        https://github.com/fireflycons/aws-toolbox/tree/master/docs/en-US/Read-ATCWLFlowLog.md

    .LINK
        https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html

    .LINK
        http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml

    .LINK
        https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html
#>

    [CmdletBinding(DefaultParametersetname='LastX')]
    param
    (
        [string]$LogGroupName = 'Flow-Logs',

        [Parameter(Mandatory = $true)]
        [string[]]$LogStreamName,

        [switch]$Interleaved,

        [Parameter(ParameterSetName = 'Range')]
        [DateTime]$StartTime,

        [Parameter(ParameterSetName = 'Range')]
        [DateTime]$EndTime,

        [Parameter(ParameterSetName = 'LastX')]
        [int]$Last = 30,

        [string]$FilterPattern = [string].Empty
    )

    $ErrorActionPreference = 'Stop'

    # Test for AWS
    $script:IsRunningInAws = $(
        try
        {
            Invoke-RestMethod -Uri 'http://169.254.169.254/latest/meta-data/instance-id' | Out-Null
            $true
        }
        catch
        {
            $false
        }
    )

    # Count all the events read
    $totalEvents = 0

    # Regex for deconstructing log record
    $logMessageRx = '^(?<Version>\d+)\s+(?<AccountId>\d+)\s+(?<InterfaceId>[\w-]+)\s+(?<SourceAddress>\d+\.\d+\.\d+\.\d+)\s+(?<DestAddress>\d+\.\d+\.\d+\.\d+)\s+(?<SourcePort>\d+)\s+(?<DestPort>\d+)\s+(?<Protocol>\d+)\s+(?<Packets>\d+)\s+(?<Bytes>\d+)\s+(?<StartTime>\d+)\s+(?<EndTime>\d+)\s+(?<Action>\w+)\s+(?<Status>\w+)'

    # UNIX epoch for timestamp -> DateTime
    $epoch = New-Object DateTime -ArgumentList (1970, 1, 1, 0, 0, 0, 0, 'Utc')

    # Create splat argument, and add start time/end time if present
    $args = @{}

    switch ($PSCmdLet.ParameterSetName)
    {
        'LastX' {

            # Start time is $Last minutes before now
            $args.Add('StartTime', [int64](([DateTime]::UtcNow.AddMinutes(0 - $Last) - $epoch).TotalMilliseconds))
        }

        'Range' {

            # Convert user supplied start and end time to milliseconds since Unix epoch
            if ($StartTime)
            {
                $args.Add('StartTime', [int64](($StartTime.ToUniversalTime() - $epoch).TotalMilliseconds))
            }

            if ($EndTime)
            {
                $args.Add('EndTime', [int64](($EndTime.ToUniversalTime() - $epoch).TotalMilliseconds))
            }
        }
    }

    if ($Interleaved)
    {
        # Interleave if requested
        $args.Add('Interleaved', $true)
    }

    $events = $(

        do
        {
            # Loop until we get no more events from AWS

            Write-Host "Reading log..."
            $log = Get-CWLFilteredLogEvent -LogGroupName $LogGroupName -LogStreamName $LogStreamName -FilterPattern $FilterPattern @args

            # Number of events in this batch
            $numEvents = ($log.Events | Measure-Object).Count

            # Add to total number processed
            $totalEvents += $numEvents

            # Numver processed in this batch (for progress meter)
            $numProcessed = 0

            Write-Host "Processing events..."
            $log.Events |
                Foreach-Object {

                # Deconstruct log record and create an object for it
                if ($_.Message -match $logMessageRx)
                {
                    # Add all the named matches to a hash
                    $h = @{}

                    $Matches.Keys |
                        Where-Object { $_ -ine '0' } |
                        Foreach-Object {
                        $h.Add($_, $Matches[$_])
                    }

                    # Convert UNIX times to local DateTimes
                    'StartTime', 'EndTime' |
                        Foreach-Object {
                        $h[$_] = $epoch.AddSeconds([long]::Parse($h[$_])).ToLocalTime()
                    }

                    # Convert integer values from strings
                    'Packets', 'Version', 'SourcePort', 'Bytes', 'DestPort', 'Protocol' |
                        Foreach-Object {
                        $h[$_] = [long]::Parse($h[$_])
                    }

                    # Add the ingestion time which is in its own field in the event record. This one's in milliseconds since epoch
                    $h.Add('IngestionTime', $epoch.AddMilliseconds($_.IngestionTime))

                    # Emit new object with properties defined by the hash
                    New-Object PSObject -Property $h
                }

                if (++$numProcessed % 50 -eq 0)
                {
                    # Update progress every 50 records processed.
                    Write-Progress -Activity "Processing event batch" -Status "$numProcessed of $numEvents" -PercentComplete ($numProcessed * 100 / $numEvents)
                }
            } |
                Select-Object -Property IngestionTime, Version, AccountId, InterfaceId, SourceAddress, DestAddress, SourcePort, DestPort, Protocol, Packets, Bytes, StartTime, EndTime, Action, Status # Order fields correctly

            # End progress
            Write-Progress -Activity "Processing event batch" -Status 'Processing' -PercentComplete 100 -Completed

            # Determine if there is more data to read and add the token argument to the argument hash for Get-CWLLogEvent
            $moreData = $false

            if ($args.ContainsKey('NextToken'))
            {
                if ($args['NextToken'] -ine $log.NextBackwardToken)
                {
                    $moreData = $true
                    $args['NextToken'] = $log.NextBackwardToken
                }
            }
            else
            {
                if (-not ([string]::IsNullOrEmpty($log.NextBackwardToken)))
                {
                    $moreData = $true
                    $args.Add('NextToken', $log.NextBackwardToken)
                }
            }
        }
        while ($moreData)
    )

    Write-Verbose "Total events processed : $totalEvents"
    Write-Verbose "Number of events returned: $(($events | Measure-Object).Count)"

    # Emit the events.
    $events
}