|
17 | 17 |
|
18 | 18 | #include <string> |
19 | 19 | #include <list> |
| 20 | +#include <array> |
20 | 21 |
|
21 | 22 | #include "src/operators/operator.h" |
22 | | -#include "libinjection/src/libinjection.h" |
23 | | - |
24 | | -namespace modsecurity { |
25 | | -namespace operators { |
| 23 | +#include "src/operators/libinjection_utils.h" |
| 24 | +#include "src/operators/libinjection_adapter.h" |
| 25 | +#include "src/utils/string.h" |
| 26 | +#include "libinjection/src/libinjection_error.h" |
26 | 27 |
|
| 28 | +namespace modsecurity::operators { |
27 | 29 |
|
28 | 30 | bool DetectSQLi::evaluate(Transaction *t, RuleWithActions *rule, |
29 | 31 | const std::string& input, RuleMessage &ruleMessage) { |
30 | | - char fingerprint[8]; |
31 | | - int issqli; |
| 32 | +#ifndef NO_LOGS |
| 33 | + const std::string loggable_input = |
| 34 | + utils::string::limitTo(80, utils::string::toHexIfNeeded(input)); |
| 35 | +#endif |
| 36 | + |
| 37 | + std::array<char, 8> fingerprint{}; |
32 | 38 |
|
33 | | - issqli = libinjection_sqli(input.c_str(), input.length(), fingerprint); |
| 39 | + const injection_result_t sqli_result = |
| 40 | + runLibinjectionSQLi(input.c_str(), input.length(), fingerprint.data()); |
34 | 41 |
|
35 | | - if (!t) { |
36 | | - goto tisempty; |
| 42 | + if (t == nullptr) { |
| 43 | + return isMaliciousLibinjectionResult(sqli_result); |
37 | 44 | } |
38 | 45 |
|
39 | | - if (issqli) { |
40 | | - t->m_matched.push_back(fingerprint); |
41 | | - ms_dbg_a(t, 4, "detected SQLi using libinjection with " \ |
42 | | - "fingerprint '" + std::string(fingerprint) + "' at: '" + |
43 | | - input + "'"); |
44 | | - if (rule && rule->hasCaptureAction()) { |
45 | | - t->m_collections.m_tx_collection->storeOrUpdateFirst( |
46 | | - "0", std::string(fingerprint)); |
47 | | - ms_dbg_a(t, 7, "Added DetectSQLi match TX.0: " + \ |
48 | | - std::string(fingerprint)); |
49 | | - } |
50 | | - } else { |
51 | | - ms_dbg_a(t, 9, "detected SQLi: not able to find an " \ |
52 | | - "inject on '" + input + "'"); |
| 46 | + switch (sqli_result) { |
| 47 | + case LIBINJECTION_RESULT_TRUE: |
| 48 | + t->m_matched.emplace_back(fingerprint.data()); |
| 49 | + |
| 50 | +#ifndef NO_LOGS |
| 51 | + ms_dbg_a(t, 4, |
| 52 | + std::string("detected SQLi using libinjection with fingerprint '") |
| 53 | + + fingerprint.data() + "' at: '" + loggable_input + "'"); |
| 54 | +#endif |
| 55 | + |
| 56 | + if (rule != nullptr && rule->hasCaptureAction()) { |
| 57 | + t->m_collections.m_tx_collection->storeOrUpdateFirst( |
| 58 | + "0", std::string(fingerprint.data())); |
| 59 | + |
| 60 | + ms_dbg_a(t, 7, |
| 61 | + std::string("Added DetectSQLi match TX.0: ") |
| 62 | + + fingerprint.data()); |
| 63 | + } |
| 64 | + break; |
| 65 | + |
| 66 | + case LIBINJECTION_RESULT_ERROR: |
| 67 | +#ifndef NO_LOGS |
| 68 | + ms_dbg_a(t, 4, |
| 69 | + std::string("libinjection parser error during SQLi analysis (") |
| 70 | + + libinjectionResultToString(sqli_result) |
| 71 | + + "); treating as match (fail-safe). Input: '" |
| 72 | + + loggable_input + "'"); |
| 73 | +#endif |
| 74 | + |
| 75 | + if (rule != nullptr && rule->hasCaptureAction()) { |
| 76 | + t->m_collections.m_tx_collection->storeOrUpdateFirst( |
| 77 | + "0", input); |
| 78 | + |
| 79 | + ms_dbg_a(t, 7, |
| 80 | + std::string("Added DetectSQLi error input TX.0: ") |
| 81 | + + input); |
| 82 | + } |
| 83 | + |
| 84 | + // Keep m_matched untouched for parser-error paths to avoid |
| 85 | + // introducing synthetic fingerprints for non-TRUE results. |
| 86 | + break; |
| 87 | + |
| 88 | + case LIBINJECTION_RESULT_FALSE: |
| 89 | +#ifndef NO_LOGS |
| 90 | + ms_dbg_a(t, 9, |
| 91 | + std::string("libinjection was not able to find any SQLi in: ") |
| 92 | + + loggable_input); |
| 93 | +#endif |
| 94 | + break; |
53 | 95 | } |
54 | 96 |
|
55 | | -tisempty: |
56 | | - return issqli != 0; |
| 97 | + return isMaliciousLibinjectionResult(sqli_result); |
57 | 98 | } |
58 | 99 |
|
59 | | - |
60 | | -} // namespace operators |
61 | | -} // namespace modsecurity |
| 100 | +} // namespace modsecurity::operators |
0 commit comments