LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 03-03-2011, 09:21 PM   #1
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Kubuntu
Posts: 136

Rep: Reputation: 15
Howto? threshold value for failed SSH comms' script


Hi all,

I was playing around with a script that seems to work relatively for my needs when SSH comms fail - AKA a user or someone attacking. This works...

But, how would I add a threshold value? lets say if there are 3 entries, then perform add the IP to iptables as a rule.


Code:
#!/bin/sh
# ------------------------------------
# FIREWALL SCRIPT
#
# March 1st, 2011
#
# Purpose:
# Add offending IP from failed SSH connections
# to the iptables (firewall) rules.
#
# ------------------------------------

## Vars:

TIMETHRESHOLD="10"

## Explanation:

# This program will match lines
# Illegal user (userid) from (host)
# Failed password for (userid) from (host) (...)
# and adds (host) to the iptables blacklist chain
# $blockchain

## Misc preparations:

rm -f hostfile
touch temphostfile

# Disabled IPs can be obtained from /etc/sysconfig/iptables
grep DROP /etc/sysconfig/iptables|awk '{print $5}' >temphostfile

# ------------------------ SSHD FAILURES -------------------------

grep Did /var/log/secure|awk '{print $12}' >>temphostfile

grep "Invalid user" /var/log/secure|awk '{print $10}' >>temphostfile

grep "Maximum login" /var/log/secure|awk '{print $7}'|sed 's/.*\[\(.*\)\])/\1/g' >>temphostfile

grep "Failed password for" /var/log/secure |awk '{print $11}' >>temphostfile

#
# ------------------ REDUCE IPS IN TMP FILE -------------------

size=`/usr/bin/wc temphostfile|awk '{print $1}'`
i=0
echo $(/usr/bin/wc temphostfile|awk '{print $1}')
echo $(cat temphostfile)

while test $i -lt $size
do
      us=`sed -n 1p temphostfile`
      sed /$us/d temphostfile >temphostfiles

      echo $us >>hostfile
      cp -f temphostfiles temphostfile
      size=`/usr/bin/wc temphostfile|awk '{print $1}'`
done

rm -f temphostfile temphostfiles temp0 temp

#
# ------------------ CREATE THE FIREWALL RULES--------------------------

size=`wc hostfile|awk '{print $1}'`
size=`expr $size + 1`
/sbin/iptables -F
i=1

while test $i -lt $size
do
        ip=`sed -n "$i"p hostfile`
        i=`expr $i + 1`
	/sbin/iptables -A INPUT -s $ip -j DROP
done
 
Old 03-03-2011, 11:44 PM   #2
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Kubuntu
Posts: 136

Original Poster
Rep: Reputation: 15
Ahh found annother way and modified it to work:

Code:
USERVAR=2

## Business logic

tail -1000 /var/log/secure | awk -v USERVAR=$USERVAR '/sshd/ && /Failed password for/ { if (/invalid user/) try[$13]++; else try[$11]++; }
END { for (h in try) if (try[h] > USERVAR) print h; }' |
while read ip
do
        # Check if IP is already blocked...
        /sbin/iptables -L -n | grep -x $ip > /dev/null
        if [ $? -eq 0 ] ; then
                # echo "Already denied ip: [$ip]" ;
                true
        else
                # Add a little logging entry
                logger -p authpriv.notice "*** Blocked SSH attempt from: $ip"
                /sbin/iptables -I INPUT -s $ip -j DROP
        fi
done
Posted a copy of it here: http://www.pacificsimplicity.ca/blog...lock-ip-script

Last edited by Lantzvillian; 03-03-2011 at 11:54 PM.
 
Old 03-04-2011, 07:31 AM   #3
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,697
Blog Entries: 1

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You could also just have iptables do the tracking and heavy lifting for you. I've got these rules monitoring my ssh port:

Code:
iptables -N AUTOBAN
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j AUTOBAN
iptables -A AUTOBAN -m recent --set --name SSH
iptables -A AUTOBAN -m recent --update --seconds 120 --hitcount 4 --name SSH -j DROP
Basically it blocks any SSH connection attempts after 4 in the last 120 seconds. The downside is that it is kind of a broad-spectrum approach in that there isn't any sort of a whitelist and depending on what kind of traffic you have, you may wind up blocking legitimate traffic for a bit. However, for my personal use, I've found this easier to maintain than the script-based approaches.
 
Old 03-05-2011, 06:38 AM   #4
Reuti
Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 970

Rep: Reputation: 184Reputation: 184
Quote:
Originally Posted by Hangdog42 View Post
Code:
iptables -N AUTOBAN
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j AUTOBAN
iptables -A AUTOBAN -m recent --set --name SSH
iptables -A AUTOBAN -m recent --update --seconds 120 --hitcount 4 --name SSH -j DROP
Yes, I use a similar rule, but before the last line you could test whether the traffic originated from a well known addres and just return from the script before counting the access.
 
  


Reply

Tags
shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh -L '>channel 3: open failed: connect failed:' Luke771 Linux - Networking 14 01-24-2012 03:14 AM
Wilress Comms failed coubroughj Linux - Laptop and Netbook 2 01-06-2008 12:43 PM
LXer: An ip_conntrack_max Threshold Script LXer Syndicated Linux News 0 07-07-2007 08:47 AM
Howto do Secured ssh from port https or port80(standard) to ssh d listening port 22 ? Xeratul Linux - General 4 11-23-2006 06:09 AM
Serial comms - script to poll /dev/ttyS0 chr15t0 Linux - Networking 1 07-08-2003 10:18 PM


All times are GMT -5. The time now is 09:53 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration