LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-14-2018, 08:43 AM   #1
dannyvdberg
LQ Newbie
 
Registered: Mar 2018
Location: Netherlands
Posts: 8

Rep: Reputation: Disabled
Use bash script in Fail2Ban Configuration


I would like to call a bash script in the mailing of Fail2Ban. The bash script calls a rest api to get the username of the blocked ip-address by fail2ban.

Code:
	# Fail2Ban configuration file
	#
	# Author: *
	#
	#

	[INCLUDES]

	before = sendmail-common.conf

	[Definition]

	# Option:  actionban
	# Notes.:  command executed when banning an IP. Take care that the
	#          command is executed with Fail2Ban user rights.
	# Tags:    See jail.conf(5) man page
	# Values:  CMD
	#
	actionban = printf %%b "Subject: [Fail2Ban] <name>: banned <ip> from `uname -n`
	            Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
	            From: <sendername> <<sender>>
	            To: <dest>\n
	            The following IP <ip> has just been banned by Fail2Ban after <failures> attempts against the acceptance environment of <name> on server.\n
	            One or the following users could be the victim: \n 
				`/etc/fail2ban/restapi/getHttpSessions.sh | grep <ip>` \n | /usr/sbin/sendmail -f <sender> <dest>

	[Init]
But however I got errors in the fail2ban log that no e-mail is sended

Code:
	`/bin/sh /etc/fail2ban/scripts/getHttpSessions.sh | grep` 10.100.00.00 \n |/usr/sbin/sendmail -f fail2ban test@test.com -- returned 1

Is there some way to use a bash script in a fail2ban configuration file that lookups the IP-address in the bash created overview?

Thanks!
 
Old 03-14-2018, 09:37 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
in there sight
http://www.the-art-of-web.com/system/fail2ban-sendmail/
something about send mail

https://www.fail2ban.org/wiki/index.php/Main_Page

under HOWTO's
 
Old 03-14-2018, 09:48 AM   #3
dannyvdberg
LQ Newbie
 
Registered: Mar 2018
Location: Netherlands
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for your answer but it says nothing about using a shell scripts with grep in a configuration file.

Quote:
Originally Posted by BW-userx View Post
 
Old 03-14-2018, 11:40 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Have you read
Code:
man 5 jail.conf
??
 
Old 03-15-2018, 02:54 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by dannyvdberg View Post
Code:
	`/bin/sh /etc/fail2ban/scripts/getHttpSessions.sh | grep` 10.100.00.00 \n |/usr/sbin/sendmail -f fail2ban test@test.com -- returned 1
have you dissected this and made sure it works manually?
 
1 members found this post helpful.
Old 03-15-2018, 03:18 AM   #6
dannyvdberg
LQ Newbie
 
Registered: Mar 2018
Location: Netherlands
Posts: 8

Original Poster
Rep: Reputation: Disabled
Yes, tested the script manually couple of times. This is the script that is called:

Code:
#!/bin/bash

USERNAME==
PASSWORD==
CONFL_URL=
CONFL_URL_MONITORING=
COOKIES=cookies.txt
HEADER="X-Atlassian-Token: no-check"

echo Logging in...
curl -s -c "$COOKIES" -H "$HEADER" -d "os_username=$USERNAME" -d "os_password=$PASSWORD" -d "os_cookie=true" -k $CONFL_URL/login.jsp --output login.html

echo Authenticating as administrator...
curl -si -c "$COOKIES" -b "$COOKIES" -H "$HEADER" -d "webSudoPassword=$PASSWORD" -d "os_cookie=true" -d "webSudoIsPost=false" -d "authenticate=Confirm" -k $CONFL_URL/authenticate.action --output auth.html

echo Lookup HTTP Sessions...
curl -s -b "$COOKIES" -H "$HEADER" -d "os_cookie=true" -d "webSudoIsPost=true" -k $CONFL_URL_MONITORING | awk -F "</*td>|</*tr>" '/<\/*t[rd]>.*[A-Z][A-Z]/ {print $10, $15 }' | cut -f 4,5,6 -d ' '

echo Cleaning up...
rm $COOKIES
Quote:
Originally Posted by ondoho View Post
have you dissected this and made sure it works manually?
 
Old 03-15-2018, 03:30 AM   #7
dannyvdberg
LQ Newbie
 
Registered: Mar 2018
Location: Netherlands
Posts: 8

Original Poster
Rep: Reputation: Disabled
I got it working! Probably some issues with syntax, but the following code works:

Thanks guys!

Quote:
# Fail2Ban configuration file
#
# Author: Danny van den Berg
#
#

[INCLUDES]

before = sendmail-common.conf

[Definition]

# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = printf %%b "Subject: [Fail2Ban] <name>: banned <ip> from `uname -n`
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sendername> <<sender>>
To: <dest>\n
The following IP <ip> has just been banned by Fail2Ban after <failures> attempts against the production environment of <name> on server lrv154ec.\n
Blocked users = `/usr/bin/gethttpsessions | grep <ip> `\n\n" | /usr/sbin/sendmail -f <sender> <dest>

[Init]

# Default name of the chain
#
name = default
 
Old 03-15-2018, 12:18 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by dannyvdberg View Post
the following code works:
but where, oh where did you edit?
/etc/fail2ban/action.d/<mycustom.conf>
or some core file from the fail2ban package??

If the latter, on fail2ban upgrade, you lose.
 
Old 03-15-2018, 12:52 PM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Regarding Habitual's comment, just make as back-up of the new file in case fail2ban wipes it out on an upgrade. I know Debian checks for custom config files, but maybe not every distro does.
 
Old 03-15-2018, 01:39 PM   #10
dannyvdberg
LQ Newbie
 
Registered: Mar 2018
Location: Netherlands
Posts: 8

Original Poster
Rep: Reputation: Disabled
It's just only the configuration file for sending emails in a jail that has changed. I recovered the original file and started all over again.
 
Old 03-15-2018, 05:49 PM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by dannyvdberg View Post
It's just only the configuration file for sending emails in a jail that has changed. I recovered the original file and started all over again.
Easier to manage a copy that will not be overwritten...

Code:
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
and do your edits in /etc/fail2ban/jail.local which will NOT be overwritten if fail2ban is upgraded.

I only put enabled jails in jail.local

good luck.
#Unanswered

Last edited by Habitual; 03-15-2018 at 05:51 PM.
 
  


Reply

Tags
bash, fail2ban, sendmail



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Fail2ban apache-pma script not working baldur2630 Linux - Security 2 09-07-2010 12:58 AM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration