-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·403 lines (311 loc) · 7.01 KB
/
functions.sh
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# pdcfw - manages PDC Linux Netfilter/IPtables firewall configuration
# functions.sh - pdcfw functions
# Author: Ilari Korhonen, KTH Royal Institute of Technology
#
# Copyright (C) 2018 KTH Royal Institute of Technology. All rights reserved.
# See LICENSE file for more information.
# define version
version="0.1.2"
# set defaults for executables and paths
fwcmd="/usr/sbin/iptables"
save="/usr/sbin/iptables-save"
restore="/usr/sbin/iptables-restore"
configfile="/etc/sysconfig/pdcfw"
prog="$(basename $0)"
# pdcfw defaults
CONFIGDIR="$(dirname $0)" # default configuration file dir
MAIN_FUNC="${CONFIGDIR}/pdcfw-main.sh" # default main function
IPTABLES_RULES="/etc/sysconfig/iptables" # default ruleset configuration file
TRUSTED_IFACES="lo" # trusted interfaces (everything allowed)
LIMIT_ICMP_ECHO="3/s" # throttling limit for ICMP Echo Requests
LIMIT_ICMP_ECHO_BURST="10" # burst limit for ICMP Echo Requests
LIMIT_LOG="3/s" # throttling limit for logging
LIMIT_LOG_BURST="10" # burst limit for logging
# bold - set terminal font to bold with ANSI escape seq
function boldfont() { printf "\e[1m"; }
# ansireset - resets previous ANSI escape seq
function ansireset() { printf "\e[0m"; }
# success - display success message (w/ fancy ANSI control seqs on a terminal)
function success()
{
[ -t 1 ] && boldfont
printf " [ OK ] "
[ -t 1 ] && ansireset
printf "\n"
}
# failure - display failure message
function failure()
{
[ -t 1 ] && boldfont
printf " [ OK ] "
[ -t 1 ] && ansireset
printf "\n"
}
# show_action - show command
function show_action()
{
case "$1" in
running)
shift
fw=$(basename $fwcmd)
${fwcmd} -S | while read line; do
printf "${fw} ${line}\n"
done
;;
status)
shift
${fwcmd} -L -v -n -t filter
;;
*)
echo "${prog}: usage ${prog} show [running|status|...]"
exit -1
;;
esac
}
# save - saves current in-memory firewall rules to a file
function save()
{
# save existing in-memory ruleset to disk
${save} -t filter > ${IPTABLES_RULES}
}
# flush - flushes current in-memory ruleset
function flush()
{
# flush in-memory ruleset
${fwcmd} --flush
}
# add_filter_rule - add a rule to IPTables filter table
function add_filter_rule()
{
local chain=""
local proto=""
local iface=""
local src=""
local sport=""
local dst=""
local dport=""
local state=""
local limit=""
local limitburst=""
local icmptype=""
local logprefix=""
local rejectwith=""
local jump=""
while [ $# -gt 0 ]; do
case "$1" in
with)
shift
chain=$1
shift
;;
via)
shift
if [ "$1" != "any" ]; then
iface="-i $1"
fi
shift
;;
proto)
shift
if [ "$1" != "any" ]; then
proto="-p $1"
fi
shift
;;
from)
shift
if [ "$1" != "any" ]; then
src="-s $1"
fi
shift
;;
sport)
shift
if [ "$1" != "any" ]; then
sport="--sport $1"
fi
shift
;;
to)
shift
if [ "$1" != "any" ]; then
dst="-d $1"
fi
shift
;;
dport)
shift
if [ "$1" != "any" ]; then
dport="--dport $1"
fi
shift
;;
stateful)
shift
state="-m state --state NEW"
;;
state)
shift
state="-m state --state $1"
shift
;;
limit)
shift
limit="-m limit --limit $1"
shift
;;
limit-burst)
shift
limitburst="--limit-burst $1"
shift
;;
icmp-type)
shift
icmptype="--icmp-type $1"
shift
;;
log-prefix)
shift
logprefix="--log-prefix $1"
shift
;;
reject-with)
shift
rejectwith="--reject-with $1"
shift
;;
jump)
shift
jump="-j $1"
shift
;;
# at first unknown argument, stop parsing
*)
break
;;
esac
done
# execute iptables add with parsed arguments and the rest
${fwcmd} -A ${chain} \
${iface} \
${proto} \
${icmptype} \
${src} \
${sport} \
${dst} \
${dport} \
${state} \
${limit} \
${limitburst} \
${rejectwith} \
${jump} \
${logprefix} \
$@
}
# allow - generic macro for a rule accepting packets
function allow()
{
add_filter_rule $@ jump ACCEPT
}
# drop - generic macro for dropping packets
function drop()
{
add_filter_rule $@ jump DROP
}
# reject - generic macro for rejecting packets
function reject()
{
add_filter_rule $@ jump REJECT
}
# log - generic macro for logging packets
function log()
{
add_filter_rule $@ jump LOG
}
# allow_trusted_interfaces - accept all packets from/to trusted network interfaces
function allow_trusted_interfaces()
{
if [ $# -eq 1 ]; then
local chain=$1
for iface in ${TRUSTED_IFACES}; do
allow with ${chain} via ${iface} from any to any
done
fi
}
# allow_established - accept all packets related to established connections
function allow_established()
{
if [ $# -eq 1 ]; then
local chain=$1
allow with ${chain} state RELATED,ESTABLISHED
fi
}
# allow_icmp_with_limits - accept ICMP packets, but only up to a limit
function allow_icmp_with_limits()
{
if [ $# -eq 1 ]; then
local chain=$1
# for ICMP Echo Requests, we limit the number of packets
allow with ${chain} proto icmp icmp-type echo-request limit ${LIMIT_ICMP_ECHO}
log with ${chain} proto icmp icmp-type echo-request log-prefix "netfilter:DROP:ICMP:"
drop with ${chain} proto icmp icmp-type echo-request
# the rest of ICMP we allow blindly, for now
allow with ${chain} proto icmp
fi
}
# drop_and_log_all - logs and drops all packets
function drop_and_log_all()
{
if [ $# -eq 1 ]; then
local chain=$1
log with ${chain} limit ${LIMIT_LOG} limit-burst ${LIMIT_LOG_BURST} log-prefix "netfilter:DROP:${chain}:"
drop with ${chain}
fi
}
# reject_and_log_all - rejects (with ICMP) and logs dropped packets
function reject_and_log_all()
{
if [ $# -eq 1 ]; then
local chain=$1
log with ${chain} limit ${LIMIT_LOG} limit-burst ${LIMIT_LOG_BURST} log-prefix "netfilter:REJECT:${chain}"
reject with ${chain} reject-with icmp-host-prohibited
fi
}
# set_default_policy - sets netfilter default policy
function set_default_policy()
{
if [ $# -eq 2 ]; then
local chain=$1
local policy=$2
# TODO: log policy reset
${fwcmd} -P ${chain} ${policy}
fi
}
# select_table - select rule table for manipulation
function select_table()
{
if [ $# -eq 1 ]; then
local table=$1
echo "*${table}"
fi
}
# commit_ruleset - commit specified ruleset
function commit_ruleset()
{
echo "COMMIT"
}
# _main - built-in main function that wraps around main()
function _main()
{
# in commit mode specify table filter (default)
if [ ${commit} == "true" ]; then
select_table filter
fi
# execute firewall ruleset starting from main()
main
# in commit mode say commit
if [ ${commit} == "true" ]; then
commit_ruleset
fi
}