After stress-testing Kamailio with sipflood tool from sippts suite (which deserves another article), not so good outcome was faced.
Using CentOS 7 default OpenSSL library (1.0.2k-fips) with using Kamailio 5.4-5.6 with TLS transport, it's quite easy to get a segfault inside tls routines. I've found that roughly 10 000 OPTIONS packets with 200 threads is enough to ruin Kamailio process.
Basically, you can DoS the whole server regardless of it's power just with a single mid-range computer.
Solution was found with using Kamailio 5.6, but with tlsa flavour and latest openssl 1.1.x compiled.
Turns out it's a really simple process.
As we're gonna need to compile Kamailio anyway, assume, that we have all necessary packets for build already on the system.
First - we need to get openssl sources:
# cd /usr/src
# wget https://www.openssl.org/source/openssl-1.1.<latest>.tar.gz
# tar xvf https://www.openssl.org/source/openssl-1.1.<latest>.tar.gz
# cd openssl-1.1.<latest>
# ./config
# make
(Optionally) Here we can make sure that this release is passing tests
# yum install perl-Test-Simple
# make test
Next step - point Kamailio to newly compiled openssl
# cd /usr/src
# wget https://www.kamailio.org/pub/kamailio/5.6.<latest>/src/kamailio-5.6.<latest>_src.tar.gz
# tar xvf kamailio-5.6.<latest>_src.tar.gz
# cd kamailio-5.6.<latest>
# sed -i "s?LIBSSL_STATIC_SRCLIB \?= no?LIBSSL_STATIC_SRCLIB \?= yes?g" ./src/modules/tlsa/Makefile
# sed -i "s?LIBSSL_STATIC_SRCPATH \?= /usr/local/src/openssl?LIBSSL_STATIC_SRCPATH \?= /usr/src/openssl-1.1.<latest>?g" ./src/modules/tlsa/Makefile
...
Than goes your usual Kamailio compiling and don't forget to replace all "tls" module mentions in kamailio.cfg to "tlsa"
Results are much better. But than I've faced, that it's possible to "eat" all TCP connections on Kamailio server with this type of flood.
First - ulimit. Never underestimate defaults.
# ulimit -n unlimited
Next steps - tune TCP stack.
Disclamer: next provided options are discussable and was not found by me and need to be adjusted to your case
kamailio.conf
...
tcp_connection_lifetime=3605
tcp_max_connections=4096
tls_max_connections=4096
tcp_connect_timeout=5
tcp_async=yes
tcp_keepidle=5
open_files_limit=4096
...
/etc/sysctl.conf
...
# To increase the amount of memory available for socket input/output queues
net.ipv4.tcp_rmem = 4096 25165824 25165824
net.core.rmem_max = 25165824
net.core.rmem_default = 25165824
net.ipv4.tcp_wmem = 4096 65536 25165824
net.core.wmem_max = 25165824
net.core.wmem_default = 65536
net.core.optmem_max = 25165824
# To limit the maximum number of requests queued to a listen socket
net.core.somaxconn = 128
# Tells TCP to instead make decisions that would prefer lower latency.
net.ipv4.tcp_low_latency=1
# Optional (it will increase performance)
net.core.netdev_max_backlog = 1000
net.ipv4.tcp_max_syn_backlog = 128
...
This will help, but not fully (at least in my case, I've must miss something and comments here are really welcomed)
As the second part I've decided to go with Fail2Ban and block flood on iptables level.
Setup is quite simple as well.
First - make sure Kamailio will log flood attempts:
kamailio.conf
...
loadmodule "pike.so"
modparam("pike", "sampling_time_unit", 2)
modparam("pike", "reqs_density_per_unit", 30)
modparam("pike", "remove_latency", 120)
...
if (!pike_check_req()) {
xlog("L_ALERT", "[SIP-FIREWALL][FAIL2BAN] $si\n");
$sht(ipban=>$si) = 1;
if ($proto != 'udp') {
tcp_close_connection();
}
drop;
}
...
Next - install and configure Fail2Ban
# yum install -y fail2ban
/etc/fail2ban/jail.local
[DEFAULT]
# Ban hosts for one hour:
bantime = 3600
# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport
action = %(action_mwl)s
[cernphone-iptables]
enabled = true
filter = mypbx
action = iptables-mypbx[name=mypbx, protocol=tcp, blocktype='REJECT --reject-with tcp-reset']
sendmail[sender=<sender_addr>, dest=<dest_addr> sendername=Fail2Ban]
logpath = <your_kamailio_logfile>
maxretry = 1
bantime = 3600s
findtime = 10s
/etc/fail2ban/action.d/iptables-mypbx.conf
[INCLUDES]
before = iptables-common.conf
[Definition]
actionstart = <iptables> -N f2b-<name>
<iptables> -A f2b-<name> -j <returntype>
<iptables> -I <chain> -p <protocol> -j f2b-<name>
actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
<actionflush>
<iptables> -X f2b-<name>
actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
actionban = <iptables> -I f2b-<name> 1 -s <ip> -p <protocol> -j <blocktype>
actionunban = <iptables> -D f2b-<name> -s <ip> -p <protocol> -j <blocktype>
/etc/fail2ban/filter.d/mypbx.local
[Definition]
# filter for kamailio messages
failregex = \[SIP-FIREWALL\]\[FAIL2BAN\] <HOST>$
# systemctl enable fail2ban
# systemctl start fail2ban
In this case we will get host banned on iptables level.