10.5 Using Swatch for Automated Log MonitoringOkay, you've painstakingly configured, tested, and fine-tuned your system logger to sort system messages by type and importance and then log them both to their respective files and to a central log server. You've also configured a log-rotation scheme that keeps as much old log data around as you think you'll need. But who's got the time to actually read all those log messages? swatch (the "Simple WATCHer") does. swatch, a free log-monitoring utility written 100% in Perl, monitors logs as they're being written and takes action when it finds something you've told it to look out for. Swatch does for logs what tripwire does for system-file integrity. 10.5.1 Installing SwatchThere are two ways to install swatch. First, of course, is via whatever binary package of swatch your Linux distribution of choice provides. (I use the term loosely here; "executable package" is more precise.) The current version of Mandrake has an RPM package of swatch, but none of the other most popular distributions (i.e., Red Hat, SuSE, Slackware, or Debian) appear to. This is just as well, though, since the second way to install swatch is quite interesting. swatch's source distribution, available from http://www.stanford.edu/~atkins/swatch, includes a sophisticated script called Makefile.PL that automatically checks for all necessary Perl modules (see Should We Let Perl Download and Install Its Own Modules? later in this chapter) and uses Perl 5's CPAN functionality to download and install any modules you need; it then generates a Makefile that can be used to build swatch. After you've installed the required modules, either automatically from swatch's Makefile.PL script or manually (and then running perl Makefile.PL), Makefile.PL should return the contents of Example 10-27. Example 10-27. Successful Makefile.PL run[root@barrelofun swatch-3.0.1]# perl Makefile.PL Checking for Time::HiRes 1.12 ... ok Checking for Date::Calc ... ok Checking for Date::Format ... ok Checking for File::Tail ... ok Checking if your kit is complete... Looks good Writing Makefile for swatch [root@barrelofun swatch-3.0.1]# Once Makefile.PL has successfully created a Makefile for swatch, you can execute the following commands to build and install it: make make test make install make realclean The make test command is optional but useful: it ensures that swatch can properly use the Perl modules we just went to the trouble of installing.
10.5.2 swatch Configuration in BriefSince the whole point of swatch is to simplify our lives, configuring swatch itself is, well, simple. swatch is controlled by a single file, $HOME/.swatchrc by default. This file contains text patterns, in the form of regular expressions, that you want swatch to watch for. Each regular expression is followed by the action(s) you wish to swatch to take whenever it encounters that text. For example, suppose you've got an Apache-based web server and you want to be alerted any time someone attempts a buffer-overflow attack by requesting an extremely long filename (URL). By trying this yourself against the web server while tailing its /var/apache/error.log, you know that Apache will log an entry that includes the string "File name too long." Suppose further that you want to be emailed every time this happens. Example 10-28 shows what you'd need to have in your .swatchrc file. Example 10-28. Simple entry in .swatchrcwatchfor /File name too long/ mail addresses=mick\@visi.com,subject=BufferOverflow_attempt As you can see, the entry begins with a watchfor statement, followed by a regular expression. If you aren't yet proficient in the use of regular expressions, don't worry: this can be as simple as a snippet of the text you want swatch to look for, spelled out verbatim between two slashes. Swatch will perform your choice of a number of actions when it matches your regular expression. In this example, we've told swatch to send email to mick\@visi.com, with a subject of BufferOverflow_attempt. Note the backslash before the @ sign — without it, Perl will interpret the @ sign as a special character. Note also that if you want spaces in your subject-line, each space needs to be escaped with a backslash — e.g., subject=Buffer\ Overflow\ attempt. Actions besides sending email include the ones in Table 10-13.
For more details on configuring these and the other actions that swatch supports, see the swatch(1) manpage. Let's take that example a step further. Suppose in addition to being emailed about buffer-overflow attempts, you want to know whenever someone hits a certain web page, but only if you're logged on to a console at the time. In the same .swatchrc file, you'd add something like Example 10-29. Example 10-29. An event that beeps and prints to consolewatchfor /wuzza.html/ echo=red bell 2
When in doubt, add either a "mail" action or some other non console-specific action (e.g., an "exec" action that triggers a script that pages you, etc.), unless, that is, the pattern in question isn't critical. Alert readers have no doubt noticed that the scenario in the previous example will work only for Apache installations in which both errors and access messages are logged to the same file. We haven't associated different expressions with different watched files, nor can we. But what if you want swatch to watch more than one log file? This is no problem. Although each .swatchrc file may describe only one watched file, there's nothing to stop you from running multiple instances of swatch, each with its own .swatchrc file. In other words, .swatchrc is the default, but not the required name for swatch configurations. To split our two examples into two files, you'd put the lines in Example 10-27 into a file called, for example, .swatchrc.hterror and the lines in Example 10-28 into a file called .swatchrc.htaccess. 10.5.3 Advanced swatch ConfigurationSo far we've only considered actions we want triggered every time a given pattern is matched. There are several ways we can control swatch's behavior with greater granularity, however. The first and most obvious is that search patterns take the form of regular expressions. Regular expressions, which really constitute a text-formatting language of their own, are incredibly powerful and responsible for a good deal of the magic of Perl, sed, vi, and many other Unix utilities. It behooves you to know at least a couple "regex" tricks. Trick number one is called alternation, and it adds a "logical or" to your regular expression in the form of a "|" sign. Consider this regular expression: /reject|failed/ This expression will match any line containing either the word "reject" or the word "failed." Use alternation when you want swatch to take the same action for more than one pattern. Trick number two is the Perl-specific regular-expression modifier "case-insensitive," also known as "slash-i" since it always follows a regular expression's trailing slash. The regular expression: /reject/i matches any line containing the word "reject" whether it's spelled "Reject," "REJECT," "rEjEcT," etc. Granted, this isn't nearly as useful as alternation, and in the interest of full disclosure, I'm compelled to mention that slash-i is one of the more CPU-intensive Perl modifiers. However, if despite your best efforts at log tailing, self attacking, etc., you aren't 100% sure how a worrisome attack might look in a log file, slash-i helps you make a reasonable guess. Another way to control swatch more precisely is to specify what time of day a given action may be performed. You can do this by sticking a when= option after any action. For example, in Example 10-30, I have a .swatchrc entry for a medium-importance event, which I want to know about via console messages during weekdays, but which I'll need email messages to know about during the weekend. Example 10-30. Actions with when option specified/file system full/ echo=red mail addresses=mick\@visi.com,subject=Volume_Full,when=7-1:1-24 The syntax of the when= option is when=range_of_days:range_of_hours. Thus, in Example 10-30, we see that any time the message "file system full" is logged, swatch will echo the log entry to the console in red ink. It will also send email, but only if it's Saturday ("7") or Sunday ("1"). 10.5.4 Running swatchSwatch expects .swatchrc to live in the home directory of the user who invokes swatch. Swatch also keeps its temporary files there by default. (Each time it's invoked, it creates and runs a script called a "watcher process," whose name ends with a dot followed by the PID of the swatch process that created it). The -c path/to/configfile and --script-dir=/path/to/scripts flags let you specify alternate locations for swatch's configuration and script files, respectively. Never keep either in a world-writable directory, however. In fact, only these files' owners should be able to read them. For example, to invoke swatch so that it reads my custom configuration file in /var/log and also uses that directory for its watcher process script, I'd use the command listed in Example 10-31. Example 10-31. Specifying nondefault pathsmylinuxbox:~# swatch -c /var/log/.swatchrc.access --script-dir=/var/log & I also need to tell swatch which file to tail, and for that I need the -t filename flag. If I wanted to use the previous command to have swatch monitor /var/log/apache/access_log, it would look like this: mylinuxbox:~# swatch -c /var/log/.swatchrc.access --script-dir=/var/log \ -t /var/log/apache/access_log &
Again, if you want swatch to monitor multiple files, you'll need to run swatch multiple times, with at least a different tailing target (-t value) specified each time and probably a different configuration file for each as well. 10.5.5 Fine-Tuning swatchOnce swatch is configured and running, we must turn our attention to the Goldilocks Goal: we want swatch to be running neither "too hot" (alerting us about routine or trivial events) nor "too cold" (never alerting us about anything). But what constitutes "just right?" There are as many answers to this question as there are uses for Unix. Anyhow, you don't need me to tell you what constitutes nuisance-level reporting: if it happens, you'll know it. You may even experience a scare or two in responding to events that set off alarms appropriately but turn out to be harmless nonetheless. Read the manual, tweak .swatchrc, and stay the course. The other scenario, in which too little is watched for, is much harder to address, especially for the beginning system administrator. By definition, anomalous events don't happen very frequently, so how do you anticipate how they'll manifest themselves in the logs? My first bit of advice is to get in the habit of browsing your system logs often enough to get a feel for what the routine operation of your systems looks like. Better still, "tail" the logs in real time. If you enter the command tail -f /var/log/messages, the last 50 lines of the system log will be printed, plus all subsequent lines, as they're generated, until you kill tail with a Control-c. This works for any file, even a log file that changes very rapidly. Another good thing you can do is to "beat up on" (probe/attack) your system in one virtual console or xterm while tailing various log files in another. nmap and Nessus, which are covered in Chapter 3 (Hardening Linux), are perfect for this. By now you may be saying, "Hey, I thought the whole reason I installed swatch was so I wouldn't have to watch log files manually!" Wrong. Swatch minimizes, but does not eliminate, the need for us to parse log files. Were you able to quit using your arithmetic skills after you got your first pocket calculator? No. For that matter, can you use a calculator in the first place unless you already know how to add, multiply, etc.? Definitely not. The same goes for log file parsing: you can't tell swatch to look for things you can't identify yourself, no more than you can ask for directions to a town whose name you've forgotten. 10.5.6 Why You Shouldn't Configure swatch Once and Forget About ItIn the same vein, I urge you to not be complacent about swatch silence. If swatch's actions don't fire very often, it could be that your system isn't getting probed or misused very much, but it's at least as likely that swatch isn't casting its net wide enough. Continue to periodically scan through your logs manually to see if you're missing anything, and continue to tweak .swatchrc. Don't forget to periodically reconsider the auditing/logging configurations of the daemons that generate log messages in the first place. Swatch won't catch events that aren't logged at all. Refer to the syslogd(8) manpage for general instructions on managing your syslogd daemon, and the manpages of the various things that log to syslog for specific instructions on changing the way they log events. |