-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow_Top_Network_Destination.ps1
362 lines (320 loc) · 11.8 KB
/
Show_Top_Network_Destination.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#Logman capture for Network Diagnostics
#requires -version 3.0
## Last modified 28/09/18 1657 BST @guyrleech
## Change these to repurpose the script - currently only supports one or other of sent or receieved being true, not both
[bool]$received = $false
[bool]$sent = $true
[bool]$perSession = $false
[bool]$perComputer = $false
[int]$top = 10
[int]$caplength = 30
if( $received -and $sent )
{
Write-Warning 'Cannot run with both $received and $sent set to true'
}
if( $perSession -and $perComputer )
{
Write-Warning 'Cannot run with both $perSession and $perCOmputer set to true'
}
if( ! $received -and ! $sent )
{
Write-Warning 'Cannot run with both $received and $sent set to false'
}
[int]$outputWidth = 400
$DebugPreference = "SilentlyContinue"
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning “You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!”
Break
}
Function Cleanup{
$ret=start-process -FilePath "$env:windir\system32\logman.exe" -ArgumentList "stop wdc -ets" -wait -PassThru
foreach($file in Get-ChildItem "$env:temp\output*.etl"){
Remove-Item $file.FullName -Force -ea SilentlyContinue
}
}
[int]$sessionId =-1
[int]$ppid = -1
if( $perSession )
{
$sessionId = $args[0]
$theSession = quser.exe $sessionId
if( ! $theSession -or ! $theSession.Count )
{
Throw "Session id $sessionId not found"
}
}
elseif( ! $perComputer )
{
$ppid = $args[0]
if( ! ( Get-Process -Id $ppid -ErrorAction SilentlyContinue ) )
{
Throw "Process id $ppid no longer exists"
}
}
if( $perComputer )
{
if ( $args.Count -ge 1 -and $args[0]){$caplength = $args[0]}
if ( $args.Count -ge 2 -and $args[1]){$top = $args[1]}
}
else
{
if ( $args.Count -ge 2 -and $args[1]){$caplength = $args[1]}
if ( $args.Count -ge 3 -and $args[2]){$top = $args[2]}
}
#write-debug "Capturing Process id: $ppid, process name $(get-process -id $ppid).processname for length: $caplength"
$connectionlogs=@()
$break=$false
$breakreason=""
$tempfile="$env:temp\output%d.etl"
#check prereqs
if (!(test-path $env:windir\system32\logman.exe)){$break=true;$breakreason="logman.exe could not be found"}
if (!$break){
cleanup
$loop=0
#loop for $caplength seconds
#Start capture here with logman
write-host "Capturing ETL logs for $caplength seconds" -nonewline
$ret=start-process -FilePath "$env:windir\system32\logman.exe" -ArgumentList "start wdc -p Microsoft-Windows-Kernel-Network 0x10 0xff -bs 64 -nb 16 38 -max 16 -mode newfile -o ""$tempfile"" -ets" -Wait -PassThru
Start-Sleep 1
if ( ! $ret -or $ret.ExitCode -ne 0){write-warning "could not start log capture process! $($ret.exitcode)";exit}
Do{
write-host "." -NoNewline
$loop++
start-sleep 1
} while($loop -lt $caplength)
write-host "Capturing complete"
$ret=start-process -FilePath "$env:windir\system32\logman.exe" -ArgumentList "stop wdc -ets" -wait -PassThru
if (!($ret.ExitCode -eq 0)){write-warning "could not stop log capture process! $($ret.exitcode)";exit}
$logs=@()
[hashtable]$connectionTable = @{}
[long]$lastKey = $null
$netobject = $null
[hashtable]$sessionProcesses = @{}
[string]$xpath = '*[System['
if( $sent )
{
$xpath += 'EventID=10 or EventID=42'
}
if( $received )
{
if( $sent )
{
$xpath += ' or '
}
$xpath += 'EventID=11 or EventID=43'
}
$xpath += ']'
if( $ppid -gt 0 )
{
$xpath += " and EventData[Data[@Name='PID']='$ppid']" ## if session then make an "or" list
}
## FilterXPath string has a maximum length of around 512 so we can't filter on all session processes
elseif( $sessionId -ge 0 )
{
Get-Process | Where-Object { $_.SessionId -eq $sessionId } | ForEach-Object {
$sessionProcesses.Add( $_.Id , $true )
}
if( ! $sessionProcesses -or ! $sessionProcesses.Count )
{
Throw "Found no processes for session $sessionId"
}
}
## else computer scope so no process filtering
$xpath += ']'
$startProcessing = [datetime]::Now
#loop for all logs
Write-host "Anaylysing captured traffic"
@( foreach($file in Get-ChildItem "$env:temp\output*.etl"){
Get-WinEvent -Path $file.fullname -Oldest -FilterXPath $xpath -ErrorAction SilentlyContinue
} ) | ForEach-Object {
if( $sessionId -lt 0 -or $sessionProcesses[ $_.Properties[0].Value -as [int] ] )
{
$log = $_
$udp=$false
$TCP=$false
$send=$false
$receive=$false
#define type
Switch ($log.id)
{
10 {$tcp=$true;$send=$true}
42 {$udp=$true;$send=$true}
11 {$tcp=$true;$receive=$true}
43 {$UDP=$true;$receive=$true}
}
$size=$log.Properties[1].Value
[string]$mainkey = $null
if( $sent )
{
$saddr = $null
$sport = $null
$daddr = [long]$log.Properties[2].Value
$dport=(($log.Properties[4].Value -band 0xff ) -shl 8 ) -bor ( ( $log.Properties[4].Value -band 0xff00 ) -shr 8)
$mainkey = "$daddr$dport"
}
if( $received )
{
$daddr = $null
$dport = $null
$saddr = [long]$log.Properties[2].Value
$sport=(($log.Properties[4].Value -band 0xff ) -shl 8 ) -bor ( ( $log.Properties[4].Value -band 0xff00 ) -shr 8)
$mainkey = "$saddr$sport"
}
## if we already have object from previous iteration and it's the same key then use that object rather than looking up again
if( ! $netobject -or $lastKey -ne $mainkey )
{
$netobject = $connectionTable[ $mainkey ]
}
if (!$netobject){
$netObject = [pscustomobject]@{
Source = $saddr
Destination = $daddr
sourceport = $sport
destinationport = $dport
UDPReceived = 0
UDPSent = 0
TCPReceived = 0
TCPSent = 0
TCPTotal = 0
UDPTotal = 0
TotalSent = 0
TotalReceived = 0
PIDs = New-Object System.Collections.ArrayList
}
$connectionTable.Add( $mainkey , $netobject )
}
if( $netobject.PIDs -notcontains $log.Properties[0].Value ) ## shortish list so lookup not too bad
{
$null = $netobject.PIDs.Add( $log.Properties[0].Value )
}
if ($tcp){
if ($send){
$netobject.tcpsent+=$size
$netobject.totalsent+=$size
}
elseif ($receive){
$netobject.tcpreceived+=$size
$netobject.totalreceived+=$size
}
$netobject.TCPtotal+=$size
}
elseif ($udp){
if ($send){
$netobject.udpsent+=$size
$netobject.totalsent+=$size
}
elseif ($receive){
$netobject.udpreceived+=$size
$netobject.totalreceived+=$size
}
$netobject.udptotal+=$size
}
$lastKey = $mainkey
}
}
}
Else{
write-warning "Script execution has been halted: $breakreason"
}
Write-Debug ( "$(Get-Date) : Data processing took {0} seconds" -f ([datetime]::Now - $startProcessing).TotalSeconds )
# Altering the size of the PS Buffer
$PSWindow = (Get-Host).UI.RawUI
$WideDimensions = $PSWindow.BufferSize
$WideDimensions.Width = $outputWidth
$PSWindow.BufferSize = $WideDimensions
[string]$heading = "`nShowing top $top network"
if( $received )
{
$heading += ' sources for '
}
else
{
$heading += ' destinations for '
}
[string]$scope = $null
if( $sessionId -ge 0 )
{
$scope = "session $sessionId"
}
elseif( $ppid -gt 0 )
{
$scope = "process $ppid ($(Get-Process -Id $ppid -ErrorAction SilentlyContinue|Select -ExpandProperty Name))"
}
else
{
$scope = 'whole computer'
}
$heading += "$scope :"
$SummaryObject = [pscustomobject][ordered]@{ "Total Connections" = ($connectionTable.count) }
if( $connectionTable.count )
{
if( $sent )
{
Add-Member -InputObject $SummaryObject -MemberType NoteProperty -Name "Total Sent (KB)" -Value ( [math]::Round(($connectionTable.GetEnumerator()|Select -ExpandProperty Value|Measure-Object -Property 'totalsent' -Sum).Sum / 1KB,1))
}
if( $received )
{
Add-Member -InputObject $SummaryObject -MemberType NoteProperty -Name "Total Received (KB)" -Value ([math]::Round(($connectionTable.GetEnumerator()|Select -ExpandProperty Value|Measure-Object -Property 'totalreceived' -Sum).Sum / 1KB,1))
}
}
else
{
"No network traffic captured during $caplength seconds for $scope"
return
}
$SummaryObject | Format-Table -AutoSize
$heading
[string[]]$rawSorter = $null
$selector = $null
if( $received )
{
$rawSorter = @( 'TotalReceived' )
$selector = [System.Collections.ArrayList]@( 'Source' , @{n='Source DNS Name';e={[system.net.dns]::GetHostByAddress($_.source)|Select -ExpandProperty HostName}} , @{n='Source port';e={$_.SourcePort}} , @{n="Total Received (KB)";e={[math]::Round($_.TotalReceived / 1KB,1)}} )
}
if( $sent )
{
$rawSorter = @( 'TotalSent' )
$selector = [System.Collections.ArrayList]@( 'Destination' , @{n='Destination DNS Name';e={[system.net.dns]::GetHostByAddress($_.destination)|Select -ExpandProperty HostName}} , @{n='Destination port';e={$_.DestinationPort}} , @{n="Total Sent (KB)";e={[math]::Round($_.TotalSent / 1KB,1)}} )
}
if( $ppid -lt 0 )
{
$null = $selector.Add( 'Process Name(s)' )
}
$connectionTable.GetEnumerator()|Select -ExpandProperty Value | Where-Object { $rawSorter } | Sort-Object -Property $rawSorter -descending | Select -First $top | ForEach-Object `
{
## reconstruct IP addresses
if( $_.source )
{
$_.source = [ipaddress]$_.source
}
if( $_.destination )
{
$_.destination = [ipaddress]$_.destination
}
$result = $_
if( $ppid -lt 0 ) ## computer or session context so have pids we need to convert to process names (and counts)
{
[hashtable]$processes = @{}
$_.Pids | ForEach-Object `
{
[string]$thisProcess = Get-Process -Id $_ -ErrorAction SilentlyContinue | Select -ExpandProperty Name -ErrorAction SilentlyContinue
if( [string]::IsNullOrEmpty( $thisProcess ) )
{
$thisProcess = $_ ## can't get process, probably as terminated
}
$existingProcess = $processes[ $thisProcess ]
if( $existingProcess )
{
$processes.Set_Item( $thisProcess , [int]$existingProcess + 1 )
}
else
{
$processes.Add( $thisProcess , [int]1 )
}
}
Add-Member -InputObject $result -MemberType NoteProperty -Name 'Process Name(s)' -Value (( $processes.GetEnumerator() | ForEach-Object { if( $_.Value -gt 1 ) { "$($_.Name)($($_.Value))" } else { "$($_.Name)" } } ) -join ',')
}
$result
} | select -Property $selector | Format-Table -AutoSize
cleanup