-
Notifications
You must be signed in to change notification settings - Fork 19
/
FiltersEngine.cpp
261 lines (214 loc) · 7.92 KB
/
FiltersEngine.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
/*
microsoft-oms-auditd-plugin
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "FiltersEngine.h"
#include "Logger.h"
#include "StringUtils.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sys/stat.h> /* for stat() */
#include <dirent.h>
#include <algorithm>
#include <unistd.h>
#include <limits.h>
std::bitset<FILTER_BITSET_SIZE> FiltersEngine::AddFilter(const ProcFilterSpec& pfs, const std::string& outputName)
{
std::bitset<FILTER_BITSET_SIZE> ret;
auto it = _filtersBitPosition.find(pfs);
if (it != _filtersBitPosition.end()) {
struct FiltersInfo& info = it->second;
ret[info.bitPosition] = 1;
if (info.outputs.count(outputName) == 0) {
info.outputs.emplace(outputName);
}
} else {
struct FiltersInfo info;
info.bitPosition = _nextBitPosition;
info.outputs.emplace(outputName);
_filtersBitPosition[pfs] = info;
ret[_nextBitPosition] = 1;
// Insert the syscalls *in order* into an unordered_map that maps each one to a bool.
// A true value indicates an exclusion syscall (no !) and a false value indicates an
// inclusion syscall (!syscall). If the syscall is already in the unordered_map then
// ignore the repetitions; e.g. take the first value for each syscall only.
std::unordered_map<std::string, bool>& syscalls = _bitPositionSyscalls[_nextBitPosition];
for (auto s : pfs._syscalls) {
if (s[0] != '!') {
if (syscalls.count(s) == 0) {
syscalls[s] = true;
}
} else {
if (syscalls.count(s.substr(1)) == 0) {
syscalls[s] = false;
}
}
}
_nextBitPosition++;
}
if (_outputs.count(outputName) == 0) {
_outputs.insert(outputName);
}
return ret;
}
std::bitset<FILTER_BITSET_SIZE> FiltersEngine::AddFilterList(const std::vector<ProcFilterSpec>& pfsVec, const std::string& outputName)
{
std::bitset<FILTER_BITSET_SIZE> ret;
for (auto pfs : pfsVec) {
ret |= AddFilter(pfs, outputName);
}
SetCommonFlagsMask();
return ret;
}
void FiltersEngine::RemoveFilter(const ProcFilterSpec& pfs, const std::string& outputName)
{
// check that the filter exists
auto it = _filtersBitPosition.find(pfs);
if (it == _filtersBitPosition.end()) {
return;
}
// check that the filter constains this output
struct FiltersInfo& info = it->second;
if ((info.outputs.count(outputName) == 0) && (info.outputs.size() > 0)) {
return;
}
if (info.outputs.size() <= 1) {
// outputs is either empty or only contains this output
_bitPositionSyscalls.erase(info.bitPosition);
_filtersBitPosition.erase(pfs);
} else {
// outputs contains this output and others
info.outputs.erase(outputName);
}
}
void FiltersEngine::RemoveFilterList(const std::vector<ProcFilterSpec>& pfsVec, const std::string& outputName)
{
for (auto pfs : pfsVec) {
RemoveFilter(pfs, outputName);
}
_outputs.erase(outputName);
SetCommonFlagsMask();
}
bool FiltersEngine::ProcessMatchFilter(const std::shared_ptr<ProcessTreeItem>& process, const ProcFilterSpec& pfs, unsigned int height)
{
if (pfs._depth != -1 && pfs._depth < height) {
return false;
}
if (pfs._match_mask & PFS_MATCH_UID) {
if (pfs._uid != process->uid()) {
return false;
}
}
if (pfs._match_mask & PFS_MATCH_GID) {
if (pfs._gid != process->gid()) {
return false;
}
}
auto exe = process->exe();
if (pfs._match_mask & PFS_MATCH_EXE_EQUALS) {
if (pfs._exeMatchValue != exe) {
return false;
}
}
if (pfs._match_mask & PFS_MATCH_EXE_STARTSWITH) {
if (!starts_with(exe, pfs._exeMatchValue)) {
return false;
}
}
if (pfs._match_mask & PFS_MATCH_EXE_CONTAINS) {
if (exe.find(pfs._exeMatchValue) == std::string::npos) {
return false;
}
}
if (pfs._match_mask & PFS_MATCH_EXE_REGEX) {
if (!re2::RE2::PartialMatch(exe, *pfs._exeRegex)) {
return false;
}
}
auto cmdline = process->cmdline();
for (auto cf : pfs._cmdlineFilters) {
if (cf._matchType == MatchEquals) {
if (cf._matchValue != cmdline) {
return false;
}
} else if (cf._matchType == MatchStartsWith) {
if (!starts_with(cmdline, cf._matchValue)) {
return false;
}
} else if (cf._matchType == MatchContains) {
if (cmdline.find(cf._matchValue) == std::string::npos) {
return false;
}
} else if (cf._matchType == MatchRegex) {
if (!re2::RE2::PartialMatch(cmdline, *cf._matchRegex)) {
return false;
}
}
}
return true;
}
std::bitset<FILTER_BITSET_SIZE> FiltersEngine::GetFlags(const std::shared_ptr<ProcessTreeItem>& process, unsigned int height)
{
std::bitset<FILTER_BITSET_SIZE> flags;
for (auto element : _filtersBitPosition) {
ProcFilterSpec pfs = element.first;
if (ProcessMatchFilter(process, pfs, height)) {
flags[element.second.bitPosition] = 1;
}
}
return flags;
}
std::bitset<FILTER_BITSET_SIZE> FiltersEngine::GetCommonFlagsMask()
{
return _globalFlagsMask;
}
void FiltersEngine::SetCommonFlagsMask()
{
std::bitset<FILTER_BITSET_SIZE> flags;
unsigned int numberOfOutputs = _outputs.size();
for (auto element : _filtersBitPosition) {
if (element.second.outputs.size() == numberOfOutputs) {
flags[element.second.bitPosition] = 1;
}
}
_globalFlagsMask = flags;
}
bool FiltersEngine::syscallIsFiltered(const std::string& syscall, const std::unordered_map<std::string, bool>& syscalls)
{
auto it = syscalls.find(syscall);
if (it != syscalls.end()) {
return it->second;
} else {
if (syscalls.count("*") > 0) {
return true;
}
}
return false;
}
bool FiltersEngine::IsEventFiltered(const std::string& syscall, const std::shared_ptr<ProcessTreeItem>& p, const std::bitset<FILTER_BITSET_SIZE>& filterFlagsMask)
{
// Get event syscall
bool filtered = false;
if (syscall.empty() || !p) {
return false;
}
// Check if this process is filtered
std::bitset<FILTER_BITSET_SIZE> matched_flags = p->flags() & filterFlagsMask;
// Find syscalls filtered for process
for (unsigned int i=0; i<_nextBitPosition; i++) {
if (matched_flags[i]) {
std::unordered_map<std::string, bool>& syscalls = _bitPositionSyscalls[i];
if (syscallIsFiltered(syscall, syscalls)) {
filtered = true;
break;
}
}
}
return filtered;
}