Sorry for not being response to your request for help sooner.
I had a bit of a hardware crisis here last week, where
what I thought was merely a blown power supply turned
out to be a failed motherboard. Getting the 2.5" SAS
drives back up and running in a different machine took
far longer than I would have guessed. That, along with
a secondary MX host that was offline for the first 36
hours after the main mail server went down was a cause
for additional excitement.
Anyway.
I've read through the mail exchange, although its a bit
hard to follow all of it.
I'll offer a couple of observations about blacklistd
and how it operates, and maybe that will shed some light
on the problem at hand. If not, well, I'd like to start
fresh with the current configuration, and what you're
seeing on your host.
Observations that might help:
1) The blacklistd support in 11.0 was broken in a couple
of significant ways. The blacklistd support in 11.1 is
thought to be fully functional. If you're not running 11.1,
you will need to update to 11.1.
2) I only use blacklistd with 'pf' in my day-to-day usage.
I extended the support in blacklistd-helper to hopefully
handle both ipfw and ipf, and it seemed to work OK for my
test setup. HOWEVER, it is entirely possible that the way
I did the ipf/ipfw support has a flaw (or more) in it.
3) The changes to the various daemons to support the
blacklist just enable sending messages (and a copy of the
fd of socket) to the blacklist daemon. The blacklist daemon
will extract information from the kernel about the socket's
other end (ie, the information about the remote system),
and stores that information in a database.
4) After the information is stored in the database, the
blacklist daemon calls the blacklistd-helper script and
that script is responsible for modifying the firewall
rules that are in effect. If the script has a bug, it's
entirely possible that the information in the database
will be out of sync with the current firewall rules in
effect.
5) If you're experiencing a situation where the number
of login attempts is greater than the cutoff for the
service (e.g., the "1662/1" noted in the email thread),
that means that whatever firewall rule that is supposed
to be blocking the service isn't blocking the traffic.
(See next item for a case where the right rules are in
the filter, but you still get a "modest" overage of
attempts vs the cutoff.)
6) On a slow-ish single-CPU host (like the sparc64 that I use
as my gateway), it's possible to get more attempts than
the cutoff for a persist, high-speed attacker.
Basically, it takes so long before the system context switches
to the blacklist daemon, and the entry gets added to the pf table.
Where "so long" is still less than a second, but the machine has
already seen 10 or 12 attempts!
For example, here's a partial list of what my gateway is reporting
right now:
root@gatekeeper-130: blacklistctl dump -a
address/ma:port id nfail last access
[...]
61.126.187.219/32:22 OK 3/3 2017/11/12 17:31:40
156.212.51.78/32:22 OK 23/3 2017/11/12 19:09:38
179.53.156.109/32:22 OK 3/3 2017/11/12 19:58:57
220.174.236.220/32:22 2/3 2017/11/12 23:39:58
198.245.63.120/32:22 OK 3/3 2017/11/13 10:36:15
You can see a couple of "normally blocked" attempts (3/3),
a single IP address that has 2 of 3 attempts, and a very,
very persistent/fast host that got in 23 attempts before
it got blocked.
7) There was a note about different usernames from the same
remote host. The blacklist support currently does not
differentiate between usernames. It is just counting the
number of attempts from a remote IP address.
There's unfinished support for having a "known bad" set of usernames,
where a single login attempt for that username will block
the remote address. This will allow (when finished), easy
blocking of the twenty or so most common usernames that are
probed.
Hopefully this will help.
-Kurt
On 11/13/17 9:17 AM, Cos Chan wrote:
>
>
> On Sat, Nov 11, 2017 at 1:42 PM, Ian Smith <smi...@nimnet.asn.au
> <mailto:smi...@nimnet.asn.au>> wrote:
>
> On Thu, 9 Nov 2017 14:25:52 +0100, Cos Chan wrote:
>
> > Dear All
> >
> > Thanks Ian's great help, I have solved problem to post banned
> entries from
> > blacklistd to ipfw.
>
> Well, we're some of the way there :) We really need Kurt Lidl's eyes on
> this to make real progress, and indications are that my and your emails
> cc'ing him were still being deferred for some reason - maybe he's away?
>
> > The original message was received at Tue, 7 Nov 2017 10:12:05
> -0500 (EST)
> > from mx2.freebsd.org <http://mx2.freebsd.org> [8.8.178.116]
> >
> > ----- Transcript of session follows -----
> > <li...@pix.net <mailto:li...@pix.net>>... Deferred: Operation timed out
> with hydra.pix.net <http://hydra.pix.net>.
> 193.201.224.218/32:22 <http://193.201.224.218/32:22> OK 1662/1
> 2017/11/13 00:31:04
>
> This IP was blocked in ipfw from last week. while I checked it last week
> Friday it was 800+/1 in blacklist and until today it become 1662.
>
> To my knowledge the ipfw should block the connection, the times of
> banned IP should be not increased?
>
> I could see more entries with more than 3/1, for example:
>
> 89.160.221.132/32:22 <http://89.160.221.132/32:22> OK 18/1
> 2017/11/13 00:01:21
> 60.125.42.119/32:22 <http://60.125.42.119/32:22> OK 3/1
> 2017/11/12 16:13:53
> 166.62.35.180/32:22 <http://166.62.35.180/32:22> OK 3/1
> 2017/11/10 06:36:25
> 202.162.221.51/32:22 <http://202.162.221.51/32:22> OK 6/1
> 2017/11/10 00:42:14
> 168.0.114.130/32:22 <http://168.0.114.130/32:22> OK 3/1
> 2017/11/10 23:40:30
> 95.145.71.165/32:22 <http://95.145.71.165/32:22> OK 3/1
> 2017/11/11 07:07:07
> 123.161.206.210/32:22 <http://123.161.206.210/32:22> OK 3/1
> 2017/11/12 18:14:00
> 203.146.208.208/32:22 <http://203.146.208.208/32:22> OK 6/1
> 2017/11/10 10:16:21
> 149.56.223.241/32:22 <http://149.56.223.241/32:22> OK 1/1
> 2017/11/12 06:09:16
> 121.169.217.98/32:22 <http://121.169.217.98/32:22> OK 9/1
> 2017/11/12 21:59:57
> 211.251.237.162/32:22 <http://211.251.237.162/32:22> OK 2/1
> 2017/11/13 12:08:07
> 103.99.0.116/32:22 <http://103.99.0.116/32:22> OK 30/1
> 2017/11/10 14:56:07
>
> These records I am not sure if they were not increased after added to
> ipfw list. but the 1662 times one, I am sure it was increased after ipfw
> had the ip in list.
>
>
> > BUT I found another problem.
> >
> > The output of blacklist dump is strange:
> >
> > $ sudo blacklistctl dump
> > address/ma:port id nfail last access
> > 96.227.104.132/32:22 <http://96.227.104.132/32:22>
> 0/2 1970/01/01 01:00:00
> > 89.245.78.187/32:22 <http://89.245.78.187/32:22> 0/2
> 1970/01/01 01:00:00
> > 116.193.162.203/32:22 <http://116.193.162.203/32:22>
> if (dbi.id <http://dbi.id>[0]) {
> /*
> * We should not be getting this since the rule
> * should have blocked the address. A possible
> * explanation is that someone removed that
> rule,
> * and another would be that we got another
> attempt
> * before we added the rule. In anycase, we
> remove
> * and re-add the rule because we don't
> want to add
> * it twice, because then we'd lose track
> of it.
> */
> (*lfun)(LOG_DEBUG, "rule exists %s", dbi.id
> <http://dbi.id>);
> (void)run_change("rem", &c, dbi.id
> <http://dbi.id>, 0);
> dbi.id <http://dbi.id>[0] = '\0';
> }
> if (c.c_nfail != -1 && dbi.count >= c.c_nfail) {
> int res = run_change("add", &c, dbi.id
> <http://dbi.id>, sizeof(dbi.id <http://dbi.id>));
> if (res == -1)
> goto out;
> sockaddr_snprintf(rbuf, sizeof(rbuf), "%a",
> (void *)&rss);
> (*lfun)(LOG_INFO,
> "blocked %s/%d:%d for %d seconds",
> rbuf, c.c_lmask, c.c_port, c.c_duration);
>
> }
> break;
>
> But if the 'add' command via blacklistd-helper fails, it will never add
> the 1 .. I'm not certain about this, but it could explain what you see,
> although I can't discern whether sshd is reporting BL_ADD or BL_ABUSE.
>
> You might instead try MaxAuthTries 4 .. sshd_config(5) says:
>
> MaxAuthTries
> Specifies the maximum number of authentication
> attempts permitted
> per connection. Once the number of failures reaches
> half this
> value, additional failures are logged. The default is 6.
>
> Half of 3 as an integer is only 1, but half of 4 is 2. See if it helps?
>
>
> I didnt change the MaxAuthTries, since I found something interesting
> from the different logs concerning that issue:
>
> From blacklistctl dump:
>
> $ sudo blacklistctl dump
> address/ma:port id nfail last access
> 78.203.146.34/32:22 <http://78.203.146.34/32:22> 0/1
> 1970/01/01 01:00:00
> 195.225.116.21/32:22 <http://195.225.116.21/32:22> 0/1
> 1970/01/01 01:00:00
> 123.31.26.123/32:22 <http://123.31.26.123/32:22> 0/1
> 1970/01/01 01:00:00
> 112.148.101.13/32:22 <http://112.148.101.13/32:22> 0/1
> 1970/01/01 01:00:00
> 93.23.6.18/32:22 <http://93.23.6.18/32:22> 0/1 1970/01/01
> 01:00:00
> 5.102.197.124/32:22 <http://5.102.197.124/32:22> 0/1
> 1970/01/01 01:00:00
> 193.154.127.32/32:22 <http://193.154.127.32/32:22> 0/1
> 1970/01/01 01:00:00
> 113.232.216.41/32:22 <http://113.232.216.41/32:22> 0/1
> In that case I test sshd MaxAuthTries=1 and blacklistd nfail=1 and still
> get wired entry.
>
> $ sudo blacklistctl dump
> address/ma:port id nfail last access
> 57.83.1.58/32:22 <http://57.83.1.58/32:22> 0/1 1970/01/01
> 01:00:00
>
> $ sudo cat auth.log | grep 57.83.1.58
> Nov 16 07:04:17 res sshd[31112]: Invalid user pi from 57.83.1.58
> Nov 16 07:04:17 res sshd[31113]: Invalid user pi from 57.83.1.58
> Nov 16 07:04:17 res sshd[31112]: Connection closed by 57.83.1.58 port
> 51140 [preauth]
> Nov 16 07:04:17 res sshd[31113]: Connection closed by 57.83.1.58 port
> 51144 [preauth]
>
> $ cat blacklistd-helper.log | grep 'Nov 16'
> ...
> Thu Nov 16 07:01:28 CET 2017 /usr/libexec/blacklistd-helper run add
> blacklistd tcp 120.237.88.186 32 22
> Thu Nov 16 07:14:05 CET 2017 /usr/libexec/blacklistd-helper run add
> blacklistd tcp 139.59.111.224 32 22
>
> No action from blacklistd-helper? how could that entry be added to database?
>
> no logs concerning from blacklistd either
>
> $ cat blacklistd.log | grep 'Nov 16'
> ...
> Nov 16 07:01:28 res blacklistd[23916]: blocked 120.237.88.186/32:22
> <http://120.237.88.186/32:22> for -1 seconds
> Nov 16 07:14:05 res blacklistd[23916]: blocked 139.59.111.224/32:22
> <http://139.59.111.224/32:22> for -1 seconds
Pre-auth failures from sshd, where the username isn't found ("Invalid
user pi"), don't count against failed login attempts, because no
authorization was ever attempted by sshd.
I made the decision not to count these against the limit in blacklistd.
There is a message sent from sshd to blacklistd when this occurs (bad
username), but this is the part that isn't implemented in the backend,
for banning addresses that hit known-bad usernames.
I suppose the case could be made that a bad username is just as serious
as a bad password for an existing username. But that's not what the
code does currently. Obviously, the code could be changed to act
differently in this case.
Blacklistd did not originally have any message types, other than
"login successful" and "login failed" for each address.
The "login successful" messages cleared all failed login attempts
for a given address. The "login failed" messages added one to the
count of failed logins for an address. If the count was over the
limit for that service (aka port), an attempt to insert rule(s)
into the packet filter to block that address.
I've added the "abusive behavior" message type, so an application
can signal blacklisted that they want the remote address blocked
immediately. The only thing that sends that is the patches to
sendmail that I have been testing. (Not even the patches in the
/usr/ports do it yet, as that capability didn't exist when I wrote
that set of patches.) The sshd daemon (currently) never sends
"abusive behavior" messages.
The "bad username" message (again, not yet implemented in the backend),
is intended to allow the administrator to configure a list of
bad usernames. If usernames on this list are flagged from an
application, the remote address is should be blocked immediately.
I've struggled a bit in terms of the design for this -- the list of
usernames cannot be tied to password file entries - obviously
one might like to have "pi" on the list of forbidden names, even
though no account exists. I've also torn about the right way
to link up the blacklist rules with the name of the list of
bad usernames to check against.
While I imagine more admins would like to have a single list of
bad usernames, and use that one list for smtp, sshd and whatever
else, others might want to have different lists for one or more
services.
I really should implement *something* and just accept that it will
be flawed and need refinement.
-Kurt