~speguero/sirubo

ref: v0.3 sirubo/sirubo -rwxr-xr-x 9.2 KiB
a7521915 — Steven Peguero makefile: account for readme and openbsd uninstall 9 months ago
                                                                                
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/sh -ue

#            .__            ___.           
#       _____|__|______ __ _\_ |__   ____  
#      /  ___/  \_  __ \  |  \ __ \ /  _ \ 
#      \___ \|  ||  | \/  |  / \_\ (  <_> )
#     /____  >__||__|  |____/|___  /\____/ 
#          \/                    \/        
#                    v0.3
#
#      ASN prefix (big tech conglomerate)
#           outbound traffic blocker.
#
#  "Special shout-out to Google and Facebook!"

umask u=rw,go=

list_asn=
list_prefixes_ipv4=
list_prefixes_ipv6=
name_program="sirubo"
name_os="$(uname -s)"
whoisdomain="whois.radb.net"  # WHOIS domain to query for ASN prefixes.

if [ "$name_os" = "OpenBSD" ]
then
	fpath_conf="/etc/$name_program.conf"
	fpath_ruleset="/etc/pf.$name_program.conf"
fi

if [ "$name_os" = "Linux" ]
then
	fpath_conf="/usr/local/etc/$name_program.conf"
	fpath_ruleset="/usr/local/etc/$name_program.ruleset"
	fname_service="$name_program.service"
fi

main()
{
	if [ "$*" = "" ] || [ "$*" = "-h" ]
	then
		usage
	fi

	if ! confirm_root
	then
		exit 1
	fi
	
	if ! confirm_prereqs "$fpath_conf"
	then
		exit 1
	fi

	if [ "$*" = "-c" ]
	then
		# Import Program Configuration File:

		list_asn="$(grep -Eo "^AS[^ ]+" "$fpath_conf")"

		# Create Firewall Ruleset Configuration File:

		if [ "$name_os" = "OpenBSD" ]
		then
			# Create Anchor in pf.conf:

			if ! grep -Eq "^anchor $name_program$" "/etc/pf.conf"
			then
				printf "anchor %s" "$name_program" >> "/etc/pf.conf"
			fi

			# Create Ruleset:

			test -f "$fpath_ruleset.tmp" && rm -f "$fpath_ruleset.tmp"
			touch "$fpath_ruleset.tmp"

			# Perform WHOIS Query of ASN(s) for Prefixes:

			wait_net_conn "$whoisdomain"
			confirm_asn "$whoisdomain" "$list_asn"

			list_prefixes_ipv4="$(print_prefixes_ipv4 "$whoisdomain" "$list_asn")"
			list_prefixes_ipv6="$(print_prefixes_ipv6 "$whoisdomain" "$list_asn")"

			if [ -z "$list_prefixes_ipv4" ] && [ -z "$list_prefixes_ipv6" ]
			then
				>&2 printf "> error: asn queries did not return any prefix results.\n"
				exit 1
			fi

			# Add Rules to Reject ASN Prefixes:
			
			if [ -n "$list_prefixes_ipv4" ]
			then
				printf "%s\n" "$list_prefixes_ipv4" | while IFS= read -r prefix
				do
					printf "block return on any inet from any to %s\n" "$prefix" >> "$fpath_ruleset.tmp"
				done
			fi

			if [ -n "$list_prefixes_ipv6" ]
			then
				printf "%s\n" "$list_prefixes_ipv6" | while IFS= read -r prefix
				do
					printf "block return on any inet6 from any to %s\n" "$prefix" >> "$fpath_ruleset.tmp"
				done
			fi

			# Create Service for Ruleset Persistency:

			print_openbsd_service_file "$name_program" "$fpath_ruleset" > "/etc/rc.d/$name_program"
			chmod ugo=rx "/etc/rc.d/$name_program"
			rcctl enable "$name_program"

			# Load Ruleset:

			if [ -f "$fpath_ruleset" ]
			then
				mv -f "$fpath_ruleset" "$fpath_ruleset.backup"
			fi

			mv -f "$fpath_ruleset.tmp" "$fpath_ruleset"
			pfctl -a "$name_program" -f "$fpath_ruleset"
		fi

		if [ "$name_os" = "Linux" ]
		then
			# Create Ruleset:

			test -f "$fpath_ruleset.tmp" && rm -f "$fpath_ruleset.tmp"
			touch "$fpath_ruleset.tmp"

			{
				printf '#!/usr/sbin/nft -f\n\n'
				printf 'add table inet %s\n' "$name_program"
				printf 'flush table inet %s\n' "$name_program"
				printf 'add chain inet %s %s { type filter hook output priority 0 ; policy accept; }\n' "$name_program" "$name_program"
			} > "$fpath_ruleset.tmp"

			# Perform WHOIS Query of ASN(s) for Prefixes:

			wait_net_conn "$whoisdomain"
			confirm_asn "$whoisdomain" "$list_asn"

			list_prefixes_ipv4="$(print_prefixes_ipv4 "$whoisdomain" "$list_asn")"
			list_prefixes_ipv6="$(print_prefixes_ipv6 "$whoisdomain" "$list_asn")"

			if [ -z "$list_prefixes_ipv4" ] && [ -z "$list_prefixes_ipv6" ]
			then
				>&2 printf "> error: asn queries did not return any prefix results.\n"
				exit 1
			fi

			# Add Rules to Reject ASN Prefixes:

			if [ -n "$list_prefixes_ipv4" ]
			then
				printf "%s\n" "$list_prefixes_ipv4" | while IFS= read -r prefix
				do
					printf "add rule inet %s %s ip daddr %s reject\n" "$name_program" "$name_program" "$prefix" >> "$fpath_ruleset.tmp"
				done
			fi

			if [ -n "$list_prefixes_ipv6" ]
			then
				printf "%s\n" "$list_prefixes_ipv6" | while IFS= read -r prefix
				do
					printf "add rule inet %s %s ip6 daddr %s reject\n" "$name_program" "$name_program" "$prefix" >> "$fpath_ruleset.tmp"
				done
			fi

			# Create Service for Ruleset Persistency:

			print_linux_systemd_service_file "$name_program" "$fpath_ruleset" > "/etc/systemd/system/$fname_service"
			systemctl -q daemon-reload
			systemctl -q reenable "$fname_service"

			# Load Ruleset:

			if [ -f "$fpath_ruleset" ]
			then
				mv -f "$fpath_ruleset" "$fpath_ruleset.backup"
			fi

			mv -f "$fpath_ruleset.tmp" "$fpath_ruleset"
			nft -f "$fpath_ruleset"
		fi

		exit
	fi

	if [ "$*" = "-f" ]
	then
		if [ -f "$fpath_ruleset" ]
		then
			less "$fpath_ruleset"
			exit
		else
			>&2 printf "> error: firewall ruleset not found.\n"
			exit 1
		fi
	fi

	if [ "$*" = "-r" ]
	then
		if [ "$name_os" = "OpenBSD" ]
		then
			rcctl enable "$name_program"
			pfctl -a "$name_program" -f "$fpath_ruleset"
		fi

		if [ "$name_os" = "Linux" ]
		then
			systemctl -q enable "$fname_service"
			nft -f "$fpath_ruleset"
		fi

		exit
	fi

	if [ "$*" = "-s" ]
	then
		if [ "$name_os" = "OpenBSD" ]
		then
			rcctl disable "$name_program"
			pfctl -a "$name_program" -F rules 2> /dev/null
		fi

		if [ "$name_os" = "Linux" ]
		then
			systemctl -q disable "$fname_service"
			nft delete table inet "$name_program" 2> /dev/null
		fi

		exit
	fi
}

confirm_asn()
{
	# Report, to user, ASNs they have specified and nonexistent ASN IPv4
	# and/or IPv6 prefixes.

	whoisdomain="$1"
	list_asn="$2"
	
	printf "\n> retrieving asn prefixes from:\n\n"
	printf "%s\n" "$list_asn" | while IFS= read -r asn
	do
		printf "  %s\n" "$asn"
	done
	printf "\n"

	printf "%s\n" "$list_asn" | while IFS= read -r asn
	do
		if ! whois -h "$whoisdomain" -- -i origin "$asn" | grep -Eq '^route\:'
		then
			>&2 printf "> warn: asn \"%s\" did not return ipv4 prefix results.\n" "$asn"
		fi

		if ! whois -h "$whoisdomain" -- -i origin "$asn" | grep -Eq '^route6\:'
		then
			>&2 printf "> warn: asn \"%s\" did not return ipv6 prefix results.\n" "$asn"
		fi
	done
}

confirm_prereqs()
{
	# Ensure program prerequisites exist.

	fpath_conf="$1"

	if ! [ -f "$fpath_conf" ]
	then
		printf "  info: creating configuration file at \"%s\". specify at least one asn before continuing.\n" "$fpath_conf"
		touch "$fpath_conf" && \
			chown 0:0 "$fpath_conf" && \
			chmod 600 "$fpath_conf"
		return 1
	fi

	if ! grep -Eqo "^AS[^ ]+" "$fpath_conf"
	then
		>&2 printf "> error: configuration file \"%s\" does not contain at least one asn.\n" "$fpath_conf"
		return 1
	fi

	if [ "$name_os" = "OpenBSD" ]
	then
		if ! which pfctl > /dev/null 2>&1
		then
			>&2 printf "> error: \"pfctl\" is a prerequisite and was not found.\n"
			return 1
		fi

		if ! which whois > /dev/null 2>&1
		then
			>&2 printf "> error: \"whois\" is a prerequisite and was not found.\n"
			return 1
		fi
	fi

	if [ "$name_os" = "Linux" ]
	then

		if ! [ -d "/run/systemd/system" ]
		then
			>&2 printf "> error: \"systemd\" is a prerequisite and was not found.\n"
			return 1
		fi

		if ! which nft > /dev/null 2>&1
		then
			>&2 printf "> error: \"nft\" is a prerequisite and was not found.\n"
			return 1
		fi

		if ! which whois > /dev/null 2>&1
		then
			>&2 printf "> error: \"whois\" is a prerequisite and was not found.\n"
			return 1
		fi
	fi

	return 0
}

confirm_root()
{
	# Ensure program is running under root.

	if [ "$(id -u)" != 0 ]
	then
		>&2 printf "> error: not running as root.\n"
		return 1
	fi

	return 0
}

print_prefixes_ipv4()
{
	# Return list of IPv4 prefixes of specified ASN.

	whoisdomain="$1"
	list_asn="$2"

	printf "%s\n" "$list_asn" | while IFS= read -r asn
	do
		whois -h "$whoisdomain" -- -i origin "$asn" | awk '/route:/ {print $2}'
	done
}

print_prefixes_ipv6()
{
	# Return list of IPv6 prefixes of specified ASN.

	whoisdomain="$1"
	list_asn="$2"

	printf "%s\n" "$list_asn" | while IFS= read -r asn
	do
		whois -h "$whoisdomain" -- -i origin "$asn" | awk '/route6:/ {print $2}'
	done
}

print_linux_systemd_service_file()
{
	name_program="$1"
	fpath_ruleset="$2"

	cat <<EOF
[Unit]
Description=ASN prefix outbound traffic blocker
After=network-online.target

[Service]
Type=oneshot
ExecStartPre=/usr/sbin/nft -f "$fpath_ruleset"
ExecStart=/usr/local/bin/$name_program -c

[Install]
WantedBy=multi-user.target
EOF
}

print_openbsd_service_file()
{
	name_program="$1"
	fpath_ruleset="$2"

	cat <<EOF
#!/bin/ksh

daemon="$(dirname "$(readlink -f "$0")")/$name_program"

. /etc/rc.d/rc.subr

rc_pre() {
	\${rcexec} "(pfctl -a "$name_program" -f "$fpath_ruleset" &)"
}

rc_start() {
	\${rcexec} "(\${daemon} -c &)"
}

rc_cmd \$1
EOF
}

usage()
{
	cat <<EOF
usage: $(basename "$0") [-c] [-f] [-r] [-s]

  -c    Create firewall ruleset and ruleset
        persistency service.

  -f    Show cached firewall ruleset.

  -r    Resume enforcement of cached firewall ruleset
        and enable ruleset persistency service.

  -s    Disable cached firewall ruleset and ruleset
        persistency service.
EOF

	exit 1
}

wait_net_conn()
{
	# To account for system reboots and network connectivity delays
	# thereafter, wait until a TCP connection to port 43 of the WHOIS
	# host opens.

	whoisdomain="$1"

	until nc -w 1 "$whoisdomain" 43 > /dev/null 2>&1
	do
		sleep 1
	done
}

main "$@"