Chapter 5. The programs sockserv and servsock

These two programs are very similar and work with two parallel threads. One thread receives the alerts and the other processes these data.

The principal of the sockserv/servsock process.

The first thread of the program sockserv[1] receives alerts from snort and stores them in a buffer in memory. The second thread takes these alerts and forwards them via TCP/IP to the servsock[2] program. This program consists of a master program waiting for connections from sockserv processes of remote sensors. For each connection one process is forked off. Each of these processes consist of two threads. One thread simply receives the incoming alerts, the second stores them to the database.

Starting with version 1.5.0 there is a third thread running, a control thread. This thread listens on a named pipe which can be used to change several options during runtime. See the file README.ctrl for further informations.

5.1. The details of sockserv

This program provides an unix domain socket for snort. One thread simply receives alerts via this socket and stores them in memory, see picture sockserv/servsock process.

Through the threading design and the use of a memory buffer the risk of loosing alerts is minimized. The output plugins from snort are reduced to a simple write statement on an unix domain socket. If more alerts are generated than sockserv can send to the central server these alerts are buffered in memory until the attack flood decreases.

To reduce the problem on memory shortage due to an high attack flood, the maximum number of alerts in the buffer can be limited. This is done via two parameters, the LowWater and HighWater marks. If more alerts than the HighWater mark are buffered in memory as many alerts are dropped until the LowWater mark is reached. All dropped alerts are written to an unix domain socket. The program drop is able to provide such a socket, receive these alerts and send them via email to a list of recipients. Otherwise, if you do not use drop, the these informations are written to stdout or syslog.

If either sockserv can not connect to servsock on startup or the connection is closed during runtime the program tries to reopen the connection after a short delay for several times.

Note: If the buffer of sockserv is empty at this point of time it tries a reconnect only if a new alert arrives.

All output can be redirected to syslog, using the facility LOCAL0 and level INFO. A daemon mode is also supported. Finally statistics could be printed on a periodical basis or once by using the named pipe of the sockserv process.

5.1.1. Options

There are several options available:

   sockserv [-bhlqv] [-A delay] [-D dropsocket] [-H HighWater] [-I interface] 
            [-L LowWater] [-m mode] [-M maxtry] [-N sensorname] [-p port] 
            [-P pidfile] [-s snortsocket] [-S server] [-V area,level] [-w dir] [-W waittime]

The sockserv options in detail

-A delay

Print every delay seconds statistics about received, sent and dropped alerts. The change of these values between delay seconds is printed in brackets. See also option -l.

-b

Start the process in the background: daemon mode. This automatically activates option -l.

-D dropsocket

If there are more than HighWater alerts buffered then the newest alerts are dropped to dropsocket until the LowWater mark is reached.

-h

Print a help message and exit.

-H HighWater

Sets the HighWater mark, see option -D. The default value is 10000.

-I interface

Interface on which snort is sniffing. This parameter is optional but together with the sensorname, see Option -N, it should be unique. By default this option is not used.

-l

Log statitiscs to syslog instead of stdout. See also option -A.

-q

Enable quiet dropping, no dropped alerts were written to the dropsocket, see option -D.

-L LowWater

Sets the LowWater mark, see option -D. The default value is 9900.

-m mode

Sets the umask to mode for the daemon mode. This affects the mode for the created unix socket and PID file. The mode can be either given in ascii, octal (with leading 0) or hex (with leading 0x) format.

-M maxtry

Sets the maximum number of tries to (re-) connect to the server. See also option -W.

-N sensorname

Sensorname which should be used in the database. By default this is the hostname of the machine running sockserv. It is now possible to use more than one instance of sockserv per sensor. Note: You have to change the unix socket for different instances, see Option -s.

-p port

Defines on which port to try to reach the server running servsock. See also option -S.

-P pidfile

Filename to store the PID. Note: This file must be writeable by the user running sockserv!

-s snortsocket

Defines the name and directory where the unix domain socket is opened for snort. The default is /tmp/snort.

Maybe the default should be changed to something like /tmp/snort_sensorname if option -N is used.

-S server

Defines the server running servsock. The name can be either a full qualified domain name or an IP address. The default is 127.0.0.1. See also option -p.

Note: The default is only useful for testing. In real productive systems you should use a separate server for the database and servsock.

-V area,value

Sets the debug level of the program. Area specifies the section of the code which should generate debug output. value should be between 0 (disabled) and 9 (maximum output). For further information read the file README.debug.

-w dir

Sets the working directory in daemon mode to dir. The default is to use the current working directory. It is useful to choose / to avoid blocking of mounted filesystems.

-W waittime

Time in seconds to wait between two tries to connect to the server. See also option -M.

5.1.2. Signalhandling

Currently the following signals are used with sockserv:

Signals used with sockserv

SIGINT

Cancels the process, prints the final statistics and performs a clean exit. The socketname and pidfile are removed.

SIGTERM

This signal results in the same behaviour as SIGINT.

SIGPIPE

This signal is ignored. If the servsock program is interrupted during the data is sended. In this case we simply try to open a new connection and therefore we have to ignore this signal.

SIGHUP[3]

If this signal is received sockserv stops and restarts. First, if enabled, all buffered alerts are dropped via dropsocket and the final statistics are printed. Further socketname and pidfile are removed to enable a restart of the program. (Otherwise the program would fail since the id does not change!)

The actual version of sockserv uses a control thread to change some parameters during runtime. This thread is also used to print statistics on a periodically interval instead of using signals.

5.1.3. Some additional notes

The drop feature is not enabled by default and has to be compiled in separately. If it is not compiled in then the options -D, -L and -H are missing in the output of the -h option. It is highly advisable to choose a very large HighWater mark to buffer as many alerts as possible. This will reduce the possibility of information loss. On the other hand the difference between HighWater and LowWater should not be too large. To minimize information loss the alerts are spooled via drop to a mail server. Normally this server is either located on the central server or is reached via this server. If there are too many alerts spooled to drop the emails become unreadable long.

Problems should only arise if the connection to the servsock program is lost for a longer period. But if there are network problems then it is alike that drop will fail too. If this happens then the alerts are written either to stdout or syslog.

Be cautious: With increasing buffer usage the memory consumption raises with about 3 kB for each alert (actually 1048 bytes per alert on an x86_32 System plus payload). But this memory is shared with the snort process. So set the HighWater to a value where it is safe for the snort process.

If a pid file exists then the program checks only for a running process with this PID. If one process is found the program exits. There is no check for which program is running, only if one with this PID exists!

Notes

[1]

This program povides an unix domain socket and connects to a server.

[2]

This program povides an server and writes the alerts via an unix domain socket to the database.

[3]

One important thing to obey is that either the program has to be started with absolute path or relative to the daemon working directory (option -w). Or the program has to be started without any path information at all and should be found in the system PATH. Otherwise the program will not find the own executable and will fail.