1
1
#! /bin/bash
2
+ set -euo pipefail
3
+ IFS=$' \n\t '
2
4
3
5
# Constants
4
6
readonly NEW_SSH_PORT=2298
@@ -11,6 +13,12 @@ log() {
11
13
echo " $( date ' +%Y-%m-%d %H:%M:%S' ) : $message " | tee -a " $LOG_FILE "
12
14
}
13
15
16
+ # Ensure log file is writable
17
+ if [[ ! -w " $( dirname " $LOG_FILE " ) " ]]; then
18
+ echo " Error: Log directory is not writable."
19
+ exit 1
20
+ fi
21
+
14
22
# Check if the script is run as root
15
23
if [[ $EUID -ne 0 ]]; then
16
24
log " Error: This script must be run as root."
@@ -33,24 +41,15 @@ if [[ ! -f "$ALLOWED_CONNECTIONS_FILE" || ! -s "$ALLOWED_CONNECTIONS_FILE" ]]; t
33
41
exit 1
34
42
fi
35
43
36
- # Function to check for the availability of firewall tools
37
- get_firewall_tool () {
38
- if command -v ufw & > /dev/null; then
39
- echo " ufw"
40
- elif command -v iptables & > /dev/null; then
41
- echo " iptables"
42
- else
43
- echo " none"
44
- fi
45
- }
44
+ # Get firewall tool
45
+ FIREWALL_TOOL=$( command -v ufw || command -v iptables || echo " none" )
46
46
47
47
# Function to add firewall rules
48
48
add_firewall_rule () {
49
49
local local_ip=" $1 "
50
50
local remote_ip=" $2 "
51
- local tool=" $3 "
52
51
53
- case " $tool " in
52
+ case " $FIREWALL_TOOL " in
54
53
" ufw" )
55
54
ufw deny from " $remote_ip " to " $local_ip "
56
55
log " Firewall rule added using ufw to block $remote_ip to $local_ip "
@@ -78,14 +77,9 @@ check_connections() {
78
77
local current_ssh_connection
79
78
current_ssh_connection=$( who | awk -v user=" $CURRENT_USER " ' $1 == user {print $NF}' | sed ' s/[()]//g' )
80
79
81
- local firewall_tool
82
- firewall_tool=$( get_firewall_tool)
83
-
84
80
log " Starting connection checks..."
85
81
86
82
while read -r line; do
87
- local local_ip remote_ip pid_program program_name local_port
88
-
89
83
if [[ " $line " =~ ^tcp.* ESTABLISHED$ ]]; then
90
84
local_ip=$( echo " $line " | awk ' {print $4}' )
91
85
remote_ip=$( echo " $line " | awk ' {print $5}' )
@@ -99,30 +93,34 @@ check_connections() {
99
93
fi
100
94
101
95
# Check if the connection is allowed based on the allowed_ips.txt file or if the local port matches NEW_SSH_PORT
102
- if ! grep -qE " ^$remote_ip \s+ $local_port $" " $ALLOWED_CONNECTIONS_FILE " && [[ $local_port -ne $NEW_SSH_PORT ]]; then
96
+ if ! grep -qE " ^$remote_ip \s* $local_port $" " $ALLOWED_CONNECTIONS_FILE " && [[ " $local_port " -ne " $NEW_SSH_PORT " ]]; then
103
97
log " Unauthorized connection: $local_ip <-> $remote_ip (Process: $program_name , PID: $pid_program )"
104
98
105
- # Prompt the user to kill the unauthorized process
106
- read -r -p " Do you want to kill this process? (yes/no) " answer_kill
107
- if [[ " $answer_kill " =~ ^(yes| y)$ ]]; then
108
- log " User chose to kill the process with PID $pid_program "
109
- kill_process " $pid_program " " 15"
110
- sleep 5
111
- if ps -p " $pid_program " > /dev/null; then
112
- log " Process with PID $pid_program is still running after SIGTERM, sending SIGKILL"
113
- kill_process " $pid_program " " 9"
99
+ if $INTERACTIVE ; then
100
+ # Prompt the user to kill the unauthorized process
101
+ read -r -p " Do you want to kill this process? (yes/no) " answer_kill
102
+ if [[ " $answer_kill " =~ ^(yes| y)$ ]]; then
103
+ log " User chose to kill the process with PID $pid_program "
104
+ kill_process " $pid_program " " 15"
105
+ sleep 5
106
+ if ps -p " $pid_program " > /dev/null; then
107
+ log " Process with PID $pid_program is still running after SIGTERM, sending SIGKILL"
108
+ kill_process " $pid_program " " 9"
109
+ fi
110
+ else
111
+ log " User chose not to kill the process with PID $pid_program "
114
112
fi
115
- else
116
- log " User chose not to kill the process with PID $pid_program "
117
- fi
118
113
119
- # Prompt the user to add a firewall rule to block the unauthorized connection
120
- read -r -p " Do you want to block this connection using firewall? (yes/no) " answer_firewall
121
- if [[ " $answer_firewall " =~ ^(yes| y)$ ]]; then
122
- log " User chose to block the connection from $remote_ip to $local_ip using $firewall_tool "
123
- add_firewall_rule " $local_ip " " $remote_ip " " $firewall_tool "
114
+ # Prompt the user to add a firewall rule to block the unauthorized connection
115
+ read -r -p " Do you want to block this connection using firewall? (yes/no) " answer_firewall
116
+ if [[ " $answer_firewall " =~ ^(yes| y)$ ]]; then
117
+ log " User chose to block the connection from $remote_ip to $local_ip using $FIREWALL_TOOL "
118
+ add_firewall_rule " $local_ip " " $remote_ip "
119
+ else
120
+ log " User chose not to block the connection from $remote_ip to $local_ip "
121
+ fi
124
122
else
125
- log " User chose not to block the connection from $remote_ip to $local_ip "
123
+ log " Running in non-interactive mode. Not prompting user for actions. "
126
124
fi
127
125
fi
128
126
fi
@@ -131,5 +129,10 @@ check_connections() {
131
129
log " Connection checks completed."
132
130
}
133
131
134
- # Running the main function
135
- check_connections
132
+ # Main function to encapsulate script logic
133
+ main () {
134
+ check_connections
135
+ }
136
+
137
+ # Execute main function
138
+ main " $@ "
0 commit comments