-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
138 lines (122 loc) · 4.21 KB
/
Main.cpp
File metadata and controls
138 lines (122 loc) · 4.21 KB
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
/*
Copyright (c) 2016-2019 Divested Computing Group
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_set>
using namespace std;
unordered_set<string> arrCompanies;
unordered_set<string> arrKeywords;
unordered_set<string> arrKeywordsSplit;
unordered_set<string> arrDomainsMaliciousCombined;
void analyzeDomain(const string domain, unordered_set<string> &arrDomainsMalicious) {
bool malicious = false;
istringstream domainSS(domain);
string token;
int c = 0;
int amtSplit = count(domain.begin(), domain.end(), '.');
while(getline(domainSS, token, '.')) {
if(c < amtSplit) {//Ignore the TLD
if(arrCompanies.count(token) > 0) {
malicious = true;
}
}
if(c < amtSplit - 1) {//Ignore the TLD and the domain
if(arrKeywords.count(token) > 0 || arrKeywordsSplit.count(token) > 0) {
malicious = true;
}
}
c++;
}
if(malicious) {
arrDomainsMalicious.insert(domain);
}
}
void loadFileToArray(const string file, unordered_set<string> &array) {
ifstream fileIn;
string lineIn;
fileIn.open(file);
while(getline(fileIn, lineIn)) {
array.insert(lineIn);
}
fileIn.close();
}
void loadCompanyList(const string file, unordered_set<string> &allowlist) {
ifstream fileIn;
string lineIn;
fileIn.open(file);
while(getline(fileIn, lineIn)) {
if(allowlist.count(lineIn) == 0) {
arrCompanies.insert(lineIn);
}
}
fileIn.close();
}
void analyzeDomainList(const int threadID, const string file) {
ifstream fileIn;
string lineIn;
unordered_set<string> arrDomainsMalicious;
int amtDomains = 0;
fileIn.open(file);
while(getline(fileIn, lineIn)) {
analyzeDomain(lineIn, arrDomainsMalicious);
amtDomains++;
if(amtDomains % 10000000 == 0) {
cout << "[" << threadID << "] Analyzed " << amtDomains << " domains so far\n";
}
}
fileIn.close();
arrDomainsMaliciousCombined.insert(arrDomainsMalicious.begin(), arrDomainsMalicious.end());
cout << "[" << threadID << "] Added " << arrDomainsMalicious.size() << " malicious domains\n";
}
void writeSetToFile(const string file, set<string> &array) {
ofstream fileOut;
fileOut.open(file);
for(string entry : array) {
fileOut << entry << "\n";
}
fileOut.close();
}
int main() {
cout << "Loading lists...\n";
unordered_set<string> arrAllowlist;
loadFileToArray("Filters/Allowlist.txt", arrAllowlist);
loadFileToArray("Filters/Keywords.txt", arrKeywords);
loadFileToArray("Filters/KeywordsSplit.txt", arrKeywordsSplit);
loadCompanyList("Filters/Companies-Primary.txt", arrAllowlist);
loadCompanyList("Filters/Companies-Martech.txt", arrAllowlist);
loadCompanyList("Filters/Companies-Better.txt", arrAllowlist);
loadCompanyList("Filters/Companies-Quids.txt", arrAllowlist);
const int totalKeywords = (arrCompanies.size() + arrKeywords.size() + arrKeywordsSplit.size());
cout << "Loaded " << totalKeywords << " matchers\n";
cout << "\t" << arrCompanies.size() << " companies\n";
cout << "\t" << arrKeywords.size() << " keywords\n";
cout << "\t" << arrKeywordsSplit.size() << " split keywords\n\n";
cout << "Analyzing domains...\n";
int threadCounter = 0;
thread adl1(analyzeDomainList, ++threadCounter, "Domains.txt");
adl1.join();
cout << "Analyzed domains\n\n";
cout << "Sorting...\n";
set<string> arrDomainsMaliciousSorted;
arrDomainsMaliciousSorted.insert(arrDomainsMaliciousCombined.begin(), arrDomainsMaliciousCombined.end());
cout << "Sorted\n\n";
cout << "Writing out...\n";
writeSetToFile("Generated/ExperimentalV4-UP.txt", arrDomainsMaliciousSorted);
cout << "Wrote out " << arrDomainsMaliciousSorted.size() << " malicious domains\n\n";
}