-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_util_firewall_whitelist_ip.sh
More file actions
executable file
·268 lines (251 loc) · 6.89 KB
/
linux_util_firewall_whitelist_ip.sh
File metadata and controls
executable file
·268 lines (251 loc) · 6.89 KB
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
#!/bin/bash
#
# Firewall - Whitelist IP
#
# Add an IP address to the firewall whitelist.
#
# Supports:
# Linux-All
#
# Category:
# Firewall
#
# Syntax:
# --ip=<ip> - IP address to whitelist (REQUIRED)
# --comment=<comment> - Optional comment for the rule
#
# License:
# AGPLv3
#
# Author:
# Charlie Powell <cdp1337@bitsnbytes.dev>
#
# Link:
# https://github.com/eVAL-Agency/ScriptsCollection
#
# Changelog:
# 20250105 - Initial version
##
# Simple wrapper to emulate `which -s`
#
# The -s flag is not available on all systems, so this function
# provides a consistent way to check for command existence
# without having to include '&>/dev/null' everywhere.
#
# Returns 0 on success, 1 on failure
#
# Arguments:
# $1 - Command to check
#
# CHANGELOG:
# 2025.12.15 - Initial version (for a regression fix)
#
function cmd_exists() {
local CMD="$1"
which "$CMD" &>/dev/null
return $?
}
##
# Get which firewall is enabled,
# or "none" if none located
function get_enabled_firewall() {
if [ "$(systemctl is-active firewalld)" == "active" ]; then
echo "firewalld"
elif [ "$(systemctl is-active ufw)" == "active" ]; then
echo "ufw"
elif [ "$(systemctl is-active iptables)" == "active" ]; then
echo "iptables"
else
echo "none"
fi
}
##
# Get which firewall is available on the local system,
# or "none" if none located
#
# CHANGELOG:
# 2025.12.15 - Use cmd_exists to fix regression bug
# 2025.04.10 - Switch from "systemctl list-unit-files" to "which" to support older systems
function get_available_firewall() {
if cmd_exists firewall-cmd; then
echo "firewalld"
elif cmd_exists ufw; then
echo "ufw"
elif systemctl list-unit-files iptables.service &>/dev/null; then
echo "iptables"
else
echo "none"
fi
}
##
# Add an "allow" rule to the firewall in the INPUT chain
#
# Arguments:
# --port <port> Port(s) to allow
# --source <source> Source IP to allow (default: any)
# --zone <zone> Zone to allow (default: public)
# --tcp|--udp Protocol to allow (default: tcp)
# --proto <tcp|udp> Protocol to allow (alternative method)
# --comment <comment> (only UFW) Comment for the rule
#
# Specify multiple ports with `--port '#,#,#'` or a range `--port '#:#'`
#
# CHANGELOG:
# 2025.11.23 - Use return codes instead of exit to allow the caller to handle errors
# 2025.04.10 - Add "--proto" argument as alternative to "--tcp|--udp"
#
function firewall_allow() {
# Defaults and argument processing
local PORT=""
local PROTO="tcp"
local SOURCE="any"
local FIREWALL=$(get_available_firewall)
local ZONE="public"
local COMMENT=""
while [ $# -ge 1 ]; do
case $1 in
--port)
shift
PORT=$1
;;
--tcp|--udp)
PROTO=${1:2}
;;
--proto)
shift
PROTO=$1
;;
--source|--from)
shift
SOURCE=$1
;;
--zone)
shift
ZONE=$1
;;
--comment)
shift
COMMENT=$1
;;
*)
PORT=$1
;;
esac
shift
done
if [ "$PORT" == "" -a "$ZONE" != "trusted" ]; then
echo "firewall_allow: No port specified!" >&2
return 2
fi
if [ "$PORT" != "" -a "$ZONE" == "trusted" ]; then
echo "firewall_allow: Trusted zones do not use ports!" >&2
return 2
fi
if [ "$ZONE" == "trusted" -a "$SOURCE" == "any" ]; then
echo "firewall_allow: Trusted zones require a source!" >&2
return 2
fi
if [ "$FIREWALL" == "ufw" ]; then
if [ "$SOURCE" == "any" ]; then
echo "firewall_allow/UFW: Allowing $PORT/$PROTO from any..."
ufw allow proto $PROTO to any port $PORT comment "$COMMENT"
elif [ "$ZONE" == "trusted" ]; then
echo "firewall_allow/UFW: Allowing all connections from $SOURCE..."
ufw allow from $SOURCE comment "$COMMENT"
else
echo "firewall_allow/UFW: Allowing $PORT/$PROTO from $SOURCE..."
ufw allow from $SOURCE proto $PROTO to any port $PORT comment "$COMMENT"
fi
return 0
elif [ "$FIREWALL" == "firewalld" ]; then
if [ "$SOURCE" != "any" ]; then
# Firewalld uses Zones to specify sources
echo "firewall_allow/firewalld: Adding $SOURCE to $ZONE zone..."
firewall-cmd --zone=$ZONE --add-source=$SOURCE --permanent
fi
if [ "$PORT" != "" ]; then
echo "firewall_allow/firewalld: Allowing $PORT/$PROTO in $ZONE zone..."
if [[ "$PORT" =~ ":" ]]; then
# firewalld expects port ranges to be in the format of "#-#" vs "#:#"
local DPORTS="${PORT/:/-}"
firewall-cmd --zone=$ZONE --add-port=$DPORTS/$PROTO --permanent
elif [[ "$PORT" =~ "," ]]; then
# Firewalld cannot handle multiple ports all that well, so split them by the comma
# and run the add command separately for each port
local DPORTS="$(echo $PORT | sed 's:,: :g')"
for P in $DPORTS; do
firewall-cmd --zone=$ZONE --add-port=$P/$PROTO --permanent
done
else
firewall-cmd --zone=$ZONE --add-port=$PORT/$PROTO --permanent
fi
fi
firewall-cmd --reload
return 0
elif [ "$FIREWALL" == "iptables" ]; then
echo "firewall_allow/iptables: WARNING - iptables is untested"
# iptables doesn't natively support multiple ports, so we have to get creative
if [[ "$PORT" =~ ":" ]]; then
local DPORTS="-m multiport --dports $PORT"
elif [[ "$PORT" =~ "," ]]; then
local DPORTS="-m multiport --dports $PORT"
else
local DPORTS="--dport $PORT"
fi
if [ "$SOURCE" == "any" ]; then
echo "firewall_allow/iptables: Allowing $PORT/$PROTO from any..."
iptables -A INPUT -p $PROTO $DPORTS -j ACCEPT
else
echo "firewall_allow/iptables: Allowing $PORT/$PROTO from $SOURCE..."
iptables -A INPUT -p $PROTO $DPORTS -s $SOURCE -j ACCEPT
fi
iptables-save > /etc/iptables/rules.v4
return 0
elif [ "$FIREWALL" == "none" ]; then
echo "firewall_allow: No firewall detected" >&2
return 1
else
echo "firewall_allow: Unsupported or unknown firewall" >&2
echo 'Please report this at https://github.com/cdp1337/ScriptsCollection/issues' >&2
return 1
fi
}
function usage() {
cat >&2 <<EOD
Usage: $0 [options]
Options:
--ip=<ip> - IP address to whitelist (REQUIRED)
--comment=<comment> - Optional comment for the rule
Add an IP address to the firewall whitelist.
EOD
exit 1
}
# Parse arguments
SOURCE=""
COMMENT=""
while [ "$#" -gt 0 ]; do
case "$1" in
--ip=*|--ip)
[ "$1" == "--ip" ] && shift 1 && SOURCE="$1" || SOURCE="${1#*=}"
[ "${SOURCE:0:1}" == "'" ] && [ "${SOURCE:0-1}" == "'" ] && SOURCE="${SOURCE:1:-1}"
[ "${SOURCE:0:1}" == '"' ] && [ "${SOURCE:0-1}" == '"' ] && SOURCE="${SOURCE:1:-1}"
;;
--comment=*|--comment)
[ "$1" == "--comment" ] && shift 1 && COMMENT="$1" || COMMENT="${1#*=}"
[ "${COMMENT:0:1}" == "'" ] && [ "${COMMENT:0-1}" == "'" ] && COMMENT="${COMMENT:1:-1}"
[ "${COMMENT:0:1}" == '"' ] && [ "${COMMENT:0-1}" == '"' ] && COMMENT="${COMMENT:1:-1}"
;;
-h|--help) usage;;
*) echo "Unknown argument: $1" >&2; usage;;
esac
shift 1
done
if [ -z "$SOURCE" ]; then
usage
fi
FIREWALL_ENABLED="$(get_enabled_firewall)"
if [ "$FIREWALL_ENABLED" == "none" ]; then
echo "No firewall enabled!" >&2
exit 1
fi
firewall_allow --zone trusted --source "$SOURCE" --comment "$COMMENT"