2014-08-15 11:40:04 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : mod_fail2ban.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2019-05-15 16:21:09 +02:00
|
|
|
%%% Purpose :
|
2014-08-15 11:40:04 +02:00
|
|
|
%%% Created : 15 Aug 2014 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2020 ProcessOne
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-11-10 17:51:22 +01:00
|
|
|
%%%
|
2014-08-15 11:40:04 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2014-08-15 11:40:04 +02:00
|
|
|
-module(mod_fail2ban).
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
2014-08-17 15:38:38 +02:00
|
|
|
-behaviour(gen_server).
|
2014-08-15 11:40:04 +02:00
|
|
|
|
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, c2s_auth_result/3,
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_stream_started/2]).
|
2014-08-15 11:40:04 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3,
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_opt_type/1, mod_options/1, depends/2, mod_doc/0]).
|
2014-08-15 11:40:04 +02:00
|
|
|
|
2018-11-30 13:33:39 +01:00
|
|
|
%% ejabberd command.
|
|
|
|
-export([get_commands_spec/0, unban/1]).
|
|
|
|
|
2014-08-17 15:38:38 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2018-11-30 13:33:39 +01:00
|
|
|
-include("ejabberd_commands.hrl").
|
2014-08-17 15:38:38 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2014-08-17 15:38:38 +02:00
|
|
|
|
|
|
|
-define(CLEAN_INTERVAL, timer:minutes(10)).
|
|
|
|
|
|
|
|
-record(state, {host = <<"">> :: binary()}).
|
2014-08-15 11:40:04 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
2014-08-17 15:38:38 +02:00
|
|
|
%%% API
|
2014-08-15 11:40:04 +02:00
|
|
|
%%%===================================================================
|
2019-05-15 16:21:09 +02:00
|
|
|
-spec c2s_auth_result(ejabberd_c2s:state(), true | {false, binary()}, binary())
|
2017-01-09 15:02:17 +01:00
|
|
|
-> ejabberd_c2s:state() | {stop, ejabberd_c2s:state()}.
|
2019-05-15 17:13:31 +02:00
|
|
|
c2s_auth_result(#{sasl_mech := Mech} = State, {false, _}, _User)
|
|
|
|
when Mech == <<"EXTERNAL">> ->
|
|
|
|
State;
|
2019-05-15 16:21:09 +02:00
|
|
|
c2s_auth_result(#{ip := {Addr, _}, lserver := LServer} = State, {false, _}, _User) ->
|
2015-04-18 11:08:05 +02:00
|
|
|
case is_whitelisted(LServer, Addr) of
|
|
|
|
true ->
|
2017-01-09 15:02:17 +01:00
|
|
|
State;
|
2015-04-18 11:08:05 +02:00
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
BanLifetime = mod_fail2ban_opt:c2s_auth_ban_lifetime(LServer),
|
|
|
|
MaxFailures = mod_fail2ban_opt:c2s_max_auth_failures(LServer),
|
2019-07-17 21:15:56 +02:00
|
|
|
UnbanTS = current_time() + BanLifetime,
|
2017-01-09 15:02:17 +01:00
|
|
|
Attempts = case ets:lookup(failed_auth, Addr) of
|
2015-04-18 11:08:05 +02:00
|
|
|
[{Addr, N, _, _}] ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ets:insert(failed_auth,
|
|
|
|
{Addr, N+1, UnbanTS, MaxFailures}),
|
|
|
|
N+1;
|
2015-04-18 11:08:05 +02:00
|
|
|
[] ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ets:insert(failed_auth,
|
|
|
|
{Addr, 1, UnbanTS, MaxFailures}),
|
|
|
|
1
|
2016-08-09 09:56:32 +02:00
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
if Attempts >= MaxFailures ->
|
|
|
|
log_and_disconnect(State, Attempts, UnbanTS);
|
|
|
|
true ->
|
|
|
|
State
|
|
|
|
end
|
2014-08-17 15:38:38 +02:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_auth_result(#{ip := {Addr, _}} = State, true, _User) ->
|
|
|
|
ets:delete(failed_auth, Addr),
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_stream_started(ejabberd_c2s:state(), stream_start())
|
|
|
|
-> ejabberd_c2s:state() | {stop, ejabberd_c2s:state()}.
|
|
|
|
c2s_stream_started(#{ip := {Addr, _}} = State, _) ->
|
2014-08-15 14:13:04 +02:00
|
|
|
case ets:lookup(failed_auth, Addr) of
|
2014-08-17 15:38:38 +02:00
|
|
|
[{Addr, N, TS, MaxFailures}] when N >= MaxFailures ->
|
2019-07-17 21:15:56 +02:00
|
|
|
case TS > current_time() of
|
2014-08-17 15:38:38 +02:00
|
|
|
true ->
|
2017-01-09 15:02:17 +01:00
|
|
|
log_and_disconnect(State, N, TS);
|
2014-08-17 15:38:38 +02:00
|
|
|
false ->
|
|
|
|
ets:delete(failed_auth, Addr),
|
2017-01-09 15:02:17 +01:00
|
|
|
State
|
2014-08-17 15:38:38 +02:00
|
|
|
end;
|
2014-08-15 11:40:04 +02:00
|
|
|
_ ->
|
2017-01-09 15:02:17 +01:00
|
|
|
State
|
2014-08-15 11:40:04 +02:00
|
|
|
end.
|
2014-08-17 15:38:38 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_mod callbacks
|
|
|
|
%%====================================================================
|
|
|
|
start(Host, Opts) ->
|
2017-11-13 12:34:59 +01:00
|
|
|
catch ets:new(failed_auth, [named_table, public,
|
|
|
|
{heir, erlang:group_leader(), none}]),
|
2018-11-30 13:33:39 +01:00
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, Host, Opts).
|
2014-08-17 15:38:38 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2018-11-30 13:33:39 +01:00
|
|
|
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
|
|
|
|
false ->
|
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec());
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, Host).
|
2014-08-17 15:38:38 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2014-08-17 15:38:38 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
2019-08-04 20:46:18 +02:00
|
|
|
init([Host|_]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2014-08-17 15:38:38 +02:00
|
|
|
ejabberd_hooks:add(c2s_auth_result, Host, ?MODULE, c2s_auth_result, 100),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_stream_started, Host, ?MODULE, c2s_stream_started, 100),
|
2014-08-17 15:38:38 +02:00
|
|
|
erlang:send_after(?CLEAN_INTERVAL, self(), clean),
|
|
|
|
{ok, #state{host = Host}}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2014-08-17 15:38:38 +02:00
|
|
|
|
|
|
|
handle_cast(_Msg, State) ->
|
2019-07-12 10:55:36 +02:00
|
|
|
?WARNING_MSG("Unexpected cast = ~p", [_Msg]),
|
2014-08-17 15:38:38 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_info(clean, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Cleaning ~p ETS table", [failed_auth]),
|
2019-07-17 21:15:56 +02:00
|
|
|
Now = current_time(),
|
2014-08-17 15:38:38 +02:00
|
|
|
ets:select_delete(
|
|
|
|
failed_auth,
|
|
|
|
ets:fun2ms(fun({_, _, UnbanTS, _}) -> UnbanTS =< Now end)),
|
|
|
|
erlang:send_after(?CLEAN_INTERVAL, self(), clean),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
2019-07-12 10:55:36 +02:00
|
|
|
?WARNING_MSG("Unexpected info = ~p", [_Info]),
|
2014-08-17 15:38:38 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, #state{host = Host}) ->
|
|
|
|
ejabberd_hooks:delete(c2s_auth_result, Host, ?MODULE, c2s_auth_result, 100),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_stream_started, Host, ?MODULE, c2s_stream_started, 100),
|
2017-02-24 14:31:39 +01:00
|
|
|
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
|
2014-08-17 15:38:38 +02:00
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
ets:delete(failed_auth)
|
|
|
|
end.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2018-11-30 13:33:39 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% ejabberd command callback.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec get_commands_spec() -> [ejabberd_commands()].
|
|
|
|
get_commands_spec() ->
|
|
|
|
[#ejabberd_commands{name = unban_ip, tags = [accounts],
|
|
|
|
desc = "Remove banned IP addresses from the fail2ban table",
|
|
|
|
longdesc = "Accepts an IP address with a network mask. "
|
|
|
|
"Returns the number of unbanned addresses, or a negative integer if there were any error.",
|
|
|
|
module = ?MODULE, function = unban,
|
|
|
|
args = [{address, binary}],
|
|
|
|
args_example = [<<"::FFFF:127.0.0.1/128">>],
|
|
|
|
args_desc = ["IP address, optionally with network mask."],
|
|
|
|
result_example = 3,
|
|
|
|
result_desc = "Amount of unbanned entries, or negative in case of error.",
|
|
|
|
result = {unbanned, integer}}].
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec unban(binary()) -> integer().
|
2018-11-30 13:33:39 +01:00
|
|
|
unban(S) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case misc:parse_ip_mask(S) of
|
|
|
|
{ok, {Net, Mask}} ->
|
2018-11-30 13:33:39 +01:00
|
|
|
unban(Net, Mask);
|
|
|
|
error ->
|
|
|
|
?WARNING_MSG("Invalid network address when trying to unban: ~p", [S]),
|
|
|
|
-1
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec unban(inet:ip_address(), 0..128) -> non_neg_integer().
|
2018-11-30 13:33:39 +01:00
|
|
|
unban(Net, Mask) ->
|
|
|
|
ets:foldl(
|
|
|
|
fun({Addr, _, _, _}, Acc) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case misc:match_ip_mask(Addr, Net, Mask) of
|
2018-11-30 13:33:39 +01:00
|
|
|
true ->
|
|
|
|
ets:delete(failed_auth, Addr),
|
|
|
|
Acc+1;
|
|
|
|
false -> Acc
|
|
|
|
end
|
2019-06-14 11:33:26 +02:00
|
|
|
end, 0, failed_auth).
|
2018-11-30 13:33:39 +01:00
|
|
|
|
2014-08-17 15:38:38 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec log_and_disconnect(ejabberd_c2s:state(), pos_integer(), non_neg_integer())
|
2017-01-09 15:02:17 +01:00
|
|
|
-> {stop, ejabberd_c2s:state()}.
|
|
|
|
log_and_disconnect(#{ip := {Addr, _}, lang := Lang} = State, Attempts, UnbanTS) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
IP = misc:ip_to_list(Addr),
|
2017-01-09 15:02:17 +01:00
|
|
|
UnbanDate = format_date(
|
2019-07-17 21:15:56 +02:00
|
|
|
calendar:now_to_universal_time(msec_to_now(UnbanTS))),
|
2019-06-22 16:08:45 +02:00
|
|
|
Format = ?T("Too many (~p) failed authentications "
|
2020-01-22 12:52:30 +01:00
|
|
|
"from this IP address (~s). The address "
|
|
|
|
"will be unblocked at ~s UTC"),
|
2017-01-09 15:02:17 +01:00
|
|
|
Args = [Attempts, IP, UnbanDate],
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Connection attempt from blacklisted IP ~ts: ~ts",
|
2018-09-19 18:33:33 +02:00
|
|
|
[IP, io_lib:fwrite(Format, Args)]),
|
2017-01-09 15:02:17 +01:00
|
|
|
Err = xmpp:serr_policy_violation({Format, Args}, Lang),
|
|
|
|
{stop, ejabberd_c2s:send(State, Err)}.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec is_whitelisted(binary(), inet:ip_address()) -> boolean().
|
2015-04-18 11:08:05 +02:00
|
|
|
is_whitelisted(Host, Addr) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Access = mod_fail2ban_opt:access(Host),
|
2015-04-18 11:08:05 +02:00
|
|
|
acl:match_rule(Host, Access, Addr) == allow.
|
|
|
|
|
2019-07-17 21:15:56 +02:00
|
|
|
-spec msec_to_now(pos_integer()) -> erlang:timestamp().
|
|
|
|
msec_to_now(MSecs) ->
|
|
|
|
Secs = MSecs div 1000,
|
2016-01-05 12:29:13 +01:00
|
|
|
{Secs div 1000000, Secs rem 1000000, 0}.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec format_date(calendar:datetime()) -> iolist().
|
2014-08-17 15:38:38 +02:00
|
|
|
format_date({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
|
|
|
io_lib:format("~2..0w:~2..0w:~2..0w ~2..0w.~2..0w.~4..0w",
|
|
|
|
[Hour, Minute, Second, Day, Month, Year]).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-07-17 21:15:56 +02:00
|
|
|
current_time() ->
|
|
|
|
erlang:system_time(millisecond).
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(c2s_auth_ban_lifetime) ->
|
2019-07-17 21:15:56 +02:00
|
|
|
econf:timeout(second);
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(c2s_max_auth_failures) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(_Host) ->
|
|
|
|
[{access, none},
|
2019-07-17 21:15:56 +02:00
|
|
|
{c2s_auth_ban_lifetime, timer:hours(1)},
|
2018-01-23 08:54:52 +01:00
|
|
|
{c2s_max_auth_failures, 20}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("The module bans IPs that show the malicious signs. "
|
|
|
|
"Currently only C2S authentication failures are detected."), "",
|
|
|
|
?T("Unlike the standalone program, 'mod_fail2ban' clears the "
|
|
|
|
"record of authentication failures after some time since the "
|
|
|
|
"first failure or on a successful authentication. "
|
|
|
|
"It also does not simply block network traffic, but "
|
2020-04-08 18:49:41 +02:00
|
|
|
"provides the client with a descriptive error message."), "",
|
|
|
|
?T("WARNING: You should not use this module behind a proxy or load "
|
|
|
|
"balancer. ejabberd will see the failures as coming from the "
|
|
|
|
"load balancer and, when the threshold of auth failures is "
|
|
|
|
"reached, will reject all connections coming from the load "
|
|
|
|
"balancer. You can lock all your user base out of ejabberd "
|
|
|
|
"when using this module behind a proxy.")],
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
|
|
|
[{access,
|
|
|
|
#{value => ?T("AccessName"),
|
|
|
|
desc =>
|
|
|
|
?T("Specify an access rule for whitelisting IP "
|
|
|
|
"addresses or networks. If the rule returns 'allow' "
|
|
|
|
"for a given IP address, that address will never be "
|
|
|
|
"banned. The 'AccessName' should be of type 'ip'. "
|
|
|
|
"The default value is 'none'.")}},
|
|
|
|
{c2s_auth_ban_lifetime,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
|
|
|
?T("The lifetime of the IP ban caused by too many "
|
|
|
|
"C2S authentication failures. The default value is "
|
|
|
|
"'1' hour.")}},
|
|
|
|
{c2s_max_auth_failures,
|
|
|
|
#{value => ?T("Number"),
|
|
|
|
desc =>
|
|
|
|
?T("The number of C2S authentication failures to "
|
|
|
|
"trigger the IP ban. The default value is '20'.")}}]}.
|