The libmsclogparser is a library, which can parse ModSecurity's (both mod_security2 and libmodsecurity3) format.
The "input" is a web server error log line, generated by ModSecurity. The output is a parsed structure.
The library is written in C, therefore you can embed it in other C, C++, Go or Rust applications. Moreover it contains several bindings for different script languages: Python, Lua, Ruby, and PHP. Bindings are also written in native C, so you don't need any code compile framework, eg. SWIG.
- Project purpose
- Licensing
- Current version
- API
- Constants
- Structure
- datapool
- entry_is_modsecline
- entry_is_broken
- log_entry_raw_length
- log_date_iso
- log_date_timestamp
- log_client
- log_entry_class
- log_modsec_msg
- log_modsec_msg_length
- log_modsec_reason
- log_modsec_operator
- log_modsec_operand
- log_modsec_target_name
- log_modsec_target_value
- log_modsec_process_error
- log_rule_file
- log_rule_line
- log_rule_id
- log_rule_rev
- log_rule_msg
- log_rule_data
- log_rule_severity
- log_rule_version
- log_rule_maturity
- log_rule_accuracy
- log_rule_tags_cnt
- log_rule_tags
- log_hostname
- log_uri
- log_unique_id
- log_server
- lineerrpool
- log_entry_errors_cnt
- Methods
- Strucure in bindings
- Compile, install
The purpose of this project is to analyze the "always present" log output of ModSecurity engines (usually the 'error.log'). As you know, there are two types of engines, and the structure of these logs is a bit different. Not only because of web servers, but in general: these log entries are made with different logic.
The writing of the code was preceded by thorough research. The expectation was that a method that knew all log formats and could recognize and show truncated (or duplicated) fields produced during known procedures.
Even though the code is written in C, but it does not use any dynamic memory handling: parse() copies the line to a buffer in the logdata structure (linebuf), so a line of MSCLOG_LINE_BUFF_SIZE (8192) bytes or longer (without the line ending) can't be parsed, parse() reports an error for it: the fields are empty, entry_is_broken is 1, and entry_is_modsecline is 1 if it's a ModSecurity line. A line that contains a NUL character (except at its end) isn't parsed either, the same way, with the error The line contains a NUL character, it can't be parsed!; the bindings handle it the same way, they don't raise an error for it. It also does not have any library dependecy (eg. PCRE/PCRE2). Therefore it's really fast and convenience to use it.
The web server appends its own context to the ModSecurity message (Nginx: , client: ADDR, server: NAME, request: "...", host: "...", referrer: "...", Apache: , referer: ...), and it contains values sent by the client without escaping. The ModSecurity fields ([file], [id], [msg], [hostname], [unique_id], ...) are searched only before this context, so fake fields in the request line or in a header can't replace the real ones. The end of the ModSecurity part is the context right after its last field ([unique_id "..."] or [ref "..."], on Nginx optionally followed by while ACTION). A fake one can be both in the context and in the message (libmodsecurity3 doesn't escape the " in the variable name and value of the match message), so if there is more than one, the end is ambiguous: the fields of the rule ([file], [id], [msg], [tag], ...) and of the tail ([hostname], [uri], [unique_id]) aren't used, only the class (log_entry_class) and the message (log_modsec_msg) are kept, the line is broken, and the error is Ambiguous end of the ModSecurity part, the fields aren't used! (an injection can hide the fields, but can't replace them). The same applies if the only context is followed by fields on a line where the real context can be missing (Apache writes , referer: only if there is a Referer header, and Nginx cuts the line at 2047 bytes): then it's a fake one in the message; and if the ModSecurity part contains more than one [file " or [id " (a message has only one, more of them are fakes, eg. in the [uri] of a line that Nginx cut right after a fake context), with the error More [file] or [id] fields in the ModSecurity part, the fields aren't used!. libmodsecurity3 doesn't escape the " in [hostname] and [uri] either, so if the ModSecurity part contains more than one [hostname " or [uri ", the line is broken, these two values aren't used, and the error is Ambiguous [hostname] or [uri] field, the values aren't used!.
libmsclogparser is dual licensed under the following licenses. You can use the software according to the terms of your chosen license.
- GNU Affero General Public License (AGPL) v3 with additional terms
- Our Own Proprietary License - please contact with us
This means, we can apply any pull requests from any contributor after the agreement of our CLA. For mor information, please check our contrbuting reference
The current version of library is 3.0.0.
The API provides two constants, the structure of a parsed line and a function:
LOG_TYPE_APACHE
LOG_TYPE_NGINX
LOGMSG_UNKNOWN
LOGMSG_WARNING
LOGMSG_ACCDENIED
LOGMSG_REQBODY
LOGMSG_ERROR
LOGMSG_AUDITLOG
LIBRARY_VERSION
MODULE_VERSION
Depends on your log source, you have to use one of LOG_TYPE_APACHE or LOG_TYPE_NGINX. If you use a different type, your output structure will empty.
LOGMSG_... constants show the modsecurity message type. For eg. it could be "ModSecurity: Warning. Pattern match...", or "ModSecurity: Access denied. ...". Depends on these messages, the parsed structure contains the member modseclinetype, which can be one of these values (see belove).
LIBRARY_VERSION shows the current library version. It has a major, a minor and a patch version number. If library will have any modification, this version number will incremented.
MODULE_VERSION exists only in binding modules. This constant describes the module version, which could be different than the LIBRARY_VERSION (normally a bigger value), and could be different in bindings.
Depends on the used binding, these constants will appear in different ways: in some cases they are available through the module prefix (eg. in Python: mscpylogparser.LOG_TYPE_APACHE) or the module namespace (eg. in Ruby: MscRubyLogParser::LOG_TYPE_NGINX, and the function is MscRubyLogParser.parse()). For more information, please check the test scripts.
Originally, this is a C structure,
typedef struct logdata {
int entry_is_modsecline;
int entry_is_broken;
size_t log_entry_raw_length;
double log_date_timestamp;
logmsgtype log_entry_class;
size_t log_modsec_msg_length;
size_t log_rule_tags_cnt;
int log_entry_errors_cnt;
char * fields[LOGFIELD_LOG_SERVER+1];
msclogpool datapool;
msclogpool lineerrpool;
} logdata;
#define log_date_iso fields[LOGFIELD_LOG_DATE_ISO]
#define log_client fields[LOGFIELD_LOG_CLIENT]
#define log_modsec_msg fields[LOGFIELD_LOG_MODSEC_MSG]
#define log_modsec_reason fields[LOGFIELD_LOG_MODSEC_REASON]
#define log_modsec_operator fields[LOGFIELD_LOG_MODSEC_OPERATOR]
#define log_modsec_operand fields[LOGFIELD_LOG_MODSEC_OPERAND]
#define log_modsec_target_name fields[LOGFIELD_LOG_MODSEC_TARGET_NAME]
#define log_modsec_target_value fields[LOGFIELD_LOG_MODSEC_TARGET_VALUE]
#define log_modsec_process_error fields[LOGFIELD_LOG_MODSEC_PROCESS_ERROR]
#define log_rule_file fields[LOGFIELD_LOG_RULE_FILE]
#define log_rule_line fields[LOGFIELD_LOG_RULE_LINE]
#define log_rule_id fields[LOGFIELD_LOG_RULE_ID]
#define log_rule_rev fields[LOGFIELD_LOG_RULE_REV]
#define log_rule_msg fields[LOGFIELD_LOG_RULE_MSG]
#define log_rule_data fields[LOGFIELD_LOG_RULE_DATA]
#define log_rule_severity fields[LOGFIELD_LOG_RULE_SEVERITY]
#define log_rule_version fields[LOGFIELD_LOG_RULE_VERSION]
#define log_rule_maturity fields[LOGFIELD_LOG_RULE_MATURITY]
#define log_rule_accuracy fields[LOGFIELD_LOG_RULE_ACCURACY]
#define log_rule_tags fields[LOGFIELD_LOG_RULE_TAGS]
#define log_hostname fields[LOGFIELD_LOG_HOSTNAME]
#define log_uri fields[LOGFIELD_LOG_URI]
#define log_unique_id fields[LOGFIELD_LOG_UNIQUE_ID]
#define log_server fields[LOGFIELD_LOG_SERVER]The structure has been refactored to use a flexible array of strings fields to store all dynamically allocated string data.
Eg. you can get rule msg with log->log_rule_msg and you can get modsecurity msg with log->log_modsec_msg, but you can refer to the same field in different ways: log_rule_msg == fields[LOGFIELD_LOG_RULE_MSG], or log_modsec_msg == fields[LOGFIELD_LOG_MODSEC_MSG].
Lets see what field contains which data.
This is a pre-allocated memory pool with fix size. Basicly, that's a structure:
#define MSCLOGPOOL_SIZE 8192
typedef struct msclogpool {
char pool[MSCLOGPOOL_SIZE];
char *currptr;
size_t offset;
} msclogpool;Type: msclogpool
This needed for the parser to work, you do not need to know about - do not touch it or read/write it!
The size of the pool is part of the ABI (the pools are embedded in logdata), so it is fixed: MSCLOGPOOL_SIZE can't be overridden at compile time. If a line has more data than fits into the pool, the values are truncated (or empty), and the line is marked as broken with an error message; the parser never writes past the pool.
Indicates that the line is produced by ModSecurity.
This is determined by searching for the ' ModSecurity:' substring (please note the leading space and :). If found, this value is 1, otherwise 0. If 0, the common fields are not filled (date_iso, date_epoch, client).
Type: int
Eg.: 0 | 1
Indicates that the line is produced by ModSecurity AND has a chunked field.
This is decided based if a field recognizable (it has at least one character, eg [v) but does not have the trailing ] or ". If the value is 1, then the lineerror will contain the reason, and lineerrpos (it's an array with two fields) will contain the start and end positions.
Type: int
Eg.: 0 | 1
Contains the length of line; this value comes from the user, and does not change.
Type: size_t
Eg.: 1238
Contains the parsed date field from the log in ISO format (YYYY-MM-DD HH:ii:ss).
Type: *char
Max length: 19 bytes
Eg.:
2022/11/05 13:19:51 [info] 58602#58602... (Nginx)
[Sat Nov 05 13:19:51.041880 2022] [:error]... (Apache)
will give
2022-11-05 13:19:51
Contains the parsed date field from the log in unix timestamp.
Type: double
Eg.:
2022/11/05 13:19:51 [info] 58602#58602... (Nginx)
[Sat Nov 05 13:19:51.041880 2022] [:error]... (Apache)
will give
1667650791 (Nginx)
1667650791.041880 (Apache)
Contains the parsed client source address and (in case of Apache) source port.
Type: *char
Max length: 49 bytes (Apache), 255 bytes (Nginx)
Eg.:
...tid 139766615992064] [client 45.134.144.140:33326] [client... (Apache)
...[ref "o12,5v51,21t:lowercase"], client: 45.134.144.140, server: ... (Nginx)
will give
45.134.144.140:33326 (Apache)
45.134.144.140 (Nginx)
As you can see, in case of Apache the parser will process the first [client] field. In case of Nginx this value is at the end of the line, in the , client: ADDR, server: NAME part that Nginx appends to the line. This part isn't escaped, and the client can send a fake one (eg. in the request line, or in a header), so the value is used only in the same cases as log_server: if the , client: ADDR, server: is right after the ModSecurity part (after its last field), the end of the ModSecurity part isn't ambiguous, and the line isn't cut by Nginx. More (fake) , client: ADDR, server: parts after it don't matter, unless one of them follows a fake [ref "..."] or [unique_id "..."] (eg. in the referrer): that's another possible end of the ModSecurity part, so the end is ambiguous, and neither the fields nor log_client are used (see the beginning of this document). Otherwise log_client is an empty string; an injection can hide the address, but can't replace it.
Contains the parsed ModSecurity message type.
Type: logmsgtype
This is an enum type, and the values can be one of the LOGMSG_... constants.
Eg.:
... ModSecurity: Warning. Pattern match "^[\\\\d.:]+$" at REQUEST_HEADERS:Host. [file "... (Apache)
will provide a LOGMSG_WARNING value for this field.
Contains the parsed ModSecurity message.
Type: *char
Eg.:
... ModSecurity: Warning. Pattern match "^[\\\\d.:]+$" at REQUEST_HEADERS:Host. [file "... (Apache)
... ModSecurity: Warning. Matched "Operator `Rx' with parameter `^[\d.:]+$' against variable `REQUEST_HEADERS:Host' (Value: `1.2.3.4' ) [file "... (Nginx)
will give
Warning. Pattern match "^[\\\\d.:]+$" at REQUEST_HEADERS:Host. (Apache)
ModSecurity: Warning. Matched "Operator `Rx' with parameter `^[\d.:]+$' against variable `REQUEST_HEADERS:Host' (Value: `1.2.3.4' ) (Nginx)
Contains the length of the message field above.
Type: size_t
Holds the reason of the rule triggered.
Type: *char
Eg.: Pattern match, detected XSS using libinjection, ...
Contains the operator of the triggered rule only in case of libmodsecurity3
Type: *char
Eg.: PmFromFile, Rx, Ge, ...
Consider the log contains: Warning. Matched "Operator PmFromFile' with parameter scanners-user-agents.data' against variable REQUEST_HEADERS:User-Agent' (Value: Mozilla/5.0 zgrab/0.x' ). In this case this value will be PmFromFile.
Contains the rule's operand what engine uses with the operator (above). This field is filled only in case of libmodsecurity3
Type: *char
Eg.: scanners-user-agents.data
Consider the log contains: Warning. Matched "Operator PmFromFile' with parameter scanners-user-agents.data' against variable REQUEST_HEADERS:User-Agent' (Value: Mozilla/5.0 zgrab/0.x' ). In this case this value will be scanners-user-agents.data.
Contains the name of the target, where the operator matched. This field is filled only in case of libmodsecurity3
Type: *char
Eg.: REQUEST_HEADERS:User-Agent
Consider the log contains: Warning. Matched "Operator PmFromFile' with parameter scanners-user-agents.data' against variable REQUEST_HEADERS:User-Agent' (Value: Mozilla/5.0 zgrab/0.x' ). In this case this value will be REQUEST_HEADERS:User-Agent.
Contains the value of the target (see below) where the operator matched (below too). This field is filled only in case of libmodsecurity3
Type: *char
Eg.: Mozilla/5.0 zgrab/0.x
Consider the log contains: Warning. Matched "Operator PmFromFile' with parameter scanners-user-agents.data' against variable REQUEST_HEADERS:User-Agent' (Value: Mozilla/5.0 zgrab/0.x' ). In this case this value will be Mozilla/5.0 zgrab/0.x.
Usually this field is filled only in case of Apache. This field contains the message if a rule error occurred.
Type: *char
Eg. (only in case of Apache)
ModSecurity: Rule 7fb77198ed38 [id "-"][file "/.../.../RESPONSE-951-DATA-LEAKAGES-SQL.conf"][line "123"] - Execution error - PCRE limits exceeded (-8): (null).
Execution error - PCRE limits exceeded (-8): (null).
Contains the parsed [file] field.
Type: *char
Eg.:
...REQUEST_HEADERS:Host. [file "/usr/share/modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf"] [line ... (Apache)
) [file "/usr/share/modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf"] [line ... (Nginx)
will give
/usr/share/modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
Important note: the pattern [file is the first "structured", easily recognizable field after the ModSecurity message. With a speciel crafted request, the client can put this pattern into that message, eg:
curl -v -F ") [file \"/dev/null\"]=@somefile.csv" http://your.host
will produce the line
... ModSecurity: Warning. Pattern match "(?<!&(?:[...]" at FILES_NAMES:%22 at [file %22/dev/null%22]. [file ...
The only recognizable pattern is [file " - a substring with leading space and trailing ". This is the "reference" point in the operation of the parser.
Contains the line number of the file.
Type: *char
Eg.:
....conf"] [line "735"] [id "...
will give
735
Contains the id of the rule.
Type: *char
Eg.:
...line "735"] [id "920350"] [msg...
will give
920350
Please note, that in some special cases this value can be a single char: -, eg.:
...Rule 7f0c68755d38 [id "-"][file "...
Contains the rev value of the rule.
Type: *char
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [rev ""] ...
Contains the msg field of the rule.
Type: *char
Max length: 511 bytes
Eg.:
....[id "920280"] [msg "Request Missing a Host Header"] [severity "WARNING"] [ver "OWASP_CRS/3.3.2"]...
will give
Request Missing a Host Header
Contains the data of the rule.
Type: *char
Eg.:
... [msg "Host header is a numeric IP address"] [data "1.2.3.4:443"] ...
will give
1.2.3.4:443
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [data ""] ...
Contains the severity of the rule.
Type: *char
Eg.:
... [severity "CRITICAL"] ... (Apache)
... [severity "2"] ... (Nginx)
will give
CRITICAL (Apache)
2 (Nginx)
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [severity ""] ...
Contains the version of the rule.
Type: *char
Eg.:
... [ver "OWASP_CRS/3.3.4"] ...
will give
OWASP_CRS/3.3.4
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [version ""] ...
Contains the maturity of the rule.
Type: *char
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [matrity ""] ...
Contains the accuracy of the rule.
Type: *char
In case of Apache, this field is optional: if rule does not have it, then the whole field will missing. In case of libmodsecurity3, this field is always presents, but could be empty:
... [accuracy ""] ...
Contains the number of recognized tags.
If a tag is chunked, but the field was recognized during parsing, it will count to this. Eg.:
[tag "application/multi] [t [hostname "..."]
will produces 2 for value of tagcnt.
Contains the list of tags of the rule.
Type: *char
Eg.:
... [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] ...
will give
application-multi, language-multi, platform-multi, attack-generic
in a list.
This field is optional both in mod_security2 and libmodsecurity3: if rule does not have it, then the whole field will missing. In this case, the result will an empty string. The number of tags is in the tagcnt above.
Note, that tags are stored one after the other in the pool. The memory map looks like:
application-multi\0language-multi\0platform-multi\0attack-generic
If you want to access all of them, you should read by sequence, while the pointer moves to after the end of the current tag + 1 position:
if (l.tagcnt > 0) {
printf("tags:");
for (size_t ti = 0; ti < l.tagcnt; ti++) {
printf(" '%s'", l.tags);
l.tags += strlen(l.tags) + 1;
}
printf("\n");
}Contains the hostname of the virtual host. This property is independent from the rule, and always presents.
Type: *char
Eg.:
... [hostname "modsecurity.digitalwave.hu"] ...
will give
modsecurity.digitalwave.hu
This field can't be chunked. If [hostname] is empty on an Nginx line, the value comes from the , host: "..." part of the Nginx context; if the context contains more than one (a fake one in the request line or in the referrer), none of them is used, the line is broken, and the error is Ambiguous 'host: ""' field, the value isn't used!.
Contains the URI of the request. This property is independent from the rule, and always presents.
Type: *char
Eg.:
... [uri "/ab2"] ...
will give
/ab2
This field can't be chunked.
Contains the unique_id of the request in webserver. This property is independent from the rule, and always presents.
Type: *char
Eg.:
... [uri "Y7Z3aSfpYv3U3aB8MPMHjAAAABY"] ...
will give
Y7Z3aSfpYv3U3aB8MPMHjAAAABY
This field can't be chunked.
Contains the server_name of the Nginx server block that served the request, from the , server: part that Nginx appends to the line, eg. _ for a default server. Compare to log_hostname, which is the Host the client sent.
It's an empty string on Apache lines, and when the value can't be determined: there is no , server: part right after the ModSecurity part (after its last field) or the name is longer than 255 bytes, the end of the ModSecurity part is ambiguous, or there is no context right after it and the line contains more than one , client: ADDR, server: (one of them is a fake, sent by the client), or the line is 2047 bytes long, so it was cut by Nginx (at NGX_MAX_ERROR_STR), and on a cut line the real context may be lost while a fake one remains. In the last two cases one error is recorded, which doesn't mark the line as broken, and log_client is empty too: the two values come from the same , client: ADDR, server: NAME part.
Type: *char
Max length: 255 bytes
Eg.:
... [hostname "asdf.invalid"] [uri "/"] [unique_id "168142074224.426959"] ..., client: 192.0.2.10, server: _, request: "GET / HTTP/1.1", host: "asdf.invalid"
will give
_
while log_hostname is asdf.invalid.
This field contains the list of error messages and positions (start, end) where the error occurred.
Type: *char
The error stored in a structure:
typedef struct msclogerr {
char *errmsg;
size_t *startpos;
size_t *endpos;
} msclogerr;To get the items, you can use the method read_msclog_err(). See this methods section.
Contains the number of errors.
Type: int
Library contains these methods:
int parse(const char *line, size_t len, loglinetype LT, logdata *l);where the arguments:
- pointer to the line itself
- lenght of the line in bytes
- type of the line, see constants above
- a logdata structure
Note, that the last argument (logdata) is necessary only in C. If you use any kind of bindings, you don't need to care of that.
parse() reads only the first len bytes of the line, and doesn't modify it. The line doesn't have to be terminated with '\0', so it can be a part of a bigger buffer, eg. of a whole log file read into memory. A line ending (\n or \r\n) at the end of the line is ignored. The logdata structure doesn't have to be cleared before the call, parse() initializes it, so the same structure can be reused for every line.
To see an example, you should check the examples directory. Here is a sample:
// bigger than MSCLOG_LINE_BUFF_SIZE: a too long line goes to parse() too,
// which reports it; the rest of an even longer line is skipped
static char line[MSCLOG_LINE_BUFF_SIZE * 2];
size_t len = 0;
while (fgets (line, sizeof(line), fp) != NULL) {
len = strlen(line);
if (len == sizeof(line) - 1 && line[len-1] != '\n') {
int ch;
while ((ch = fgetc(fp)) != EOF && ch != '\n') {
;
}
}
if (len > 0) {
parse(line, len, t, &l);
if (l.entry_is_modsecline == 1) {
...int parse_checked (const char * line, size_t linelen, size_t len, loglinetype t, logdata * l);Like parse(), for a line of linelen bytes and a len that comes from the caller (eg. from a script): if len is bigger than linelen, the whole line is parsed (parse() never reads after its end), the line is broken, the error The given length (LEN) is bigger than the line (LINELEN bytes)! is added with the positions linelen and len, and it returns 1. The bindings use it.
void read_msclog_err(msclogpool *pool, msclogerr *err);where the arguments:
- pointer to the pool, namely
l.lineerrpool - pointer to the structure of
msclogerr, see above
The example is above, but here again:
if (l.lineerrcnt > 0) {
l.lineerrpool.currptr = l.lineerrpool.pool; // set the ptr to the start pos
for (int c=0; c < l.lineerrcnt; c++) { // iterate while cnt counts
msclogerr err; // structure above
read_msclog_err(&l.lineerrpool, &err); // call the fn and fill it
printf("%s - %zu:%zu\n", err.errmsg, err.startpos, err.endpos);
}
}Eg.: consider a line with chunked field:
..."] [severity "CRITICAL"] [v [hostname "...
In this case, the [version] field is chunked, therefore the message will
Field [version] is chunked! - 1234:1236
Note: this method is not available in bindings, because the errors are expanded.
As you can see above, in the available bindings (Lua, PHP, Python, Ruby) there is only one function exists, the parse(). For more details, please see the tests scripts under the bindings/ directory.
In Python, the line can be a str or a bytes object, and its length is given in bytes. If the line is bytes, the values of the fields and the error messages in log_entry_errors are bytes too, so a line that isn't valid UTF-8 can be parsed without decoding it.
The second argument of parse() is the length of the line in bytes. If it's bigger than the line, the bindings parse the whole line (they never read after its end), set entry_is_broken, and add an error to log_entry_errors, eg. The given length (816) is bigger than the line (716 bytes)!, its positions in log_entry_errors_pos are the length of the line and the given length. A negative length, or a log type other than LOG_TYPE_APACHE and LOG_TYPE_NGINX raises an error (ValueError in Python and PHP 8, ArgumentError in Ruby, a Lua error; PHP 7 raises a warning and parse() returns false). In C, parse() returns 1 for an unknown log type, with empty fields and the error Unknown log type!.
Note, that the PHP script runs with this command (after build and install):
php -d extension=mscphplogparser bindings/phptest.php /path/to/log apacheThe given structure from the parse() method is a bit different than the C structure above. Here is the used one (in JSON):
{
"entry_is_modsecline": 1,
"entry_is_broken": 0,
"log_entry_raw_length": 711,
"log_date_iso": "2026-01-12 22:14:41",
"log_date_timestamp": 1768252481.384971,
"log_client": "1.2.3.4",
"log_entry_class": 1,
"log_modsec_msg": "Warning. Matched phrase \"/.env\" at REQUEST_FILENAME.",
"log_modsec_msg_length": 52,
"log_modsec_reason": "Matched phrase",
"log_modsec_operator": "",
"log_modsec_operand": "/.env",
"log_modsec_target_name": "REQUEST_FILENAME",
"log_modsec_target_value": "",
"log_modsec_process_error": "",
"log_rule_file": "/usr/share/modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf",
"log_rule_line": "125",
"log_rule_id": "930130",
"log_rule_rev": "",
"log_rule_msg": "Restricted File Access Attempt",
"log_rule_data": "Matched Data: /.env found within REQUEST_FILENAME: /.env",
"log_rule_severity": "CRITICAL",
"log_rule_version": "OWASP_CRS/3.3.8",
"log_rule_maturity": "",
"log_rule_accuracy": "",
"log_rule_tags_cnt": 8,
"log_rule_tags": [
"application-multi",
"language-multi",
"platform-multi",
"attack-lfi",
"paranoia-level/1",
"OWASP_CRS",
"capec/1000/255/153/126",
"PCI/6.5.4"
],
"log_hostname": "modsecurity.digitalwave.hu",
"log_uri": "/.env",
"log_unique_id": "aWVkQc5DKnYkg6rxU5qO6gAAAFI",
"log_server": "",
"log_entry_errors_cnt": 0,
"log_entry_errors": [],
"log_entry_errors_pos": []
}or an Nginx output:
{
"entry_is_modsecline": 1,
"entry_is_broken": 0,
"log_entry_raw_length": 851,
"log_date_iso": "2026-01-19 08:04:23",
"log_date_timestamp": 1768806263.0,
"log_client": "4.3.2.1",
"log_entry_class": 1,
"log_modsec_msg": "Warning. Matched \"Operator `Rx' with parameter `(?:^([\\d.]+|\\[[\\da-f:]+\\]|[\\da-f:]+)(:[\\d]+)?$)' against variable `REQUEST_HEADERS:Host' (Value: `1.2.3.4' )",
"log_modsec_msg_length": 162,
"log_modsec_reason": "",
"log_modsec_operator": "Rx",
"log_modsec_operand": "(?:^([\\d.]+|\\[[\\da-f:]+\\]|[\\da-f:]+)(:[\\d]+)?$)",
"log_modsec_target_name": "REQUEST_HEADERS:Host",
"log_modsec_target_value": "1.2.3.4",
"log_modsec_process_error": "",
"log_rule_file": "/etc/crs/4/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf",
"log_rule_line": "712",
"log_rule_id": "920350",
"log_rule_rev": "",
"log_rule_msg": "Host header is a numeric IP address",
"log_rule_data": "1.2.3.4",
"log_rule_severity": "4",
"log_rule_version": "OWASP_CRS/4.22.0",
"log_rule_maturity": "0",
"log_rule_accuracy": "0",
"log_rule_tags_cnt": 8,
"log_rule_tags": [
"application-multi",
"language-multi",
"platform-multi",
"attack-protocol",
"paranoia-level/1",
"OWASP_CRS",
"OWASP_CRS/PROTOCOL-ENFORCEMENT",
"capec/1000/210/272"
],
"log_hostname": "1.2.3.4",
"log_uri": "/",
"log_unique_id": "176880626392.586715",
"log_server": "_",
"log_entry_errors_cnt": 0,
"log_entry_errors": [],
"log_entry_errors_pos": []
}
Please see the parsed structure (in case of Nginx) in this example.
To build the library, you need at least:
- a C compiler
- autotools
- make
There is no other library dependencies, if you want to use it for your C codes.
If you want to bind the library for any kind of supported supported languages (Python3, Lua, Ruby, PHP), you have to install its development tool. The Python binding needs the setuptools and pip modules too (eg. on Debian/Ubuntu: apt install python3-dev python3-setuptools python3-pip), configure stops with an error if one of them is missing.
To compile the code, just download the source and type as regular user:
./configure --helpYou can set the target directory given --prefix=/path/to, and set other options.
The generated files (configure, Makefile.in, ...) are in the repository, so autotools aren't needed for the build, and make doesn't regenerate them. If you change configure.ac or a Makefile.am, run autoreconf -fi, or configure with --enable-maintainer-mode.
To compile the bindings, these options are avaliable:
--enable-python-binding \
--enable-lua-binding \
--enable-ruby-binding \
--enable-php-bindingPlease note, that if you have more versions of a choosed language, you have to set explicit the used interpreter. Eg.:
PYTHON=/usr/bin/python3 RUBY=/path/to/ruby LUA=/path/to/lua5.4 PHP=/usr/local/bin/php7.4 \
./configure \
--enable-python-binding \
--enable-lua-binding \
--enable-ruby-binding \
--enable-php-bindingAt the end of the configure script run you will see the summary:
----------------------------------------------------------------------
msclogparser Version 3.0.0 configuration:
OS Type Linux
Prefix /usr/local
Preprocessor gcc -E
C Compiler gcc -g -O2
Bindings:
Python yes
Lua yes
Ruby yes
PHP yes
-----------------------------------------------------------------------
Now you type make and sudo make install. This will build the library, and the modules for interpreters. Also will install the compiled modules to the destination directories.
To run the tests, type make check. The tests are plain C programs under tests/, without any framework. Without configure (or with sanitizers) you can run the same tests with the tests/run-tests.sh script, eg.:
CFLAGS="-g -fsanitize=address,undefined" tests/run-tests.shTo avoid the name collison of modules in the bindings/ directory, the compiled modules will have different names: mscpylogparser.so for Python, msclualogparser.so for Lua, mscrubylogparser.so for Ruby, and mscphplogparser.so for PHP.
Both for native and embedding usage you can find an example under the examples/ and bindings directory. The names of tests scripts are pytest.py, luatest.lua, rubytest.rb and phptest.php. Please note, that the necessary import module names are different in each language.
In those tests scripts you can see the method to access for the constants and how should you call the parse() method.
In case of PHP please note, that normally PHP does not allow to import the extension from other place than the php.ini listed. You should install the module before you want to test it.
Also if you do not set --prefix, then it will be the /usr/local, and the library will installed to /usr/local/lib. Perhaps you should run sudo ldconfig after the install process.
If you have any problem or feature request, please open an issue on Github, or send us an e-mail: modsecurity at digitalwave dot hu.