Github user simonellistonball commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/451#discussion_r100937396
  
    --- Diff: 
metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/cef/CEFParser.java
 ---
    @@ -0,0 +1,274 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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
    + *
    + * 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.
    + */
    +
    +package org.apache.metron.parsers.cef;
    +
    +import java.nio.charset.Charset;
    +import java.time.Clock;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Map.Entry;
    +import java.util.regex.Matcher;
    +import java.util.regex.Pattern;
    +
    +import org.apache.metron.parsers.BasicParser;
    +import org.apache.metron.parsers.ParseException;
    +import org.apache.metron.parsers.utils.DateUtils;
    +import org.apache.metron.parsers.utils.SyslogUtils;
    +import org.json.simple.JSONObject;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +public class CEFParser extends BasicParser {
    +   private static final long serialVersionUID = 1L;
    +
    +   protected static final Logger LOG = 
LoggerFactory.getLogger(CEFParser.class);
    +   private static final String HEADER_CAPTURE_PATTERN = "[^\\|]*";
    +   private static final String EXTENSION_CAPTURE_PATTERN = "(?<!\\\\)=";
    +   private static final Charset UTF_8 = Charset.forName("UTF-8");
    +
    +   private Pattern p;
    +   private Pattern pext;
    +
    +   public void init() {
    +
    +           // CEF Headers: Device Vendor|Device Product|Device 
Version|Device Event
    +           // Class ID|Name|Severity
    +
    +           String syslogTime = 
"(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\\b
 +(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]) 
(?!<[0-9])(?:2[0123]|[01]?[0-9]):(?:[0-5][0-9])(?::(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?))(?![0-9])?";
    +           String syslogTime5424 = 
"(?:\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2}))";
    +           String syslogPriority = "<(?:[0-9]+)>";
    +           String syslogHost = "[a-z0-9\\.\\\\-_]+";
    +
    +           StringBuilder sb = new StringBuilder("(?<syslogTime>");
    +           sb.append(syslogTime);
    +           sb.append("|");
    +           sb.append(syslogTime5424);
    +           sb.append(")?");
    +
    +           sb.append("(?<syslogHost>");
    +           sb.append(syslogHost);
    +           sb.append(")?");
    +
    +           sb.append("(?<syslogPriority>");
    +           sb.append(syslogPriority);
    +           sb.append(")?");
    +
    +           sb.append(".*");
    +
    +           sb.append("CEF:0\\|");
    +
    +           headerBlock("DeviceVendor", sb);
    +           sb.append("\\|");
    +           headerBlock("DeviceProduct", sb);
    +           sb.append("\\|");
    +           headerBlock("DeviceVersion", sb);
    +           sb.append("\\|");
    +           headerBlock("DeviceEvent", sb);
    +           sb.append("\\|");
    +           headerBlock("Name", sb);
    +           sb.append("\\|");
    +           headerBlock("Severity", sb);
    +           sb.append("\\|");
    +
    +           // extension capture:
    +           sb.append("(?<extensions>.*)");
    +           String pattern = sb.toString();
    +
    +           p = Pattern.compile(pattern);
    +
    +           // key finder for extensions
    +           pext = Pattern.compile(EXTENSION_CAPTURE_PATTERN);
    +   }
    +
    +   @SuppressWarnings("unchecked")
    +   public List<JSONObject> parse(byte[] rawMessage) {
    +           List<JSONObject> messages = new ArrayList<>();
    +
    +           String cefString = new String(rawMessage, UTF_8);
    +
    +           Matcher matcher = p.matcher(cefString);
    +
    +           while (matcher.find()) {
    +                   JSONObject obj = new JSONObject();
    +                   if (matcher.matches()) {
    +                           LOG.info(String.format("Found %d groups", 
matcher.groupCount()));
    +                           obj.put("DeviceVendor", 
matcher.group("DeviceVendor"));
    +                           obj.put("DeviceProduct", 
matcher.group("DeviceProduct"));
    +                           obj.put("DeviceVersion", 
matcher.group("DeviceVersion"));
    +                           obj.put("DeviceEvent", 
matcher.group("DeviceEvent"));
    +                           obj.put("Name", matcher.group("Name"));
    +                           obj.put("Severity", 
standardizeSeverity(matcher.group("Severity")));
    +                   }
    +
    +                   String ext = matcher.group("extensions");
    +                   Matcher m = pext.matcher(ext);
    +
    +                   int index = 0;
    +                   String key = null;
    +                   String value = null;
    +                   Map<String, String> labelMap = new HashMap<String, 
String>();
    +
    +                   while (m.find()) {
    +                           if (key == null) {
    +                                   key = ext.substring(index, m.start());
    +                                   index = m.end();
    +                                   if (!m.find()) {
    +                                           break;
    +                                   }
    +                           }
    +                           value = ext.substring(index, m.start());
    +                           index = m.end();
    +                           int v = value.lastIndexOf(" ");
    +                           if (v > 0) {
    +                                   String temp = value.substring(0, 
v).trim();
    +                                   if (key.endsWith("Label")) {
    +                                           labelMap.put(key.substring(0, 
key.length() - 5), temp);
    +                                   } else {
    +                                           obj.put(key, temp);
    +                                   }
    +                                   key = value.substring(v).trim();
    +                           }
    +                   }
    +                   value = ext.substring(index);
    +
    +                   // Build a map of Label extensions to apply later
    +                   if (key.endsWith("Label")) {
    +                           labelMap.put(key.substring(0, key.length() - 
5), value);
    +                   } else {
    +                           obj.put(key, value);
    +                   }
    +
    +                   // Apply the labels to custom fields
    +                   for (Entry<String, String> label : labelMap.entrySet()) 
{
    +                           mutate(obj, label.getKey(), label.getValue());
    +                   }
    +
    +                   // Rename standard CEF fields to comply with Metron 
standards
    +                   obj = mutate(obj, "dst", "ip_dst_addr");
    +                   obj = mutate(obj, "dpt", "ip_dst_port");
    +                   obj = convertToInt(obj, "ip_dst_port");
    +
    +                   obj = mutate(obj, "src", "ip_src_addr");
    +                   obj = mutate(obj, "spt", "ip_src_port");
    +                   obj = convertToInt(obj, "ip_src_port");
    +
    +                   obj = mutate(obj, "act", "deviceAction");
    +                   // applicationProtocol
    +                   obj = mutate(obj, "app", "protocol");
    +
    +                   obj.put("original_string", cefString);
    +
    +                   // apply timestamp from message if present, using rt, 
syslog
    +                   // timestamp,
    +                   // default to current system time
    +
    +                   if (obj.containsKey("rt")) {
    +                           String rt = (String) obj.get("rt");
    --- End diff --
    
    True. I was wondering if someone would care enough to notice that. I'll add.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to