public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Anno v. Heimburg" <anno@vonheimburg.de>
To: gentoo-user@lists.gentoo.org
Subject: [gentoo-user]  Re: SSH brute force attacks and blacklist.py
Date: Wed, 27 Feb 2008 22:39:15 +0100	[thread overview]
Message-ID: <fq4la3$kqn$1@ger.gmane.org> (raw)
In-Reply-To: 47C5B4F9.9060701@j-schmitz.net

Justin wrote:

> Try fail2ban

Alternatively, you can use the builtin iptables connection rate limiter.

Excerpt from my home-grown firewall script:

------------
for port in $INPUT_LIMITER_TCPPORTS; do
        $IPT_IN -p tcp  --dport $port -m state --state NEW -m \
recent --name "limit-${port}" --set
        $IPT_IN -p tcp  --dport $port -m state --state NEW -m \
recent --name "limit-${port}" --rcheck --seconds
$INPUT_LIMITER_TIME --hitcount $INPUT_LIMITER_COUNT -j \
LOG --log-prefix "limit-rjct-${port} "
        $IPT_IN -p tcp  --dport $port -m state --state NEW -m \
recent --name "limit-${port}" --rcheck --seconds
$INPUT_LIMITER_TIME --hitcount $INPUT_LIMITER_COUNT -j REJECT \
        $IPT_IN -p tcp  --dport $port -m state --state NEW -j
LOG --log-level notice --log-prefix "limit-acpt-${port} " \
        $IPT_IN -p tcp  --dport $port -m state --state NEW -j ACCEPT
done
----------------

It limits the number of new connections on each port in
INPUT_LIMITER_TCPPORTS from any individual host to INPUT_LIMITER_COUNT
within INPUT_LIMITER_TIME.

More precisely, it does the following:

1. When a new connection is established by a previously unkown host, set a
mark (first rule).
2. When the number of marks from that host has exceeded the specified upper
connection limit, reject the connection (third rule), you could also drop.
3. Otherwise, accept the connection (fifth rule)

Rules numbers 2 and 4 are for logging purposes only, and have no impact on
functionality. By using --log-prefix, you can use your logging daemon's
filtering capabilities to sort these requests into new 

The count is reset after INPUT_LIMITER_TIME seconds have passed. Thus, after
exceeding INPUT_LIMITER_COUNT, you have to wait for $INPUT_LIMITER_SECONDS
before a new attempt.

Oh yeah, $IPT_IN is shorthand for "${IPTABLES} -t filter -A INPUT", where
${IPTABLES} points to the iptables executable, of course.

The advantage of this solution is that it does not rely on log files parsing
or any other magic, it simply counts the number of connections from each
host on a specific port. It it does very easy on CPU and very stable, it
continues working as long as your kernel works.

The disadvantage is that it does not rely on log files parsing or any other
magic, it simply counts the number of connections from each host on a
specific port. It cannot do anything clever. Also, your iptables -L output
gets a bit cluttered by adding five rules for every port you want to
rate-limit.

Anno.

-- 
gentoo-user@lists.gentoo.org mailing list



  reply	other threads:[~2008-02-27 21:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-27 17:51 [gentoo-user] SSH brute force attacks and blacklist.py Steve
2008-02-27 18:09 ` [gentoo-user] " 7v5w7go9ub0o
2008-02-27 18:14   ` 7v5w7go9ub0o
2008-02-27 18:12 ` [gentoo-user] " Alan McKinnon
2008-02-27 19:07 ` Justin
2008-02-27 21:39   ` Anno v. Heimburg [this message]
2008-02-28 16:31     ` [gentoo-user] " Willie Wong
2008-02-27 20:24 ` Remy Blank
2008-02-27 23:01   ` Iain Buchanan
2008-02-28  9:55   ` Etaoin Shrdlu
2008-02-28 11:13     ` Steve
2008-02-28 16:19       ` Willie Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='fq4la3$kqn$1@ger.gmane.org' \
    --to=anno@vonheimburg.de \
    --cc=gentoo-user@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox