-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfail2ban.sh
More file actions
executable file
·55 lines (45 loc) · 1.47 KB
/
Copy pathfail2ban.sh
File metadata and controls
executable file
·55 lines (45 loc) · 1.47 KB
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
#!/bin/bash
# SniffCat fail2ban Integration
# Reports banned IPs from fail2ban to the SniffCat API
#
# https://github.com/Rexikon/SniffCat-Fail2Ban
# SPDX-License-Identifier: GPL-3.0-or-later
set -euo pipefail
# --- Paths ---
INSTALL_DIR="/opt/sniffcat"
CONFIG_FILE="${INSTALL_DIR}/sniffcat.conf"
LOG_FILE="/var/log/sniffcat.log"
API_URL="https://api.sniffcat.com/api/v1/report"
# --- Arguments from fail2ban ---
IP="${1:-}"
JAIL="${2:-}"
FAILURES="${3:-}"
# --- Functions ---
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [SniffCat] $*" >> "$LOG_FILE" 2>/dev/null || true
}
die() {
log "ERROR: $*"
exit 1
}
# --- Validation ---
[[ -z "$IP" ]] && die "No IP address provided"
# --- Load config ---
[[ -f "$CONFIG_FILE" ]] || die "Config file not found: $CONFIG_FILE"
# shellcheck source=/dev/null
source "$CONFIG_FILE"
[[ -z "${SNIFFCAT_TOKEN:-}" ]] && die "SNIFFCAT_TOKEN not set in $CONFIG_FILE"
# --- Report to SniffCat API ---
RESPONSE=$(curl -s -w "\n%{http_code}" --max-time 10 -X POST "$API_URL" \
-H "Content-Type: application/json" \
-H "X-Secret-Token: ${SNIFFCAT_TOKEN}" \
--data "{
\"ip\": \"${IP}\",
\"categories\": [17],
\"comment\": \"Banned by fail2ban | jail: ${JAIL}, failures: ${FAILURES}\"
}" 2>&1) || true
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | head -n -1)
if [[ ! "$HTTP_CODE" =~ ^2[0-9]{2}$ ]]; then
log "ERROR: IP=${IP} jail=${JAIL} failures=${FAILURES} — HTTP ${HTTP_CODE}: ${BODY}"
fi