#!/bin/bash # Fail2ban for NextcloudPi # # Copyleft 2017 by Ignacio Nunez Hernanz # GPL licensed (see end of file) * Use at your own risk! # # More at: https://ownyourbits.com/2017/02/24/nextcloudpi-fail2ban-installer/ # # time to ban an IP that exceeded attempts # cooldown time for incorrect passwords # bad attempts before banning an IP # Option to activate email notifications # email to send notifications to install() { apt-get update apt-get install --no-install-recommends -y python3-systemd apt-get install --no-install-recommends -y fail2ban whois update-rc.d fail2ban disable rm -f /etc/fail2ban/jail.d/defaults-debian.conf # tweak fail2ban email local F=/etc/fail2ban/action.d/sendmail-common.conf sed -i 's|Fail2Ban|NextCloudPi|' /etc/fail2ban/action.d/sendmail-whois-lines.conf grep -q actionstart_ "$F" || sed -i 's|actionstart|actionstart_|' "$F" grep -q actionstop_ "$F" || sed -i 's|actionstop|actionstop_|' "$F" } configure() { [[ $ACTIVE != "yes" ]] && { service fail2ban stop update-rc.d fail2ban disable echo "fail2ban disabled" return } local NCLOG="/var/www/nextcloud/data/nextcloud.log" local NCLOG1="$(ncc config:system:get logfile)" [[ "$NCLOG1" != "" ]] && NCLOG="$NCLOG1" local BASEDIR=$( dirname "$NCLOG" ) [ -d "$BASEDIR" ] || { echo -e "directory $BASEDIR not found"; return 1; } sudo -u www-data touch "$NCLOG" || { echo -e "ERROR: user www-data does not have write permissions on $NCLOG"; return 1; } ncc config:system:set loglevel --value=2 ncc config:system:set log_type --value=file # Filters cat > /etc/fail2ban/filter.d/nextcloud.conf <<'EOF' [INCLUDES] before = common.conf [Definition] _groupsre = (?:(?:,?\s*"\w+":(?:"[^"]+"|\w+))*) failregex = ^\{%(_groupsre)s,?\s*"remoteAddr":""%(_groupsre)s,?\s*"message":"Login failed: datepattern = ,?\s*"time"\s*:\s*"%%Y-%%m-%%d[T ]%%H:%%M:%%S(%%z)?" ignoreregex = EOF cat > /etc/fail2ban/filter.d/ufwban.conf <<'EOF' [INCLUDES] before = common.conf [Definition] failregex = UFW BLOCK.* SRC= ignoreregex = EOF mkdir -p /etc/systemd/system/fail2ban.service.d cat > /etc/systemd/system/fail2ban.service.d/touch-ufw-log.conf <<'EOF' [Service] ExecStartPre=/bin/touch /var/log/ufw.log EOF [[ "$MAILALERTS" == "yes" ]] && local ACTION=action_mwl || local ACTION=action_ # Jails cat > /etc/fail2ban/jail.conf <