Skip to content

Instantly share code, notes, and snippets.

@Johannestegner
Last active August 9, 2020 12:41
Show Gist options
  • Select an option

  • Save Johannestegner/130ea6c6cd71bd3be26730aa836c5f5a to your computer and use it in GitHub Desktop.

Select an option

Save Johannestegner/130ea6c6cd71bd3be26730aa836c5f5a to your computer and use it in GitHub Desktop.
UFW Auto-updater

UFW Auto Updater

Prerequisites

To use this script, you need to have a PGP key to sign the keys with.
The script expects the gpg2 binary to exist in /usr/bin/ and the UFW binary to exist in /usr/sbin, if not, change the code to fit your case.

Installation!

SSH to the server which is intended to automatically update the UFW ruleset.
Import the PGP key and make it trusted:

export PGP_KEY="xxx--your-pgp-fingerprint--xxx"
gpg2 --receive-keys --keyserver ha.pool.sks-keyservers.net ${PGP_KEY}
gpg2 --edit-key ${PGP_KEY}

Download the script and place it in your path (or move it to a dir which is in path already):

wget https://gist.githubusercontent.com/Johannestegner/130ea6c6cd71bd3be26730aa836c5f5a/raw/ufw-cron.sh -O /usr/local/bin/ufw-cron
chmod +x /usr/local/bin/ufw-cron

Change the GIST_URI variable in the script to point to your gist with rules.
Set up a cronjob which runs as often as you wish it to poll:

crontab -e

*/5 * * * * /usr/local/bin/ufw-cron TYPE > /var/log/ufw-cron.log 2>&1

The script takes one argument, and that is the type of ruleset to use (file name in your gist).

Create a new UFW ruleset

Create a new file for your ruleset. Each line should be a UFW rule (exclude the ufw command):

web.txt

allow ssh
allow http
allow https

Add it to your gist.

Fetch the file from the gist (to be sure it have the correct encoding and line-endings etc that the server will have) and create a signature:

wget https://gist.github.com/me/gist-id/raw/web.txt
gpg2 --detach-sign -a web.txt
clip < web.txt.asc

Add the new asc file to the gist.

MIT License

Copyright (c) 2020 Jitesoft

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.

#! /bin/bash
TYPE="${1}"
GIST_URI="https://gist.githubusercontent.com/your-user/your-gist-id"
FILE_TYPE="txt"
URI="${GIST_URI}/raw/${TYPE}"
DIR="/etc/ufw-cron.d"
mkdir -p "${DIR}"
touch "${DIR}/latest.${FILE_TYPE}"
wget $URI.${FILE_TYPE} -O "${DIR}/tmp.${FILE_TYPE}"
wget $URI.${FILE_TYPE}.asc -O "${DIR}/tmp.${FILE_TYPE}.asc"
/usr/bin/gpg2 --verify "${DIR}/tmp.${FILE_TYPE}.asc" || exit 1
rm "${DIR}/tmp.${FILE_TYPE}.asc"
STATUS="$(cmp --silent "${DIR}/tmp.${FILE_TYPE}" "${DIR}/latest.${FILE_TYPE}"; echo $?)"
if [ $STATUS -ne 0 ]; then
echo "Copy script."
cp "${DIR}/tmp.${FILE_TYPE}" "${DIR}/latest.${FILE_TYPE}"
echo "Reset firewall"
/usr/sbin/ufw --force reset
echo "Update firewall"
while read p; do
/usr/sbin/ufw $p
done < "${DIR}/latest.${FILE_TYPE}"
echo "enable firewall"
/usr/sbin/ufw --force enable
else
echo "The files did not differ, which means that the latest rules are applied."
fi
echo "Remove temp file."
rm "${DIR}/tmp.${FILE_TYPE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment