SPECTER - the Userspace Logging Daemon

Michal Kwiatkowski <ruby@joker.linuxstuff.pl>

Last modified: 2004/06/20


This is the documentation for specter, userspace logging daemon. specter makes use of the Linux >= 2.4.x packet filter subsystem (iptables) and the ULOG target for iptables.

1. DESIGN

1.1 Concept

I want to provide a flexible, almost universal logging daemon for netfilter ULOG target. It is not optimized in any way, the goal is to keep as simple as possible. These are my thoughts about how the architecture which is most capable of doing that:

Input plugins

It should be possible to add plugins / runtime modules for new protocols, etc. For example the standard logging daemon provides source-ip, dest-ip, source-port, dest-port, etc. Logging for variuos other protocols (GRE, IPsec, ...) may be implemented as modules.

Output plugins

... describe how and where to put the information gained by input plugins. The easiest way is to build a line per packet and fprint it to a file. Some people might want to log into a SQL database or want an output conforming to the intrusion detection systems communication draft from the IETF.

1.2 Details

The major clue is providing a framework which is as flexible as possible. Nobody knows what strange network protocols are out there :) Flexibility depends on the communication between the output of the input plugins and input of the output plugins.

Harald, following Rusty's advise, implemented type-key-value triples, which work quite well for that purpose. Structure used for exchanging data between input and output plugins is defined in specter.h, and is called specter_iret_t. Most of time, output plugins precisely know what data they need, so there must exist good querying system, as input keys are dynamically defined and stored. Up to ulogd 0.3 this was done by several linked list iterations, which weren't obviously very fast. In 0.9 Harald implemented usage of hash tables initialized during init. The idea was good, but deep levels of data structures one had to dig into to get simple value and somewhat obscure style (like accessing ulogd_keyh[] from the inside of plugin) forced me (Michal) to rewrite this again. That's when fork from ulogd happened. Abandoning hash tables, specter implementation use only pointers accessed by general function find_iret(). To simplify usage of that pointers, simple data structure specter_local_ret_t and few macros defined in plugins/lret.h were also created. Note they're not the obligatory extension; one can create his own implementation based on single find_iret() definition.

2. INSTALLATION

2.1 Linux kernel

First you will need a 2.4.x or 2.6.x kernel. If you have a kernel >= 2.4.18-pre8, it already has the kernel suport for ULOG (ipt_ULOG.o).

If you have an older kernel version (between 2.4.0 and 2.4.18-pre6), you can use the patch-o-matic system of netfilter/iptables, as described in the following section.

If you experience problems like described in my mail http://lists.netfilter.org/pipermail/netfilter-devel/2004-June/015860.html you should apply ipt_ULOG patch you can find in contrib/ subdirectory. For some reason it wasn't merged into netfilter source.

2.2 ipt_ULOG from netfilter/iptables patch-o-matic

You only need to read this chapter if you have a 2.4.x kernel <= 2.4.18-pre6.

In order to put the ipt_ULOG module into your kernel source, you need the latest iptables package, or even better: the latest CVS snapshot. A description how to obtain this is provided on the netfilter homepage http://www.netfilter.org/.

To run patch-o-matic, just type

make patch-o-matic
in the userspace directory of netfilter CVS.

2.3 specter

Recompiling the source

Download the specter package from http://joker.linuxstuff.pl and untar it.

If you want to build specter with MySQL support, type './configure --with-mysql'. You may also have to specify the path of the mysql libraries using '--with-mysql=path'. To build specter without MySQL support, just use './configure'.

The same procedure apply to PostgreSQL support (use './configure --with-pgsql' with or without path to libraries).

If you have other applications using libipulog library contained with this package, you may consider building it shared. To enable this, use './configure --with-sharedlib'.

To compile and install the program, call 'make install'.

2.4 front-ends

There are several front-ends for viewing logs generated by specter. Although they where designed for ulogd, there should be no problem with using them with specter. Here are few links to that kind of projects:

CCZE

http://bonehunter.rulez.org/CCZE.phtml

Pothos

http://sourceforge.net/projects/pothos/

ulogd frontend

http://johoho.eggheads.org/files/ulogd_php.tar.bz2

ulog-monitor

http://w5.cs.uni-sb.de/~gogo/homepage/ulog-monitor/

ulog-php

http://www.inl.fr/article.php3?id_article=7

You can also find sample php query script in contrib/.

3. CONFIGURATION

3.1 iptables ULOG target

Quick Setup

Just add rules using the ULOG target to your firewalling chain. A very basic example:

iptables -A FORWARD -j ULOG --ulog-nlgroup 32 --ulog-prefix foo 

To increase logging performance, try to use the

--ulog-qthreshold N
option (where 1 < N <= 50). The number you specify is the amout of packets batched together in one multipart netlink message. If you set this to 20, the kernel schedules specter only once every 20 packets. All 20 packets are then processed by specter. This reduces the number of context switches between kernel and userspace.

Of course you can combine the ULOG target with the different netfilter match modules. For a more detailed description, have a look at the netfilter HOWTO's, available on the netfilter homepage.

ULOG target reference

--ulog-nlgroup N

The number of the netlink multicast group to which ULOG'ed packets are sent. In specter, you can specify different task for different netlink groups, see specter configfile syntax reference section for more details.

--ulog-cprange N

Copyrange. This works like the 'snaplen' paramter of tcpdump. You can specify a number of bytes up to which the packet is copied. If you say '40', you will receive the first fourty bytes of every packet. Leave it to '0' if you want whole packet to be copied to userspace, which is mostly what you need.

--ulog-qthreshold N

Queue threshold. If a packet is matched by the iptables rule, and already N packets are in the queue, the queue is flushed to userspace. You can use this to implement a policy like: Use a big queue in order to gain high performance, but still have certain packets logged immediately to userspace.

--ulog-prefix STRING

A string that is associated with every packet logged by this rule. You can use this option to later tell from which rule the packet was logged.

ipt_ULOG module parameters

The ipt_ULOG kernel module has a couple of module loadtime parameters which can (and should) be tuned to accomodate the needs of the application:

nlbufsiz N

Netlink buffer size. A buffer of the specified size N is allocated for every netlink group that is used. Please note that due to restrictions of the kernel memory allocator, we cannot have a buffer size > 128kBytes. Larger buffer sizes increase the performance, since less kernel/userspace context switches are needed for the same amount of packets. The backside of this performance gain is a potentially larger delay. The default value is 4096 bytes, which is quite small.

flushtimeout N

The flushtimeout determines, after how many clock ticks (on alpha: 1ms, on x86 and most other platforms: 10ms time units) the buffer/queue is to be flushed, even if it is not full. This can be used to have the advantage of a large buffer, but still a finite maximum delay introduced. The default value is set to 10 seconds.

Example:
modprobe ipt_ULOG nlbufsiz=65535 flushtimeout=100
This would use a buffer size of 64k and a flushtimeout of 100 clockticks (1 second on x86).

3.2 specter

specter is what this is all about, so let's describe it's configuration...

specter configfile syntax reference

specter reads its configuration parameters from file, which is mostly `/etc/specter.conf'. It is divided into blocks. Each block start with a opening curly bracket { and end with closing curly bracket }. Nesting of blocks (opening new block inside another) is forbidden, and there's no need for that in specter configuration. In order to distinguish between blocks, each has a name. Allowed names for blocks are numbers within range 1-32, which describe different options for given netlink group, and a special name global, which is used to specify general daemon parameters. You cannot define the same block twice, but don't have to define all of them. In most configurations you'll be fine with two or three blocks.

Each block have to start in a new line, then goes its name and opening bracket. Inside block you specify options, each in a separate line, and end a block with a closing bracket. So, in general, block definition looks like this:

name {
option value
# comment
option "long value that needs spaces"
option
option value # another comment
   ...
}

As you can see, not every option needs a value, in that case its presence will override a default (see below for specific options description). A hash # is used as a comment, as it will cause a rest of line to be ignored. Of course you can use comments everywhere, not only inside blocks. If you need to set an option to a string containing spaces or tabs, you can enclose it inside double quotation marks, as shown above. And if you ever manage to write a very long config line, you can cut it by \ and continue your statement in the line below.

Available global options are:

errignore

This options causes specter to continue running despite of errors generated by plugins. That doesn't affect initialization process, when all errors cause an exit. This option can be useful on heavy-load systems, when you expect some malloc() to fail. It doesn't take any arguments.

logfile

Path to a file you want specter messages to get logged to. Can be set to stdout or stderr

loglevel

The lower the value, the more information is logged. If you experience any problems, check lowest, debug loglevel=1, so that you can see all messages. The highest loglevel is 8, which cause only fatal errors to be shown. The default is 3.

rmem

Size of the netlink socket receive memory. You should set this to at least the size of the kernel buffer (nlbufsiz parameter of the ipt_ULOG module). Please note that there is a maximum limit in /proc/sys/net/core/rmem_max which you cannot exceed by increasing the ``rmem'' parameter. You may need to raise the system-wide maximum limit before. You can define this variable in kilobytes (suffix it by 'K') or in megabytes (use 'M' suffix).

bufsize

Size of the receive buffer. You should set this to at least the size rmem option has. Like rmem can be suffixed by 'M' or 'K'.

specter commandline option reference

Apart from the configfile, there are a couple of commandline options to specter:

-h --help

Print a help message about the commandline options.

-V --version

Print version information about specter.

-d --daemon

For off into daemon mode. Unless you are debugging, you will want to use this most of the time.

-c --configfile

Using this commandline option, an alternate config file can be used. This is important if multiple instances of specter are to be run on a single machine.

-u --uid

This option tells specter to drops its privileges and run as given user.

4. AVAILABLE PLUGINS

specter does nearly nothing on its own, it uses plugins for all the dirty work. They are divided into two groups. Input plugins analyze a packet and create hash table concerning received data, in the form like key=value. They don't open files nor they take any input from user. Only output plugins take options. They actually use data from input plugins - save it into logs/databases or execute appropriate commands. So it's vital for you to learn about their configuration, because it's the essence of using specter.

Any option can appear only once in a single block, but there's obviously an exception. There's one option that can appear in a different blocks many times. That's of course plugin statement. As an argument you have to specify full path to the plugin binary, which will mostly be `/lib/specter/specter_PLUGINNAME.so'.

4.1 Input plugins

specter comes with the following input plugins:

specter_BASE.so

Basic input plugin for nfmark, timestamp, mac address, ip header, tcp header, udp header, icmp header, ah/esp header... Most output plugins need this very important plugin.

specter_PWSNIFF.so

Example input plugin to log plaintext passwords as used with FTP and POP3. Don't blame me for writing this plugin! The protocols are inherently insecure, and there are a lot of other tools for sniffing passwords... it's just an example.

specter_LOCAL.so

This is a 'virtual interpreter'. It doesn't really return any information on the packet itself, rather the local system time and hostname. Please note that the time is the time at the time of logging, not the packets receive time.

4.2 Output plugins

specter comes with the following output plugins:

specter_EXEC.so

This plugin executes specified command when packet is received. By proper use of its functions you can dynamically change your firewall configuration, or even set up simple port-knocking utility.

execstr

That option defines a command that should be executed. Don't rely on your $PATH environment variable, and provide full path to an executable. Few printf-like macros can be used, which are expanded during parsing of every packet:

%I

interface packet got received from

%O

interface packet is going to be sent to

%S

IP address of source host

%D

IP address of destination host

%P

IP protocol number (see /etc/protocols)

%s

TCP/UDP source port

%d

TCP/UDP destination port

%i

ICMP type value

execforce

When a macros expansion is being done, and any field is empty, executing of a given command is aborted. For example, if you have %i in your execstr and specter gets a tcp packet, command won't be executed, 'cos given macro cannot be expanded (there's no ICMP type field in a TCP packet). You can override this behavior by setting execforce option. Instead of bogus data, string "invalid" will be placed. It's up to executed application to work with that.

execwait

If this options is set, daemon will wait until application terminates. It's probably not a good idea to actually use it. If you definitely need it, do it with caution, because it can freeze the whole daemon. Enforcing execution limits should be set in iptables rules by use of limit module, for example.

specter_OPRINT.so

A very simple output module, dumping all packets in the format

===>PACKET BOUNDARY
key=value
key=value
...
===>PACKET BOUNDARY
...
to a file. The only useful application is debugging.

The module defines the following configuration directives:

dumpfile

The filename where it should log to. The default is /var/log/specter.oprint

specter_LOGEMU.so

An output module which tries to emulate the old syslog-based LOG targed as far as possible. Logging is done to a seperate textfile instead of syslog, though.

The module defines the following configuration directives:

logemufile

The filename where it should log to. The default is /var/log/specter.logemu

logemusync

Define this option if you want to have your logfile written synchronously. This may reduce performance, but makes your log-lines appear immediately.

specter_MYSQL.so

An output plugin for logging into a mysql database. This is only compiled if you have the mysql libraries installed, and the configure script was able to detect them. (that is: --with-mysql was specified for ./configure)

The plugin automagically inserts the data into the configured table; It connects to mysql during the startup phase of specter and obtains a list of the columns in the table. Then it tries to resolve the column names against keys of input plugins. This way you can easly select which information you want to log - just by the layout of the table.

If, for example, your table contains a field called 'ip_saddr', specter will resolve this against the key 'ip.saddr' and put the ip address as 32bit unsigned integer into the table.

You may want to have a look at the file 'doc/mysql.table' as an example table including fields to log all keys from specter_BASE.so. Just delete the fields you are not interested in, and create the table.

The module defines the following configuration directives:

mysqldb

Name of the mysql database

mysqltable

Name of the table to which specter should log

mysqlhost

Name of the mysql database host

mysqluser

Name of the mysql user

mysqlpass

Password for mysql

specter_PGSQL.so

An output plugin for logging into a postgresql database. This is only compiled if you have the postresql libraries installed, and the configure script was able to detect them. (that is: --with-pgsql was specified for ./configure)

The plugin automagically inserts the data into the configured table; It connects to postgresql during the startup phase of specter and obtains a list of the columns in the table. Then it tries to resolve the column names against keys of input plugins. This way you can easly select which information you want to log - just by the layout of the table.

If, for example, your table contains a field called 'ip_saddr', specter will resolve this against the key 'ip.saddr' and put the ip address as 32bit unsigned integer into the table.

You may want to have a look at the file 'doc/pgsql.table' as an example table including fields to log all keys from specter_BASE.so. Just delete the fields you are not interested in, and create the table.

The module defines the following configuration directives:

pgsqldb

Name of the postgresql database

pgsqltable

Name of the table to which specter should log

pgsqlhost

Name of the postgresql database host

pgsqluser

Name of the postgresql user

pgsqlpass

Password for postgresql

specter_PCAP.so

An output plugin that can be used to generate libpcap-style packet logfiles. This can be useful for later analysing the packet log with tools like tcpdump or ethereal.

The module defines the following configuration directives:

pcapfile

The filename where it should log to. The default is: /var/log/specter.pcap

pcapsync

Set this option if you want to have your pcap logfile written synchronously. This may reduce performance, but makes your packets appear immediately in the file on disk.

specter_SYSLOG.so

This plugin behaves much like LOGEMU, but logs its input into syslog.

Two options are allowed:

syslog_facility

Facility a message should be logged with. See syslog(3) manual page. specter accepts following facilities: deamon kernel, user, localx (where x is from 0 to 7).

syslog_level

Importance of a message. All standard syslog levels are allowed: emerg, alert, crit, err, warning, notice, info, debug.

5. QUESTIONS / COMMENTS

Comments / questions / ... are all welcomed.

Just drop me a note to ruby@joker.linuxstuff.pl.

If an error doesn't happen during compilation time, you are encoured to get from specter as many information as you can. To do that configure it with --enable-debug option enabled, and set loglevel (in global options) to 1. Include information about your system (architecture, libraries) and a description to help me in reproducing this bug.