Browse Source

kamailio.cfg: added flood detection

- can be enabled via #!define WITH_ANTIFLOOD
- uses pike to detect flood and htable to ban the IP for a while
Daniel-Constantin Mierla 15 năm trước cách đây
mục cha
commit
f77093de4f
1 tập tin đã thay đổi với 41 bổ sung0 xóa
  1. 41 0
      etc/kamailio.cfg

+ 41 - 0
etc/kamailio.cfg

@@ -59,6 +59,11 @@
 #     - adjust CFGDIR/tls.cfg as needed
 #     - adjust CFGDIR/tls.cfg as needed
 #     - define WITH_TLS
 #     - define WITH_TLS
 #
 #
+# *** To enable anti-flood detection execute:
+#     - adjust pike and htable=>ipban settings as needed (default is
+#       block if more than 16 requests in 2 seconds and ban for 300 seconds)
+#     - define WITH_ANTIFLOOD
+#
 # *** To enhance accounting execute:
 # *** To enhance accounting execute:
 #     - enable mysql
 #     - enable mysql
 #     - define WITH_ACCDB
 #     - define WITH_ACCDB
@@ -223,6 +228,11 @@ loadmodule "rtpproxy.so"
 loadmodule "tls.so"
 loadmodule "tls.so"
 #!endif
 #!endif
 
 
+#!ifdef WITH_ANTIFLOOD
+loadmodule "htable.so"
+loadmodule "pike.so"
+#!endif
+
 # ----------------- setting module-specific parameters ---------------
 # ----------------- setting module-specific parameters ---------------
 
 
 
 
@@ -353,6 +363,16 @@ modparam("usrloc", "nat_bflag", FLB_NATB)
 modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
 modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
 #!endif
 #!endif
 
 
+#!ifdef WITH_ANTIFLOOD
+# ----- pike params -----
+modparam("pike", "sampling_time_unit", 2)
+modparam("pike", "reqs_density_per_unit", 16)
+modparam("pike", "remove_latency", 4)
+
+# ----- htable params -----
+# ip ban htable with autoexpire after 5 minutes
+modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
+#!endif
 
 
 ####### Routing Logic ########
 ####### Routing Logic ########
 
 
@@ -450,6 +470,27 @@ route[RELAY] {
 
 
 # Per SIP request initial checks
 # Per SIP request initial checks
 route[REQINIT] {
 route[REQINIT] {
+#!ifdef WITH_ANTIFLOOD
+	# flood dection from same IP and traffic ban for a while
+	# be sure you exclude checking trusted peers, such as pstn gateways
+	# - local host excluded (e.g., loop to self)
+	if(src_ip!=myself)
+	{
+		if($sht(ipban=>$si)!=$null)
+		{
+			# ip is already blocked
+			xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
+			exit;
+		}
+		if (!pike_check_req())
+		{
+			xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
+			$sht(ipban=>$si) = 1;
+			exit;
+		}
+	}
+#!endif
+
 	if (!mf_process_maxfwd_header("10")) {
 	if (!mf_process_maxfwd_header("10")) {
 		sl_send_reply("483","Too Many Hops");
 		sl_send_reply("483","Too Many Hops");
 		exit;
 		exit;