Skip to content

Commit 86ce479

Browse files
Rufus125zimmerle
authored andcommitted
Adds new operator to check for data leakage of Austrian social security number
1 parent 6d266fa commit 86ce479

File tree

11 files changed

+7445
-7174
lines changed

11 files changed

+7445
-7174
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ TESTS+=test/test-cases/regression/operator-validate-byte-range.json
166166
TESTS+=test/test-cases/regression/operator-verifycc.json
167167
TESTS+=test/test-cases/regression/operator-verifycpf.json
168168
TESTS+=test/test-cases/regression/operator-verifyssn.json
169+
TESTS+=test/test-cases/regression/operator-verifysvnr.json
169170
TESTS+=test/test-cases/regression/request-body-parser-json.json
170171
TESTS+=test/test-cases/regression/request-body-parser-multipart-crlf.json
171172
TESTS+=test/test-cases/regression/request-body-parser-multipart.json

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ OPERATORS = \
225225
operators/verify_cc.cc \
226226
operators/verify_cpf.cc \
227227
operators/verify_ssn.cc \
228+
operators/verify_svnr.cc \
228229
operators/within.cc \
229230
operators/unconditional_match.cc
230231

src/operators/operator.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "src/operators/verify_cc.h"
5959
#include "src/operators/verify_cpf.h"
6060
#include "src/operators/verify_ssn.h"
61+
#include "src/operators/verify_svnr.h"
6162
#include "src/operators/within.h"
6263
#include "src/operators/unconditional_match.h"
6364

@@ -185,6 +186,7 @@ Operator *Operator::instantiate(std::string op, std::string param_str) {
185186
IF_MATCH(verifycc) { return new VerifyCC(std::move(param)); }
186187
IF_MATCH(verifycpf) { return new VerifyCPF(std::move(param)); }
187188
IF_MATCH(verifyssn) { return new VerifySSN(std::move(param)); }
189+
IF_MATCH(verifysvnr) { return new VerifySVNR(std::move(param)); }
188190
IF_MATCH(within) { return new Within(std::move(param)); }
189191

190192
IF_MATCH(unconditionalmatch) {

src/operators/verify_svnr.cc

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
#include "src/operators/verify_svnr.h"
3+
4+
#include <string>
5+
6+
#include "src/operators/operator.h"
7+
8+
#include "modsecurity/rule.h"
9+
#include "modsecurity/rule_message.h"
10+
#include "modsecurity/rules_properties.h"
11+
namespace modsecurity {
12+
namespace operators {
13+
14+
int VerifySVNR::convert_to_int(const char c)
15+
{
16+
int n;
17+
if ((c>='0') && (c<='9'))
18+
n = c - '0';
19+
else
20+
n = 0;
21+
return n;
22+
}
23+
24+
bool VerifySVNR::verify(const char *svnrnumber, int len) {
25+
int var_len = len;
26+
int sum = 0;
27+
unsigned int i = 0, svnr_len = 10;
28+
int svnr[11];
29+
char s_svnr[11];
30+
char bad_svnr[12][11] = { "0000000000",
31+
"0123456789",
32+
"1234567890",
33+
"1111111111",
34+
"2222222222",
35+
"3333333333",
36+
"4444444444",
37+
"5555555555",
38+
"6666666666",
39+
"7777777777",
40+
"8888888888",
41+
"9999999999"};
42+
43+
while ((*svnrnumber != '\0') && ( var_len > 0))
44+
{
45+
if (*svnrnumber != '-' || *svnrnumber != '.')
46+
{
47+
if (i < svnr_len && isdigit(*svnrnumber))
48+
{
49+
s_svnr[i] = *svnrnumber;
50+
svnr[i] = convert_to_int(*svnrnumber);
51+
i++;
52+
}
53+
}
54+
svnrnumber++;
55+
var_len--;
56+
}
57+
58+
59+
if (i != svnr_len)
60+
{
61+
return 0;
62+
}
63+
else
64+
{
65+
for (i = 0; i< svnr_len; i++)
66+
{
67+
if (strncmp(s_svnr,bad_svnr[i],svnr_len) == 0)
68+
{
69+
return 0;
70+
}
71+
}
72+
}
73+
//Laufnummer mit 3, 7, 9
74+
//Geburtsdatum mit 5, 8, 4, 2, 1, 6
75+
sum = svnr[0] * 3 + svnr[1] * 7 + svnr[2] * 9 + svnr[4] * 5 + svnr[5] * 8 + svnr[6] * 4 + svnr[7] * 2 + svnr[8] * 1 + svnr[9] * 6;
76+
sum %= 11;
77+
if(sum == 10){
78+
sum = 0;
79+
}
80+
if (sum == svnr[3])
81+
{
82+
return true;
83+
}
84+
return false;
85+
}
86+
87+
88+
bool VerifySVNR::evaluate(Transaction *t, Rule *rule,
89+
const std::string& input, std::shared_ptr<RuleMessage> ruleMessage) {
90+
std::list<SMatch> matches;
91+
bool is_svnr = false;
92+
int i;
93+
94+
if (m_param.empty()) {
95+
return is_svnr;
96+
}
97+
98+
for (i = 0; i < input.size() - 1 && is_svnr == false; i++) {
99+
matches = m_re->searchAll(input.substr(i, input.size()));
100+
101+
for (const auto & i : matches) {
102+
is_svnr = verify(i.str().c_str(), i.str().size());
103+
if (is_svnr) {
104+
logOffset(ruleMessage, i.offset(), i.str().size());
105+
if (rule && t && rule->m_containsCaptureAction) {
106+
t->m_collections.m_tx_collection->storeOrUpdateFirst(
107+
"0", i.str());
108+
ms_dbg_a(t, 7, "Added VerifySVNR match TX.0: " + \
109+
i.str());
110+
}
111+
112+
goto out;
113+
}
114+
}
115+
}
116+
117+
out:
118+
return is_svnr;
119+
}
120+
121+
122+
} // namespace operators
123+
} // namespace modsecurity

src/operators/verify_svnr.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
#ifndef SRC_OPERATORS_VERIFY_SVNR_H_
3+
#define SRC_OPERATORS_VERIFY_SVNR_H_
4+
5+
#include <string>
6+
#include <memory>
7+
#include <utility>
8+
9+
#include "src/operators/operator.h"
10+
#include "src/utils/regex.h"
11+
12+
13+
namespace modsecurity {
14+
using Utils::SMatch;
15+
using Utils::regex_search;
16+
using Utils::Regex;
17+
18+
namespace operators {
19+
20+
class VerifySVNR : public Operator {
21+
public:
22+
/** @ingroup ModSecurity_Operator */
23+
explicit VerifySVNR(std::unique_ptr<RunTimeString> param)
24+
: Operator("VerifySVNR", std::move(param)) {
25+
m_re = new Regex(m_param);
26+
}
27+
28+
~VerifySVNR() {
29+
delete m_re;
30+
}
31+
bool evaluate(Transaction *transaction, Rule *rule,
32+
const std::string &input) override {
33+
return evaluate(transaction, NULL, input, NULL);
34+
}
35+
bool evaluate(Transaction *transaction,
36+
const std::string &input) override {
37+
return evaluate(transaction, NULL, input);
38+
}
39+
bool evaluate(Transaction *transaction, Rule *rule,
40+
const std::string& input,
41+
std::shared_ptr<RuleMessage> ruleMessage) override;
42+
43+
int convert_to_int(const char c);
44+
bool verify(const char *ssnumber, int len);
45+
46+
private:
47+
Regex *m_re;
48+
};
49+
50+
} // namespace operators
51+
} // namespace modsecurity
52+
53+
54+
#endif // SRC_OPERATORS_VERIFY_SVNR_H_
55+

0 commit comments

Comments
 (0)