-
Notifications
You must be signed in to change notification settings - Fork 0
/
Firewall.ps1
161 lines (156 loc) · 5.69 KB
/
Firewall.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
# Script: Windows Firewall Rule Analysis with Port Information
# Purpose: Analyzes Windows Firewall rules, matching them with port configurations and application filters
# Outputs: Table of firewall rules with associated ports, protocols, and application information
# Error handling preference
$ErrorActionPreference = 'Stop'
try {
Write-Verbose "Retrieving all firewall rules..."
# Cache all firewall rules for performance optimization
# This is faster than retrieving rules individually later
$allRules = Get-NetFirewallRule
if (-not $allRules) {
throw "Failed to retrieve firewall rules. Ensure you have administrative privileges."
}
Write-Verbose "Retrieving application filters..."
# Get application filters which contain program/process information
$allAppFilters = Get-NetFirewallApplicationFilter
if (-not $allAppFilters) {
Write-Warning "No application filters found. Program information may be limited."
}
Write-Verbose "Processing port filters and matching with rules..."
# Main analysis: Get port filters and create detailed report
Get-NetFirewallPortFilter |
Group-Object LocalPort, Protocol |
Select-Object @{
Name = 'Local Port'
Expression = {
# Try converting port to integer for proper sorting
# Keep as string if not a valid integer (e.g., "Any")
try {
$port = $_.Group[0].LocalPort
if ($p = $port -as [int]) {
$p
} else {
$port
}
} catch {
Write-Warning "Error processing local port: $_"
"Error"
}
}
},
@{
Name = 'Remote Port'
Expression = {
# Similar conversion for remote ports
try {
$port = $_.Group[0].RemotePort
if ($p = $port -as [int]) {
$p
} else {
$port
}
} catch {
Write-Warning "Error processing remote port: $_"
"Error"
}
}
},
@{
Name = 'Protocol'
Expression = {
try {
$_.Group[0].Protocol
} catch {
Write-Warning "Error retrieving protocol: $_"
"Unknown"
}
}
},
@{
Name = 'Enabled'
Expression = {
try {
# Get the current group of rules
$group = $_.Group
# Find matching rules that are enabled
$script:rules = $allRules | Where-Object {
$group.InstanceID -contains $_.Name -and
$_.Enabled -ieq 'true'
}
$script:rules | Select-Object -ExpandProperty Enabled -Unique
} catch {
Write-Warning "Error checking enabled status: $_"
$false
}
}
},
@{
Name = 'Program'
Expression = {
try {
$group = $_.Group
# Match application filters with current rules
$script:appRule = $allAppFilters | Where-Object {
$group.InstanceId -contains $_.InstanceId
}
# Get unique program paths
$programs = $script:appRule.Program | Select-Object -Unique
if ($programs) {
$programs
} else {
"Any Program" # Default when no specific program is specified
}
} catch {
Write-Warning "Error retrieving program information: $_"
"Error"
}
}
},
@{
Name = 'Direction'
Expression = {
try {
$script:rules | Select-Object -ExpandProperty Direction -Unique
} catch {
Write-Warning "Error retrieving direction: $_"
"Unknown"
}
}
},
@{
Name = 'Action'
Expression = {
try {
$script:rules | Select-Object -ExpandProperty Action -Unique
} catch {
Write-Warning "Error retrieving action: $_"
"Unknown"
}
}
},
@{
Name = 'Rules'
Expression = {
try {
# Join all rule names with newlines for readability
($script:rules |
Select-Object -ExpandProperty DisplayName -Unique |
Sort-Object) -join "`n"
} catch {
Write-Warning "Error retrieving rule names: $_"
"Error retrieving rules"
}
}
} |
# Filter to show only enabled rules
Where-Object { $_.Enabled } |
# Sort by local port for better organization
Sort-Object 'Local Port' | Format-Table
} catch {
Write-Error "Critical error in firewall analysis: $_"
throw # Re-throw the error for proper handling by calling script
} finally {
# Clean up variables to free memory
Remove-Variable -Name allRules, allAppFilters, rules, appRule -ErrorAction SilentlyContinue
}