forked from microsoft/OMS-Auditd-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpret.cpp
351 lines (333 loc) · 12.6 KB
/
Interpret.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
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 "Interpret.h"
#include "Translate.h"
#include "StringUtils.h"
#include "StringTable.h"
#include "Logger.h"
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <arpa/inet.h>
#ifndef NO_INTERP_EXTRA_PROTO
#include <linux/ax25.h>
#include <linux/atm.h>
#include <linux/x25.h>
#include <linux/ipx.h>
#endif
template <typename T>
inline bool field_to_int(const EventRecordField& field, T& val, int base) {
errno = 0;
val = static_cast<T>(strtol(field.RawValuePtr(), nullptr, base));
return errno == 0;
}
template <typename T>
inline bool field_to_uint(const EventRecordField& field, T& val, int base) {
errno = 0;
val = static_cast<T>(strtoul(field.RawValuePtr(), nullptr, base));
return errno == 0;
}
static StringTable<int> s_fam_table(-1, {
{"local", AF_LOCAL},
{"inet", AF_INET},
{"ax25", AF_AX25},
{"ipx", AF_IPX},
{"appletalk", AF_APPLETALK},
{"netrom", AF_NETROM},
{"bridge", AF_BRIDGE},
{"atmpvc", AF_ATMPVC},
{"x25", AF_X25},
{"inet6", AF_INET6},
{"rose", AF_ROSE},
{"decnet", AF_DECnet},
{"netbeui", AF_NETBEUI},
{"security", AF_SECURITY},
{"key", AF_KEY},
{"netlink", AF_NETLINK},
{"packet", AF_PACKET},
{"ash", AF_ASH},
{"econet", AF_ECONET},
{"atmsvc", AF_ATMSVC},
{"rds", AF_RDS},
{"sna", AF_SNA},
{"irda", AF_IRDA},
{"pppox", AF_PPPOX},
{"wanpipe", AF_WANPIPE},
{"llc", AF_LLC},
{"can", AF_CAN},
{"tipc", AF_TIPC},
{"bluetooth", AF_BLUETOOTH},
{"iucv", AF_IUCV},
{"rxrpc", AF_RXRPC},
{"isdn", AF_ISDN},
{"phonet", AF_PHONET},
{"ieee802154", AF_IEEE802154},
{"caif", 37},
{"alg", 38},
{"nfc", 39},
{"vsock", 40},
});
bool InterpretSockaddrField(std::string& out, const EventRecord& record, const EventRecordField& field) {
// It is assumed that a sockaddr will never exceed 1024 bytes
std::array<uint8_t, 1024> _buf;
if (!decode_hex(_buf.data(), _buf.size(), field.RawValuePtr(), field.RawValueSize())) {
out = "malformed-host(";
out.append(field.RawValue());
out.append(")");
return false;
}
size_t bsize = field.RawValueSize()/2;
const struct sockaddr *saddr = reinterpret_cast<struct sockaddr *>(_buf.data());
out.append("{ fam=");
auto fam = s_fam_table.ToString(saddr->sa_family);
if (!fam.empty()) {
out.append(fam);
} else {
out.append("unknown-family(");
out.append(std::to_string(saddr->sa_family));
out.append(")");
}
out.append(" ");
switch (saddr->sa_family) {
case AF_LOCAL: {
auto addr = reinterpret_cast<struct sockaddr_un *>(_buf.data());
// Calculate the sun_path size based on what was actually provided by the kernel
// What the kernel emits may be smaller than sizeof(addr->sun_path) and may not be null terminated.
size_t path_size = bsize-offsetof(struct sockaddr_un, sun_path);
out.append("path=");
if (addr->sun_path[0] != 0) {
// The sun_path might not be NUL terminated, so limit strlen to the minimum of path_size or sizeof(addr->sun_path)
out.append(addr->sun_path, strnlen(addr->sun_path, std::min(path_size, sizeof(addr->sun_path))));
} else {
out.push_back('@');
if (path_size > 1) {
out.append(&addr->sun_path[1], path_size - 1);
}
}
out.append(" }");
break;
}
case AF_INET: {
std::array<char, INET_ADDRSTRLEN+1> _abuf;
auto addr = reinterpret_cast<struct sockaddr_in *>(_buf.data());
if (bsize < sizeof(struct sockaddr_in)) {
out.append("sockaddr len too short }");
break;
}
if (inet_ntop(AF_INET, &addr->sin_addr, _abuf.data(), _abuf.size()) != nullptr) {
out.append("laddr=");
out.append(_abuf.data());
} else {
out.append("(error resolving addr) }");
break;
}
out.append(" lport=");
append_int(out, addr->sin_port);
out.append(" }");
break;
}
#ifndef NO_INTERP_EXTRA_PROTO
case AF_AX25: {
auto addr = reinterpret_cast<struct sockaddr_ax25 *>(_buf.data());
if (bsize < sizeof(struct sockaddr_ax25)) {
out.append("ax25 len too short }");
break;
}
out.append("call=");
tty_escape_string_append(out, addr->sax25_call.ax25_call, sizeof(addr->sax25_call.ax25_call));
out.append(" }");
break;
}
case AF_IPX: {
auto addr = reinterpret_cast<struct sockaddr_ipx *>(_buf.data());
if (bsize < sizeof(struct sockaddr_ipx)) {
out.append("ipx len too short }");
break;
}
out.append("lport=");
append_int(out, addr->sipx_port);
out.append("ipx-net=");
append_uint(out, addr->sipx_network);
out.append(" }");
break;
}
case AF_ATMPVC: {
auto addr = reinterpret_cast<struct sockaddr_atmpvc *>(_buf.data());
if (bsize < sizeof(struct sockaddr_atmpvc)) {
out.append("atmpvc len too short }");
break;
}
out.append("int=");
append_uint(out, addr->sap_addr.itf);
out.append(" }");
break;
}
case AF_X25: {
auto addr = reinterpret_cast<struct sockaddr_x25 *>(_buf.data());
if (bsize < sizeof(struct sockaddr_x25)) {
out.append("x25 len too short }");
break;
}
out.append("laddr=");
// Valid X25 address is null terminated and only decimal digits (0-9).
addr->sx25_addr.x25_addr[15] = 0;
out.append(addr->sx25_addr.x25_addr);
break;
}
#endif
case AF_INET6: {
std::array<char, INET6_ADDRSTRLEN+1> _abuf;
auto addr = reinterpret_cast<struct sockaddr_in6 *>(_buf.data());
if (bsize < sizeof(struct sockaddr_in6)) {
out.append("sockaddr6 len too short }");
break;
}
if (inet_ntop(AF_INET6, &addr->sin6_addr, _abuf.data(), _abuf.size()) != nullptr) {
out.append("laddr=");
out.append(_abuf.data());
} else {
out.append("(error resolving addr) }");
break;
}
out.append(" lport=");
append_int(out, addr->sin6_port);
out.append(" }");
break;
}
case AF_NETLINK: {
auto addr = reinterpret_cast<struct sockaddr_nl *>(_buf.data());
if (bsize < sizeof(struct sockaddr_nl)) {
out.append("netlink len too short }");
break;
}
out.append("nlnk-fam=");
append_uint(out, addr->nl_family);
out.append("nlnk-pid=");
append_uint(out, addr->nl_pid);
out.append(" }");
break;
}
default:
out.append("(unsupported) }");
break;
}
return true;
}
bool InterpretField(std::string& out, const EventRecord& record, const EventRecordField& field, field_type_t field_type) {
static std::string_view SV_ARCH = "arch";
switch (field_type) {
case field_type_t::ARCH: {
uint32_t arch;
if (!field_to_uint(field, arch, 16)) {
arch = 0;
}
auto m = ArchToMachine(arch);
if (m == MachineType::UNKNOWN) {
Logger::Warn("InterpretField: Invalid arch=%s", field.RawValuePtr());
out = "unknown-arch(" + std::string(field.RawValue()) + ")";
} else {
MachineToName(m, out);
}
return true;
}
case field_type_t::SYSCALL: {
auto arch_field = record.FieldByName(SV_ARCH);
if (!arch_field) {
out = "unknown-syscall(" + std::string(field.RawValue()) + ")";
return true;
}
uint32_t arch;
if (!field_to_uint(arch_field, arch, 16)) {
arch = 0;
}
auto mt = ArchToMachine(arch);
if (mt == MachineType::UNKNOWN) {
Logger::Warn("InterpretField: Invalid arch=%s", arch_field.RawValuePtr());
out = "unknown-syscall(" + std::string(field.RawValue()) + ")";
return true;
}
int syscall;
if (field_to_int(field, syscall, 10)) {
SyscallToName(mt, syscall, out);
} else {
out = "unknown-syscall(" + std::string(field.RawValue()) + ")";
}
return true;
}
case field_type_t::SOCKADDR:
return InterpretSockaddrField(out, record, field);
case field_type_t::SESSION:
if (field.RawValue() == "4294967295") {
out = "unset";
return true;
}
return false;
case field_type_t::MODE: {
if (out.capacity() < 128) {
out.reserve(128);
}
out.resize(0);
unsigned int mode;
if (!field_to_int(field, mode, 8)) {
out = "unknown-mode(" + std::string(field.RawValue()) + ")";
}
switch (mode & S_IFMT) {
case S_IFSOCK:
out = "socket";
break;
case S_IFLNK:
out = "link";
break;
case S_IFREG:
out = "file";
break;
case S_IFBLK:
out = "block";
break;
case S_IFDIR:
out = "dir";
break;
case S_IFCHR:
out = "character";
break;
case S_IFIFO:
out = "fifo";
break;
default: {
auto ptr = out.data() + out.size();
out.resize(out.size() + 4);
snprintf(ptr, 4, "%03o", (mode & S_IFMT) / (S_IFMT & ~(S_IFMT - 1)));
out.resize(out.size() - 1); // Remove NULL added by snprintf
break;
}
}
if (mode & S_ISUID) {
out.append(",suid");
}
if (mode & S_ISGID) {
out.append(",sgid");
}
if (mode & S_ISVTX) {
out.append(",sticky");
}
out.push_back(',');
auto ptr = out.data() + out.size();
out.resize(out.size() + 4);
snprintf(ptr, 4, "%03o", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
out.resize(out.size() - 1); // Remove NULL added by snprintf
return true;
}
default:
return false;
}
}