Skip to content

Commit 2cde193

Browse files
authored
Merge pull request #2680 from SpiderLabs/v3/dev/issue_2606_a
Add ctl:auditengine action support
2 parents cc83a1b + 2d51efa commit 2cde193

File tree

15 files changed

+5049
-4840
lines changed

15 files changed

+5049
-4840
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ TESTS+=test/test-cases/regression/action-ctl_request_body_access.json
9696
TESTS+=test/test-cases/regression/action-ctl_request_body_processor.json
9797
TESTS+=test/test-cases/regression/action-ctl_request_body_processor_urlencoded.json
9898
TESTS+=test/test-cases/regression/action-ctl_rule_engine.json
99+
TESTS+=test/test-cases/regression/action-ctl_audit_engine.json
99100
TESTS+=test/test-cases/regression/action-ctl_rule_remove_by_id.json
100101
TESTS+=test/test-cases/regression/action-ctl_rule_remove_by_tag.json
101102
TESTS+=test/test-cases/regression/action-ctl_rule_remove_target_by_id.json

headers/modsecurity/audit_log.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
#ifndef HEADERS_MODSECURITY_AUDIT_LOG_H_
2323
#define HEADERS_MODSECURITY_AUDIT_LOG_H_
2424

25-
#include "modsecurity/transaction.h"
26-
2725

2826
#ifdef __cplusplus
2927

3028
namespace modsecurity {
29+
class Transaction;
3130
namespace audit_log {
3231
namespace writer {
3332
class Writer;
@@ -177,6 +176,10 @@ class AuditLog {
177176
static int addParts(int parts, const std::string& new_parts);
178177
static int removeParts(int parts, const std::string& new_parts);
179178

179+
void setCtlAuditEngineActive() {
180+
m_ctlAuditEngineActive = true;
181+
}
182+
180183
bool merge(AuditLog *from, std::string *error);
181184

182185
std::string m_path1;
@@ -203,6 +206,7 @@ class AuditLog {
203206
std::string m_relevant;
204207

205208
audit_log::writer::Writer *m_writer;
209+
bool m_ctlAuditEngineActive; // rules have at least one action On or RelevantOnly
206210
};
207211

208212

headers/modsecurity/transaction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef struct Rules_t RulesSet;
4949
#include "modsecurity/collection/collection.h"
5050
#include "modsecurity/variable_origin.h"
5151
#include "modsecurity/anchored_set_variable_translation_proxy.h"
52+
#include "modsecurity/audit_log.h"
5253

5354

5455
#ifndef NO_LOGS
@@ -529,6 +530,12 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
529530
*/
530531
std::list< std::pair<int, std::string> > m_auditLogModifier;
531532

533+
/**
534+
* This transaction's most recent action ctl:auditEngine
535+
*
536+
*/
537+
audit_log::AuditLog::AuditLogStatus m_ctlAuditEngine;
538+
532539
/**
533540
* This variable holds all the messages asked to be save by the utilization
534541
* of the actions: `log_data' and `msg'. These should be included on the

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ACTIONS = \
118118
actions/capture.cc \
119119
actions/chain.cc \
120120
actions/ctl/audit_log_parts.cc \
121+
actions/ctl/audit_engine.cc \
121122
actions/ctl/rule_engine.cc \
122123
actions/ctl/request_body_processor_json.cc \
123124
actions/ctl/request_body_processor_xml.cc \

src/actions/ctl/audit_engine.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2022 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include "src/actions/ctl/audit_engine.h"
17+
18+
#include <string>
19+
20+
#include "modsecurity/rules_set_properties.h"
21+
#include "modsecurity/rules_set.h"
22+
#include "modsecurity/transaction.h"
23+
24+
namespace modsecurity {
25+
namespace actions {
26+
namespace ctl {
27+
28+
29+
bool AuditEngine::init(std::string *error) {
30+
31+
std::string what(m_parser_payload, 12, m_parser_payload.size() - 12);
32+
33+
if (what == "on") {
34+
m_auditEngine = audit_log::AuditLog::AuditLogStatus::OnAuditLogStatus;
35+
} else if (what == "off") {
36+
m_auditEngine = audit_log::AuditLog::AuditLogStatus::OffAuditLogStatus;
37+
} else if (what == "relevantonly") {
38+
m_auditEngine = audit_log::AuditLog::AuditLogStatus::RelevantOnlyAuditLogStatus;
39+
} else {
40+
error->assign("Internal error. Expected: On, Off or RelevantOnly; " \
41+
"got: " + m_parser_payload);
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
bool AuditEngine::evaluate(RuleWithActions *rule, Transaction *transaction) {
49+
std::stringstream a;
50+
a << "Setting SecAuditEngine to ";
51+
a << std::to_string(m_auditEngine);
52+
a << " as requested by a ctl:auditEngine action";
53+
54+
ms_dbg_a(transaction, 8, a.str());
55+
56+
transaction->m_ctlAuditEngine = m_auditEngine;
57+
return true;
58+
}
59+
60+
61+
} // namespace ctl
62+
} // namespace actions
63+
} // namespace modsecurity

src/actions/ctl/audit_engine.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2022 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include <string>
17+
18+
#include "modsecurity/rules_set_properties.h"
19+
#include "modsecurity/actions/action.h"
20+
21+
#include "modsecurity/audit_log.h"
22+
23+
24+
#ifndef SRC_ACTIONS_CTL_AUDIT_ENGINE_H_
25+
#define SRC_ACTIONS_CTL_AUDIT_ENGINE_H_
26+
27+
namespace modsecurity {
28+
class Transaction;
29+
30+
namespace actions {
31+
namespace ctl {
32+
33+
34+
class AuditEngine : public Action {
35+
public:
36+
explicit AuditEngine(const std::string &action)
37+
: Action(action, RunTimeOnlyIfMatchKind),
38+
m_auditEngine(audit_log::AuditLog::AuditLogStatus::NotSetLogStatus) { }
39+
40+
bool init(std::string *error) override;
41+
bool evaluate(RuleWithActions *rule, Transaction *transaction) override;
42+
43+
audit_log::AuditLog::AuditLogStatus m_auditEngine;
44+
};
45+
46+
47+
} // namespace ctl
48+
} // namespace actions
49+
} // namespace modsecurity
50+
51+
#endif // SRC_ACTIONS_CTL_AUDIT_ENGINE_H_

src/audit_log/audit_log.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <fstream>
2323

24+
#include "modsecurity/transaction.h"
2425
#include "modsecurity/rule_message.h"
2526
#include "src/audit_log/writer/https.h"
2627
#include "src/audit_log/writer/parallel.h"
@@ -61,7 +62,8 @@ AuditLog::AuditLog()
6162
m_status(NotSetLogStatus),
6263
m_type(NotSetAuditLogType),
6364
m_relevant(""),
64-
m_writer(NULL) { }
65+
m_writer(NULL),
66+
m_ctlAuditEngineActive(false) { }
6567

6668

6769
AuditLog::~AuditLog() {
@@ -210,7 +212,8 @@ bool AuditLog::setType(AuditLogType audit_type) {
210212
bool AuditLog::init(std::string *error) {
211213
audit_log::writer::Writer *tmp_writer;
212214

213-
if (m_status == OffAuditLogStatus || m_status == NotSetLogStatus) {
215+
if ((m_status == OffAuditLogStatus || m_status == NotSetLogStatus)
216+
&& !m_ctlAuditEngineActive) {
214217
if (m_writer) {
215218
delete m_writer;
216219
m_writer = NULL;
@@ -275,7 +278,13 @@ bool AuditLog::saveIfRelevant(Transaction *transaction) {
275278

276279
bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) {
277280
bool saveAnyway = false;
278-
if (m_status == OffAuditLogStatus || m_status == NotSetLogStatus) {
281+
282+
AuditLogStatus transactionAuditLogStatus(m_status);
283+
if (transaction->m_ctlAuditEngine != NotSetLogStatus) {
284+
transactionAuditLogStatus = transaction->m_ctlAuditEngine;
285+
}
286+
287+
if (transactionAuditLogStatus == OffAuditLogStatus || transactionAuditLogStatus == NotSetLogStatus) {
279288
ms_dbg_a(transaction, 5, "Audit log engine was not set.");
280289
return true;
281290
}
@@ -287,7 +296,7 @@ bool AuditLog::saveIfRelevant(Transaction *transaction, int parts) {
287296
}
288297
}
289298

290-
if ((m_status == RelevantOnlyAuditLogStatus
299+
if ((transactionAuditLogStatus == RelevantOnlyAuditLogStatus
291300
&& this->isRelevant(transaction->m_httpCodeReturned) == false)
292301
&& saveAnyway == false) {
293302
ms_dbg_a(transaction, 9, "Return code `" +
@@ -353,6 +362,10 @@ bool AuditLog::merge(AuditLog *from, std::string *error) {
353362
m_format = from->m_format;
354363
}
355364

365+
if (from->m_ctlAuditEngineActive) {
366+
m_ctlAuditEngineActive = from->m_ctlAuditEngineActive;
367+
}
368+
356369
return init(error);
357370
}
358371

0 commit comments

Comments
 (0)