Skip to content

Commit 7316f2d

Browse files
Easton97-Jensjens
authored andcommitted
Refine libinjection updates and test coverage
- update adapter and detection sources - improve multithreaded unit tests - add SQLi/XSS logging changes - hide test override symbols - fix linker visibility for test hooks
1 parent 6f26d6b commit 7316f2d

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

src/operators/detect_sqli.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
#include "src/operators/operator.h"
2323
#include "src/operators/libinjection_utils.h"
2424
#include "src/operators/libinjection_adapter.h"
25+
#include "src/utils/string.h"
2526
#include "libinjection/src/libinjection_error.h"
2627

2728
namespace modsecurity::operators {
2829

2930
bool DetectSQLi::evaluate(Transaction *t, RuleWithActions *rule,
3031
const std::string& input, RuleMessage &ruleMessage) {
32+
#ifndef NO_LOGS
33+
const std::string loggable_input =
34+
utils::string::limitTo(80, utils::string::toHexIfNeeded(input));
35+
#endif
3136

3237
std::array<char, 8> fingerprint{};
3338

@@ -42,9 +47,11 @@ bool DetectSQLi::evaluate(Transaction *t, RuleWithActions *rule,
4247
case LIBINJECTION_RESULT_TRUE:
4348
t->m_matched.emplace_back(fingerprint.data());
4449

50+
#ifndef NO_LOGS
4551
ms_dbg_a(t, 4,
4652
std::string("detected SQLi using libinjection with fingerprint '")
47-
+ fingerprint.data() + "' at: '" + input + "'");
53+
+ fingerprint.data() + "' at: '" + loggable_input + "'");
54+
#endif
4855

4956
if (rule != nullptr && rule->hasCaptureAction()) {
5057
t->m_collections.m_tx_collection->storeOrUpdateFirst(
@@ -57,11 +64,13 @@ bool DetectSQLi::evaluate(Transaction *t, RuleWithActions *rule,
5764
break;
5865

5966
case LIBINJECTION_RESULT_ERROR:
67+
#ifndef NO_LOGS
6068
ms_dbg_a(t, 4,
6169
std::string("libinjection parser error during SQLi analysis (")
6270
+ libinjectionResultToString(sqli_result)
6371
+ "); treating as match (fail-safe). Input: '"
64-
+ input + "'");
72+
+ loggable_input + "'");
73+
#endif
6574

6675
if (rule != nullptr && rule->hasCaptureAction()) {
6776
t->m_collections.m_tx_collection->storeOrUpdateFirst(
@@ -77,9 +86,11 @@ bool DetectSQLi::evaluate(Transaction *t, RuleWithActions *rule,
7786
break;
7887

7988
case LIBINJECTION_RESULT_FALSE:
89+
#ifndef NO_LOGS
8090
ms_dbg_a(t, 9,
8191
std::string("libinjection was not able to find any SQLi in: ")
82-
+ input);
92+
+ loggable_input);
93+
#endif
8394
break;
8495
}
8596

src/operators/detect_xss.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
#include "src/operators/operator.h"
2121
#include "src/operators/libinjection_utils.h"
2222
#include "src/operators/libinjection_adapter.h"
23+
#include "src/utils/string.h"
2324
#include "libinjection/src/libinjection_error.h"
2425

2526
namespace modsecurity::operators {
2627

2728
bool DetectXSS::evaluate(Transaction *t, RuleWithActions *rule,
2829
const std::string& input, RuleMessage &ruleMessage) {
30+
#ifndef NO_LOGS
31+
const std::string loggable_input =
32+
utils::string::limitTo(80, utils::string::toHexIfNeeded(input));
33+
#endif
2934

3035
const injection_result_t xss_result =
3136
runLibinjectionXSS(input.c_str(), input.length());
@@ -44,20 +49,25 @@ bool DetectXSS::evaluate(Transaction *t, RuleWithActions *rule,
4449
break;
4550

4651
case LIBINJECTION_RESULT_ERROR:
52+
#ifndef NO_LOGS
4753
ms_dbg_a(t, 4,
4854
std::string("libinjection parser error during XSS analysis (")
4955
+ libinjectionResultToString(xss_result)
5056
+ "); treating as match (fail-safe). Input: "
51-
+ input);
57+
+ loggable_input);
58+
#endif
5259
if (rule != nullptr && rule->hasCaptureAction()) {
5360
t->m_collections.m_tx_collection->storeOrUpdateFirst("0", input);
5461
ms_dbg_a(t, 7, std::string("Added DetectXSS error input TX.0: ") + input);
5562
}
5663
break;
5764

5865
case LIBINJECTION_RESULT_FALSE:
66+
#ifndef NO_LOGS
5967
ms_dbg_a(t, 9,
60-
std::string("libinjection was not able to find any XSS in: ") + input);
68+
std::string("libinjection was not able to find any XSS in: ")
69+
+ loggable_input);
70+
#endif
6171
break;
6272
}
6373

src/operators/libinjection_adapter.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
/*
22
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2021 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+
*
314
*/
415

516
#include "src/operators/libinjection_adapter.h"

src/operators/libinjection_adapter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
/*
22
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2021 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+
*
314
*/
415

516
#ifndef SRC_OPERATORS_LIBINJECTION_ADAPTER_H_

test/unit/unit.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ UnitTestResult perform_unit_test_once(const UnitTest &t, modsecurity::Transactio
175175
template<typename TestType>
176176
UnitTestResult perform_unit_test_multithreaded(const UnitTest &t,
177177
modsecurity_test::ModSecurityTestContext &context) {
178+
(void)context;
178179

179180
constexpr auto NUM_THREADS = 50;
180181
constexpr auto ITERATIONS = 5'000;
@@ -189,9 +190,11 @@ UnitTestResult perform_unit_test_multithreaded(const UnitTest &t,
189190
{
190191
auto &result = results[i];
191192
threads[i] = std::thread(
192-
[&item, &t, &result, &context]()
193+
[&item, &t, &result]()
193194
{
194-
auto transaction = context.create_transaction();
195+
modsecurity_test::ModSecurityTestContext thread_context(
196+
"ModSecurity-unit mtstress-thread");
197+
auto transaction = thread_context.create_transaction();
195198
for (auto j = 0; j != ITERATIONS; ++j)
196199
result = TestType::eval(*item.get(), t, transaction);
197200
});

0 commit comments

Comments
 (0)