-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.js
270 lines (246 loc) · 6.32 KB
/
parser.js
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
const
Pri = require("./pri.js"),
CEF = require("./cef.js"),
LEEF = require('./leef.js');
const RXS = {
"pri" : /^<\d+>/,
"prinmr" : /^\d+ /,
"prival" : /<(\d+)>/,
"month" : /^[A-Za-z][a-z]{2} /,
"day" : /^\d{1,2} /,
"time" : /^\d+:\d+:\d+ /,
"ts" : /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\S+ /,
"invalid" : /[^a-zA-Z0-9\.\$\-_#%\/\[\]\(\)]/,
"sdata" : /\[(\S+)( [^\=]+\=\"[^\"]*\")+\]/g,
"asdata" : /^\s*[^\[]+\[/,
"bsdata" : /^\s*\[/,
"cef" : /^CEF:\d+/,
"leef" : /^LEEF:(1|2)\.0/
}
const DOPS = {
cef : true,
leef : true,
fields : true,
pid : true,
generateTimestamp: true
}
function peek(arr) {
do {
var item = arr.shift();
if(item===undefined) return item;
else item = item.trim();
}while(!item);
return item;
}
function assign(entry,item) {
if(!entry.host) entry.host = item.trim();
else if(!entry.appName) entry.appName = item.trim();
else if(!entry.pid) entry.pid = item.trim();
else if(!entry.messageid) entry.messageid = item.trim();
else if(!entry.structuredData) {
entry.structuredData = item.trim();
return false;
}
else return true;
}
function parse(line,opts) {
if(opts)
opts = Object.assign({},DOPS,opts);
else
opts = DOPS;
var pri = line.match(RXS.pri);
var entry = {
originalMessage : line
};
// First priority
if(pri) {
entry.pri = pri[0];
entry.prival = parseInt(entry.pri.match(RXS.prival)[1]);
var prival = Pri.get(entry.prival);
entry.facilityval = prival.facility;
entry.levelval = prival.level;
entry.facility = Pri.FACILITY[prival.facility].id;
entry.level = Pri.LEVEL[prival.level].id;
}
else {
entry.pri = "";
entry.prival = NaN;
}
//Split message
var items = line.substring(entry.pri.length).split(" ");
// Date search
var endparse = false;
while(line.length && !endparse) {
var item = peek(items)+" ";
// RFC RFC5424
if(item.match(RXS.prinmr)) {
entry.version = parseInt(item);
entry.type = "RFC5424";
item = peek(items)+" ";
if(item.match(RXS.ts)) {
entry.ts = new Date(Date.parse(item.match(RXS.ts)[0].trim()));
}
}
// BSD
else if(item.match(RXS.month)) {
entry.type = "BSD";
var month = item.trim();
var day = peek(items);
var time = peek(items);
var year = new Date().getYear() + 1900
entry.ts = new Date(Date.parse(year+" "+month+" "+day+" "+time));
}
else {
entry.type = "UNKNOWN";
items.unshift(item.trim());
}
endparse = true;
}
// No timestamp
if(!entry.ts && opts.generateTimestamp) entry.ts = new Date();
// Is a standard syslog message
if(entry.type) {
endparse = false;
function invalidate(item) {
items.unshift(item);
entry.message = items.join(" ");
endparse = true;
}
while(line.length && !endparse) {
var item = peek(items);
if(!item) {
endparse = true;
}
else if(item.endsWith(":")){
let eitem = item.replace(/:$/,"").trim();
if(eitem.match(RXS.invalid)) {
invalidate(item);
}
else {
assign(entry,eitem)
entry.message = items.join(" ");
endparse = true;
}
}
else if(!items.length) {
invalidate(item);
}
else {
// Invalid item (malformed message)
if(item.match(RXS.invalid)) {
invalidate(item);
}
else {
var r = assign(entry,item.replace(/: $/,"").trim())
if(r===true) {
items.unshift(item);
entry.message = items.join(" ");
endparse = true;
}
else if(r===false) {
entry.message = items.join(" ");
endparse = true;
}
}
}
}
}
else {
entry.message = items.join(" ");
}
// Chained hostnames
entry.chain = (entry.host||"").split("/");
entry.host = entry.chain.pop();
// Structured data
if(entry.type=="RFC5424") {
// Look if sdata if before or after message
let bsdata = RXS.bsdata.test(entry.message);
let asdata = RXS.asdata.test(entry.message);
var sdata = entry.message.match(RXS.sdata) || [];
var idx=0;
entry.structuredData = sdata.map(item=>{
var map = {}, nokeys = [];
var lastKey = null;
idx = entry.message.indexOf(item)+item.length+1;
item.replace(/(^\[)|(\]$)/g,"").split(" ").forEach((t,i)=>{
// Extra space
if(!t.trim()) return;
// First element (ID of data)
if(i==0) {
map["$id"] = t;
}
// Key/Pair values
else {
var kv = t.split("=");
// Correct key/value pair
if(kv[0] && kv[1] && kv[1]!='"') {
lastKey = kv.shift();
map[lastKey] = kv.join("=").replace(/\"/g,"");
}
// Last key had values separated by spaces
else if(kv[0] && kv[1]===undefined){
map[lastKey] += " "+(kv[0]||"").replace(/\"/g,"");
}
else if(kv[0] && (!kv[1].length || kv[1]=='"')){
map[lastKey] += " "+(kv[0]||"").replace(/\"/g,"")+"=";
}
}
});
return map;
});
// Structured data parsed successfuly
if(entry.structuredData.length) {
let sidx = entry.message.indexOf("[");
// sdata before message
if(bsdata) {
if(sidx>=0) entry.header = line.substring(0,line.length-entry.message.length);
entry.message = entry.message.substring(idx);
}
// sdata after message
else if(asdata) {
if(sidx>=0) {
entry.header = line.substring(0,line.length-entry.message.length);
entry.message = entry.message.substring(0,sidx);
}
}
}
}
// CEF Event message
if(opts.cef!==false && RXS.cef.test(entry.message)) {
entry.type = "CEF";
let cef = CEF.parse(entry.message);
entry.cef = cef.headers;
entry.fields = cef.fields;
}
// LEEF Event message
else if(opts.leef!==false && RXS.leef.test(entry.message)) {
entry.type = "LEEF";
let leef = LEEF.parse(entry.message);
entry.leef = leef.headers;
entry.fields = leef.fields;
}
// Default syslog message
else if(opts.fields!==false && entry.type!="UNKNOWN"){
// Message with fields
var fields = [];
entry.message.split(",").forEach(kv=>{
var prop = kv.split("=");
if(prop.length==2)
fields[prop[0]] = prop[1];
});
entry.fields = fields;
}
// header
entry.header = entry.header || line.substring(0,line.length-entry.message.length);
entry.message = entry.message.trim();
// PID
if(opts.pid && entry.appName && entry.appName.endsWith("]")) {
let idx = entry.appName.indexOf("[");
if(idx>=0) {
entry.pid = entry.appName.substring(idx+1,entry.appName.length-1);
entry.appName = entry.appName.substring(0,idx);
}
}
return entry;
}
module.exports = function(line,opts) {try {return parse(line,opts)}catch(err){return {err:err}}};