-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg_install_gui.sh
2624 lines (2225 loc) · 95.9 KB
/
pkg_install_gui.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Colors for output
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[1;33m"
TEXTRESET="\033[0m"
RESET='\033[0m'
# Ensure the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
#Function to install Netdata
install_netdata() {
clear
echo -e "${GREEN}Installing Netdata...${TEXTRESET}"
sleep 2
if ! sudo dnf -y update; then
echo -e "${RED}System update failed. Exiting.${TEXTRESET}"
exit 1
fi
echo -e "${YELLOW}Installing EPEL repository...${TEXTRESET}"
if ! sudo dnf -y install epel-release; then
echo -e "${RED}EPEL repository installation failed. Exiting.${TEXTRESET}"
exit 1
fi
echo -e "${YELLOW}Enabling CodeReady Builder repository...${TEXTRESET}"
if ! sudo dnf config-manager --set-enabled crb; then
echo -e "${RED}Failed to enable CodeReady Builder repository. Exiting.${TEXTRESET}"
exit 1
fi
echo -e "${YELLOW}Installing required packages...${TEXTRESET}"
if ! sudo dnf -y install wget; then
echo -e "${RED}Required packages installation failed. Exiting.${TEXTRESET}"
exit 1
fi
echo -e "${YELLOW}Downloading and executing Netdata installation script...${TEXTRESET}"
if wget -O /tmp/netdata-kickstart.sh https://get.netdata.cloud/kickstart.sh; then
if ! sh /tmp/netdata-kickstart.sh --stable-channel --disable-telemetry --non-interactive; then
echo -e "${RED}Netdata installation failed. Exiting.${TEXTRESET}"
exit 1
fi
else
echo -e "${RED}Failed to download Netdata installation script. Exiting.${TEXTRESET}"
exit 1
fi
echo -e "${GREEN}Cleaning up temporary files...${TEXTRESET}"
rm -f /tmp/netdata-kickstart.sh
echo -e "${GREEN}Netdata installation completed successfully.${TEXTRESET}"
echo -e "${YELLOW}Locating inside interfaces...${TEXTRESET}"
inside_interfaces=$(nmcli -t -f NAME,DEVICE connection show --active | awk -F: '$1 ~ /-inside$/ {print $2}')
if [ -z "$inside_interfaces" ]; then
echo -e "${RED}No interface with '-inside' profile found. Exiting...${TEXTRESET}"
exit 1
fi
echo -e "${GREEN}Inside interfaces found: $inside_interfaces${TEXTRESET}"
echo -e "${YELLOW}Configuring nftables rules for Netdata...${TEXTRESET}"
sudo systemctl enable nftables
sudo systemctl start nftables
if ! sudo nft list tables | grep -q 'inet filter'; then
sudo nft add table inet filter
fi
if ! sudo nft list chain inet filter input &>/dev/null; then
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
fi
for iface in $inside_interfaces; do
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" tcp dport 19999 accept"; then
sudo nft add rule inet filter input iifname "$iface" tcp dport 19999 accept
echo -e "${GREEN}Rule added: Allow Netdata on port 19999 for interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow Netdata on port 19999 for interface $iface${TEXTRESET}"
fi
done
rfwb_status=$(systemctl is-active rfwb-portscan)
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Stopping rfwb-portscan service before saving nftables configuration...${TEXTRESET}"
systemctl stop rfwb-portscan
fi
sudo nft list ruleset >/etc/sysconfig/nftables.conf
echo -e "${YELLOW}Restarting nftables service to apply changes...${TEXTRESET}"
sudo systemctl restart nftables
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Restarting rfwb-portscan service...${TEXTRESET}"
systemctl start rfwb-portscan
fi
echo -e "${YELLOW}Current rules in the input chain:${TEXTRESET}"
sudo nft list chain inet filter input
echo -e "${GREEN}Netdata Install Complete...${TEXTRESET}"
sleep 4
}
#Function to install snmpd
install_snmpd() {
clear
# Function to validate IP address or network
function validate_ip_or_network() {
local ip_network=$1
if [[ $ip_network =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?$ ]]; then
IFS='/' read -r ip prefix <<<"$ip_network"
for octet in $(echo $ip | tr '.' ' '); do
if ((octet < 0 || octet > 255)); then
echo -e "${RED}Invalid IP address or network: $ip_network${TEXTRESET}"
return 1
fi
done
if [ -n "$prefix" ] && ((prefix < 0 || prefix > 32)); then
echo -e "${RED}Invalid prefix length: $prefix${TEXTRESET}"
return 1
fi
return 0
else
echo -e "${RED}Invalid IP address or network format: $ip_network${TEXTRESET}"
return 1
fi
}
# Function to locate the server's private IP address using nmcli
find_private_ip() {
# Find the interface ending with -inside
interface=$(nmcli device status | awk '/-inside/ {print $1}')
if [ -z "$interface" ]; then
echo -e "${RED}Error: No interface ending with '-inside' found.${TEXTRESET}"
exit 1
fi
# Extract the private IP address for the found interface
ip=$(nmcli -g IP4.ADDRESS device show "$interface" | awk -F/ '{print $1}')
if [ -z "$ip" ]; then
echo -e "${RED}Error: No IP address found for the interface $interface.${TEXTRESET}"
exit 1
fi
echo "$interface"
}
# Install SNMP daemon
echo -e "${GREEN}Installing SNMP daemon...${TEXTRESET}"
sleep 2
yum install -y net-snmp net-snmp-utils
# Ask user for SNMP version
echo -e "${YELLOW}Select SNMP version to run:${TEXTRESET}"
echo "1) SNMPv1"
echo "2) SNMPv2c"
echo "3) SNMPv3"
read -p "Enter the number corresponding to your choice (1, 2, or 3): " snmp_version
while ! [[ "$snmp_version" =~ ^[1-3]$ ]]; do
echo -e "${RED}Invalid selection. Please enter 1, 2, or 3.${TEXTRESET}"
read -p "Enter the number corresponding to your choice (1, 2, or 3): " snmp_version
done
# Ask for SNMP community string if SNMPv1 or SNMPv2c is selected
if [ "$snmp_version" == "1" ] || [ "$snmp_version" == "2" ]; then
read -p "Enter the SNMP community string (default is 'public'): " community_string
community_string=${community_string:-public}
fi
# If SNMPv3 is selected, gather additional credentials
if [ "$snmp_version" == "3" ]; then
read -p "Enter SNMPv3 username: " snmpv3_user
read -p "Enter SNMPv3 authentication protocol (MD5/SHA): " auth_protocol
read -sp "Enter SNMPv3 authentication password: " auth_pass
echo
read -p "Enter SNMPv3 privacy protocol (DES/AES): " priv_protocol
read -sp "Enter SNMPv3 privacy password: " priv_pass
echo
fi
# Ask user for IP address or network
read -p "Enter the IP address or network (e.g., 192.168.1.0/24) allowed to monitor this device: " allowed_network
while ! validate_ip_or_network "$allowed_network"; do
read -p "Please enter a valid IP address or network: " allowed_network
done
# Ask for system location and contact
read -p "Enter system location: " syslocation
read -p "Enter system contact: " syscontact
# Configure firewall using nftables
inside_interfaces=$(nmcli device status | awk '/-inside/ {print $1}')
for iface in $inside_interfaces; do
# Check and add rule for SNMP (UDP) on port 161
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" udp dport 161 accept"; then
sudo nft add rule inet filter input iifname "$iface" udp dport 161 accept
echo -e "${GREEN}Rule added: Allow SNMP (UDP) on interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow SNMP (UDP) on interface $iface${TEXTRESET}"
fi
done
# Check and handle rfwb-portscan service
rfwb_status=$(systemctl is-active rfwb-portscan)
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Stopping rfwb-portscan service before saving nftables configuration...${TEXTRESET}"
systemctl stop rfwb-portscan
fi
# Save nftables configuration
echo -e "${YELLOW}Saving nftables configuration...${TEXTRESET}"
nft list ruleset >/etc/sysconfig/nftables.conf
# Restart rfwb-portscan service if it was active
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Restarting rfwb-portscan service...${TEXTRESET}"
systemctl start rfwb-portscan
fi
# Show the added rules in the input chain
echo -e "${YELLOW}Current rules in the input chain:${TEXTRESET}"
sudo nft list chain inet filter input
# Backup existing configuration
echo -e "${YELLOW}Backing up existing configuration file...${TEXTRESET}"
cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.backup
# Create a new configuration file based on user input and the provided template
echo -e "${YELLOW}Configuring SNMP...${TEXTRESET}"
cat <<EOF >/etc/snmp/snmpd.conf
###############################################################################
# System contact information
syslocation $syslocation
syscontact $syscontact
###############################################################################
# Access Control
###############################################################################
com2sec notConfigUser $allowed_network ${community_string:-public}
# SNMPv3 user setup
$(if [ "$snmp_version" == "3" ]; then
echo "createUser $snmpv3_user $auth_protocol \"$auth_pass\" $priv_protocol \"$priv_pass\""
echo "rouser $snmpv3_user"
fi)
# Views and Access
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
view systemview included .1
access notConfigGroup "" any noauth exact systemview none none
###############################################################################
# Additional SNMP Views
###############################################################################
view rwview included ip.ipRouteTable.ipRouteEntry.ipRouteIfIndex
view rwview included ip.ipRouteTable.ipRouteEntry.ipRouteMetric1
view rwview included ip.ipRouteTable.ipRouteEntry.ipRouteMetric2
view rwview included ip.ipRouteTable.ipRouteEntry.ipRouteMetric3
view rwview included ip.ipRouteTable.ipRouteEntry.ipRouteMetric4
###############################################################################
# Process checks.
###############################################################################
# Ensure nftables is running
proc nftables
###############################################################################
# Load Average Checks
###############################################################################
load 12 14 14
###############################################################################
# Disk checks
###############################################################################
disk / 10000000 # Ensure at least 10GB of space
###############################################################################
# Extensible sections.
###############################################################################
# Uncomment and modify the following examples as needed:
# exec echotest /bin/echo hello world
# exec shelltest /bin/sh /tmp/shtest
EOF
# Start and enable SNMP service
echo -e "${YELLOW}Starting SNMP service...${TEXTRESET}"
systemctl start snmpd
systemctl enable snmpd
# Validate that the service is running
if systemctl status snmpd | grep "active (running)" >/dev/null; then
echo -e "${GREEN}SNMP service is running successfully.${TEXTRESET}"
else
echo -e "${RED}Failed to start SNMP service. Please check the configuration.${TEXTRESET}"
fi
# Continue with the rest of the script
echo -e "${GREEN}SNMP Daemon Install Complete...${TEXTRESET}"
sleep 4
}
#Function to install rfwb-portscan detection
# Function to install rfwb-portscan detection
install_portscan() {
# Script to set up nftables for detecting and blocking port scans on Red Hat systems
clear
echo -e "Installing RFWB-Portscan Detection engine..."
sleep 2
# Ensure the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Install nftables if not already installed
if ! command -v nft &>/dev/null; then
echo "Installing nftables..."
yum install -y nftables
fi
# Create or update the configuration file for user settings
CONFIG_FILE="/etc/rfwb-portscan.conf"
if [ ! -f "$CONFIG_FILE" ]; then
cat <<EOF >"$CONFIG_FILE"
# Configuration file for RFWB-Portscan service
# Maximum number of retries to obtain an external IP address. Set to 0 for infinite retries.
MAX_RETRIES=10
# Initial delay in seconds before retrying to obtain the external IP address.
INITIAL_DELAY=10
# Multiplier to increase the delay after each failed attempt (exponential backoff).
RETRY_MULTIPLIER=2
# Ports to monitor for port scan detection. Separate ports with commas.
MONITORED_PORTS="20, 21, 22, 23, 25, 53, 67, 68, 69, 80, 110, 111, 119, 135, 137, 138, 139, 143, 161, 162, 179, 389, 443, 445, 465, 514, 515, 587, 631, 636, 993, 995"
# Timeout for dynamically blocked IPs. Use 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days.
# Set to '0' for no timeout (indefinitely).
BLOCK_TIMEOUT="30m"
EOF
fi
# Load configuration settings
source "$CONFIG_FILE"
# Ensure the hosts.blocked file exists
BLOCKED_FILE="/etc/nftables/hosts.blocked"
if [ ! -f "$BLOCKED_FILE" ]; then
touch "$BLOCKED_FILE"
fi
# Ensure the ignore networks configuration file exists and populate with RFC 1918 networks
IGNORE_NETWORKS_FILE="/etc/nftables/ignore_networks.conf"
if [ ! -f "$IGNORE_NETWORKS_FILE" ]; then
echo "Creating ignore networks configuration file with RFC 1918 networks."
cat <<EOF >"$IGNORE_NETWORKS_FILE"
# Ignore file for rfwb-nft-portscan
# The port scan detection will ignore any IP addresses or networks placed into this file
# Network example
# 192.168.210.0/24
# Host example 192.168.210.10/32
# Entries must be one per line
192.168.0.0/16
10.0.0.0/8
172.16.0.0/12
EOF
# Verify if the file was created and populated correctly
if [ -f "$IGNORE_NETWORKS_FILE" ]; then
echo "Ignore networks configuration file created successfully."
else
echo "Failed to create ignore networks configuration file."
exit 1
fi
else
echo "Ignore networks configuration file already exists."
fi
# Verify file content
echo "Current contents of $IGNORE_NETWORKS_FILE:"
cat "$IGNORE_NETWORKS_FILE"
# Function to find the network interface based on connection name ending
find_interface() {
local suffix="$1"
nmcli -t -f DEVICE,CONNECTION device status | awk -F: -v suffix="$suffix" '$2 ~ suffix {print $1}'
}
# Determine the outside interface
OUTSIDE_INTERFACE=$(find_interface "-outside")
if [[ -z "$OUTSIDE_INTERFACE" ]]; then
echo "Error: Could not determine the outside interface. Please check your connection names."
exit 1
fi
# Retry mechanism to get the external IP address with exponential backoff
EXTERNAL_IP=""
attempt=1
delay=$INITIAL_DELAY
while :; do
EXTERNAL_IP=$(ip -4 addr show "$OUTSIDE_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n 1)
if [[ -n "$EXTERNAL_IP" ]]; then
break
fi
if [[ $MAX_RETRIES -ne 0 && $attempt -gt $MAX_RETRIES ]]; then
echo "Error: Failed to determine the external IP address after $MAX_RETRIES attempts. Exiting."
exit 1
fi
echo "Attempt $attempt: Could not determine the external IP address for interface $OUTSIDE_INTERFACE. Retrying in $delay seconds..."
sleep "$delay"
((attempt++))
delay=$((delay * RETRY_MULTIPLIER))
done
echo "Protecting outside interface: $OUTSIDE_INTERFACE with IP: $EXTERNAL_IP"
# Load ignore networks from the configuration file
IGNORE_NETWORKS=$(cat "$IGNORE_NETWORKS_FILE")
echo "Using ignore networks: $IGNORE_NETWORKS"
# Prepare elements string for blocked IPs set if not empty
ELEMENTS=""
if [ -s "$BLOCKED_FILE" ]; then
ELEMENTS=$(sed ':a;N;$!ba;s/\n/, /g' "$BLOCKED_FILE")
fi
# Create nftables configuration directory
NFT_CONF_DIR="/etc/nftables"
mkdir -p "$NFT_CONF_DIR"
# Create nftables configuration file
NFT_CONF_FILE="$NFT_CONF_DIR/portscan.conf"
# Clear the configuration file before writing new rules
cat <<EOL >"$NFT_CONF_FILE"
table inet portscan {
set dynamic_block {
type ipv4_addr
flags timeout
timeout $BLOCK_TIMEOUT
EOL
# Only add elements if there are IPs
if [ -n "$ELEMENTS" ]; then
cat <<EOL >>"$NFT_CONF_FILE"
elements = { $ELEMENTS }
EOL
fi
cat <<EOL >>"$NFT_CONF_FILE"
}
chain input {
type filter hook input priority 0; policy accept;
# Allow established and related connections
ct state established,related accept
# Drop packets from dynamically blocked IPs
ip saddr @dynamic_block drop
# Detect SYN packets from untrusted sources on the outside interface
iifname "$OUTSIDE_INTERFACE" tcp flags syn limit rate 10/minute log prefix "Port Scan Detected: " counter
# Use configured ports for detection
ip daddr $EXTERNAL_IP tcp dport { $MONITORED_PORTS } ct state new limit rate 3/minute log prefix "Port Scan Detected: " counter
}
}
EOL
# Apply the new nftables configuration
/usr/sbin/nft -f "$NFT_CONF_FILE"
# Verify that the nftables configuration is applied
if ! nft list tables | grep -q "inet portscan"; then
echo "Error: The portscan table is not initialized. Exiting."
exit 1
fi
# Create a pre-start script to log the outside interface and IP
PRE_START_SCRIPT="/usr/local/bin/rfwb-portscan-prestart.sh"
cat <<EOF >"$PRE_START_SCRIPT"
#!/bin/bash
# Configuration for retry mechanism
MAX_RETRIES=10
INITIAL_DELAY=5
RETRY_MULTIPLIER=2
LOG_FILE="/var/log/rfwb-portscan.log"
# Initialize log file
echo "Starting rfwb-portscan pre-start script at \$(date)" > "\$LOG_FILE"
OUTSIDE_INTERFACE=""
EXTERNAL_IP=""
attempt=1
delay=\$INITIAL_DELAY
while :; do
OUTSIDE_INTERFACE=\$(nmcli -t -f DEVICE,CONNECTION device status | awk -F: -v suffix="-outside" '\$2 ~ suffix {print \$1}')
EXTERNAL_IP=\$(ip -4 addr show "\$OUTSIDE_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n 1)
# Log the current state
echo "\$(date): Attempt \$attempt - Interface: \$OUTSIDE_INTERFACE, IP: \$EXTERNAL_IP" >> "\$LOG_FILE"
# Check if nftables service is active
if ! systemctl is-active --quiet nftables; then
echo "\$(date): nftables service is not active. Waiting for nftables service..." >> "\$LOG_FILE"
systemctl start nftables
sleep "\$delay"
((attempt++))
delay=\$((delay * RETRY_MULTIPLIER))
continue
fi
if [[ -n "\$OUTSIDE_INTERFACE" && -n "\$EXTERNAL_IP" ]]; then
echo "\$(date): Successfully determined interface and IP." >> "\$LOG_FILE"
break
fi
if [[ \$attempt -ge \$MAX_RETRIES ]]; then
echo "\$(date): Error: Could not determine the outside interface or external IP address after \$attempt attempts. Exiting." >> "\$LOG_FILE"
exit 1
fi
echo "\$(date): Attempt \$attempt failed. Retrying in \$delay seconds..." >> "\$LOG_FILE"
sleep "\$delay"
((attempt++))
delay=\$((delay * RETRY_MULTIPLIER))
done
echo "\$(date): Starting service: Protecting outside interface: \$OUTSIDE_INTERFACE with IP: \$EXTERNAL_IP" >> "\$LOG_FILE"
logger "rfwb-portscan: Protecting outside interface: \$OUTSIDE_INTERFACE with IP: \$EXTERNAL_IP"
EOF
# Make the pre-start script executable
chmod +x "$PRE_START_SCRIPT"
# Create a stop script to clean up nftables configuration
STOP_SCRIPT="/usr/local/bin/rfwb-portscan-stop.sh"
cat <<'EOF' >"$STOP_SCRIPT"
#!/bin/bash
echo "Flushing and removing dynamic block set, and resetting hosts.blocked file."
# Flush all rules in the input chain to remove references to the set
nft flush chain inet portscan input
# Delete the dynamic block set
nft delete set inet portscan dynamic_block
# Delete the table to remove all configurations
nft delete table inet portscan
# Reset the hosts.blocked file
truncate -s 0 /etc/nftables/hosts.blocked
echo "Dynamic block and configurations have been removed."
EOF
# Make the stop script executable
chmod +x "$STOP_SCRIPT"
# Create a custom handler script to manage start action
HANDLER_SCRIPT="/usr/local/bin/rfwb-portscan-handler.sh"
cat <<EOF >"$HANDLER_SCRIPT"
#!/bin/bash
# Source configuration file
CONFIG_FILE="/etc/rfwb-portscan.conf"
if [ -f "\$CONFIG_FILE" ]; then
source "\$CONFIG_FILE"
else
echo "Configuration file \$CONFIG_FILE not found. Using default settings."
fi
# Check if the script is called with a restart action
if [[ "\$1" == "restart" ]]; then
echo "Error: Restart action is not supported. Please use systemctl start and stop verbs."
exit 1
fi
OUTSIDE_INTERFACE="\$(nmcli -t -f DEVICE,CONNECTION device status | awk -F: -v suffix="-outside" '\$2 ~ suffix {print \$1}')"
EXTERNAL_IP="\$(ip -4 addr show "\$OUTSIDE_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n 1)"
if [[ -z "\$OUTSIDE_INTERFACE" || -z "\$EXTERNAL_IP" ]]; then
echo "Error: Could not determine the outside interface or external IP address. Exiting."
exit 1
fi
# Flush existing rules to prevent duplicates
if nft list tables | grep -q "inet portscan"; then
nft delete table inet portscan
fi
# Regenerate nftables configuration file
NFT_CONF_FILE="/etc/nftables/portscan.conf"
cat <<EOL >"\$NFT_CONF_FILE"
table inet portscan {
set dynamic_block {
type ipv4_addr
flags timeout
timeout \$BLOCK_TIMEOUT
}
chain input {
type filter hook input priority 0; policy accept;
# Allow established and related connections
ct state established,related accept
# Drop packets from dynamically blocked IPs
ip saddr @dynamic_block drop
# Detect SYN packets from untrusted sources on the outside interface
iifname "\$OUTSIDE_INTERFACE" tcp flags syn limit rate 10/minute log prefix "Port Scan Detected: " counter
# Use configured ports for detection
ip daddr \$EXTERNAL_IP tcp dport { \$MONITORED_PORTS } ct state new limit rate 3/minute log prefix "Port Scan Detected: " counter
}
}
EOL
# Apply the new nftables configuration
/usr/sbin/nft -f "\$NFT_CONF_FILE"
# Verify that the nftables configuration is applied
if ! nft list tables | grep -q "inet portscan"; then
echo "Error: The portscan table is not initialized. Exiting."
exit 1
fi
EOF
# Make the handler script executable
chmod +x "$HANDLER_SCRIPT"
# Create systemd service file
SYSTEMD_SERVICE_FILE="/etc/systemd/system/rfwb-portscan.service"
cat <<EOL >"$SYSTEMD_SERVICE_FILE"
[Unit]
Description=Port Scan Detection Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStartPre=$PRE_START_SCRIPT
ExecStart=$HANDLER_SCRIPT start
ExecStop=$STOP_SCRIPT
Type=oneshot
RemainAfterExit=yes
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOL
# Reload systemd and enable service
systemctl daemon-reload
systemctl enable rfwb-portscan.service
systemctl start rfwb-portscan.service
# Function to append unique IPs to the blocked file, ignoring networks from ignore_networks.conf
append_blocked_ip() {
local ip="$1"
for ignore_network in $IGNORE_NETWORKS; do
if ipcalc -c "$ip" "$ignore_network" >/dev/null 2>&1; then
echo "Ignoring IP $ip from scanning"
return
fi
done
if ! grep -q "^$ip$" "$BLOCKED_FILE"; then
echo "$ip" >>"$BLOCKED_FILE"
echo "Blocked IP $ip added to $BLOCKED_FILE"
# Ensure the table and set are correctly initialized before adding elements
if nft list tables | grep -q "inet portscan"; then
nft add element inet portscan dynamic_block { $ip }
else
echo "Error: The portscan table or dynamic_block set is not initialized."
fi
fi
}
# Monitor logs and update dynamic block
journalctl -kf | while read -r line; do
if [[ "$line" == *"Port Scan Detected:"* ]]; then
ip=$(echo "$line" | grep -oP '(?<=SRC=)\d+\.\d+\.\d+\.\d+')
if [[ -n "$ip" ]]; then
append_blocked_ip "$ip"
fi
fi
done &
echo "nftables port scan detection and blocking service has been installed and started for the outside interface."
echo "Blocked IPs are logged to $BLOCKED_FILE."
# Setup logging notifications
echo "Port scan events will be logged with the prefix 'Port Scan Detected:' in the system logs."
echo "To view these logs, you can use a command such as: journalctl -xe | grep 'Port Scan Detected'"
echo -e "${GREEN}Rocky Firewall Builder Port Scan Detection Complete...${TEXTRESET}"
sleep 4
}
# Function to install REQUIRED
install_required() {
clear
echo -e "${GREEN}Installing Required OS packages...${TEXTRESET}"
sleep 2
dnf -y config-manager --set-enabled crb
dnf -y install epel-release
dnf -y clean all
dnf -y update
dnf -y install ntsysv iptraf fail2ban tuned
echo -e "${GREEN}Required Package installation complete.${TEXTRESET}"
sleep 4
}
# Function to install ddns
install_ddclient() {
clear
echo -e "${GREEN}Installing ddns client (ddclient)...${TEXTRESET}"
sleep 2
dnf -y install ddclient
echo -e "${GREEN}ddns client (ddclient) installation complete.${TEXTRESET}"
sleep 4
}
# Function to install BIND
install_bind() {
clear
echo -e "${GREEN}Installing BIND...${TEXTRESET}"
sleep 2
dnf -y install bind
echo -e "${GREEN}BIND installation complete.${TEXTRESET}"
# Function to locate the inside interface and its sub-interfaces
find_inside_interfaces() {
# Find the main interface with a connection name ending in '-inside'
main_interface=$(nmcli device status | awk '/-inside/ {print $1}')
if [ -z "$main_interface" ]; then
echo -e "${RED}No interface with '-inside' profile found. Exiting...${TEXTRESET}"
exit 1
fi
# Find all sub-interfaces (e.g., VLANs) associated with the main interface
sub_interfaces=$(nmcli device status | awk -v main_intf="$main_interface" '$1 ~ main_intf "\\." {print $1}')
# Combine main interface and sub-interfaces into a single list
inside_interfaces="$main_interface $sub_interfaces"
echo -e "${GREEN}Inside interfaces found: $inside_interfaces${TEXTRESET}"
}
# Function to set up nftables rules for DNS on the inside interfaces
setup_nftables_for_dns() {
# Ensure the nftables service is enabled and started
sudo systemctl enable nftables
sudo systemctl start nftables
# Create a filter table if it doesn't exist
if ! sudo nft list tables | grep -q 'inet filter'; then
sudo nft add table inet filter
fi
# Create an input chain if it doesn't exist
if ! sudo nft list chain inet filter input &>/dev/null; then
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
fi
# Add rules to allow DNS on the inside interfaces
for iface in $inside_interfaces; do
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" udp dport 53 accept"; then
sudo nft add rule inet filter input iifname "$iface" udp dport 53 accept
echo -e "${GREEN}Rule added: Allow DNS (UDP) on interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow DNS (UDP) on interface $iface${TEXTRESET}"
fi
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" tcp dport 53 accept"; then
sudo nft add rule inet filter input iifname "$iface" tcp dport 53 accept
echo -e "${GREEN}Rule added: Allow DNS (TCP) on interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow DNS (TCP) on interface $iface${TEXTRESET}"
fi
done
# Check and handle rfwb-portscan service
rfwb_status=$(systemctl is-active rfwb-portscan)
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Stopping rfwb-portscan service before saving nftables configuration...${TEXTRESET}"
systemctl stop rfwb-portscan
fi
# Save the current nftables configuration
sudo nft list ruleset >/etc/sysconfig/nftables.conf
# Restart the nftables service to apply changes
echo -e "${YELLOW}Restarting nftables service to apply changes...${TEXTRESET}"
sudo systemctl restart nftables
# Restart rfwb-portscan service if it was active
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Restarting rfwb-portscan service...${TEXTRESET}"
systemctl start rfwb-portscan
fi
# Show the added rules in the input chain
echo -e "${YELLOW}Current rules in the input chain:${TEXTRESET}"
sudo nft list chain inet filter input
}
# Execute functions
find_inside_interfaces
setup_nftables_for_dns
# Continue with the rest of the script
echo -e "${GREEN}BIND Install Complete...${TEXTRESET}"
sleep 4
}
# Function to install ISC KEA
install_isc_kea() {
clear
echo -e "${GREEN}Installing ISC KEA...${TEXTRESET}"
sleep 2
dnf -y install epel-release
curl -1sLf 'https://dl.cloudsmith.io/public/isc/kea-2-6/cfg/setup/bash.rpm.sh' | sudo bash
sudo dnf -y update
dnf -y install isc-kea
echo -e "${GREEN}ISC KEA installation complete.${TEXTRESET}"
# Function to locate the inside interfaces
find_inside_interfaces() {
# Find the main interface with a connection name ending in '-inside'
main_interface=$(nmcli device status | awk '/-inside/ {print $1}')
if [ -z "$main_interface" ]; then
echo -e "${RED}No interface with '-inside' profile found. Exiting...${TEXTRESET}"
exit 1
fi
# Find all sub-interfaces (e.g., VLANs) associated with the main interface
sub_interfaces=$(nmcli device status | awk -v main_intf="$main_interface" '$1 ~ main_intf "\\." {print $1}')
# Combine main interface and sub-interfaces into a single list
inside_interfaces="$main_interface $sub_interfaces"
echo -e "${GREEN}Inside interfaces found: $inside_interfaces${TEXTRESET}"
}
# Function to set up nftables rules for DHCP on the inside interfaces
setup_nftables_for_dhcp() {
# Ensure the nftables service is enabled and started
sudo systemctl enable nftables
sudo systemctl start nftables
# Create a filter table if it doesn't exist
if ! sudo nft list tables | grep -q 'inet filter'; then
sudo nft add table inet filter
fi
# Create an input chain if it doesn't exist
if ! sudo nft list chain inet filter input &>/dev/null; then
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
fi
# Add rules to allow DHCP on the inside interfaces
for iface in $inside_interfaces; do
# Allow DHCP for IPv4 (UDP port 67)
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" udp dport 67 accept"; then
sudo nft add rule inet filter input iifname "$iface" udp dport 67 accept
echo -e "${GREEN}Rule added: Allow DHCP (IPv4) on interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow DHCP (IPv4) on interface $iface${TEXTRESET}"
fi
# Allow DHCP for IPv6 (UDP port 547)
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" udp dport 547 accept"; then
sudo nft add rule inet filter input iifname "$iface" udp dport 547 accept
echo -e "${GREEN}Rule added: Allow DHCP (IPv6) on interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow DHCP (IPv6) on interface $iface${TEXTRESET}"
fi
done
# Check and handle rfwb-portscan service
rfwb_status=$(systemctl is-active rfwb-portscan)
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Stopping rfwb-portscan service before saving nftables configuration...${TEXTRESET}"
systemctl stop rfwb-portscan
fi
# Save the current nftables configuration
sudo nft list ruleset >/etc/sysconfig/nftables.conf
# Restart the nftables service to apply changes
echo -e "${YELLOW}Restarting nftables service to apply changes...${TEXTRESET}"
sudo systemctl restart nftables
# Restart rfwb-portscan service if it was active
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Restarting rfwb-portscan service...${TEXTRESET}"
systemctl start rfwb-portscan
fi
# Show the added rules in the input chain
echo -e "${YELLOW}Current rules in the input chain:${TEXTRESET}"
sudo nft list chain inet filter input
}
# Execute functions
find_inside_interfaces
setup_nftables_for_dhcp
# Continue with the rest of the script
echo -e "${GREEN}ISC-KEA Install Complete...${TEXTRESET}"
sleep 4
}
# Function to install COCKPIT
install_cockpit() {
clear
echo -e "${GREEN}Installing Cockpit...${TEXTRESET}"
sleep 2
dnf -y install cockpit cockpit-storaged cockpit-files tuned
echo -e "${GREEN}Cockpit installation complete.${TEXTRESET}"
# Function to locate the inside interfaces
find_inside_interfaces() {
# Find all active interfaces with a name ending in '-inside'
inside_interfaces=$(nmcli -t -f NAME,DEVICE connection show --active | awk -F: '$1 ~ /-inside$/ {print $2}')
if [ -z "$inside_interfaces" ]; then
echo -e "${RED}No interface with '-inside' profile found. Exiting...${TEXTRESET}"
exit 1
fi
echo -e "${GREEN}Inside interfaces found: $inside_interfaces${TEXTRESET}"
}
# Function to set up nftables rules for Cockpit on the inside interfaces
setup_nftables_for_cockpit() {
# Ensure the nftables service is enabled and started
sudo systemctl enable nftables
sudo systemctl start nftables
# Create a filter table if it doesn't exist
if ! sudo nft list tables | grep -q 'inet filter'; then
sudo nft add table inet filter
fi
# Create an input chain if it doesn't exist
if ! sudo nft list chain inet filter input &>/dev/null; then
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
fi
# Add rules to allow Cockpit on the inside interfaces using port 9090
for iface in $inside_interfaces; do
if ! sudo nft list chain inet filter input | grep -q "iifname \"$iface\" tcp dport 9090 accept"; then
sudo nft add rule inet filter input iifname "$iface" tcp dport 9090 accept
echo -e "${GREEN}Rule added: Allow Cockpit on port 9090 for interface $iface${TEXTRESET}"
else
echo -e "${YELLOW}Rule already exists: Allow Cockpit on port 9090 for interface $iface${TEXTRESET}"
fi
done
# Check and handle rfwb-portscan service
rfwb_status=$(systemctl is-active rfwb-portscan)
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Stopping rfwb-portscan service before saving nftables configuration...${TEXTRESET}"
systemctl stop rfwb-portscan
fi
# Save the current nftables configuration
sudo nft list ruleset >/etc/sysconfig/nftables.conf
# Restart the nftables service to apply changes
echo -e "${YELLOW}Restarting nftables service to apply changes...${TEXTRESET}"
sudo systemctl restart nftables
# Restart rfwb-portscan service if it was active
if [ "$rfwb_status" == "active" ]; then
echo -e "${YELLOW}Restarting rfwb-portscan service...${TEXTRESET}"
systemctl start rfwb-portscan
fi
# Show the added rules in the input chain
echo -e "${YELLOW}Current rules in the input chain:${TEXTRESET}"
sudo nft list chain inet filter input
}
# Execute functions
find_inside_interfaces
setup_nftables_for_cockpit
# Enable and start cockpit.socket
systemctl enable --now cockpit.socket
systemctl start cockpit.socket
# Continue with the rest of the script