I have openSMPTD
running a mail server. It seems I am getting brute forced attacked currently, although I doubt they will get in. That being said, I would like to alleviate the burden on my server with the constant log-in attempts. I am configuring fail2ban
to ban the offenders. However, I can not quite get the correct regex and it is driving me mad!
I saw this link on the freeBSD forum about the fail2ban
regex to catch it. However, even that regex did not seem to capture the example text they provided. I edited their regex to no avail. I am hoping someone can give me some pointers as I am super weak in regexs.
My log files looks like:
Dec 25 20:03:29 frick mail.info smtpd[16849]: f7fa148a43b34578 smtp connected address=193.169.254.42 host=<unknown>
Dec 25 20:03:30 frick mail.info smtpd[16849]: f7fa148a43b34578 smtp tls ciphers=TLSv1.2:ECDHE-RSA-AES128-SHA256:128
Dec 25 20:03:30 frick mail.info smtpd[16849]: f7fa148a43b34578 smtp authentication user=info result=permfail
Dec 25 20:03:30 frick mail.info smtpd[16849]: f7fa148a43b34578 smtp failed-command command="AUTH LOGIN (password)" result="535 Authentication failed"
Dec 25 20:03:30 frick mail.info smtpd[16849]: f7fa148a43b34578 smtp disconnected reason=quit
"
So let's break this down: the line that shows they are brute forcing is:
f7fa148a43b34578 smtp failed-command command="AUTH LOGIN (password)" result="535 Authentication failed"
Where we see authentication failed. However, oddly, openSMTPD does not include their ip address to ban in that line: We have to look a variable number above it to:
f7fa148a43b34578 smtp connected address=193.169.254.42 host=<unknown>
Where we can see the offender's ip address. Okay, fair enough. The current regex I have that selects all the text is:
^.*smtp connected address=\w[1-9.]* host=[a-z<>]*\n*[a-zA-Z0-9_.-\s\]\[:=<>"()]*
although fail2ban
wants a more fine grained one. I am not sure I have a clean way of getting the appropiate ip address to ban as it is possible two people connect at once, and then the wrong line is picked up to ban.
Any suggestions?
address=\w[
- what is the\w
for? The IP starts with a number right away. The host section I read zero or more of a-z<>, then zero or more newlines, then zero or more of mostly anything. I would consider to preprocess the lines to match the correct mail ids together and then use it fail2ban, it may be quite annoying, but I think you have to match the correct pairs of "f7fa148a43b34578" lines together.(?<=: )(?<token_identifier>[a-zA-Z0-9]*)
(?<=: )(?<token_identifier>[a-zA-Z0-9]*) smtp connected address=(?<ipaddress>[0-9:.])*
This gets me the token and the ip address in a capture group