-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelWeb.cpp
More file actions
executable file
·237 lines (201 loc) · 5.47 KB
/
IntelWeb.cpp
File metadata and controls
executable file
·237 lines (201 loc) · 5.47 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
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
#include "IntelWeb.h"
#include "DiskMultiMap.h"
#include "InteractionTuple.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <set>
using namespace std;
IntelWeb::IntelWeb()
{}
IntelWeb::~IntelWeb() // FREE ANY DYNAMICALLY ALLOCATED MEMORY
{
close();
}
bool IntelWeb::createNew(const string& filePrefix, unsigned int maxDataItems) // DONE
{
close();
int bucknum = int(maxDataItems / 0.7);
bool success = m_from.createNew(filePrefix + "-from-hash-table.dat", bucknum);
bool success2 = m_to.createNew(filePrefix + "-to-hash-table.dat", bucknum);
bool success3 = prevalence.createNew(filePrefix + "-prevalence-hash-table.dat", 2 * bucknum);
if (success && success2 && success3)
return true;
else
{
close();
return false;
}
}
bool IntelWeb::openExisting(const string& filePrefix) // DONE
{
close();
bool success = m_from.openExisting(filePrefix + "-from-hash-table.dat");
bool success2 = m_to.openExisting(filePrefix + "-to-hash-table.dat");
bool success3 = prevalence.openExisting(filePrefix + "-prevalence-hash-table.dat");
if (success && success2 && success3)
return true;
else
{
close();
return false;
}
}
void IntelWeb::close() // DONE
{
m_from.close();
m_to.close();
prevalence.close();
}
bool IntelWeb::ingest(const string& telemetryFile) // DONE
{
ifstream inf(telemetryFile);
if (!inf)
return false;
string line;
while (getline(inf, line))
{
istringstream iss(line);
string context;
string from;
string to;
if (!(iss >> context >> from >> to))
continue;
if (context.size() > 120 || from.size() > 120 || to.size() > 120)
continue;
m_from.insert(from, to, context);
m_to.insert(to, from, context);
prevalence.insert(from, "", "");
prevalence.insert(to, "", "");
}
return true;
}
unsigned int IntelWeb::crawl(const vector<string>& indicators, unsigned int minPrevalenceToBeGood,
vector<string>& badEntitiesFound, vector<InteractionTuple>& badInteractions)
{
unordered_map<string, DiskMultiMap::Iterator> fromIts;
unordered_map<string, DiskMultiMap::Iterator> toIts;
unordered_map<string, bool> isGood;
queue<string> badEntities;
set<string> o_BadEntities;
set<InteractionTuple> o_InteractionTuples;
badEntitiesFound.clear();
badInteractions.clear();
for (int i = 0; i < indicators.size(); i++)
badEntities.push(indicators[i]);
while (!badEntities.empty())
{
DiskMultiMap::Iterator fromIter;
DiskMultiMap::Iterator toIter;
auto it = fromIts.find(badEntities.front());
auto it2 = toIts.find(badEntities.front());
fromIter = m_from.search(badEntities.front());
toIter = m_to.search(badEntities.front());
if (fromIter.isValid())
{
if (it == fromIts.end())
fromIts.insert(make_pair(badEntities.front(), fromIter));
else
fromIter = it->second;
if (fromIter.isValid())
{
o_BadEntities.insert(badEntities.front());
MultiMapTuple mmt1 = *fromIter;
auto ip = isGood.find(mmt1.value);
bool prevIsGood;
if (ip == isGood.end())
{
prevIsGood = countPrevalence(mmt1.value, minPrevalenceToBeGood);
isGood.insert(std::make_pair(mmt1.value, prevIsGood));
}
else
prevIsGood = ip->second;
if (!prevIsGood)
{
o_BadEntities.insert(mmt1.value);
badEntities.push(mmt1.value);
}
InteractionTuple it1;
it1.context = mmt1.context;
it1.from = mmt1.key;
it1.to = mmt1.value;
o_InteractionTuples.insert(it1);
++fromIter;
fromIts[badEntities.front()] = fromIter;
}
}
if (toIter.isValid())
{
if (it2 == toIts.end())
toIts.insert(make_pair(badEntities.front(), toIter));
else
toIter = it2->second;
if (toIter.isValid())
{
o_BadEntities.insert(badEntities.front());
MultiMapTuple mmt2 = *toIter;
auto ip2 = isGood.find(mmt2.value);
bool prevIsGood;
if (ip2 == isGood.end())
{
prevIsGood = countPrevalence(mmt2.value, minPrevalenceToBeGood);
isGood.insert(make_pair(mmt2.value, prevIsGood));
}
else
prevIsGood = ip2->second;
if (!prevIsGood)
{
o_BadEntities.insert(mmt2.value);
badEntities.push(mmt2.value);
}
InteractionTuple it2;
it2.context = mmt2.context;
it2.from = mmt2.value;
it2.to = mmt2.key;
o_InteractionTuples.insert(it2);
++toIter;
toIts[badEntities.front()] = toIter;
}
}
badEntities.pop();
}
for (auto it = o_BadEntities.begin(); it != o_BadEntities.end(); it++)
badEntitiesFound.push_back(*it);
for (auto it = o_InteractionTuples.begin(); it != o_InteractionTuples.end(); it++)
badInteractions.push_back(*it);
return (unsigned int)badEntitiesFound.size();
}
bool IntelWeb::purge(const string& entity) // DONE
{
bool purged = false;
DiskMultiMap::Iterator it = m_from.search(entity);
while (it.isValid())
{
MultiMapTuple m = *it;
string k = m.key;
string v = m.value;
string c = m.context;
++it;
m_from.erase(k, v, c);
m_to.erase(v, k, c);
purged = true;
}
DiskMultiMap::Iterator it2 = m_to.search(entity);
while (it2.isValid())
{
MultiMapTuple m = *it2;
string k = m.key;
string v = m.value;
string c = m.context;
++it2;
m_to.erase(k, v, c);
m_from.erase(v, k, c);
purged = true;
}
prevalence.erase(entity, "", "");
return purged;
}