Skip to content

Commit 31a25c1

Browse files
authored
Create README.md
1 parent 2ce1f6e commit 31a25c1

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

projects/secure-vps/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Fail2Ban Alerts to Discord
2+
A simple guide to setup fail2ban action for sending an alert message to discord channel.
3+
4+
1. Create a Discord Webhook
5+
- Go to your Discord Server → Select a channel.
6+
- Click Edit Channel → Integrations → Webhooks → New Webhook.
7+
- Name it (ex: Fail2Ban Alerts) and copy the Webhook URL.
8+
- Example: https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz
9+
10+
2. Create the Discord Notification Script
11+
- install `jq`:
12+
```
13+
sudo apt install jq
14+
```
15+
- Create the file /usr/local/bin/fail2ban-discord.sh:
16+
```
17+
#!/bin/bash
18+
19+
JAIL="$1"
20+
IP="$2"
21+
MATCHES="$3"
22+
23+
WEBHOOK_URL="YOUR-WEBHOOK-URL"
24+
25+
HOSTNAME=$(hostname)
26+
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
27+
28+
MESSAGE="** Fail2Ban Alert**
29+
**Server:** ${HOSTNAME}
30+
**Time:** ${TIMESTAMP}
31+
**Jail:** \`${JAIL}\`
32+
**Banned IP:** \`${IP}\`
33+
**Reason:** ${MATCHES}"
34+
35+
# Send to Discord
36+
curl -s -H "Content-Type: application/json" \
37+
-X POST \
38+
-d "$(jq -nc --arg content "$MESSAGE" '{content: $content}')" \
39+
"$WEBHOOK_URL"
40+
```
41+
- Make it executable:
42+
```
43+
sudo chmod +x /usr/local/bin/fail2ban-discord.sh
44+
```
45+
3. Create a Custom Fail2Ban Action
46+
- Create /etc/fail2ban/action.d/discord-ban.conf:
47+
```
48+
[Definition]
49+
actionstart =
50+
actionstop =
51+
actioncheck =
52+
actionban = /usr/local/bin/fail2ban-discord.sh "<name>" "<ip>" "<matches>"
53+
actionunban =
54+
```
55+
56+
4. Apply It in Jail Config
57+
- Edit /etc/fail2ban/jail.local or create if not existing:
58+
```
59+
[sshd]
60+
enabled = true
61+
port = ssh
62+
logpath = /var/log/auth.log
63+
maxretry = 3
64+
findtime = 600
65+
bantime = 3600
66+
action = discord-ban
67+
```
68+
5. Restart fail2ban
69+
```
70+
sudo systemctl restart fail2ban
71+
```
72+
73+
6. Test
74+
To test, you can intentionally trigger a failed login from a different IP or use:
75+
```
76+
sudo fail2ban-client set sshd banip 1.2.3.4
77+
```
78+
You should receive an alert in your Discord channel.

0 commit comments

Comments
 (0)