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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/* pmsnare.c
*
* this detects logs sent by Snare and cleans them up so that they can be processed by the normal parser
*
* there are two variations of this, if the client is set to 'syslog' mode it sends
*
* <pri>timestamp<sp>hostname<sp>tag<tab>otherstuff
*
* if the client is not set to syslog it sends
*
* hostname<tab>tag<tab>otherstuff
*
* The tabs can be represented in different ways. This module will auto-detect the tab representation based on
* the global config settings, but they can be overridden for each instance in the config file if needed.
*
* ToDo, take advantage of items in the message itself to set more friendly information
* where the normal parser will find it by re-writing more of the message
*
* Interesting information includes:
*
* in the case of windows snare messages:
* the system hostname is field 12
* the severity is field 3 (criticality ranging form 0 to 4)
* the source of the log is field 4 and may be able to be mapped to facility
*
*
* created 2010-12-13 by David Lang based on pmlastmsg
* Modified 2017-05-29 by Shane Lawrence.
*
* This file is part of rsyslog.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* -or-
* see COPYING.ASL20 in the source distribution
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "rsyslog.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "conf.h"
#include "syslogd-types.h"
#include "template.h"
#include "msg.h"
#include "module-template.h"
#include "glbl.h"
#include "errmsg.h"
#include "parser.h"
#include "datetime.h"
#include "unicode-helper.h"
#include "rsconf.h"
MODULE_TYPE_PARSER
MODULE_TYPE_NOKEEP
PARSER_NAME("rsyslog.snare")
MODULE_CNFNAME("pmsnare")
/* internal structures
*/
DEF_PMOD_STATIC_DATA
DEFobjCurrIf(glbl)
DEFobjCurrIf(parser)
DEFobjCurrIf(datetime)
/* static data */
static int bParseHOSTNAMEandTAG; /* cache for the equally-named global param - performance enhancement */
/* Keep a list of parser instances so we can apply global settings after config is loaded. */
typedef struct modInstances_s {
instanceConf_t *root;
instanceConf_t *tail;
} modInstances_t;
static modInstances_t *modInstances = NULL;
struct modConfData_s {
rsconf_t *pConf; /* our overall config object */
};
static modConfData_t *modConf = NULL;
/* Per-instance config settings. */
static struct cnfparamdescr parserpdescr[] = {
{ "parser.controlcharacterescapeprefix", eCmdHdlrGetChar, 0 },
{ "parser.escapecontrolcharactersonreceive", eCmdHdlrBinary, 0 },
{ "parser.escapecontrolcharactertab", eCmdHdlrBinary, 0},
{ "parser.escapecontrolcharacterscstyle", eCmdHdlrBinary, 0 }
};
static struct cnfparamblk parserpblk = {
CNFPARAMBLK_VERSION,
sizeof(parserpdescr)/sizeof(struct cnfparamdescr),
parserpdescr
};
struct instanceConf_s {
int bEscapeCCOnRcv;
int bEscapeTab;
int bParserEscapeCCCStyle;
uchar cCCEscapeChar;
int tabLength;
char tabRepresentation[5];
struct instanceConf_s *next;
};
/* Creates the instance and adds it to the list of instances. */
static rsRetVal createInstance(instanceConf_t **pinst) {
instanceConf_t *inst;
DEFiRet;
CHKmalloc(inst = malloc(sizeof(instanceConf_t)));
inst->next = NULL;
*pinst = inst;
/* Add to list of instances. */
if(modInstances == NULL) {
CHKmalloc(modInstances = malloc(sizeof(modInstances_t)));
modInstances->tail = modInstances->root = NULL;
}
if (modInstances->tail == NULL) {
modInstances->tail = modInstances->root = inst;
} else {
modInstances->tail->next = inst;
modInstances->tail = inst;
}
finalize_it:
RETiRet;
}
BEGINnewParserInst
struct cnfparamvals *pvals = NULL;
int i;
CODESTARTnewParserInst
DBGPRINTF("newParserInst (pmsnare)\n");
inst = NULL;
CHKiRet(createInstance(&inst));
/* Mark these as unset so we know if they should be overridden later. */
inst->bEscapeCCOnRcv = -1;
inst->bEscapeTab = -1;
inst->bParserEscapeCCCStyle = -1;
inst->cCCEscapeChar = '\0';
/* If using the old config, just use global settings for each instance. */
if (lst == NULL)
FINALIZE;
/* If using the new config, process module settings for this instance. */
if((pvals = nvlstGetParams(lst, &parserpblk, NULL)) == NULL) {
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS);
}
if(Debug) {
dbgprintf("pmsnare: parser param blk:\n");
cnfparamsPrint(&parserpblk, pvals);
}
for(i = 0 ; i < parserpblk.nParams ; ++i) {
if(!pvals[i].bUsed)
continue;
if(!strcmp(parserpblk.descr[i].name, "parser.escapecontrolcharactersonreceive")) {
inst->bEscapeCCOnRcv = pvals[i].val.d.n;
} else if(!strcmp(parserpblk.descr[i].name, "parser.escapecontrolcharactertab")) {
inst->bEscapeTab = pvals[i].val.d.n;
} else if(!strcmp(parserpblk.descr[i].name, "parser.escapecontrolcharacterscstyle")) {
inst->bParserEscapeCCCStyle = pvals[i].val.d.n;
} else if(!strcmp(parserpblk.descr[i].name, "parser.controlcharacterescapeprefix")) {
inst->cCCEscapeChar = (uchar) *es_str2cstr(pvals[i].val.d.estr, NULL);
} else {
dbgprintf("pmsnare: program error, non-handled param '%s'\n", parserpblk.descr[i].name);
}
}
finalize_it:
CODE_STD_FINALIZERnewParserInst
if(lst != NULL)
cnfparamvalsDestruct(pvals, &parserpblk);
if(iRet != RS_RET_OK)
free(inst);
ENDnewParserInst
BEGINfreeParserInst
CODESTARTfreeParserInst
dbgprintf("pmsnare: free parser instance %p\n", pInst);
ENDfreeParserInst
BEGINisCompatibleWithFeature
CODESTARTisCompatibleWithFeature
if(eFeat == sFEATUREAutomaticSanitazion)
iRet = RS_RET_OK;
if(eFeat == sFEATUREAutomaticPRIParsing)
iRet = RS_RET_OK;
ENDisCompatibleWithFeature
/* Interface with the global config. */
BEGINbeginCnfLoad
CODESTARTbeginCnfLoad
modConf = pModConf;
pModConf->pConf = pConf;
ENDbeginCnfLoad
BEGINsetModCnf
CODESTARTsetModCnf
/* Could use module-globals here, but not global globals. */
(void) lst;
ENDsetModCnf
BEGINendCnfLoad
instanceConf_t *inst;
CODESTARTendCnfLoad
dbgprintf("pmsnare: Begin endCnfLoad\n");
/* Loop through each parser instance and apply global settings to any option that hasn't been overridden.
* This can't be done any earlier because the config wasn't fully loaded until now. */
for(inst = modInstances->root; inst != NULL; inst = inst->next) {
if(inst->bEscapeCCOnRcv == -1)
inst->bEscapeCCOnRcv = glbl.GetParserEscapeControlCharactersOnReceive(modConf->pConf);
if(inst->bEscapeTab == -1)
inst->bEscapeTab = glbl.GetParserEscapeControlCharacterTab(modConf->pConf);
if(inst->bParserEscapeCCCStyle == -1)
inst->bParserEscapeCCCStyle = glbl.GetParserEscapeControlCharactersCStyle(modConf->pConf);
if(inst->cCCEscapeChar == '\0')
inst->cCCEscapeChar = glbl.GetParserControlCharacterEscapePrefix(modConf->pConf);
/* Determine tab representation. Possible options:
* "#011" escape on, escapetabs on, no change to prefix (default)
* "?011" prefix changed in config
* "\\t" C style
* '\t' escape turned off
*/
if (inst->bEscapeCCOnRcv && inst->bEscapeTab) {
if (inst->bParserEscapeCCCStyle) {
strncpy(inst->tabRepresentation, "\\t", 5);
} else {
strncpy(inst->tabRepresentation, "#011", 5);
inst->tabRepresentation[0] = inst->cCCEscapeChar;
}
} else {
strncpy(inst->tabRepresentation, "\t", 5);
}
inst->tabLength=strlen(inst->tabRepresentation);
/* TODO: This debug message would be more useful if it told which Snare instance! */
dbgprintf("pmsnare: Snare parser will treat '%s' as tab.\n", inst->tabRepresentation);
}
assert(pModConf == modConf);
ENDendCnfLoad
BEGINcheckCnf
CODESTARTcheckCnf
ENDcheckCnf
BEGINactivateCnf
CODESTARTactivateCnf
ENDactivateCnf
BEGINfreeCnf
instanceConf_t *inst, *del;
CODESTARTfreeCnf
for(inst = modInstances->root ; inst != NULL ; ) {
del = inst;
inst = inst->next;
free(del);
}
free(modInstances);
ENDfreeCnf
BEGINparse2
uchar *p2parse;
int lenMsg;
int snaremessage; /* 0 means not a snare message, otherwise it's the index of the tab after the tag */
CODESTARTparse2
dbgprintf("Message will now be parsed by fix Snare parser.\n");
assert(pMsg != NULL);
assert(pMsg->pszRawMsg != NULL);
/* check if this message is of the type we handle in this (very limited) parser
*
* - Find out if the first separator is a tab.
* - If it is, see if the second word is one of our expected tags.
* - If so, flag as Snare and replace the first tab with space so that
* hostname and syslog tag are going to be parsed properly
* - Else not a snare message, abort.
* - Else assume valid 3164 timestamp, move over to the syslog tag.
* - See if syslog header is followed by tab and one of our expected tags.
* - If so, flag as Snare.
* - See if either type flagged as Snare.
* - If so, replace the tab with a space so that it will be parsed properly.
*/
snaremessage=0;
/* note: offAfterPRI is already the number of PRI chars (do not add one!) */
lenMsg = pMsg->iLenRawMsg - pMsg->offAfterPRI;
/* point to start of text, after PRI */
p2parse = pMsg->pszRawMsg + pMsg->offAfterPRI;
dbgprintf("pmsnare: msg to look at: [%d]'%s'\n", lenMsg, p2parse);
if((unsigned) lenMsg < 30) {
/* too short, can not be "our" message */
dbgprintf("pmsnare: Message is too short to be Snare!\n");
ABORT_FINALIZE(RS_RET_COULD_NOT_PARSE);
}
/* Find the first separator and check if it's a tab. */
while(lenMsg && *p2parse != ' ' && *p2parse != '\t' && *p2parse != pInst->tabRepresentation[0]) {
--lenMsg;
++p2parse;
}
if ((lenMsg > pInst->tabLength) && (strncasecmp((char *)p2parse, pInst->tabRepresentation,
pInst->tabLength) == 0)) {
dbgprintf("pmsnare: tab separated message\n");
dbgprintf("pmsnare: tab [%d]'%s' msg at the first separator: [%d]'%s'\n",
pInst->tabLength, pInst->tabRepresentation, lenMsg, p2parse);
/* Look for the Snare tag. */
if(strncasecmp((char*)(p2parse + pInst->tabLength), "MSWinEventLog", 13) == 0) {
dbgprintf("Found a non-syslog Windows Snare message.\n");
snaremessage = p2parse - pMsg->pszRawMsg + pInst->tabLength + 13;
}
else if(strncasecmp((char*) (p2parse + pInst->tabLength), "LinuxKAudit", 11) == 0) {
dbgprintf("Found a non-syslog Linux Snare message.\n");
snaremessage = p2parse - pMsg->pszRawMsg + pInst->tabLength + 11;
} else {
/* Tab-separated but no Snare tag-> can't be Snare! */
ABORT_FINALIZE(RS_RET_COULD_NOT_PARSE);
}
/* This is a non-syslog Snare message. Example:
* other.lab.home MSWinEventLog 1 Security 606129 Wed May 17 02:25:10 2017
*/
/* Remove the tab between the hostname and Snare tag. */
*p2parse = ' ';
p2parse++;
lenMsg--;
lenMsg -= (pInst->tabLength-1); /* size of tab goes from tabLength to 1, so shorten
the message by the difference */
memmove(p2parse, p2parse+(pInst->tabLength-1), lenMsg);
/* move the message portion up to overwrite the tab */
*(p2parse + lenMsg) = '\0';
pMsg->iLenRawMsg -= (pInst->tabLength-1);
pMsg->iLenMSG -= (pInst->tabLength-1);
snaremessage -= (pInst->tabLength-1);
} else {
/* The first separator is not a tab. Look for a syslog Snare message. Example:
* <14>May 17 02:25:10 syslog.lab.home MSWinEventLog 1 Security 606129
Wed May 17 02:25:10 2017
*/
/* go back to the beginning of the message */
lenMsg = pMsg->iLenRawMsg - pMsg->offAfterPRI;
/* offAfterPRI is already the number of PRI chars (do not add one!) */
p2parse = pMsg->pszRawMsg + pMsg->offAfterPRI;
/* skip over timestamp and space (15 chars + space). */
lenMsg -=16;
p2parse +=16;
/* skip over what should be the hostname and space */
while(lenMsg && *p2parse != ' ') {
--lenMsg;
++p2parse;
}
if (lenMsg){
--lenMsg;
++p2parse;
}
dbgprintf("pmsnare: tab [%d]'%s' msg after the timestamp and hostname: [%d]'%s'\n",
pInst->tabLength,pInst->tabRepresentation,lenMsg, p2parse);
/* Look for the Snare tag. */
if(lenMsg > 13 && strncasecmp((char*) p2parse, "MSWinEventLog", 13) == 0) {
dbgprintf("Found a syslog Windows Snare message.\n");
snaremessage = p2parse - pMsg->pszRawMsg + 13;
}
else if(lenMsg > 11 && strncasecmp((char*) p2parse, "LinuxKAudit", 11) == 0) {
dbgprintf("pmsnare: Found a syslog Linux Snare message.\n");
snaremessage = p2parse - pMsg->pszRawMsg + 11;
}
}
if(snaremessage) {
/* Skip to the end of the tag. */
p2parse = pMsg->pszRawMsg + snaremessage;
lenMsg = pMsg->iLenRawMsg - snaremessage;
/* Remove the tab after the tag. */
*p2parse = ' ';
p2parse++;
lenMsg--;
lenMsg -= (pInst->tabLength-1); /* size of tab goes from tabLength to 1, so shorten
the message by the difference */
memmove(p2parse, p2parse+(pInst->tabLength-1), lenMsg);
/* move the message portion up to overwrite the tab */
*(p2parse + lenMsg) = '\0';
pMsg->iLenRawMsg -= (pInst->tabLength-1);
pMsg->iLenMSG -= (pInst->tabLength-1);
DBGPRINTF("pmsnare: new message: [%d]'%s'\n", lenMsg, pMsg->pszRawMsg + pMsg->offAfterPRI);
}
ABORT_FINALIZE(RS_RET_COULD_NOT_PARSE);
finalize_it:
ENDparse2
BEGINmodExit
CODESTARTmodExit
/* release what we no longer need */
objRelease(glbl, CORE_COMPONENT);
objRelease(parser, CORE_COMPONENT);
objRelease(datetime, CORE_COMPONENT);
ENDmodExit
BEGINqueryEtryPt
CODESTARTqueryEtryPt
CODEqueryEtryPt_STD_MOD_QUERIES
CODEqueryEtryPt_STD_CONF2_QUERIES
CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES
CODEqueryEtryPt_STD_PMOD2_QUERIES
CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES
ENDqueryEtryPt
BEGINmodInit()
CODESTARTmodInit
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
CODEmodInit_QueryRegCFSLineHdlr
CHKiRet(objUse(glbl, CORE_COMPONENT));
CHKiRet(objUse(parser, CORE_COMPONENT));
CHKiRet(objUse(datetime, CORE_COMPONENT));
DBGPRINTF("snare parser init called, compiled with version %s\n", VERSION);
bParseHOSTNAMEandTAG = glbl.GetParseHOSTNAMEandTAG(loadConf);
/* cache value, is set only during rsyslogd option processing */
ENDmodInit
/* vim:set ai:
*/
|