-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMonitor.cpp
252 lines (234 loc) · 6.69 KB
/
Monitor.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
#include "Monitor.h"
Monitor::Monitor()
{
hService = NULL;
}
Monitor::~Monitor()
{
stdext::hash_map<wstring, std::list<Permission*>*>::iterator it;
for(it = permissionMap.begin(); it != permissionMap.end(); it++)
{
std::list<Permission*>::iterator lit;
for(lit = it->second->begin(); lit != it->second->end(); lit++)
{
delete (*lit);
}
it->second->clear();
}
permissionMap.clear();
unInstallKernelDriver();
}
bool
Monitor::isEventAllowed(std::wstring eventType, std::wstring subject, std::wstring object)
{
stdext::hash_map<wstring, std::list<Permission*>*>::iterator it;
std::transform(eventType.begin(),eventType.end(),eventType.begin(),std::towlower);
it = permissionMap.find(eventType);
PERMISSION_CLASSIFICATION excluded = NO_MATCH;
if(it != permissionMap.end())
{
std::list<Permission*>* lp = it->second;
std::list<Permission*>::iterator lit;
for(lit = lp->begin(); lit != lp->end(); lit++)
{
PERMISSION_CLASSIFICATION newExcluded = (*lit)->Check(subject,object);
if( newExcluded == ALLOWED)
{
if(excluded != DISALLOWED)
excluded = ALLOWED;
} else if(newExcluded == DISALLOWED) {
excluded = DISALLOWED;
}
}
}
if(excluded == ALLOWED)
{
return true;
} else {
return false;
}
}
void
Monitor::clearExclusionList()
{
stdext::hash_map<wstring, std::list<Permission*>*>::iterator it;
for(it = permissionMap.begin(); it != permissionMap.end(); it++)
{
std::list<Permission*>* lp = it->second;
std::list<Permission*>::iterator lit;
for(lit = lp->begin(); lit != lp->end(); lit++)
{
Permission* p = *lit;
if(!p->permaneant)
{
lp->remove(p);
delete (p);
}
}
}
}
bool
Monitor::installKernelDriver(wstring driverPath, wstring driverName, wstring driverDescription)
{
SC_HANDLE hSCManager;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if(hSCManager)
{
//printf("%ls: Kernel driver path: %ls\n", driverName.c_str(), driverPath.c_str());
hService = CreateService(hSCManager, driverName.c_str(),
driverDescription.c_str(),
SERVICE_START | DELETE | SERVICE_STOP,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_IGNORE,
driverPath.c_str(),
NULL, NULL, NULL, NULL, NULL);
if(!hService)
{
hService = OpenService(hSCManager, driverName.c_str(),
SERVICE_START | DELETE | SERVICE_STOP);
}
if(hService)
{
if(StartService(hService, 0, NULL))
{
printf("Loaded kernel driver: %ls\n", driverName.c_str());
} else {
DWORD err = GetLastError();
if(err == ERROR_SERVICE_ALREADY_RUNNING)
{
printf("Driver already loaded: %ls\n", driverName.c_str());
} else {
printf("Error loading kernel driver: %ls - 0x%08x\n", driverName.c_str(), err);
CloseServiceHandle(hSCManager);
return false;
}
}
} else {
printf("Error loading kernel driver: %ls - 0x%08x\n", driverName.c_str(), GetLastError());
CloseServiceHandle(hSCManager);
return false;
}
CloseServiceHandle(hSCManager);
return true;
}
printf("Error loading kernel driver: %ls - OpenSCManager 0x%08x\n", driverName.c_str(), GetLastError());
return false;
}
void
Monitor::unInstallKernelDriver()
{
if(hService != NULL)
{
SERVICE_STATUS ss;
ControlService(hService, SERVICE_CONTROL_STOP, &ss);
DeleteService(hService);
CloseServiceHandle(hService);
}
hService = NULL;
}
void
Monitor::loadExclusionList(wstring file)
{
string line;
int lineNumber = 0;
DebugPrint(L"Monitor-loadExclusionList: Loading list - %ls\n", file.c_str());
ifstream exclusionList (file.c_str());
if (exclusionList.is_open())
{
while (! exclusionList.eof() )
{
getline (exclusionList,line);
lineNumber++;
if(line.length() > 0 && line.at(0) != '#') {
try {
if(line.at(0) == '+' || line.at(0) == '-')
{
vector<std::wstring> splitLine;
typedef split_iterator<string::iterator> sf_it;
for(sf_it it=make_split_iterator(line, token_finder(is_any_of("\t")));
it!=sf_it(); ++it)
{
splitLine.push_back(copy_range<std::wstring>(*it));
}
if(splitLine.size() == 4)
{
if(splitLine[1] == L".*" || splitLine[1] == L".+")
{
printf("%ls ERROR on line %i: The action type is not supposed to be a regular expression\n", file.c_str(), lineNumber);
} else {
addExclusion(splitLine[0], splitLine[1], splitLine[2], splitLine[3]);
}
} else {
printf("%ls token ERROR on line %i\n", file.c_str(), lineNumber);
}
} else {
printf("%ls ERROR no exclusion type (+,-) on line %i\n", file.c_str(), lineNumber);
}
} catch(boost::regex_error r) {
printf("%ls ERROR on line %i\n", file.c_str(), lineNumber);
printf("\t%s\n", r.what());
}
}
}
} else {
printf("Could not open file: %ls\n", file.c_str());
}
}
void
Monitor::prepareStringForExclusion(wstring* s)
{
wstring from = L"\\";
wstring to = L"\\\\";
size_t offset = 0;
while((offset = s->find(from, offset)) != wstring::npos)
{
s->replace(offset,
from.size(),
to);
offset += to.length();
}
from = L".";
to = L"\\.";
offset = 0;
while((offset = s->find(from, offset)) != wstring::npos)
{
s->replace(offset,
from.size(),
to);
offset += to.length();
}
}
void
Monitor::addExclusion(wstring excluded, wstring action, wstring subject, wstring object , bool permaneant)
{
//printf("Adding exclusion\n");
try {
Permission* p = new Permission();
if(excluded == L"yes" || excluded == L"+")
{
p->allow = true;
} else if(excluded == L"no" || excluded == L"-"){
p->allow = false;
}
p->permaneant = permaneant;
boost::wregex subjectRegex(subject.c_str(), boost::wregex::icase);
boost::wregex objectRegex(object.c_str(), boost::wregex::icase);
p->objects.push_back(objectRegex);
p->subjects.push_back(subjectRegex);
std::transform(action.begin(),action.end(),action.begin(),std::towlower);
stdext::hash_map<wstring, std::list<Permission*>*>::iterator it;
it = permissionMap.find(action);
if(it == permissionMap.end())
{
std::list<Permission*>*l = new list<Permission*>();
l->push_back(p);
permissionMap.insert(Permission_Pair(action, l));
} else {
std::list<Permission*>* lp = it->second;
lp->push_back(p);
}
} catch(boost::regex_error r) {
throw r;
}
}