pavement

Block repeated illegal or failed SSH logins

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Spam cleanup)
(minor updates)
Line 127: Line 127:
  
 
[[Category:Securing FreeBSD]]
 
[[Category:Securing FreeBSD]]
 +
 +
== Student Goes From Homeless to Harvard ==
 +
 +
Despite being abandoned to homelessness by her parents, Dawn Loggins worked as a high school custodian by day and studied hard by night to become the first person from her school to ever be admitted to Harvard.
 +
 +
[[http://goodvillenews.com/Student-Goes-From-Homeless-to-Harvard-QX9Vg4.html Student Goes From Homeless to Harvard]]
 +
 +
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]
 +
 +
== The Power of Words and Emotional Bonding! ==
 +
 +
If you become steadfast in your abstentions of thoughts of harm directed toward others, all living creatures will cease to feel enmity in your presence. PatanjaliWords have power, incredible power, and this power lies in each and every one of us. Of course, its up to us to use this power for the greater good of all or not.
 +
 +
[[http://goodvillenews.com/The-Power-of-Words-and-Emotional-Bonding-2wgncv.html The Power of Words and Emotional Bonding!]]
 +
 +
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]
 +
 +
== Cat Saves Owner Hours After Adoption ==
 +
 +
A newly-adopted cat repaid his owners loving gesture earlier this month by saving her from a medical emergency just hours after he was brought home, the Green Bay Press Gazette reports.
 +
 +
[[http://goodvillenews.com/Cat-Saves-Owner-Hours-After-Adoption-ixHpp7.html Cat Saves Owner Hours After Adoption]]
 +
 +
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]
 +
 +
== Journey to the End of the Earth ==
 +
 +
I realized quickly, after just having traveled to various villages in rural India, that distance is relative. Hailing from a city like San Francisco, going even a few hours outside of town is far but twelve hours outside of a major city? I half expected to run into another country.
 +
 +
[[http://goodvillenews.com/Journey-to-the-End-of-the-Earth-tbNql3.html Journey to the End of the Earth]]
 +
 +
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]
 +
 +
== Disney Brings "Cars" To Life With Cars Land ==
 +
 +
After five years in the making, Disney California Adventure is finally ready to rev up the engines on Cars Land. Disneys latest creation, based on the hit Pixar movie, brings the popular characters to life at Disney California Adventure.
 +
 +
[[http://goodvillenews.com/Disney-Brings-Cars-To-Life-With-Cars-Land-YfdwZj.html Disney Brings "Cars" To Life With Cars Land]]
 +
 +
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]

Revision as of 06:36, 24 August 2012

Contents

Introduction

We're starting to see a rash of password guessing attacks via SSH on exposed BSD servers which are running the SSH daemon. These login attempts are coming from multiple addresses, which makes some people suspect that they're being carried out by a network of "bots" rather than a single attacker.

Limiting SSH login sessions

In your sshd_config file the following settings can also help slow down such attacks.

  • LoginGraceTime
The server disconnects after this time if the user has not successfully logged in. If the value is 0, there is no time limit. The default is 120 seconds.
  • MaxStartups
Specifies the maximum number of concurrent unauthenticated connections to the sshd daemon. Additional connections will be dropped until authentication succeeds or the LoginGraceTime expires for a connection. The default is 10. Alternatively, random early drop can be enabled by specifying the three colon separated values "start:rate:full" (e.g.,"10:30:60"). sshd will refuse connection attempts with a probability of "rate/100" (30%) if there are currently "start" (10) unauthenticated connections. The probability increases linearly and all connection attempts are refused if the number of unauthenticated connections reaches "full" (60).

Firewall repeated illegal or failed SSH logins attempts

To firewall failed login attempts, a simple script that will scan the log file for illegal or failed attempts and firewall repeated IP's will do the trick. It will slow down and stop a brute force dictionary login attack.

Using the examples below you can create a file called sshd-fwscan.sh, then use cron to run the file every x minutes and it will automatically firewall the IP once it detects 5 or more failed login attempts.

/etc/syslog.conf

You need an auth.* line in your syslog.conf file in order to log all authentications.

auth.*                                          /var/log/auth.log

Using IPFW

sshd-fwscan.sh

#!/bin/sh
if ipfw show | awk '{print $1}' | grep -q 20000 ; then
        ipfw delete 20000
fi
# This catches repeated attempts for both legal and illegal users
# No check for duplicate entries is performed, since the rule
# has been deleted.
awk '/sshd/ && (/Invalid user/ || /authentication error/) {try[$(NF)]++}
END {for (h in try) if (try[h] > 5) print h}' /var/log/auth.log |
while read ip
do
        ipfw -q add 20000 deny tcp from $ip to any in
done

Note: To make sure IP's expire we delete and add rule 20000 of the firewall each time, thus if the IP's are no longer duplicates in the auth.log they are no longer firewalled.

Using IPF

sshd-fwscan.sh

#!/bin/sh
IFS='
'
for rules in `ipfstat -i | grep "group 20000"` ; do
       echo "$rules" | ipf -r -f -
done
for ips in `cat /var/log/auth.log | grep sshd | grep "Illegal" | awk '{print $10}' | uniq -d` ; do
       echo "block in quick from $ips to any group 20000" | ipf -f -
done
cat /var/log/auth.log | grep sshd | grep "Failed" | rev  | cut -d\  -f 4 | rev | sort | uniq -c | \
( while read num ips; do
   if [ $num -gt 5 ]; then
        if ! ipfstat -i | grep $ips ; then
               echo "block in quick from $ips to any group 20000" | ipf -f -
       fi
    fi
 done
)


Note: 
To make sure IP's expire we delete and add group 20000 of the firewall each time, 
thus if the IP's are no longer duplicates in the auth.log they are no longer firewalled. 
You will need to add a rule like "block in on rl0 from any to any head 20000" to your ipf rule 
set (BEFORE your actual blocking group of rules) for this to work.

Using PF

sshd-fwscan.sh

#!/bin/sh
/sbin/pfctl -t ssh-violations -T flush
for ips in `cat /var/log/auth.log | grep sshd | grep -i "illegal" | awk '{print $10}' | uniq -d` ; do
       /sbin/pfctl -t ssh-violations -T add $ips 
done
cat /var/log/auth.log | grep sshd | grep -i "failed" | rev  | cut -d\  -f 4 | rev | sort | uniq -c | \
( while read num ips; do
    if [ $num -gt 5 ]; then
         if ! /sbin/pfctl -s rules | grep -q $ips ; then
                /sbin/pfctl -t ssh-violations -T add $ips 
        fi
    fi
  done
)

Note: To make sure IP's expire we delete and add a table called ssh-violations, thus if the IP's are no longer duplicates in the auth.log they are no longer firewalled.

/etc/pf.conf

table <ssh-violations> persist file "/etc/ssh-violations"
...
block drop in from <ssh-violations> to any
Note: 
When using the OpenBSD Packet Filter (PF) you must also edit your pf.conf
file to add the above table and rule.
Important: 
If this rule is added before a "pass in" rule for port 22, use the "quick" option to ensure that
OpenBSD Packet Filter (PF) drops the packet immediately, without further inspection of the
ruleset. See the man 5 pf.conf for details.

Copyrights

sshd-fwscan.sh

# Copyright (c) 2004,2005 RPTN.Net,
# Copyright (c) 2005 DaveG.ca, 
# Copyright (c) 2006 Bob (kba at ats32.ru)
# You may use this code under the GPL, version 2 or newer.
# Updates for IPF by Sasha.by

Automatically firewall IP's

/etc/crontab

In order to have the script run every 10 minutes and firewall offenders you can use something like this in your crontab file:

*/10    *       *       *       *       root    /operator/sshd-fwscan.sh

Note: Some users might prefer a tailing method rather then a scanning/searching method, but all we really want is to slow down such attacks to reduce their chances of cracking a user account and not waste our resources. The odds that a password gets cracked under 10 minutes should be rare. (The longer the password is, mixed with letters numbers and symbols, the longer it takes to crack.)

External links

Student Goes From Homeless to Harvard

Despite being abandoned to homelessness by her parents, Dawn Loggins worked as a high school custodian by day and studied hard by night to become the first person from her school to ever be admitted to Harvard.

[Student Goes From Homeless to Harvard]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

The Power of Words and Emotional Bonding!

If you become steadfast in your abstentions of thoughts of harm directed toward others, all living creatures will cease to feel enmity in your presence. PatanjaliWords have power, incredible power, and this power lies in each and every one of us. Of course, its up to us to use this power for the greater good of all or not.

[The Power of Words and Emotional Bonding!]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Cat Saves Owner Hours After Adoption

A newly-adopted cat repaid his owners loving gesture earlier this month by saving her from a medical emergency just hours after he was brought home, the Green Bay Press Gazette reports.

[Cat Saves Owner Hours After Adoption]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Journey to the End of the Earth

I realized quickly, after just having traveled to various villages in rural India, that distance is relative. Hailing from a city like San Francisco, going even a few hours outside of town is far but twelve hours outside of a major city? I half expected to run into another country.

[Journey to the End of the Earth]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Disney Brings "Cars" To Life With Cars Land

After five years in the making, Disney California Adventure is finally ready to rev up the engines on Cars Land. Disneys latest creation, based on the hit Pixar movie, brings the popular characters to life at Disney California Adventure.

[Disney Brings "Cars" To Life With Cars Land]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Personal tools