LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 04-11-2012, 03:31 AM   #1
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Rep: Reputation: 32
Is it possible to block an IP address after x amount of login failures?


As per subject, I'm lookinmg to find a way of blocking IP addresses automatically, temporarily or otherwise after so many failed login attempts.

I guess I'd want this to cover ssh, http, pop

My messages log is filled with:

Apr 9 08:45:22 morpheus sshd[8845]: Failed password for invalid user oracle from 119.82.75.221 port 48998 ssh2
Apr 9 08:45:30 morpheus sshd[8847]: Invalid user test from 119.82.75.221
Apr 9 08:45:30 morpheus sshd[8847]: Failed password for invalid user test from 119.82.75.221 port 49193 ssh2
Apr 9 08:46:51 morpheus popa3d[8851]: Authentication passed for plisken
Apr 9 08:46:53 morpheus popa3d[8851]: 12426 messages (148807652 bytes) loaded
Apr 9 08:47:06 morpheus popa3d[8851]: 0 (0) deleted, 12426 (148807652) left
Apr 9 08:59:20 morpheus popa3d[8920]: Authentication passed for maggie
Apr 9 08:59:20 morpheus popa3d[8920]: 5 messages (289465 bytes) loaded
Apr 9 08:59:20 morpheus popa3d[8920]: 0 (0) deleted, 5 (289465) left
Apr 9 09:16:19 morpheus -- MARK --
Apr 9 09:26:41 morpheus sshd[9016]: Failed password for root from 119.82.75.221 port 56424 ssh2
Apr 9 09:26:44 morpheus sshd[9018]: Failed password for root from 119.82.75.221 port 56575 ssh2
Apr 9 09:26:47 morpheus sshd[9020]: Failed password for root from 119.82.75.221 port 56733 ssh2
Apr 9 09:26:50 morpheus sshd[9022]: Failed password for root from 119.82.75.221 port 56894 ssh2
Apr 9 09:26:52 morpheus sshd[9024]: Failed password for root from 119.82.75.221 port 57089 ssh2
Apr 9 09:26:56 morpheus sshd[9026]: Failed password for root from 119.82.75.221 port 57197 ssh2
Apr 9 09:27:03 morpheus sshd[9028]: Failed password for root from 119.82.75.221 port 57423 ssh2
Apr 9 09:27:06 morpheus sshd[9030]: Failed password for root from 119.82.75.221 port 57857 ssh2
Apr 9 09:27:09 morpheus sshd[9032]: Failed password for root from 119.82.75.221 port 58071 ssh2
Apr 9 09:27:16 morpheus sshd[9034]: Failed password for root from 119.82.75.221 port 58222 ssh2
Apr 9 09:27:20 morpheus sshd[9036]: Failed password for root from 119.82.75.221 port 58653 ssh2
Apr 9 09:27:23 morpheus sshd[9038]: Failed password for root from 119.82.75.221 port 58920 ssh2
Apr 9 09:27:25 morpheus sshd[9040]: Failed password for root from 119.82.75.221 port 59069 ssh2
Apr 9 09:27:27 morpheus sshd[9042]: Invalid user oracle from 119.82.75.221
Apr 9 09:27:27 morpheus sshd[9042]: Failed password for invalid user oracle from 119.82.75.221 port 59203 ssh2
Apr 9 09:27:29 morpheus sshd[9044]: Invalid user test from 119.82.75.221
Apr 9 09:27:29 morpheus sshd[9044]: Failed password for invalid user test from 119.82.75.221 port 59342 ssh2


In the past, I manually added IP addresses to the /etc/hosts.deny file.
I'm guessing I could search the log files for a specific string and then add the IP address to this file. something tells me I tried this method years ago without much sucess though.

All help/comments appreciated.
 
Old 04-11-2012, 03:32 AM   #2
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,876

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
fail2ban FTW!

It watches arbitrary log files and generates iptables rules to block the offenders.

Very flexible.

Last edited by descendant_command; 04-11-2012 at 03:34 AM.
 
Old 04-11-2012, 04:07 AM   #3
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 530

Rep: Reputation: Disabled
I too recommend fail2ban, but remember that the attacker might be behind a proxy (they usually are), and even then that IP might not be the router the person is using at home.
 
Old 04-14-2012, 02:26 PM   #4
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
Fail2ban now up and running nicely, well it is working LOL (and rather well)

Any tips on using the iptables route or the deny.hosts route?

Additionally, I couldnt find any mail configurations (jails) in there, I'd like to cover pop3 failures too. At the moment, only using ssh/apache/proftpd
 
Old 04-14-2012, 02:34 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by plisken View Post
Any tips on using the iptables route or the deny.hosts route?
That's no contest: iptables.


Quote:
Originally Posted by plisken View Post
I couldnt find any mail configurations (jails) in there, I'd like to cover pop3 failures too.
If there isn't one then we will create one. What application do you use and what messages does it emit (use the source, Luke)?
 
Old 04-14-2012, 07:13 PM   #6
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
@ unSpawn your rep is at 1337, you got to be fookin kiding me LOL

I'm using:

pop3d-0.6.3-i486
sendmail-0.8.12.10
procmail-3.15.2 (if relevent)

Having searched my /var/log/messages I cant actually find a failed pop3d login attempt, but I'd still like to cover this off

Thanks...
 
Old 04-15-2012, 06:14 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by plisken View Post
Having searched my /var/log/messages I cant actually find a failed pop3d login attempt
Well then fake a few for me, OK? ;-p

So far I've got
Code:
^%(__prefix_line)spop3.* badlogin: .*\[<HOST>\] .* PAM auth error\s*$
^%(__prefix_line)spop3.* badlogin: .*\[<HOST>\] .* authentication failure\s*$
^%(__prefix_line)spop3.* error: PAM: authentication error for illegal user .* from <HOST>\s*$
^%(__prefix_line)s(?:auth: PAM error: )?Authentication failure for .* from <HOST>\s*$
w/o knowing if it's complete or current.
 
Old 04-15-2012, 09:07 AM   #8
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
This is what failed pop stuff looks like in my /var/log/messages

Note, no sign of an IP address unline my ssh failures.
Code:
Apr 15 14:10:50 morpheus popa3d[2833]: Authentication failed for UNKNOWN USER
Apr 15 14:11:01 morpheus popa3d[2842]: Authentication failed for UNKNOWN USER
Apr 15 14:11:11 morpheus popa3d[2852]: Authentication failed for UNKNOWN USER
Apr 15 14:11:22 morpheus popa3d[2859]: Authentication failed for UNKNOWN USER
Apr 15 14:11:32 morpheus popa3d[2867]: Authentication failed for UNKNOWN USER
my /var/log/maillog shows:

Code:
Apr 15 14:10:40 morpheus sm-mta[2832]: q3FDAeAC002832: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA
Apr 15 14:10:51 morpheus sm-mta[2841]: q3FDApAC002841: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA
Apr 15 14:11:01 morpheus sm-mta[2851]: q3FDB1AC002851: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA
Apr 15 14:11:12 morpheus sm-mta[2858]: q3FDBCAC002858: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA
Apr 15 14:11:22 morpheus sm-mta[2866]: q3FDBMAC002866: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA
Apr 15 14:11:32 morpheus sm-mta[2872]: q3FDBWAC002872: server.blackgate.nl [94.1
03.145.204] (may be forged) did not issue MAIL/EXPN/VRFY/ETRN during connection
to MTA

Last edited by plisken; 04-15-2012 at 09:20 AM.
 
Old 04-15-2012, 10:07 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Your var/log/messages (do test with fail2ban-regex) should be like
Code:
^%(__prefix_line)spopa3d .* Authentication failed .* from <HOST>\s*$
but sm-mta from /var/log/maillog AFAIK are MTA messages, not POP.
 
Old 04-16-2012, 05:42 AM   #10
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
This is where one of my problems arise, as there is no <host> in my logs:

Code:
Apr 15 14:10:50 morpheus popa3d[2833]: Authentication failed for UNKNOWN USER
Apr 15 14:11:01 morpheus popa3d[2842]: Authentication failed for UNKNOWN USER
Apr 15 14:11:11 morpheus popa3d[2852]: Authentication failed for UNKNOWN USER
Apr 15 14:11:22 morpheus popa3d[2859]: Authentication failed for UNKNOWN USER
Apr 15 14:11:32 morpheus popa3d[2867]: Authentication failed for UNKNOWN USER
Quote:
but sm-mta from /var/log/maillog AFAIK are MTA messages, not POP.
I just thought the enties in both messages and maillog were related as you can see the times and also the PIDs match up

A failed pop authentication looks like this in /var/log/messages:
Code:
Apr 16 11:47:35 morpheus popa3d[13635]: Authentication failed for realUsername
I guess I may have two options, one to try and force the pop3d to log IP addresses as well and the other option is to use the IP address in the maillog instead.

Last edited by plisken; 04-16-2012 at 05:49 AM.
 
Old 04-16-2012, 10:54 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by plisken View Post
This is where one of my problems arise, as there is no <host> in my logs
Crap. I suppose popa3d doesn't support PAM auth either?


Quote:
Originally Posted by plisken View Post
I just thought the enties in both messages and maillog were related as you can see the times and also the PIDs match up
A failed MTA send doesn't necessarily mean a failed POP3(S) login?..


Quote:
Originally Posted by plisken View Post
I guess I may have two options, one to try and force the pop3d to log IP addresses as well and the other option is to use the IP address in the maillog instead.
IMHO if you have two options then it's to make pop3d to log IP addresses or run the daemon from a super server like Xinetd.
 
Old 04-16-2012, 10:59 AM   #12
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
i use combo of PAM pam_passwdqc.so and denyhosts service. so i'll share what i do on a rhel 5.7

denyhosts will write sshd specific entries into hosts.deny, 3 failed logins for a known account gets the src a 15min block, after that another 3 failed gets PAM to block for 1hr, so essentially 1hr:15min lockout per 6 failed attempts, then the system will dynamically allow the src IP to auth again after that time period.

for ssh specifically they all work together to deny IP as follows:


Code:
********** PASSWORD REQUIREMENTS AND LOCKOUT **********
changed password requirements and lockout using PAM modules in
/etc/pam.d/system-auth config file
perhaps use the switch "quiet" in pam_tally2 auth for more
"dark" systems, this basically locks the account without 
telling user the account was locked. perhaps omit "unlock_time" 
for more secure systems, this would force the sys admin to 
manually unlock the auth. lockout time is set to 3600 (1hr). 
also note the deny is set to 6 (this coincides with sshd_config 
and denyhosts). "root" will be tallied. please note, this is 
not the same as "passwd -l [user]". we also change the strength 
of password hashes for shadow, so in password section notice 
that pam_unix.so uses sha512 instead of the default md5.

# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
# added pam_tally2 in auth and account
# added use of pam_passwdqc and disabled pam_cracklib in password
auth        required      pam_tally2.so onerr=fail deny=6 unlock_time=3600 audit
auth        required      pam_env.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     required      pam_tally2.so
account     required      pam_unix.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so

#password    requisite     pam_cracklib.so try_first_pass retry=3
password    requisite      pam_passwdqc.so min=disabled,disabled,12,8,8 random=0 passphrase=0 retry=3 similar=deny enforce=everyone ask_oldauthtok
password    sufficient    pam_unix.so sha512 shadow remember=12 try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so


================================================

********** hosts.allow AND hosts.deny **********
these files are read by tcpwrappers (/usr/sbin/tcpd)
lets protect inet services from IP that should not be connecting,
etc. note, only services that are wrapped with tcpd will read 
these files, etc. hosts.deny will send a email alert if connection
is not from RFC1918, etc. "denyhosts" will also dynamically update
hosts.deny. if hosts.deny gets mucked up real bad then it can be
fixed by copying /etc/hosts.deny.orig.bak back to hosts.deny, etc.

allow localhost IP
# /etc/hosts.allow
# allow all localhost connections
ALL: 127.0.0.0/255.0.0.0


this hosts.deny file will flat-out deny connectivity from any
non-RFC1918 IP.
# /etc/hosts.deny
# deny all non-RFC1918 addresses and alert on such
#
ALL: ALL EXCEPT 10.0.0.0/255.0.0.0, 172.16.0.0/255.240.0.0, 192.168.0.0/255.255.0.0 \
: spawn (/bin/mail -s "ALERT\: ILLEGAL %d ATTEMPT from IP %h" "1@.gov \
2@.gov 3@.gov 4@.gov 5@.gov") &
#######################################################


and yes, i have a line of hashes in there because denyhosts will
dynamically add/remove entries after this line, makes it easy 
to see denyhosts entries, etc.

=======================================================

********** denyhosts SERVICE INSTALL **********
denyhosts is a control used to thwart off brute force login attacks,
etc.

this service is used to dynamically update /etc/hosts.deny. 
out-of-the-box it watches for failed logins to sshd but can be 
used for watching any log for anything you want. this install 
is "DenyHosts version: 2.6". two DoS fixes (one REGEX5 and one
REGEX7) were applied to usr/lib/python2.4/site-packages/DenyHosts/regex.py (see https://bugzilla.redhat.com/show_bug.cgi?id=244943
and https://bugzilla.redhat.com/attachment.cgi?id=157433&action=diff). 

python is a pre-req (see documentation, etc).

the denyhosts package will install on Red Hat / Fedora, Mandrake,
FreeBSD / OpenBSD, SuSE, Mac OS X. the denyhosts package will
install to /usr/share/denyhosts and there is a README there which
explains everything about denyhosts. this service is setup as a
daemon (a runtime init 2-5 service, run "chkconfig --list |grep
denyhosts" to see. you can control the service using the "service"
command). see the README.


this Linux rhel 5.7 system has access/account controls at several
layers. the 1st layer is network access, and from the host side is
controlled through tcp wrappers (tcpd). sshd by default uses tcpd
(not all services do). sshd_config limits auth tries to 3 before
disconnecting the connection, but this alone does not stop the src
from connecting again, so this is where denyhosts comes in to
assist. denyhosts is configured to block source after 3 consecutive
failed logins (for known accounts) and 10 failed logins (for 
unknown accounts) which will result in a 15min src IP network 
deny. after 15min the src IP will be purged from the deny list 
and is again allowed to connect. after 6 consecutive failed 
logins PAM will lock a valid account for 1hr.

configuration is held in /usr/share/denyhosts/denyhosts.cfg (config
options are explained in the README file).

(settings that were changed, all others left at default)
# PURGE_DENY:
PURGE_DENY = 15m

# PURGE_THRESHOLD
PURGE_THRESHOLD = 0

# DENY_THRESHOLD_INVALID
DENY_THRESHOLD_INVALID = 10

# DENY_THRESHOLD_VALID
DENY_THRESHOLD_VALID = 3

# DENY_THRESHOLD_ROOT
DENY_THRESHOLD_ROOT = 3

# DENY_THRESHOLD_RESTRICTED
DENY_THRESHOLD_RESTRICTED = 1

# HOSTNAME_LOOKUP
HOSTNAME_LOOKUP=no

# ADMIN_EMAIL
ADMIN_EMAIL = 1@.gov, 2@.gov, 3@.gov, 4@.gov, 5@.gov

# SMTP_HOST and SMTP_PORT
SMTP_HOST = 10.21.0.59
SMTP_PORT = 25

# SMTP_FROM
SMTP_FROM = DenyHosts <no-reply@myHost.gov>

# SYSLOG_REPORT
SYSLOG_REPORT=YES

# AGE_RESET_VALID
AGE_RESET_VALID=25d

# AGE_RESET_ROOT
AGE_RESET_ROOT=25d

# AGE_RESET_RESTRICTED
AGE_RESET_RESTRICTED=25d

# AGE_RESET_INVALID
AGE_RESET_INVALID=25d

# RESET_ON_SUCCESS
RESET_ON_SUCCESS = yes

# DAEMON_LOG_TIME_FORMAT
DAEMON_LOG_TIME_FORMAT = %b %d %H:%M:%S

# DAEMON_SLEEP
DAEMON_SLEEP = 15s

# DAEMON_PURGE
DAEMON_PURGE = 5m

# SYNC_INTERVAL
SYNC_INTERVAL = 10000h

# SYNC_UPLOAD
SYNC_UPLOAD = no

# SYNC_DOWNLOAD
SYNC_DOWNLOAD = no

Last edited by Linux_Kidd; 04-16-2012 at 11:16 AM.
 
Old 04-16-2012, 11:21 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Linux_Kidd View Post
denyhosts will write sshd specific entries into hosts.deny,
Please read the link I posted in #5 again.


Quote:
Originally Posted by Linux_Kidd View Post
for ssh specifically
...but we're talking POP3 here?
 
Old 04-16-2012, 12:47 PM   #14
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
what exactly is "pop3d-0.6.3-i486" ?? google search returns nothing.
 
Old 04-16-2012, 01:26 PM   #15
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
Quote:
IMHO if you have two options then it's to make pop3d to log IP addresses or run the daemon from a super server like Xinetd.
I was sure the pop3d was running from inetd and the lines below, shows this (think):
Code:
# Post Office Protocol version 3 (POP3) server:
pop3    stream  tcp     nowait  root    /usr/sbin/tcpd  /usr/sbin/popa3d
Quote:
what exactly is "pop3d-0.6.3-i486"
Just the version of the pop3 daemon running. http://www.openwall.com/popa3d/


I think I'm looking at maybe using a different pop3 service or perhaps recompiling this with options (if possible) to log IP's

Last edited by plisken; 04-16-2012 at 01:29 PM.
 
  


Reply



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
Report last login failures SecondMet Linux - Newbie 0 07-11-2008 09:41 AM
PAM causing login failures cylarz Linux - Networking 1 12-18-2006 06:20 AM
ping failures resolve external public dns/ip address dkeller626 Linux - Networking 2 06-29-2006 09:20 PM
User login failures ombatult Linux - Software 11 02-25-2006 10:11 AM
How we can record the login failures? mahabooba Linux - Security 7 02-08-2006 04:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:34 AM.

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