forked from owasp-modsecurity/ModSecurity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbl.cc
241 lines (204 loc) · 6.63 KB
/
rbl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address [email protected].
*
*/
#include "src/operators/rbl.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include "modsecurity/rules_set.h"
#include "src/operators/operator.h"
namespace modsecurity {
namespace operators {
std::string Rbl::mapIpToAddress(const std::string &ipStr, Transaction *trans) const {
std::string addr;
int h0, h1, h2, h3;
std::string key;
if (trans && trans->m_rules->m_httpblKey.m_set == true) {
key = trans->m_rules->m_httpblKey.m_value;
}
if (sscanf(ipStr.c_str(), "%d.%d.%d.%d", &h0, &h1, &h2, &h3) != 4) {
ms_dbg_a(trans, 0, std::string("Failed to understand `" + ipStr +
"' as a valid IP address, assuming domain format input"));
addr = ipStr + "." + m_service;
return addr;
}
if (m_demandsPassword && key.empty()) {
ms_dbg_a(trans, 0, std::string("Missing RBL key, cannot continue " \
"with the operator execution, please set the key using: " \
"SecHttpBlKey"));
return addr;
}
addr = std::to_string(h3) + "." +
std::to_string(h2) + "." +
std::to_string(h1) + "." +
std::to_string(h0) + "." +
m_service;
if (m_demandsPassword) {
addr = key + "." + addr;
}
return addr;
}
void Rbl::futherInfo_httpbl(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans) {
char *respBl;
int first, days, score, type;
#ifndef NO_LOGS
std::string ptype;
#endif
respBl = inet_ntoa(sin->sin_addr);
if (sscanf(respBl, "%d.%d.%d.%d", &first, &days, &score, &type) != 4) {
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " failed: bad response");
return;
}
if (first != 127) {
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " failed: bad response");
return;
}
#ifndef NO_LOGS
switch (type) {
case 0:
ptype = "Search Engine";
break;
case 1:
ptype = "Suspicious IP";
break;
case 2:
ptype = "Harvester IP";
break;
case 3:
ptype = "Suspicious harvester IP";
break;
case 4:
ptype = "Comment spammer IP";
break;
case 5:
ptype = "Suspicious comment spammer IP";
break;
case 6:
ptype = "Harvester and comment spammer IP";
break;
case 7:
ptype = "Suspicious harvester comment spammer IP";
break;
default:
ptype = " ";
}
#endif
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded. %s: " \
+ std::to_string(days) + " " \
"days since last activity, threat score " \
+ std::to_string(score) + ". Case: " + ptype);
}
void Rbl::futherInfo_spamhaus(unsigned int high8bits, const std::string &ipStr,
Transaction *trans) {
switch (high8bits) {
case 2:
case 3:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded " \
"(Static UBE sources).");
break;
case 4:
case 5:
case 6:
case 7:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded " \
"(Illegal 3rd party exploits).");
break;
case 10:
case 11:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded " \
"(Delivering unauthenticated SMTP email).");
break;
default:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded ");
break;
}
}
void Rbl::futherInfo_uribl(unsigned int high8bits, const std::string &ipStr,
Transaction *trans) {
switch (high8bits) {
case 2:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (BLACK).");
break;
case 4:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (GREY).");
break;
case 8:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (RED).");
break;
case 14:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded " \
"(BLACK,GREY,RED).");
break;
case 255:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded " \
"(DNS IS BLOCKED).");
break;
default:
ms_dbg_a(trans, 4, "RBL lookup of " + ipStr + " succeeded (WHITE).");
break;
}
}
void Rbl::furtherInfo(struct sockaddr_in *sin, const std::string &ipStr,
Transaction *trans, RblProvider provider) {
unsigned int high8bits = sin->sin_addr.s_addr >> 24;
switch (provider) {
case RblProvider::UnknownProvider:
ms_dbg_a(trans, 2, "RBL lookup of " + ipStr + " succeeded.");
break;
case RblProvider::httpbl:
futherInfo_httpbl(sin, ipStr, trans);
break;
case RblProvider::uribl:
futherInfo_uribl(high8bits, ipStr, trans);
break;
case RblProvider::spamhaus:
futherInfo_spamhaus(high8bits, ipStr, trans);
break;
}
}
bool Rbl::evaluate(Transaction *t, RuleWithActions *rule,
const std::string& ipStr,
std::shared_ptr<RuleMessage> ruleMessage) {
struct addrinfo *info = NULL;
std::string host = Rbl::mapIpToAddress(ipStr, t);
int rc = 0;
if (host.empty()) {
return false;
}
rc = getaddrinfo(host.c_str(), NULL, NULL, &info);
if (rc != 0) {
if (info != NULL) {
freeaddrinfo(info);
}
ms_dbg_a(t, 5, "RBL lookup of " + ipStr + " failed.");
return false;
}
struct sockaddr *addr = info->ai_addr;
struct sockaddr_in *sin = (struct sockaddr_in *) addr;
furtherInfo(sin, ipStr, t, m_provider);
freeaddrinfo(info);
if (rule && t && rule->hasCaptureAction()) {
t->m_collections.m_tx_collection->storeOrUpdateFirst(
"0", std::string(ipStr));
ms_dbg_a(t, 7, "Added RXL match TX.0: " + \
std::string(ipStr));
}
return true;
}
} // namespace operators
} // namespace modsecurity