Skip to content

Commit a15f881

Browse files
author
Felipe Zimmerle
committed
Honor the SecRuleEngine while filtering connections
The SecRuleEngine has the capability to Enable, Disable or even to place the ModSecurity in DetectionOnly mode. The SecReadStateLimit and SecWriteStateLimit were not honoring such state, due the fact that our configuration belongs to requests not to connections, the only struct that exists while those filters are placed. By adding a global variable "conn_limits_filter_state" we are now able to identify the current state of the ModSecurity, once the configuration is loaded this variable is set and used by the connections filters.
1 parent 0037a07 commit a15f881

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

apache2/apache2_config.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,18 +2144,30 @@ static const char *cmd_rule(cmd_parms *cmd, void *_dcfg,
21442144
static const char *cmd_rule_engine(cmd_parms *cmd, void *_dcfg, const char *p1)
21452145
{
21462146
directory_config *dcfg = (directory_config *)_dcfg;
2147+
21472148
if (dcfg == NULL) return NULL;
21482149

2149-
if (strcasecmp(p1, "on") == 0) dcfg->is_enabled = MODSEC_ENABLED;
2150-
else
2151-
if (strcasecmp(p1, "off") == 0) dcfg->is_enabled = MODSEC_DISABLED;
2152-
else
2153-
if (strcasecmp(p1, "detectiononly") == 0) {
2150+
if (strcasecmp(p1, "on") == 0)
2151+
{
2152+
dcfg->is_enabled = MODSEC_ENABLED;
2153+
}
2154+
else if (strcasecmp(p1, "off") == 0)
2155+
{
2156+
dcfg->is_enabled = MODSEC_DISABLED;
2157+
}
2158+
else if (strcasecmp(p1, "detectiononly") == 0)
2159+
{
21542160
dcfg->is_enabled = MODSEC_DETECTION_ONLY;
21552161
dcfg->of_limit_action = RESPONSE_BODY_LIMIT_ACTION_PARTIAL;
21562162
dcfg->if_limit_action = REQUEST_BODY_LIMIT_ACTION_PARTIAL;
2157-
} else
2158-
return apr_psprintf(cmd->pool, "ModSecurity: Invalid value for SecRuleEngine: %s", p1);
2163+
}
2164+
else
2165+
{
2166+
return apr_psprintf(cmd->pool, "ModSecurity: Invalid value for " \
2167+
"SecRuleEngine: %s", p1);
2168+
}
2169+
2170+
conn_limits_filter_state = dcfg->is_enabled;
21592171

21602172
return NULL;
21612173
}

apache2/mod_security2.c

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ unsigned long int DSOLOCAL msc_pcre_match_limit_recursion = 0;
6363

6464
int DSOLOCAL status_engine_state = STATUS_ENGINE_DISABLED;
6565

66+
int DSOLOCAL conn_limits_filter_state = 0;
67+
6668
unsigned long int DSOLOCAL conn_read_state_limit = 0;
6769
TreeRoot DSOLOCAL *conn_read_state_whitelist = 0;
6870
TreeRoot DSOLOCAL *conn_read_state_suspicious_list = 0;
@@ -1419,27 +1421,28 @@ static int hook_connection_early(conn_rec *conn)
14191421
}
14201422
}
14211423

1422-
14231424
if (conn_read_state_limit > 0 && ip_count_r > conn_read_state_limit)
14241425
{
14251426
if (conn_read_state_suspicious_list &&
14261427
(tree_contains_ip(conn->pool,
14271428
conn_read_state_suspicious_list, client_ip, NULL, &error_msg) <= 0))
14281429
{
1429-
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1430-
"ModSecurity: Too many threads [%ld] of %ld allowed in " \
1431-
"READ state from %s - There is a suspission list but " \
1432-
"that IP is not part of it, access granted", ip_count_r,
1433-
conn_read_state_limit, client_ip);
1430+
if (conn_limits_filter_state == MODSEC_DETECTION_ONLY)
1431+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1432+
"ModSecurity: Too many threads [%ld] of %ld allowed " \
1433+
"in READ state from %s - There is a suspission list " \
1434+
"but that IP is not part of it, access granted",
1435+
ip_count_r, conn_read_state_limit, client_ip);
14341436
}
1435-
14361437
else if (tree_contains_ip(conn->pool,
14371438
conn_read_state_whitelist, client_ip, NULL, &error_msg) > 0)
14381439
{
1439-
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1440-
"ModSecurity: Too many threads [%ld] of %ld allowed in " \
1441-
"READ state from %s - Ip is on whitelist, access granted",
1442-
ip_count_r, conn_read_state_limit, client_ip);
1440+
if (conn_limits_filter_state == MODSEC_DETECTION_ONLY)
1441+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1442+
"ModSecurity: Too many threads [%ld] of %ld allowed " \
1443+
"in READ state from %s - Ip is on whitelist, access " \
1444+
"granted", ip_count_r, conn_read_state_limit,
1445+
client_ip);
14431446
}
14441447
else
14451448
{
@@ -1448,7 +1451,9 @@ static int hook_connection_early(conn_rec *conn)
14481451
"threads [%ld] of %ld allowed in READ state from %s - " \
14491452
"Possible DoS Consumption Attack [Rejected]", ip_count_r,
14501453
conn_read_state_limit, client_ip);
1451-
return OK;
1454+
1455+
if (conn_limits_filter_state == MODSEC_ENABLED)
1456+
return OK;
14521457
}
14531458
}
14541459

@@ -1458,19 +1463,22 @@ static int hook_connection_early(conn_rec *conn)
14581463
(tree_contains_ip(conn->pool,
14591464
conn_write_state_suspicious_list, client_ip, NULL, &error_msg) <= 0))
14601465
{
1461-
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1462-
"ModSecurity: Too many threads [%ld] of %ld allowed in " \
1463-
"WRITE state from %s - There is a suspission list but " \
1464-
"that IP is not part of it, access granted", ip_count_w,
1465-
conn_read_state_limit, client_ip);
1466+
if (conn_limits_filter_state == MODSEC_DETECTION_ONLY)
1467+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1468+
"ModSecurity: Too many threads [%ld] of %ld allowed " \
1469+
"in WRITE state from %s - There is a suspission list " \
1470+
"but that IP is not part of it, access granted",
1471+
ip_count_w, conn_read_state_limit, client_ip);
14661472
}
14671473
else if (tree_contains_ip(conn->pool,
14681474
conn_write_state_whitelist, client_ip, NULL, &error_msg) > 0)
14691475
{
1470-
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1471-
"ModSecurity: Too many threads [%ld] of %ld allowed in " \
1472-
"WRITE state from %s - Ip is on whitelist, access granted",
1473-
ip_count_w, conn_read_state_limit, client_ip);
1476+
if (conn_limits_filter_state == MODSEC_DETECTION_ONLY)
1477+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1478+
"ModSecurity: Too many threads [%ld] of %ld allowed " \
1479+
"in WRITE state from %s - Ip is on whitelist, " \
1480+
"access granted", ip_count_w, conn_read_state_limit,
1481+
client_ip);
14741482
}
14751483
else
14761484
{
@@ -1479,7 +1487,9 @@ static int hook_connection_early(conn_rec *conn)
14791487
"threads [%ld] of %ld allowed in WRITE state from %s - " \
14801488
"Possible DoS Consumption Attack [Rejected]", ip_count_w,
14811489
conn_write_state_limit, client_ip);
1482-
return OK;
1490+
1491+
if (!conn_limits_filter_state == MODSEC_ENABLED)
1492+
return OK;
14831493
}
14841494
}
14851495
}

apache2/modsecurity.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ extern DSOLOCAL unsigned long int msc_pcre_match_limit_recursion;
145145

146146
extern DSOLOCAL int status_engine_state;
147147

148+
extern DSOLOCAL int conn_limits_filter_state;
149+
148150
extern DSOLOCAL unsigned long int conn_read_state_limit;
149151
extern DSOLOCAL TreeRoot *conn_read_state_whitelist;
150152
extern DSOLOCAL TreeRoot *conn_read_state_suspicious_list;

0 commit comments

Comments
 (0)