Skip to content

Commit

Permalink
Merge pull request #12 from balena-os/kyle/fail2ban
Browse files Browse the repository at this point in the history
Run fail2ban service sidecar with sshd jail
  • Loading branch information
flowzone-app[bot] authored Nov 22, 2023
2 parents 611163b + 872e6de commit 89df976
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@ services:
- home:/home
- docker:/var/lib/docker
- ssh:/etc/ssh
- shared:/shared

fail2ban:
build: fail2ban
network_mode: host
cap_add:
- NET_ADMIN
- NET_RAW
volumes:
- fail2ban:/data
- shared:/shared:ro
environment:
F2B_LOG_TARGET: STDOUT
F2B_LOG_LEVEL: INFO
F2B_DB_PURGE_AGE: 1d

volumes:
home: {}
docker: {}
ssh: {}
shared: {}
fail2ban: {}
3 changes: 3 additions & 0 deletions fail2ban/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM crazymax/fail2ban:1.0.2

COPY jail.d/ /data/jail.d/
9 changes: 9 additions & 0 deletions fail2ban/jail.d/sshd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[sshd]
enabled = true
chain = INPUT
port = ssh
filter = sshd[mode=aggressive]
logpath = /shared/logs/sshd.log
# datepattern = ^+%b %d %H:%M:%S
maxretry = 5
bantime = 1h
12 changes: 9 additions & 3 deletions yocto-build-env/s6-overlay/s6-rc.d/sshd/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ set -euo pipefail
[[ ${VERBOSE:-,,} =~ true|yes|on|1 ]] && set -x

mkdir -p /run/sshd
mkdir -p /shared/logs

ssh-keygen -A

# run sshd in the foreground
/usr/sbin/sshd -De \
# run sshd in the foreground and format the logs such
# that fail2ban can parse them
exec /usr/sbin/sshd -De \
-o "LogLevel=${SSHD_LOG_LEVEL:-INFO}" \
-o PermitRootLogin=no \
-o PasswordAuthentication=no \
-o PubkeyAuthentication=yes \
-o UsePAM=yes \
-o AcceptEnv="LANG LC_*" \
-o PrintMotd=no \
-o Banner=none
-o Banner=none \
2>&1 |
while IFS= read -r line; do
echo "$(date '+%b %d %H:%M:%S') $HOSTNAME sshd[$$]: $line"
done | tee /shared/logs/sshd.log

0 comments on commit 89df976

Please sign in to comment.