.\" .\" SEC (Simple Event Correlator) 2.9.2 - sec.man .\" Copyright (C) 2000-2023 Risto Vaarandi .\" .\" This program is free software; you can redistribute it and/or .\" modify it under the terms of the GNU General Public License .\" as published by the Free Software Foundation; either version 2 .\" of the License, or (at your option) any later version. .\" .\" This program is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public License .\" along with this program; if not, write to the Free Software .\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. .\" .TH sec 1 "June 2023" "SEC 2.9.2" .SH NAME sec \- simple event correlator .SH SYNOPSIS .TP .B sec [--conf= ...] .br [--input=[=] ...] .br [--input-timeout=] .br [--timeout-script=] .br [--reopen-timeout=] .br [--check-timeout=] .br [--poll-timeout=] .br [--socket-timeout=] .br [--blocksize=] .br [--bufsize=] .br [--evstoresize=] .br [--cleantime=] .br [--log=] .br [--syslog=] .br [--debug=] .br [--pid=] .br [--dump=] .br [--user=] .br [--group= ...] .br [--umask=] .br [--ruleperf | --noruleperf] .br [--dumpfts | --nodumpfts] .br [--dumpfjson | --nodumpfjson] .br [--quoting | --noquoting] .br [--tail | --notail] .br [--fromstart | --nofromstart] .br [--detach | --nodetach] .br [--jointbuf | --nojointbuf] .br [--keepopen | --nokeepopen] .br [--rwfifo | --norwfifo] .br [--childterm | --nochildterm] .br [--intevents | --nointevents] .br [--intcontexts | --nointcontexts] .br [--testonly | --notestonly] .br [--help] [-?] .br [--version] .SH DESCRIPTION SEC is an event correlation tool for advanced event processing which can be harnessed for event log monitoring, for network and security management, for fraud detection, and for any other task which involves event correlation. Event correlation is a procedure where a stream of events is processed, in order to detect (and act on) certain event groups that occur within predefined time windows. Unlike many other event correlation products which are heavyweight solutions, SEC is a lightweight and platform-independent event correlator which runs as a single process. The user can start it as a daemon, employ it in shell pipelines, execute it interactively in a terminal, run many SEC processes simultaneously for different tasks, and use it in a wide variety of other ways. .PP SEC reads lines from files, named pipes, or standard input, matches the lines with patterns (regular expressions, Perl subroutines, etc.) for recognizing input events, and correlates events according to the rules in its configuration file(s). Rules are matched against input in the order they are given in the configuration file. If there are two or more configuration files, rule sequence from every file is matched against input (unless explicitly specified otherwise). SEC can produce output by executing external programs (e.g., .BR snmptrap (1) or .BR mail (1)), by writing to files, by sending data to TCP and UDP based servers, by calling precompiled Perl subroutines, etc. .PP SEC can be run in various ways. For example, the following command line starts it as a daemon, in order to monitor events appended to the /var/log/messages syslog file with rules from /etc/sec/syslog.rules: .PP /usr/bin/sec --detach --conf=/etc/sec/syslog.rules \\ --input=/var/log/messages .PP Each time /var/log/messages is rotated, a new instance of /var/log/messages is opened and processed from the beginning. The following command line runs SEC in a shell pipeline, configuring it to process lines from standard input, and to exit when the /usr/bin/nc tool closes its standard output and exits: .PP /usr/bin/nc -l 8080 | /usr/bin/sec --notail --input=- \\ --conf=/etc/sec/my.conf .PP Some SEC rules start event correlation operations, while other rules react immediately to input events or system clock. For example, suppose that SEC has been started with the following command line .PP /usr/bin/sec --conf=/etc/sec/sshd.rules --input=/var/log/secure .PP in order to monitor the /var/log/secure syslog file for sshd events. Also, suppose that the /etc/sec/sshd.rules configuration file contains the following rule for correlating SSH failed login syslog events: .PP type=SingleWithThreshold .br ptype=RegExp .br pattern=sshd\\[\\d+\\]: Failed .+ for (\\S+) from [\\d.]+ port \\d+ ssh2 .br desc=Three SSH login failures within 1m for user $1 .br action=pipe '%s' /bin/mail -s 'SSH login alert' root@localhost .br window=60 .br thresh=3 .PP The .I pattern field of the rule defines the pattern for recognizing input events, while the .I ptype field defines its type (regular expression). Suppose that user risto fails to log in over SSH and the following message is logged to /var/log/secure: .PP Dec 16 16:24:59 myserver sshd[13685]: Failed password for risto from 10.12.2.5 port 41063 ssh2 .PP This input message will match the regular expression pattern of the above rule, and the match variable $1 will be set to the string .I risto (see .BR perlre (1) for details). After a match, SEC will evaluate the operation description string given with the .I desc field. This is done by substituting $1 with its current value which yields .IR "Three SSH login failures within 1m for user risto" . SEC will then check if there already exists an event correlation operation identified with this string and triggered by the same rule. If the operation is not found, SEC will create a new operation for the user name risto, and the occurrence time of the input event will be recorded into the operation. Note that for event occurrence time SEC always uses the current time as returned by the .BR time (2) system call, *not* the timestamp extracted from the event. .PP Suppose that after 25 seconds, a similar SSH login failure event for the same user name is observed. In this case, a running operation will be found for the operation description string .IR "Three SSH login failures within 1m for user risto" , and the occurrence time of the second event is recorded into the operation. If after 30 seconds a third event for the user name risto is observed, the operation has processed 3 events within 55 seconds. Since the threshold condition "3 events within 60 seconds" (as defined by the .I thresh and .I window fields) is now satisfied, SEC will execute the action defined with the .I action field -- it will fork a command .PP /bin/mail -s 'SSH login alert' root@localhost .PP with a pipe connected to its standard input. Then, SEC writes the operation description string .I "Three SSH login failures within 1m for user risto" (held by the %s special variable) to the standard input of the command through the pipe. In other words, an e-mail warning is sent to the local root-user. Finally, since there are 5 seconds left until the end of the event correlation window, the operation will consume the following SSH login failure events for user risto without any further action, and finish after 5 seconds. .PP The above example illustrates that the .I desc field of a rule defines the scope of event correlation and influences the number of operations created by the rule. For example, if we set the .I desc field to .IR "Three SSH login failures within 1m" , the root-user would be also alerted on 3 SSH login failure events for *different* users within 1 minute. In order to avoid clashes between operations started by different rules, operation ID contains not only the value set by the .I desc field, but also the rule file name and the rule number inside the file. For example, if the rule file /etc/sec/sshd.rules contains one rule .PP type=SingleWithThreshold .br ptype=RegExp .br pattern=sshd\\[\\d+\\]: Failed .+ for (\\S+) from [\\d.]+ port \\d+ ssh2 .br desc=Three SSH login failures within 1m for user $1 .br action=pipe '%s' /bin/mail -s 'SSH login alert' root@localhost .br window=60 .br thresh=3 .PP and the event .PP Dec 16 16:24:59 myserver sshd[13685]: Failed password for risto from 10.12.2.5 port 41063 ssh2 .PP is the first matching event for the above rule, this event will trigger a new event correlation operation with the ID .PP /etc/sec/sshd.rules | 0 | Three SSH login failures within 1m for user risto .PP (0 is the number assigned to the first rule in the file, see EVENT CORRELATION OPERATIONS section for more information). .PP The following simple example demonstrates that event correlation schemes can be defined by combining several rules. In this example, two rules harness contexts and synthetic events for achieving their goal: .PP type=SingleWithThreshold .br ptype=RegExp .br pattern=sshd\\[\\d+\\]: Failed .+ for (\\S+) from [\\d.]+ port \\d+ ssh2 .br desc=Three SSH login failures within 1m for user $1 .br action=event 3_SSH_LOGIN_FAILURES_FOR_$1 .br window=60 .br thresh=3 .PP type=EventGroup .br ptype=RegExp .br pattern=3_SSH_LOGIN_FAILURES_FOR_(\\S+) .br context=!USER_$1_COUNTED && !COUNTING_OFF .br count=create USER_$1_COUNTED 60 .br desc=Repeated SSH login failures for 30 distinct users within 1m .br action=pipe '%s' /bin/mail -s 'SSH login alert' root@localhost; \\ create COUNTING_OFF 3600 .br window=60 .br thresh=30 .PP The first rule looks almost identical to the rule from the previous example, but its .I action field is different -- after three SSH login failures have been observed for the same user name within one minute by an event correlation operation, the operation will emit the synthetic event 3_SSH_LOGIN_FAILURES_FOR_. Although synthetic events are created by SEC, they are treated like regular events received from input sources and are matched against rules. .PP The regular expression pattern of the second rule will match the 3_SSH_LOGIN_FAILURES_FOR_ event and start a new event correlation operation if no such events have been previously seen. Also, each time a synthetic event for some user name has matched the rule, a context with the lifetime of 1 minute for that user name is created (see the .I count field). Note that this prevents further matches for the same user name, since a synthetic event for can match the rule only if the context USER__COUNTED *does not* exist (as requested by the boolean expression in the .I context field; see CONTEXTS AND CONTEXT EXPRESSIONS section for more information). .PP The operation started by the second rule sends an e-mail warning to the local root-user if 30 synthetic events have been observed within 1 minute (see the .I thresh and .I window fields). Note that due to the use of the USER__COUNTED contexts, all synthetic events concern different user names. After sending an e-mail warning, the operation will also create the context COUNTING_OFF with the lifetime of 1 hour, and will continue to run until the 1 minute event correlation window expires. After the operation has finished, the presence of the COUNTING_OFF context will keep the second rule disabled (as requested by the boolean expression in the .I context field). Therefore, at most one e-mail warning per 1 hour is issued by above rules. .PP The above examples have presented the event correlation capabilities of SEC in a very brief fashion. The following sections will provide an in-depth discussion of SEC features. .SH OPTIONS .TP .B \-\-conf= expand to filenames (with the Perl .BR glob () function) and read event correlation rules from every file. Multiple .B \-\-conf options can be specified at command line. Each time SEC receives a signal that forces a configuration reload, is re-evaluated. See also INPUT PROCESSING AND TIMING section for a discussion on rule processing order for multiple configuration files. .TP .B \-\-input=[=] expand to filenames (with the Perl .BR glob () function) and use the files as input sources. An input file can be a regular file, named pipe, or standard input if .B \- was specified. Multiple .B \-\-input options can be specified at command line. Each time SEC receives the .B SIGHUP or .B SIGABRT signal, is re-evaluated. If SEC experiences a system error when reading from an input file, it will close the file (use the .B \-\-reopen\-timeout option for reopening the file). If is given, SEC will set up the context each time it reads a line from input files that correspond to . This will help the user to write rules that match data from particular input source(s) only. When there is an .B \-\-input option with specified, it will automatically enable the .B \-\-intcontexts option. See INTERNAL EVENTS AND CONTEXTS section for more information. .TP .BR \-\-input\-timeout= ", " \-\-timeout\-script= if SEC has not observed new data in an input file during seconds (or the file was closed seconds ago), will be executed with command line parameters 1 and . If fresh data become available again, will be executed with command line parameters 0 and . Setting to 0 disables this behavior (this is also the default). Note that .B \-\-input_timeout and .B \-\-timeout_script options can be used as synonyms for .B \-\-input\-timeout and .BR \-\-timeout\-script , respectively. .TP .B \-\-reopen\-timeout= if an input file is in the closed state (e.g., SEC fails to open the file at startup, because it has not been created yet), SEC will attempt to reopen the file after every seconds until open succeeds. Setting to 0 disables this behavior (this is also the default). This option has no meaning when the .B \-\-notail option is also specified. Note that .B \-\-reopen_timeout is a synonym for .BR \-\-reopen\-timeout . .TP .B \-\-check\-timeout= if SEC has not observed new data in an input file, the file will not be polled (both for status and data) during the next seconds. Setting to 0 disables this behavior (this is also the default). Note that .B \-\-check_timeout is a synonym for .BR \-\-check\-timeout . .TP .B \-\-poll\-timeout= a real number that specifies how many seconds SEC will sleep when no new data were read from input files. Default is 0.1 seconds. Note that .B \-\-poll_timeout is a synonym for .BR \-\-poll\-timeout . .TP .B \-\-socket\-timeout= if a network connection to a remote peer can't be established within seconds, give up. Default is 60 seconds. Note that .B \-\-socket_timeout is a synonym for .BR \-\-socket\-timeout . .TP .B \-\-blocksize= the number of bytes SEC will attempt to read at once from an input file. Default is 8192 bytes (i.e., read from input files by 8KB blocks). .TP .B \-\-bufsize= set all input buffers to hold lines. The content of input buffers will be compared with patterns that are part of rule definitions (i.e., no more than lines can be matched by a pattern at a time). If is set to 0, SEC will determine the proper value for by checking event matching patterns of all SEC rules. Default is 0 (i.e., determine the size of input buffers automatically). .TP .B \-\-evstoresize= set an upper limit to the number of events in context event stores. Default is 0 which sets no limit. .TP .B \-\-cleantime= time interval in seconds that specifies how often internal event correlation and context lists are processed, in order to accomplish time-related tasks and to remove obsolete elements. See INPUT PROCESSING AND TIMING section for more information. Default is 1 second. .TP .B \-\-log= use for logging SEC activities. Note that if the SEC standard error is connected to a terminal, messages will also be logged there, in order to facilitate debugging. .TP .B \-\-syslog= use syslog for logging SEC activities. All messages will be logged with the facility , e.g., .I local0 (see .BR syslog (3) for possible facility values). Warning: be careful with using this option if SEC is employed for monitoring syslog log files, because message loops might occur. .TP .B \-\-debug= set logging verbosity for SEC. Setting debuglevel to means that all messages of level and lower are logged (e.g., if is 3, messages from levels 1-3 are logged). The following levels are recognized by SEC: .br 1 - critical messages (severe faults that cause SEC to terminate, e.g., a failed system call) .br 2 - error messages (faults that need attention, e.g., an incorrect rule definition in a configuration file) .br 3 - warning messages (possible faults, e.g., a command forked from SEC terminated with a non-zero exit code) .br 4 - notification messages (normal system level events and interrupts, e.g., the reception of a signal) .br 5 - informative messages (information about external programs forked from SEC) .br 6 - debug messages (detailed information about all SEC activities) .br Default is 6 (i.e., log everything). See SIGNALS section for information on how to change at runtime. .TP .B \-\-pid= SEC will store its process ID to at startup. .TP .B \-\-dump= SEC will use as its dump file for writing performance and debug data. With the .B \-\-dumpfts option, a timestamp suffix is appended to the dump file name. With the .B \-\-dumpfjson option, dump file is produced in JSON format. See SIGNALS section for more information. Default is /tmp/sec.dump. .TP .BR \-\-user= ", " \-\-group= if SEC is started with effective user ID 0, it will drop root privileges by switching to user and group . The .B \-\-group option can't be used without the .B \-\-user option. If the .B \-\-user option is given without .BR \-\-group , primary group of the user is assumed for . If several groups are provided with multiple .B \-\-group options, SEC switches to the first group with other groups as supplementary groups. .TP .B \-\-umask= set file mode creation mask to at SEC startup, where is a value from the range 0..0777 (see also .BR umask (2)). Octal, decimal, hexadecimal, and binary values can be specified for (e.g., octal mask 0027 can also be expressed as 23, 0x17, and 0b000010111). .TP .BR \-\-ruleperf ", " \-\-noruleperf if the .B \-\-ruleperf option is specified, performance data (e.g., total consumed CPU time) is collected for each rule and reported in dump file. Default is .BR \-\-noruleperf . .TP .BR \-\-dumpfts ", " \-\-nodumpfts if the .B \-\-dumpfts option is specified, a timestamp suffix (seconds since Epoch) is appended to the dump file name that reflects the file creation time. Default is .BR \-\-nodumpfts . .TP .BR \-\-dumpfjson ", " \-\-nodumpfjson if the .B \-\-dumpfjson option is specified, dump file is produced in JSON format. Default is .BR \-\-nodumpfjson . .TP .BR \-\-quoting ", " \-\-noquoting if the .B \-\-quoting option is specified, operation description strings that are supplied to command lines of .IR shellcmd , .IR spawn , and .I cspawn actions will be put inside single quotes. Each single quote (') that strings originally contain will be masked. This option prevents the shell from interpreting special symbols that operation description strings might contain. Default is .BR \-\-noquoting . .TP .BR \-\-tail ", " \-\-notail if the .B \-\-notail option is specified, SEC will process all data that are currently available in input files and exit after reaching all EOFs. If all input is received from a pipe and the .B \-\-notail option is given, SEC terminates when the last writer closes the pipe (EOF condition). Please note that with named pipes .B \-\-notail should be used with .BR \-\-norwfifo . With the .B \-\-tail option, SEC will jump to the end of input files and wait for new lines to arrive. Each input file is tracked both by its name and i-node, and input file rotations are handled seamlessly. If the input file is recreated or truncated, SEC will reopen it and process its content from the beginning. If the input file is removed (i.e., there is just an i-node left without a name), SEC will keep the i-node open and wait for the input file recreation. Default is .BR \-\-tail . .TP .BR \-\-fromstart ", " \-\-nofromstart these flags have no meaning when the .B \-\-notail option is also specified. When used in combination with .B \-\-tail (or alone, since .B \-\-tail is enabled by default), .B \-\-fromstart will force SEC to read and process input files from the beginning to the end, before the 'tail' mode is entered. Default is .BR \-\-nofromstart . .TP .BR \-\-detach ", " \-\-nodetach if the .B \-\-detach option is specified, SEC will disassociate itself from the controlling terminal and become a daemon at startup (note that SEC will close its standard input, standard output, and standard error, and change its working directory to the root directory). Default is .BR \-\-nodetach . .TP .BR \-\-jointbuf ", " \-\-nojointbuf if the .B \-\-jointbuf option is specified, SEC uses joint input buffer for all input sources (the size of the buffer is set with the .B \-\-bufsize option). The .B \-\-nojointbuf option creates a separate input buffer for each input file, and a separate buffer for all synthetic and internal events (the sizes of all buffers are set with the .B \-\-bufsize option). The .B \-\-jointbuf option allows multiline patterns to match lines from several input sources, while the .B \-\-nojointbuf pattern restricts the matching to lines from one input source only. See INPUT PROCESSING AND TIMING section for more information. If the size of input buffer(s) is 1 (either explicitly set with .B \-\-bufsize=1 or automatically determined from SEC rules), .B \-\-jointbuf option is enabled, otherwise the default is .BR \-\-nojointbuf . .TP .BR \-\-keepopen ", " \-\-nokeepopen if the .B \-\-keepopen option is specified, SEC will keep input files open across soft restarts. When the .B SIGABRT signal is received, SEC will not reopen input files which have been opened previously, but will only open input files which are in the closed state. The .B \-\-nokeepopen option forces SEC to close and (re)open all input files during soft restarts. Default is .BR \-\-keepopen . .TP .BR \-\-rwfifo ", " \-\-norwfifo if the .BR \-\-norwfifo option is specified, named pipe input files are opened in read-only mode. In this mode, the named pipe has to be reopened when the last writer closes the pipe, in order to clear the EOF condition on the pipe. With the .BR \-\-rwfifo option, named pipe input files are opened in read-write mode, although SEC never writes to the pipes. In this mode, the pipe does not need to be reopened when an external writer closes it, since there is always at least one writer on the pipe and EOF will never appear. Therefore, if the .BR \-\-notail option has been given, .BR \-\-norwfifo should also be specified. Default is .BR \-\-rwfifo . .TP .BR \-\-childterm ", " \-\-nochildterm if the .B \-\-childterm option is specified, SEC will send the .B SIGTERM signal to all its child processes when it terminates or goes through a full restart. Default is .BR \-\-childterm . .TP .BR \-\-intevents ", " \-\-nointevents SEC will generate internal events when it starts up, when it receives certain signals, and when it terminates gracefully. Specific rules can be written to match those internal events, in order to accomplish special tasks at SEC startup, restart, and shutdown. See INTERNAL EVENTS AND CONTEXTS section for more information. Default is .BR \-\-nointevents . .TP .BR \-\-intcontexts ", " \-\-nointcontexts SEC will create an internal context when it reads a line from an input file. This will help the user to write rules that match data from particular input source only. See INTERNAL EVENTS AND CONTEXTS section for more information. Default is .BR \-\-nointcontexts . .TP .BR \-\-testonly ", " \-\-notestonly if the .B \-\-testonly option is specified, SEC will exit immediately after parsing the configuration file(s). If the configuration file(s) contained no faulty rules, SEC will exit with 0, otherwise with 1. Default is .BR \-\-notestonly . .TP .BR \-\-help ", " \-? SEC will output usage information and exit. .TP .B \-\-version SEC will output version information and exit. .PP Note that options can be introduced both with the single dash (-) and double dash (--), and both the equal sign (=) and whitespace can be used for separating the option name from the option value. For example, .B \-conf= and .B \-\-conf options are equivalent. .SH CONFIGURATION FILES Each SEC configuration file consists of rule definitions which are separated by empty lines, whitespace lines and/or comment lines. Each rule definition consists of keyword=value fields, one keyword and value per line. Values are case insensitive only where character case is not important (like the values specifying rule types, e.g., 'Single' and 'single' are treated identically). The backslash character (\\) may be used at the end of a line to continue the current rule field in the next line. Lines which begin with the number sign (#) are treated as comments and ignored (whitespace characters may precede #). Any comment line, empty line, whitespace line, or end of file will terminate the preceding rule definition. For inserting comments into rule definitions, the .I rem keyword can be used. For example, the following lines define two rules: .PP type=Single .br rem=this rule matches any line which contains \\ three consecutive A characters and writes the string \\ "three A characters were observed" to standard output .br ptype=SubStr .br pattern=AAA .br desc=Three A characters .br action=write - three A characters were observed .br # This comment line ends preceding rule definition. .br # The following rule works like the previous rule, .br # but looks for three consecutive B characters and .br # writes the string "three B characters were observed" .br # to standard output .br type=Single .br ptype=SubStr .br pattern=BBB .br desc=Three B characters .br action=write - three B characters were observed .PP Apart from keywords that are part of rule definitions, .I label keywords may appear anywhere in the configuration file. The value of each .I label keyword will be treated as a label that can be referred to in rule definitions as a point-of-continue. This allows for continuing event processing at a rule that follows the label, after the current rule has matched and processed the event. .PP The points-of-continue are defined with .I continue* fields. Accepted values for these fields are: .TP .I TakeNext after an event has matched the rule, search for matching rules in the configuration file will continue from the next rule. .TP .I GoTo