-
Notifications
You must be signed in to change notification settings - Fork 6
/
FirewallRules.ps1
157 lines (140 loc) · 7.22 KB
/
FirewallRules.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
#This will help change Windows Firewall Rules much faster than going through the GUI. Keep in mind, there's no input checking and this was built for simple ease of use.
#******************************************************************
#******************************************************************
#** **
#** Firewall Rules **
#** Written by: Z3R0th **
#** **
#** **
#******************************************************************
#******************************************************************
#Sets Protocol to TCP if TCP is selected.
function TCP-Rule {
$Protocol = "TCP"
}
#Sets Protocol to UDP if UDP is selected
function UDP-Rule {
$Protocol = "UDP"
}
#Sets Protocol to both TCP and UDP if selected
function Both-Rule {
#Checks user input to ensure proper values were assigned.
Write-Host "
+-------------++-------------------------++------------------------------------------------------+
| Name || Description || Your Input
+-------------++-------------------------++------------------------------------------------------+
| DisplayName || The Display Name || $DisplayName
| Direction || Inbound or Outbound || $Direction
| Description || Description of Rule || $Description
| Enabled || Active or not || $Enabled
| Action || Allow or deny || $Action
| Protocol || TCP or UDP || $Protocol
| LocalPort || Port for Rule || $LocalPort
+-------------++-------------------------++------------------------------------------------------+
"
#Reiterating upon the above box.
$Answer = Read-Host -Prompt "Alright, does everything look right to you?"
If (( $Answer -eq "n" ) -or ( $Answer -eq "no" ) -or ( $Answer -eq "N" ) -or ( $Answer -eq "No" )) {
exit
}
If (($Answer -eq "y" ) -or ($Answer -eq "yes" ) -or ($Answer -eq "Y" ) -or ($Answer -eq "Yes" )) {
New-NetFirewallRule -DisplayName "$DisplayName for TCP" -Direction "$Direction" -Action "$Action" -Enabled "$Enabled" -Description "$Description for TCP" -LocalPort "$LocalPort" -Protocol "TCP"
New-NetFirewallRule -DisplayName "$DisplayName for UDP" -Direction "$Direction" -Action "$Action" -Enabled "$Enabled" -Description "$Description for UDP" -LocalPort "$LocalPort" -Protocol "UDP"
exit
}
}
Function Invoke-FirewallRules {
clear
echo ""
Write-Host "
_______ ____ ____ __
/ ____(_)_______ _ ______ _/ / / / __ \__ __/ /__ _____
/ /_ / / ___/ _ \ | /| / / __ ` / / / / /_/ / / / / / _ \/ ___/
/ __/ / / / / __/ |/ |/ / /_/ / / / / _, _/ /_/ / / __(__ )
/_/ /_/_/ \___/|__/|__/\__,_/_/_/ /_/ |_|\__,_/_/\___/____/
"
echo ""
#Defines what you want the Firewall Rule named.
$DisplayName = Read-Host -Prompt "What would you like to name this Firewall Rule?"
echo ""
#Defines whether or not the rule will apply to Inbound or Outbound traffic
$Direction = Read-Host -Prompt "Would you like this to be Inbound or Outbound?"
if (( $Direction -eq "Inbound" ) -or ( $Direction -eq "in" ) -or ( $Direction -eq "In" ) -or ( $Direction -eq "inbound" )) {
$Direction = "Inbound"
}
if (( $Direction -eq "Outbound" ) -or ( $Direction -eq "out" ) -or ( $Direction -eq "Out" ) -or ($Direction -eq "outbound" )) {
$Direction = "Outbound"
}
if (( $Direction -eq $null ) -or ( $Direction -eq "") -or ( $Direction -eq " " )) {
Write-Host "You entered a null value. This is not acceptable." ; exit
}
echo ""
#Defines whether or not you would like to enter in a description for your rule. I would recommend doing so to prevent confusion in the future.
$Description = Read-Host -Prompt "Would you like to enter a description for your Firewall Rule?"
if (( $Description -eq "y" ) -or ( $Description -eq "yes" ) -or ( $Description -eq "Y" ) -or ( $Description -eq "Yes" )) {
$Description = Read-Host -Prompt "Please enter the description here"
}
if (( $Description -eq "n" ) -or ( $Description -eq "no" ) -or ( $Description -eq "N" ) -or ( $Description -eq "No" )) {
$Description = $null
}
echo ""
#Defines whether or not you wish to have this rule go into effect immediately or if you would like to build it but have not not be active.
$Enabled = Read-Host -Prompt "Would you like to enable this rule now? [ Y / N ]"
if (( $Enabled -eq "Y" ) -or ( $Enabled -eq "Yes" ) -or ( $Enabled -eq "y" ) -or ( $Enabled -eq "yes" )) {
$Enabled = "True"
}
if (($Enabled -eq "n" ) -or ($Enabled -eq "no" ) -or ($Enabled -eq "N" ) -or ($Enabled -eq "No" )) {
$Enabled = "False"
}
if (( $Enabled -eq $null ) -or ( $Enabled -eq "") -or ( $Enabled -eq " " )) {
Write-Host "You entered a null value. This is not acceptable." ; exit
}
echo ""
#Defines whether or not you would like to let traffic through or block.
$Action = Read-Host -Prompt "Would you like to Allow or Block this traffic?"
if (( $Action -eq "allow" ) -or ( $Action -eq "Allow" )) {
$Action = "allow"
}
if (( $Action -eq "block" ) -or ( $Action -eq "Block" )) {
$Action = "Block"
}
echo ""
#Defines the port that this Firewall Rule applies to
$LocalPort = Read-Host -Prompt "What port would you like this to be on?"
echo ""
$Protocol = Read-Host -Prompt "Do you want this to be TCP, UDP, or Both?"
if (( $Protocol -eq "TCP" ) -or ( $Protocol -eq "tcp" )) {
TCP-Rule
}
if (( $Protocol -eq "UDP" ) -or ( $Protocol -eq "udp" )) {
UDP-Rule
}
if (( $Protocol -eq "Both" ) -or ( $Protocol -eq "both" )) {
Both-Rule
}
echo ""
#Checks user input to ensure proper values were assigned.
Write-Host "
+-------------++-------------------------++------------------------------------------------------+
| Name || Description || Your Input
+-------------++-------------------------++------------------------------------------------------+
| DisplayName || The Display Name || $DisplayName
| Direction || Inbound or Outbound || $Direction
| Description || Description of Rule || $Description
| Enabled || Active or not || $Enabled
| Action || Allow or deny || $Action
| Protocol || TCP or UDP || $Protocol
| LocalPort || Port for Rule || $LocalPort
+-------------++-------------------------++------------------------------------------------------+
"
#Reiterating upon the above box.
$Answer = Read-Host -Prompt "Alright, does everything look right to you?"
If (( $Answer -eq "n" ) -or ( $Answer -eq "no" ) -or ( $Answer -eq "N" ) -or ( $Answer -eq "No" )) {
exit
}
If (($Answer -eq "y" ) -or ($Answer -eq "yes" ) -or ($Answer -eq "Y" ) -or ($Answer -eq "Yes" )) {
New-NetFirewallRule -DisplayName "$DisplayName" -Direction "$Direction" -Action "$Action" -Enabled "$Enabled" -Description "$Description" -LocalPort "$LocalPort" -Protocol "$Protocol"
}
}
#Runs the command.
Invoke-FirewallRules