Skip to content

Commit 3c83fa6

Browse files
authored
Update conchecker.sh
Added strict mode to improve script safety. Ensured log file directory is writable. Simplified firewall tool detection. Improved grep for matching IPs and ports. Encapsulated main logic in a main function. Added a flag to control interactivity, making the script usable in automated environments.
1 parent c6912c1 commit 3c83fa6

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

conchecker.sh

+41-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
24

35
# Constants
46
readonly NEW_SSH_PORT=2298
@@ -11,6 +13,12 @@ log() {
1113
echo "$(date '+%Y-%m-%d %H:%M:%S'): $message" | tee -a "$LOG_FILE"
1214
}
1315

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+
1422
# Check if the script is run as root
1523
if [[ $EUID -ne 0 ]]; then
1624
log "Error: This script must be run as root."
@@ -33,24 +41,15 @@ if [[ ! -f "$ALLOWED_CONNECTIONS_FILE" || ! -s "$ALLOWED_CONNECTIONS_FILE" ]]; t
3341
exit 1
3442
fi
3543

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")
4646

4747
# Function to add firewall rules
4848
add_firewall_rule() {
4949
local local_ip="$1"
5050
local remote_ip="$2"
51-
local tool="$3"
5251

53-
case "$tool" in
52+
case "$FIREWALL_TOOL" in
5453
"ufw")
5554
ufw deny from "$remote_ip" to "$local_ip"
5655
log "Firewall rule added using ufw to block $remote_ip to $local_ip"
@@ -78,14 +77,9 @@ check_connections() {
7877
local current_ssh_connection
7978
current_ssh_connection=$(who | awk -v user="$CURRENT_USER" '$1 == user {print $NF}' | sed 's/[()]//g')
8079

81-
local firewall_tool
82-
firewall_tool=$(get_firewall_tool)
83-
8480
log "Starting connection checks..."
8581

8682
while read -r line; do
87-
local local_ip remote_ip pid_program program_name local_port
88-
8983
if [[ "$line" =~ ^tcp.*ESTABLISHED$ ]]; then
9084
local_ip=$(echo "$line" | awk '{print $4}')
9185
remote_ip=$(echo "$line" | awk '{print $5}')
@@ -99,30 +93,34 @@ check_connections() {
9993
fi
10094

10195
# 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
10397
log "Unauthorized connection: $local_ip <-> $remote_ip (Process: $program_name, PID: $pid_program)"
10498

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"
114112
fi
115-
else
116-
log "User chose not to kill the process with PID $pid_program"
117-
fi
118113

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
124122
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."
126124
fi
127125
fi
128126
fi
@@ -131,5 +129,10 @@ check_connections() {
131129
log "Connection checks completed."
132130
}
133131

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

Comments
 (0)