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 | 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× | 'use strict'; /** Code partially adapted from node-cef logging library, used under MPL 2.0 License. */ const _ = require('lodash'); const util = require('util'); const requiredParams = ['vendor', 'product', 'version', 'signature', 'name', 'severity']; const CEF_VERSION = '0'; var Formatter = module.exports = function Formatter(config) { config = config || {}; this.vendor = config.vendor || 'Menlo Security'; this.product = config.product || 'MSIP'; this.version = config.version || '1.0'; this.signature = config.signature || '100'; this.severity = config.severity || '5'; return this; }; Formatter.prototype = { /** * Convert a string to a legal CEF string * * @param text * (string) The text to filter * * According to the CEF:0 spec, page 4: * * - Escape pipes (|) in the prefix, but not necessarily the * extension. * * - Escape backslashes (\) in the prefix, but not necessarily the * extension. * * - Escape equal signs (=) in the extensions, but not necessarily * the prefix. * * - Multi-line fields can be sent by Common Event Format (CEF) by * encoding the newline character as \n or \r. Multiple lines * are only allowed in the value part of the extensions. * * Therefore we have two text sanitizers: * * - sanitizePrefixField() * - sanitizeExtensionValue() * */ inputToString: function inputToString(input) { if (typeof input === 'undefined') { return typeof input; } else if (typeof input === 'object') { return JSON.stringify(input, null, 2); } return input.toString(); }, /** * Escape pipes and backslashes in the prefix. Equal signs are ok. Newlines are forbidden. */ sanitizePrefixField: function sanitizePrefixField(input) { input = this.inputToString(input); var output = ''; var nextChar = ''; for (var i = 0; i < input.length; i++) { // A backslash is already escaping the next char? if (input[i] === '\\') { nextChar = input[i+1]; if (/[\\|]/.test(nextChar)) { output += '\\' + nextChar; i += 1; } else { output += '\\\\'; } // An unescaped backslash or pipe? } else if (/[\\|]/.test(input[i])) { if (input[i-1] !== '\\') { output += '\\' + input[i]; } // Replace newlines with a space to maintain some legibility } else if (/[\r\n]/.test(input[i])) { output += ' '; } else { output += input[i]; } } return output; }, /** * Escape equal signs in the extensions. Canonicalize newlines. * CEF spec leaves it up to us to choose \r or \n for newline. * We choose \n as the default. */ sanitizeExtensionValue: function sanitizeExtensionValue(input) { input = this.inputToString(input); var output = ''; for (var i = 0; i < input.length; i++) { // Escape equal signs if (input[i] === '=') { if (input[i-1] !== '\\') { output += '\\' + input[i]; } else { output += input[i]; } // Canonicalize whitespace } else if (input[i] === '\r') { // convert \r\n to \n if (input[i+1] === '\n') { output += '\n'; i += 1; // convert plain \r to \n } else { output += '\n'; } } else { output += input[i]; } } return output; }, /** * Format log message extensions * * @param extensions * (object) A dictionary of key/value pairs to format * * Returns a string like 'key1=value1 key2=value2'. Whitespace in * values is preserved (with some possible modification of * newlines). */ formatExtensions: function formatExtensions(extensions) { if (typeof extensions !== 'object') { return ''; } // Convert extensions dictionary to a string like 'food=pie barm=42' var extensionArray = []; var value = ''; Object.keys(extensions).forEach(function(key) { value = extensions[key]; extensionArray.push(util.format('%s=%s', key, this.sanitizeExtensionValue(value))); }.bind(this)); return extensionArray.join(' '); }, /** * Format KVP messages * * @param extensions * (object) A dictionary of key/value pairs to format * * Returns a string like "key1='value1' key2='value2'". Whitespace in * values is preserved (with some possible modification of * newlines). */ formatExtensionsKVP: function formatExtensionsKVP(extensions) { if (typeof extensions !== 'object') { return ''; } // Convert extensions dictionary to a string like 'food=pie barm=42' var extensionArray = []; var value = ''; var string_to_push = ''; Object.keys(extensions).forEach(function(key) { value = this.sanitizeExtensionValue(extensions[key]); if (value.indexOf(' ') !== -1) { string_to_push = util.format("%s='%s'", //jshint ignore:line key, value); } else { string_to_push = util.format('%s=%s', key, value); } extensionArray.push(string_to_push); }.bind(this)); return extensionArray.join(' '); }, /* * Find the event type from the payload. */ getEventType: function getEventType(payload) { var potential_events = ['file_download', 'file_upload', 'flash', 'ssl_inspection_exception', 'page_request']; var event_type = 'unknown_event_type'; _.forEach(potential_events, function(potential_event) { if (payload[potential_event] === 'true') { event_type = potential_event; return false; } }); return event_type; }, /* * Accept dictionary containing log values. Create a key called extensions that * correlates to the extension dictionary. */ transformPayloadKVP: function transformPayloadKVP(payload) { //elements of the logs that could have multiple elements var list_extensions = {'server_top_level_details': 'threats', 'top_level_risks': 'threat_types', 'cats': 'categories', 'dest_ips': 'dst'}; var necessary_fields = new Set(['pe_action', 'protocol', 'userid', 'request_type','browser_and_version', 'x-client-ip', 'url', 'event_time', 'referer', 'response_code', 'content-type', 'user-agent']); var payload_info = {}; _.forEach(payload, function (value, key) { if (key in list_extensions && value.length > 0) { payload_info[list_extensions[key]] = value.join(', '); } else if (necessary_fields.has(key) && value) { payload_info[key] = value; } else if (_.contains(requiredParams, key) && value) { payload_info[key] = value; } }); payload_info.name = this.getEventType(payload); return payload_info; }, /* * Accept dictionary containing log values. Create a key called extensions that * correlates to the extension dictionary. Translate all possible keys to their * CEF equivalent according to the HP Arcsight Standards. * See Logging Document: https://goo.gl/ZNKzJc */ transformPayload: function transformPayload(payload) { //elements of the logs that could have multiple elements var list_extensions = {'server_top_level_details': 'threats', 'top_level_risks': 'threat_types', 'cats': 'categories', 'dest_ips': 'dst'}; var cef_changes = {'pe_action': 'act', 'protocol': 'app', 'userid': 'suid', 'request_type': 'requestMethod', 'browser_and_version': 'requestClientApplication', 'x-client-ip': 'src', 'url': 'request', 'event_time': 'end', 'referer': 'referer', 'response_code': 'responseCode', 'content-type': 'contentType', 'user-agent': 'requestClientApplication'}; var extensions = {}; var cef_info = {}; if (payload.browser_and_version !== 'undefined_undefined') { payload['user-agent'] = payload.browser_and_version; } else { payload.browser_and_version = payload['user-agent']; } _.forEach(payload, function (value, key) { if (key in list_extensions && value.length > 0) { extensions[list_extensions[key]] = value.join(', '); } else if (key in cef_changes && value) { extensions[cef_changes[key]] = value; } else if (_.contains(requiredParams, key) && value) { cef_info[key] = value; } }); cef_info.extensions = extensions; cef_info.name = this.getEventType(payload); return cef_info; }, addProductInfo: function addProductInfo(params) { // If none supplied, use default vendor, product, version if (! (params.vendor && params.product)) { params.vendor = this.vendor; params.product = this.product; } if (! params.version) { params.version = this.version; } if (! params.severity) { params.severity = this.severity; } if (! params.signature) { params.signature = this.signature; } return params; }, formatKVP: function formatKVP(params) { params = this.transformPayloadKVP(params); params = this.addProductInfo(params); return this.formatExtensionsKVP(params); }, formatCEF: function formatCEF(params) { var err = null; params = this.transformPayload(params); var extensions = params.extensions; params = this.addProductInfo(params); // Check that required params are present and contain a value. // If not, return an error. var paramKeys = Object.keys(params); requiredParams.forEach(function checkRequiredKeys(requiredKey) { if (paramKeys.indexOf(requiredKey) === -1) { err = new Error(util.format("Missing required key '%s' from params", //jshint ignore: line requiredKey)); } else { // Sanitize prefix fields params[requiredKey] = this.sanitizePrefixField(params[requiredKey]); } }.bind(this)); if (err) { return (err); } params.severity = parseInt(params.severity, 10); // Build the CEF prefix var output = util.format('CEF:%s|%s|%s|%s|%s|%s|%s', CEF_VERSION, params.vendor, params.product, params.version, params.signature, params.name, params.severity); // Add any extensions if (extensions) { output += '|' + this.formatExtensions(extensions); } return null, output; } }; |