forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraud.cpp
295 lines (262 loc) · 8.03 KB
/
fraud.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <algorithm>
#include "fraud.h"
TimePeriod::TimePeriod(SqlDb_row *dbRow) {
if(dbRow) {
is_hourmin = atoi((*dbRow)["is_hourmin"].c_str());
from_hour = atoi((*dbRow)["from_hour"].c_str());
from_minute = atoi((*dbRow)["from_minute"].c_str());
to_hour = atoi((*dbRow)["to_hour"].c_str());
to_minute = atoi((*dbRow)["to_minute"].c_str());
is_weekday = atoi((*dbRow)["is_weekday"].c_str());
from_weekday = atoi((*dbRow)["from_weekday"].c_str());
to_weekday = atoi((*dbRow)["to_weekday"].c_str());
is_monthday = atoi((*dbRow)["is_monthday"].c_str());
from_monthday = atoi((*dbRow)["from_monthday"].c_str());
to_monthday = atoi((*dbRow)["to_monthday"].c_str());
is_month = atoi((*dbRow)["is_month"].c_str());
from_month = atoi((*dbRow)["from_month"].c_str());
to_month = atoi((*dbRow)["to_month"].c_str());
} else {
is_hourmin = false;
from_hour = 0;
from_minute = 0;
to_hour = 0;
to_minute = 0;
is_weekday = false;
from_weekday = 0;
to_weekday = 0;
is_monthday = false;
from_monthday = 0;
to_monthday = 0;
is_month = false;
from_month = 0;
to_month = 0;
}
}
CountryCodes::CountryCodes() {
}
void CountryCodes::load() {
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select *\
from country_code\
where parent_id is null");
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
continents[row["code"]] = row["name"];
}
sqlDb->query("select country.*,\
continent.code as continent\
from country_code country\
join country_code continent on (continent.id = country.parent_id)\
where country.parent_id is not null;");
while(row = sqlDb->fetchRow()) {
countries[row["code"]] = row["name"];
countryContinent[row["code"]] = row["continent"];
continentCountry[row["continent"]].push_back(row["code"]);
}
delete sqlDb;
}
bool CountryCodes::isCountry(const char *code) {
map<string, string>::iterator iter;
iter = countries.find(code);
return(iter != countries.end());
}
string CountryCodes::getNameCountry(const char *code) {
map<string, string>::iterator iter;
iter = countries.find(code);
return(iter != countries.end() ? iter->second : "");
}
string CountryCodes::getNameContinent(const char *code) {
map<string, string>::iterator iter;
iter = continents.find(code);
return(iter != continents.end() ? iter->second : "");
}
string CountryCodes::getName(const char *code) {
return(isCountry(code) ? getNameCountry(code) : getNameContinent(code));
}
string CountryCodes::getContinent(const char *code) {
map<string, string>::iterator iter;
iter = countryContinent.find(code);
return(iter != continents.end() ? iter->second : "");
}
CountryPrefixes::CountryPrefixes() {
}
void CountryPrefixes::load() {
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * from country_code_prefix order by prefix");
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
data.push_back(CountryPrefix_rec(
row["prefix"].c_str(),
row["country_code"].c_str(),
row["descr"].c_str()));
}
std::sort(data.begin(), data.end());
delete sqlDb;
}
GeoIP_country::GeoIP_country() {
}
void GeoIP_country::load() {
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select * from geoip_country order by ip_from");
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
data.push_back(GeoIP_country_rec(
atol(row["ip_from"].c_str()),
atol(row["ip_to"].c_str()),
row["country"].c_str()));
}
std::sort(data.begin(), data.end());
delete sqlDb;
}
FraudAlert::FraudAlert(eFraudAlertType type, unsigned int dbId) {
this->type = type;
this->dbId = dbId;
concurentCallsLimit = 0;
typeChangeLocation = _typeLocation_NA;
}
FraudAlert::~FraudAlert() {
}
void FraudAlert::loadAlert() {
SqlDb *sqlDb = createSqlObject();
char dbIdStr[10];
sprintf(dbIdStr, "%u", dbId);
sqlDb->query(string(
"select alerts.*,\
(select group_concat(number) \
from alerts_groups ag\
join cb_number_groups g on (g.id=ag.number_group_id)\
where ag.type = 'number_whitelist' and ag.alerts_id = alerts.id) as fraud_whitelist_number_g,\
(select group_concat(number)\
from alerts_groups ag\
join cb_number_groups g on (g.id=ag.number_group_id)\
where ag.type = 'number_blacklist' and ag.alerts_id = alerts.id) as fraud_blacklist_number_g,\
(select group_concat(ip)\
from alerts_groups ag\
join cb_ip_groups g on (g.id=ag.ip_group_id)\
where ag.type = 'ip_whitelist' and ag.alerts_id = alerts.id) as fraud_whitelist_ip_g,\
(select group_concat(ip)\
from alerts_groups ag\
join cb_ip_groups g on (g.id=ag.ip_group_id)\
where ag.type = 'ip_blacklist' and ag.alerts_id = alerts.id) as fraud_blacklist_ip_g\
from alerts\
where id = ") + dbIdStr);
dbRow = sqlDb->fetchRow();
if(defFilterIp()) {
ipFilter.addWhite(dbRow["fraud_whitelist_ip"].c_str());
ipFilter.addWhite(dbRow["fraud_whitelist_ip_g"].c_str());
ipFilter.addBlack(dbRow["fraud_blacklist_ip"].c_str());
ipFilter.addBlack(dbRow["fraud_blacklist_ip_g"].c_str());
}
if(defFilterNumber()) {
phoneNumberFilter.addWhite(dbRow["fraud_whitelist_number"].c_str());
phoneNumberFilter.addWhite(dbRow["fraud_whitelist_number_g"].c_str());
phoneNumberFilter.addBlack(dbRow["fraud_blacklist_number"].c_str());
phoneNumberFilter.addBlack(dbRow["fraud_blacklist_number_g"].c_str());
}
delete sqlDb;
if(defFraudDef()) {
loadFraudDef();
}
if(defConcuretCallsLimit()) {
concurentCallsLimit = atoi(dbRow["fraud_concurent_calls_limit"].c_str());
}
if(defTypeChangeLocation()) {
typeChangeLocation = dbRow["fraud_type_change_location"] == "country" ? _typeLocation_country :
dbRow["fraud_type_change_location"] == "continent" ? _typeLocation_continent :
_typeLocation_NA;
}
if(defChangeLocationOk()) {
changeLocationOk = split(dbRow["fraud_change_location_ok"].c_str(), ",", true);
}
if(defDestLocation()) {
destLocation = split(dbRow["fraud_dest_location"].c_str(), ",", true);
}
}
void FraudAlert::loadFraudDef() {
SqlDb *sqlDb = createSqlObject();
char dbIdStr[10];
sprintf(dbIdStr, "%u", dbId);
sqlDb->query(string(
"select *\
from alerts_fraud\
where alerts_id = ") + dbIdStr);
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
addFraudDef(&row);
}
delete sqlDb;
}
FraudAlert_rcc_timePeriods::FraudAlert_rcc_timePeriods(const char *descr, int concurentCallsLimit, unsigned int dbId) {
this->descr = descr;
this->concurentCallsLimit = concurentCallsLimit;
this->dbId = dbId;
}
void FraudAlert_rcc_timePeriods::loadTimePeriods() {
SqlDb *sqlDb = createSqlObject();
char dbIdStr[10];
sprintf(dbIdStr, "%u", dbId);
sqlDb->query(string(
"select *\
from alerts_fraud_timeperiod\
where alerts_fraud_id = ") + dbIdStr);
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
timePeriods.push_back(TimePeriod(&row));
}
delete sqlDb;
}
void FraudAlert_rcc::addFraudDef(SqlDb_row *row) {
timePeriods.push_back(FraudAlert_rcc_timePeriods(
(*row)["descr"].c_str(),
atoi((*row)["concurent_calls_limit"].c_str()),
atol((*row)["id"].c_str())));
}
FraudAlert_rcc::FraudAlert_rcc(unsigned int dbId)
: FraudAlert(_rcc, dbId) {
}
FraudAlert_chc::FraudAlert_chc(unsigned int dbId)
: FraudAlert(_chc, dbId) {
}
FraudAlert_chcr::FraudAlert_chcr(unsigned int dbId)
: FraudAlert(_chcr, dbId) {
}
FraudAlert_d::FraudAlert_d(unsigned int dbId)
: FraudAlert(_d, dbId) {
}
FraudAlerts::~FraudAlerts() {
clear();
}
void FraudAlerts::loadAlerts() {
SqlDb *sqlDb = createSqlObject();
sqlDb->query("select id, alert_type from alerts\
where alert_type > 20 and\
(disable is null or not disable)");
SqlDb_row row;
while(row = sqlDb->fetchRow()) {
FraudAlert *alert;
unsigned int dbId = atol(row["id"].c_str());
switch(atoi(row["alert_type"].c_str())) {
case FraudAlert::_rcc:
alert = new FraudAlert_rcc(dbId);
break;
case FraudAlert::_chc:
alert = new FraudAlert_chc(dbId);
break;
case FraudAlert::_chcr:
alert = new FraudAlert_chcr(dbId);
break;
case FraudAlert::_d:
alert = new FraudAlert_d(dbId);
break;
}
alerts.push_back(alert);
}
delete sqlDb;
}
void FraudAlerts::clear() {
for(size_t i = 0; i < alerts.size(); i++) {
delete alerts[i];
}
alerts.clear();
}