Created on
08-23-2024
03:13 AM
Edited on
09-02-2024
02:07 AM
By
Jean-Philippe_P
Description |
The article describes how to create a dataset using regular expressions based on syslog logs to make a report. |
Scope | FortiAnalyzer. |
Solution |
Glossary and terminology:
Regular expression (REGEX): Regular expressions (REGEX) are patterns used to match and manipulate text.
Prerequisites:
Explanations of the syslog log format:
A Syslog message typically consists of three main parts:
The MSG portion is the interesting part, where we need to extract the desired value from the text to construct the relevant SQL query for the dataset.
For this KB article, an example is shown. For the needs, it is necessary to adapt the SQL request (PostgreSQL).
Here is a list of important metacharacters to know when building a regular expression in PostgreSQL:
These metacharacters help you define precise patterns to search, extract, and manipulate text in PostgreSQL using regular expressions.
Step 1 – View and analyze the syslog log format.
Below is an example of a syslog log stored on the FortiAnalyzer database: (Go to syslog ADOM -> logView -> syslog).
1 2024-07-11T13:55:28+04:00 172.22.8.98 PulseSecure: - - - 2024-07-11 13:55:28 - ER-AUH-ISA10 - [1.5.95.123] Default Network::user.name@domain.com (SAM-Realm)[][] - Login succeeded for user.name@domain.com /SAM-Realm from xx.xx.xx.xx with 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.
It can be asked to create a report table like the below one based on syslog logs.
In PostgreSQL, the REGEXP_MATCHES() function is used by FortiAnalyzer to search a string for a pattern specified by a regular expression and return the matches. The function returns an array of text, where each element is a substring that matches a parenthesized subexpression within the regular expression.
REGEXP_MATCHES (source_string, pattern, [flags]).
source_string: The string to be searched. pattern: The regular expression pattern to match. flags: (Optional) Modifiers that change the behavior of the regular expression. Common flags include:
Step 2 - Create the SQL dataset query using the REGEX function.
Here is an example of a PostgreSQL dataset query that uses regular expressions: (Go to Reports -> Report Definitions -> Datasets, and select Create New).
SELECT REGEXP_MATCHES(`msg`, '([\w]+-[\w]+-[\w]+) - \[[^\]]+\] Default Network::[^@]+@[^()]+') AS "Login Device", REGEXP_MATCHES(`msg`, 'Default Network::(.*)\(.*\)\[') AS "Login User", REGEXP_MATCHES(`msg`, '\[([\d\.]+)]') AS "Public IP", REGEXP_MATCHES(`msg`, '([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})') AS "Login Time", CASE WHEN `msg` ~* 'Login succeeded' THEN 'Login succeeded' ELSE 'Login failed' END AS "Login Status" FROM $log WHERE $filter GROUP BY msg ORDER BY "Login Device"
The below regular expression is used to extract the 'Login User' from the msg text:
REGEXP_MATCHES(`msg`, 'Default Network::(.*)\(.*\)\[') AS "Login User",
How it works:
Purpose: It finds the user’s name in a message like 'Default Network::username (other details)[more info]' and extracts 'username' as the 'Login User'.
Notes:
If any columns of the table are empty, it indicates that the regex is incorrect and needs to be adjusted. For example:
Related documents: |