From 4e450a270db046cba1a2d38b9c492ac96628ec90 Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 19 Jun 2024 23:11:09 +0200 Subject: [PATCH] Init --- .gitignore | 1 + DEBIAN/control | 6 ++++++ LICENSE | 7 ++++++ README.md | 13 +++++++++++ etc/sshutter.conf | 3 +++ etc/systemd/system/sshutterd.service | 8 +++++++ usr/sbin/sshutterd | 32 ++++++++++++++++++++++++++++ 7 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 DEBIAN/control create mode 100644 LICENSE create mode 100644 README.md create mode 100644 etc/sshutter.conf create mode 100644 etc/systemd/system/sshutterd.service create mode 100755 usr/sbin/sshutterd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c00df13 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.deb diff --git a/DEBIAN/control b/DEBIAN/control new file mode 100644 index 0000000..a2cb2ad --- /dev/null +++ b/DEBIAN/control @@ -0,0 +1,6 @@ +Package: sshutter +Version: 0.1 +Maintainer: Martin "maride" Dessauer +Architecture: all +Description: ping-conditional SSH firewall +Depends: iputils-ping, nftables diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..36793f1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2024 Martin "maride" Dessauer + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..99fe6d5 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# sshutter + +Apply firewall rules depending on ping results + +## Motivation + +SSH is, by nature, a very powerful and mighty tool which can be found on almost any UNIX system - and some Windows hosts, too. However, incidents like [the XZ backdoor](https://en.wikipedia.org/wiki/XZ_Utils_backdoor) have shown that SSH is not invulnerable, and a critical vulnerability would be as desastrous as [EternalBlue](https://en.wikipedia.org/wiki/EternalBlue) on Windows hosts. + +This situation asks for dynamic solutions. And **sshutter** is such a dynamic solution. It activates or deactivates firewall rules for dropping requests to SSH as long as a certain IP address or network is reachable. + +## Building/Packing + +To create the Debian/Ubuntu package for sshutter, run `dpkg-deb --root-owner-group --build . sshutter-v0.1.deb` diff --git a/etc/sshutter.conf b/etc/sshutter.conf new file mode 100644 index 0000000..5ee1c4d --- /dev/null +++ b/etc/sshutter.conf @@ -0,0 +1,3 @@ +TARGET=10.0.0.1 +WHITELIST=10.0.0.0/16 +PORT=22 diff --git a/etc/systemd/system/sshutterd.service b/etc/systemd/system/sshutterd.service new file mode 100644 index 0000000..dbf92cd --- /dev/null +++ b/etc/systemd/system/sshutterd.service @@ -0,0 +1,8 @@ +[Unit] +Description=sshutter - ping-conditional SSH firewall + +[Service] +ExecStart=/usr/sbin/sshutterd + +[Install] +WantedBy=multi-user.target diff --git a/usr/sbin/sshutterd b/usr/sbin/sshutterd new file mode 100755 index 0000000..ceab9a7 --- /dev/null +++ b/usr/sbin/sshutterd @@ -0,0 +1,32 @@ +#!/bin/bash + +if [ -f "/etc/sshutter.conf" ]; then + . /etc/sshutter.conf +fi + +function log { + echo "$(date +'%Y-%m-%d %H:%M:%S') $@" +} + +if [ "$UID" -ne 0 ]; then + echo "Error: must be root." 1>&2 + exit 1 +fi + +while [ true ]; do + ping -c 3 "$TARGET" 1>/dev/null + if [ "$?" -eq 0 ]; then + # Ping successful, apply shutter + log "[sshutter] Blocking port 22 for IPs outside $WHITELIST" + nft add table inet filter + nft add chain inet filter sshutterv4 \{ type filter hook input priority filter \; policy accept \; \} + nft add rule inet filter sshutterv4 tcp dport "$PORT" ip saddr "$WHITELIST" accept + nft add rule inet filter sshutterv4 tcp dport "$PORT" drop + else + # Ping failed, lift shutter + log "[sshutter] Releasing port block" + nft destroy chain inet filter sshutterv4 + fi + + sleep 60 +done