Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit bcb0074

Browse files
committed
fix(parser): improve error handling
1 parent ab70f55 commit bcb0074

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

src/main/java/entrypoint/Config.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private Config() {
2020
}
2121
private static JsonNode pathConfig;
2222
private static String currentlyLoggedOnUser;
23+
public final static String NOT_AVAILABLE_TEXT = "N/A";
2324
private static final String CONFIG_DIR_PATH =
2425
System.getProperty("user.dir") + File.separator + ".config";
2526
private static final String ACCOUNTS_FILE_PATH =
@@ -110,7 +111,7 @@ public static void firstRunAction() {
110111
} else {
111112
logger.log(Level.SEVERE,"[FirstRun] Unable to create path config file.");
112113
}
113-
Utility.extractFileToLocal("GeoLite2-Country.mmdb", ".");
114+
Utility.extractFileToLocal("db/GeoLite2-Country.mmdb", ".");
114115
} else {
115116
showAlert(ERROR_LABEL,
116117
"Problem occured during first-run action. The program can not continue."

src/main/java/loganalyzer/ApacheParser.java

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,39 @@ private ApacheParser() {
2121
throw new IllegalStateException("Utility class");
2222
}
2323
private static final Pattern ipAddrPattern = Pattern.compile("((\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}))");
24+
private static final Pattern ipv6AddrPattern = Pattern.compile("([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])");
2425
private static final Pattern timestampPattern = Pattern.compile("\\[(\\d{2}/[A-Za-z]{3}/\\d{4}:\\d{2}:\\d{2}:\\d{2} [+\\-]\\d{4})]");
2526
private static final Pattern userAgentPattern = Pattern.compile("\"([^\"]*)\"[^\"]*$");
2627
private static final Logger logger = Logger.getLogger(ApacheParser.class.getName());
2728
public static String parseIpAddress(String logLine) {
28-
return findFirstMatch(logLine, ipAddrPattern);
29+
if (findFirstMatch(logLine, ipAddrPattern) == null) {
30+
return findFirstMatch(logLine, ipv6AddrPattern);
31+
} else {
32+
return findFirstMatch(logLine, ipAddrPattern);
33+
}
2934
}
3035
public static String parseTimestamp(String logLine) {
3136
String parsedLog = findFirstMatch(logLine, timestampPattern);
3237
if (parsedLog != null) {
33-
return parsedLog.substring(1, parsedLog.length() - 1);
38+
try {
39+
return parsedLog.substring(1, parsedLog.length() - 1);
40+
} catch (Exception e) {
41+
return Config.NOT_AVAILABLE_TEXT;
42+
}
3443
} else {
35-
return null;
44+
return Config.NOT_AVAILABLE_TEXT;
3645
}
3746
}
3847
public static String parseUserAgent(String logLine) {
3948
String parsedLog = findFirstMatch(logLine, userAgentPattern);
4049
if (parsedLog != null) {
41-
return parsedLog.substring(1, parsedLog.length() - 1);
50+
try {
51+
return parsedLog.substring(1, parsedLog.length() - 1);
52+
} catch (Exception e) {
53+
return Config.NOT_AVAILABLE_TEXT;
54+
}
4255
} else {
43-
return null;
56+
return Config.NOT_AVAILABLE_TEXT;
4457
}
4558
}
4659

@@ -51,39 +64,60 @@ public static String[] parseAllInOne(String logLine) {
5164
public static String parseMethod(String[] aioArr) {
5265
String tempMethodValue = getElementSafely(aioArr, 5);
5366
if (tempMethodValue != null) {
54-
return tempMethodValue.replace("\"", "");
67+
try {
68+
return tempMethodValue.replace("\"", "");
69+
} catch (Exception e) {
70+
return Config.NOT_AVAILABLE_TEXT;
71+
}
5572
}
56-
return null;
73+
return Config.NOT_AVAILABLE_TEXT;
5774
}
5875

5976
public static String parseProtocol(String[] aioArr) {
6077
String tempMethodValue = getElementSafely(aioArr, 7);
6178
if (tempMethodValue != null) {
62-
return tempMethodValue.replace("\"", "");
79+
try {
80+
return tempMethodValue.replace("\"", "");
81+
} catch (Exception e) {
82+
return Config.NOT_AVAILABLE_TEXT;
83+
}
6384
}
64-
return null;
85+
return Config.NOT_AVAILABLE_TEXT;
6586
}
6687

6788
public static String parseRequestPath(String[] aioArr) {
6889
String tempMethodValue = getElementSafely(aioArr, 6);
6990
if (tempMethodValue != null) {
70-
return tempMethodValue.replace("\"", "");
91+
try {
92+
return tempMethodValue.replace("\"", "");
93+
} catch (Exception e) {
94+
return Config.NOT_AVAILABLE_TEXT;
95+
}
7196
}
72-
return null;
97+
return Config.NOT_AVAILABLE_TEXT;
7398
}
7499

75100
public static int parseStatusCode(String[] aioArr) {
76101
String tempMethodValue = getElementSafely(aioArr, 8);
77102
if (tempMethodValue != null) {
78-
return Integer.parseInt(tempMethodValue.replace("\"", ""));
103+
try {
104+
return Integer.parseInt(tempMethodValue.replace("\"", ""));
105+
} catch (Exception e) {
106+
return 0;
107+
}
108+
79109
}
80110
return 0;
81111
}
82112

83113
public static int parseContentLength(String[] aioArr) {
84114
String tempMethodValue = getElementSafely(aioArr, 9);
85115
if (tempMethodValue != null) {
86-
return Integer.parseInt(tempMethodValue.replace("\"", ""));
116+
try {
117+
return Integer.parseInt(tempMethodValue.replace("\"", ""));
118+
} catch (Exception e) {
119+
return 0;
120+
}
87121
}
88122
return 0;
89123
}
@@ -102,8 +136,7 @@ public static Apache parseLogLine(String line) {
102136
parseUserAgent(line)
103137
);
104138
} catch (Exception e) {
105-
System.out.println(e);
106-
System.out.println(line);
139+
logger.log(Level.WARNING, "An error occurred during parsing phase: {0}", e);
107140
}
108141
return null;
109142
}

src/main/java/loganalyzer/ModSecurityParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static String parseAttackType(String data) {
3636
String filename = filePath.substring(filePath.lastIndexOf('/') + 1);
3737
return filename.replace(".conf", "");
3838
} else {
39-
return null;
39+
return Config.NOT_AVAILABLE_TEXT;
4040
}
4141
}
4242

@@ -47,7 +47,7 @@ public static String parseAttackMsg(String data) {
4747
if (matcher.find()) {
4848
return matcher.group(1);
4949
} else {
50-
return null;
50+
return Config.NOT_AVAILABLE_TEXT;
5151
}
5252
}
5353

@@ -58,7 +58,7 @@ public static String parseAttackData(String logEntry) {
5858
if (matcher.find()) {
5959
return matcher.group(1);
6060
} else {
61-
return null;
61+
return Config.NOT_AVAILABLE_TEXT;
6262
}
6363
}
6464

@@ -69,7 +69,7 @@ public static String parseSeverity(String data) {
6969
if (matcher.find()) {
7070
return matcher.group(1);
7171
} else {
72-
return null;
72+
return Config.NOT_AVAILABLE_TEXT;
7373
}
7474
}
7575

@@ -81,7 +81,7 @@ public static String parseVersion(String data) {
8181
if (matcher.find()) {
8282
return matcher.group(1);
8383
} else {
84-
return null;
84+
return Config.NOT_AVAILABLE_TEXT;
8585
}
8686
}
8787

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package loganalyzer;
2+
3+
import static loganalyzer.ApacheParser.parseLogLine;
4+
5+
public class TestApacheParser {
6+
public static void main(String[] args) {
7+
Apache apacheParsed = parseLogLine("127.0.0.1 - - [03/Jul/2024:18:21:46 +0700] \"GET /dashboard/ HTTP/1.1\" 304 - \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0\"");
8+
System.out.println(apacheParsed);
9+
}
10+
}

0 commit comments

Comments
 (0)