ottobackwards commented on a change in pull request #1408: METRON-2118: Added a LEEF parser URL: https://github.com/apache/metron/pull/1408#discussion_r285256233
########## File path: metron-platform/metron-parsing/metron-parsers/src/main/java/org/apache/metron/parsers/leef/LEEFParser.java ########## @@ -0,0 +1,236 @@ +/** + * 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.leef; + +import java.lang.invoke.MethodHandles; +import java.nio.charset.Charset; +import java.text.SimpleDateFormat; +import java.time.Clock; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +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.cef.CEFParser; +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 LEEFParser extends BasicParser { + private static final long serialVersionUID = 1L; + + protected static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final String HEADER_CAPTURE_PATTERN = "[^\\|]*"; + private static final Charset UTF_8 = Charset.forName("UTF-8"); + private static final String EXTENSION_CAPTURE_PATTERN = "(?<!\\\\)="; + + 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(""); + sb.append("(?<syslogPriority>"); + sb.append(syslogPriority); + sb.append(")?"); + sb.append("(?<syslogTime>"); + sb.append(syslogTime); + sb.append("|"); + sb.append(syslogTime5424); + sb.append(")?"); + + sb.append("(?<syslogHost>"); + sb.append(syslogHost); + sb.append(")?"); + + sb.append(".*"); + + sb.append("LEEF:(?<Version>1.0|2.0|0)?\\|"); + + headerBlock("DeviceVendor", sb); + sb.append("\\|"); + headerBlock("DeviceProduct", sb); + sb.append("\\|"); + headerBlock("DeviceVersion", sb); + sb.append("\\|"); + headerBlock("DeviceEvent", sb); + sb.append("\\|"); + + // add optional delimiter header (only applicable for LEEF 2.0) + sb.append("("); + headerBlock("Delimiter", sb); + sb.append("\\|"); + sb.append(")?"); + + // extension capture: + sb.append(" ?(?<extensions>.*)"); + String pattern = sb.toString(); + + p = Pattern.compile(pattern); + + pext = Pattern.compile(EXTENSION_CAPTURE_PATTERN); + } + + @SuppressWarnings("unchecked") Review comment: We could also support multiple messages, but that could be a follow on ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services