mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-11 13:47:05 +02:00
Fix generation of geoIP filtering action
This commit is contained in:
@@ -233,28 +233,9 @@ action_mwlg = %(action_)s
|
|||||||
|
|
||||||
// writeFail2banAction creates or updates the action file with the AlertCountries.
|
// writeFail2banAction creates or updates the action file with the AlertCountries.
|
||||||
func writeFail2banAction(alertCountries []string) error {
|
func writeFail2banAction(alertCountries []string) error {
|
||||||
// If "all" is included in AlertCountries, allow all countries
|
|
||||||
if len(alertCountries) == 1 && strings.ToLower(alertCountries[0]) == "all" {
|
|
||||||
alertCountries = []string{"CH DE IT FR UK US"} // Match everything
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert country list into properly formatted Python set syntax
|
// Join the alertCountries into a comma-separated string
|
||||||
//countries := strings.Join(alertCountries, "','")
|
countriesFormatted := strings.Join(alertCountries, ",")
|
||||||
//countriesFormatted := fmt.Sprintf("'%s'", countries)
|
|
||||||
|
|
||||||
// Convert country list into properly formatted Bash syntax
|
|
||||||
countries := strings.Join(alertCountries, "' '")
|
|
||||||
countriesFormatted := fmt.Sprintf("' %s '", countries)
|
|
||||||
|
|
||||||
//actionConfig := `[Definition]
|
|
||||||
//actionstart =
|
|
||||||
//actionban = python3 -c '
|
|
||||||
//import sys
|
|
||||||
//from geoip import geolite2
|
|
||||||
//country = geolite2.lookup(sys.argv[1]).country
|
|
||||||
//if country in {{ALERT_COUNTRIES}}:
|
|
||||||
// sys.exit(0) # Send alert
|
|
||||||
//sys.exit(1) # Do not send alert'
|
|
||||||
|
|
||||||
// Define the Fail2Ban action file content
|
// Define the Fail2Ban action file content
|
||||||
actionConfig := fmt.Sprintf(`[INCLUDES]
|
actionConfig := fmt.Sprintf(`[INCLUDES]
|
||||||
@@ -265,47 +246,49 @@ before = sendmail-common.conf
|
|||||||
|
|
||||||
[Definition]
|
[Definition]
|
||||||
|
|
||||||
# bypass ban/unban for restored tickets
|
# Bypass ban/unban for restored tickets
|
||||||
norestored = 1
|
norestored = 1
|
||||||
|
|
||||||
# Option: actionban
|
# Option: actionban
|
||||||
# Notes.: command executed when banning an IP. Take care that the
|
# This executes the Python script with <ip> and the list of allowed countries.
|
||||||
# command is executed with Fail2Ban user rights.
|
# If the country matches the allowed list, it sends the email.
|
||||||
|
actionban = /etc/fail2ban/scripts/check_geoip.py <ip> "%s" && (
|
||||||
actionban = bash -c '
|
printf %%%%b "Subject: [Fail2Ban] <name>: banned <ip> from <fq-hostname>
|
||||||
COUNTRY="<geoip_cc>"
|
Date: `+"`LC_ALL=C date +\"%%%%a, %%%%d %%%%h %%%%Y %%%%T %%%%z\"`"+`
|
||||||
if [[ " %s " =~ " $COUNTRY " ]]; then
|
From: <sendername> <<sender>>
|
||||||
( printf %%%%b "Subject: [Fail2Ban] <name>: banned <ip> from <fq-hostname>\n"
|
To: <dest>\n
|
||||||
printf "Date: `+"`LC_ALL=C date +\"%%%%a, %%%%d %%%%h %%%%Y %%%%T %%%%z\"`"+`\n"
|
Hi,\n
|
||||||
printf "From: <sendername> <<sender>>\n"
|
The IP <ip> has just been banned by Fail2Ban after <failures> attempts against <name>.\n\n
|
||||||
printf "To: <dest>\n\n"
|
Here is more information about <ip>:\n"
|
||||||
printf "Hi,\n"
|
%%(_whois_command)s;
|
||||||
printf "The IP <ip> has just been banned by Fail2Ban after <failures> attempts against <name>.\n\n"
|
printf %%%%b "\nLines containing failures of <ip> (max <grepmax>)\n";
|
||||||
printf "Here is more information about <ip>:\n"
|
%%(_grep_logs)s;
|
||||||
printf "%%%%(_whois_command)s\n"
|
printf %%%%b "\n
|
||||||
printf "\nLines containing failures of <ip> (max <grepmax>)\n"
|
Regards,\n
|
||||||
printf "%%%%(_grep_logs)s\n"
|
Fail2Ban" ) | <mailcmd>
|
||||||
printf "\n\nRegards,\nFail2Ban\n"
|
|
||||||
) | <mailcmd>
|
|
||||||
fi'
|
|
||||||
|
|
||||||
[Init]
|
[Init]
|
||||||
|
|
||||||
# Default name of the chain
|
# Default name of the chain
|
||||||
#
|
|
||||||
name = default
|
name = default
|
||||||
|
|
||||||
# Path to the log files which contain relevant lines for the abuser IP
|
# Path to log files containing relevant lines for the abuser IP
|
||||||
#
|
|
||||||
logpath = /dev/null
|
logpath = /dev/null
|
||||||
|
|
||||||
# Number of log lines to include in the email
|
# Number of log lines to include in the email
|
||||||
#
|
# grepmax = 1000
|
||||||
#grepmax = 1000
|
# grepopts = -m <grepmax>
|
||||||
#grepopts = -m <grepmax>
|
|
||||||
`, countriesFormatted)
|
`, countriesFormatted)
|
||||||
|
|
||||||
return os.WriteFile(actionFile, []byte(actionConfig), 0644)
|
// Write the action file
|
||||||
|
//actionFilePath := "/etc/fail2ban/action.d/ui-custom-action.conf"
|
||||||
|
err := os.WriteFile(actionFile, []byte(actionConfig), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write action file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Action file successfully written to %s\n", actionFile)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadSettings reads fail2ban-ui-settings.json into currentSettings.
|
// loadSettings reads fail2ban-ui-settings.json into currentSettings.
|
||||||
|
|||||||
Reference in New Issue
Block a user