http://search.cpan.org/CPAN/authors/id/S/ST/STBEY/Date-Calc-5.3.tar.gz然后,
http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/TimeDate-1.1301.tar.gz
http://search.cpan.org/CPAN/authors/id/M/MG/MGRABNAR/File-Tail-0.98.tar.gz
http://search.cpan.org/CPAN/authors/id/J/JH/JHI/Time-HiRes-1.37.tar.gz
$ tar zxvf $SOURCE/Date-calc-5.3.tar.gz
$ cd $SOURCE/Date-Calc-5.2
$ perl Makefile.PL
$ make
$ make test
$ su
# make install
# Swatch configuration file for constant monitoring ignore /news/ # Server problems -- we consider this to be relatively low-priority, so we just get email. watchfor /restart|panic|halt/ bell [email protected],subject=Log_Data_Server_Info # SSH stuff -- also low-priority, but we still want to know when users are ssh'd in. watchfor /SSH|sshd2/ [email protected],subject=Log_Data_SSH # We want to know if someone tries to SU root -- does everything (emails multiple people, # emails cell phone, pages Admin with pager code 911) watchfor /root/ bell [email protected]:[email protected]:5551212@cell_messaging.com,subject=Log_Data_ROOT throttle 0:2:0,use=regex # so it doesn't confuse the modem and interrupt paging exec chat -f /etc/chatscript.txt -v -s -T 911 < /dev/modem > /dev/modem # router problems - does everything (emails multiple people, emails cell phone, pages Admin # with pager code 444) watchfor /router/ bell [email protected]:[email protected]:5551212@cell_messaging.com,subject=Log_Data_Router throttle 0:2:0,use=regex exec chat -f /etc/chatscript.txt -v -s -T 444 < /dev/modem > /dev/modem # If adduser is run, could be critical -- does everything (emails multiple people, emails cell # phone, pages Admin with pager code 666) watchfor /addgrp|adduser/ bell [email protected]:[email protected]:5551212@cell_messaging.com,subject=Log_Data_Adduser throttle 0:2:0,use=regex exec chat -f /etc/chatscript.txt -v -s -T 666 < /dev/modem > /dev/modem # Bad login attempts -- mails us and pages Admin with pager code 888 watchfor /authentication failure/ bell [email protected]:[email protected]:5551212@cell_messaging.com,subject=Log_Data_Bad_Login throttle 0:2:0,use=regex exec chat -f /etc/chatscript.txt -v -s -T 888 < /dev/modem > /dev/modem # end of swatch.conf3.2 Swatch可以设置成开机后自动后台运行,只需要在rc.local加入一行:
# Sample Swatch configuration file for constant monitoring. ignore /news/ watchfor /restart|panic|halt/ bell [email protected]:[email protected],subject=Log_Data_Crash watchfor /SSH|sshd2/ # watch for both the session and the daemon echo bell throttle 0:2:0,use=regex # in case of multiple attempts in rapid succession; # ssh also generates multiple log lines even for a # single successful login [email protected],subject=Log_Data_SSH watchfor /blah|blah/ etc... # End of script (a more detailed sample config file is included at the end of this document)
3.6另一个conf例子
The main configuration file is /etc/swatch/swatch.conf. Here is a good example:
File: /etc/swatch/swatch.conf |
# Global swatch filter file # To ignore a IP-range ignore /216\.239\.37\./ # Invalid SSH Login Attempts watchfor /(: [iI]nvalid [uU]ser )(.*)( from )(.*)$/ throttle threshold=3,delay=0:1:0,key=$4 mail addresses=admin\@domain.com,subject="SSH:\ Invalid\ User\ Access-IPTables\ Rule\ Added" exec "/sbin/iptables -A swatch_rejects -s $4 -j DROP" # Failed SSH Login Attempts watchfor /(: [fF]ailed password for )(.*)( from )(.*)( port )(.*)$/ throttle threshold=3,delay=0:1:0,key=$4 mail addresses=admin\@domain.com,subject="SSH:\ Invalid\ User\ Access-IPTables\ Rule\ Added" exec "/sbin/iptables -A swatch_rejects -s $4 -j DROP" # Invalid SSH Login Attempts. Another one - just formed differently watchfor /([aA]uthentication [fF]ailure for [iI]llegal [uU]ser )(.*)( from )(.*)$/ throttle threshold=3,delay=0:1:0,key=$4 mail addresses=admin\@domain.com,subject="SSH:\ Invalid\ User\ Access-IPTables\ Rule\ Added" exec "/sbin/iptables -A swatch_rejects -s $4 -j DROP" |
A little explanation of whats being done:
ignore /216\.239\.37\./This is to ignore, in this case, a IP-range. Very usefull to minimize the possibility that you lock yourself out.
watchfor /(: [iI]nvalid [uU]ser )(.*)( from )(.*)$/ watchfor /(: [fF]ailed password for )(.*)( from )(.*)( port )(.*)$/ watchfor /([aA]uthentication [fF]ailure for [iI]llegal [uU]ser )(.*)( from )(.*)$/This is to search our logs for the string between //. The parens in the first watchfor are important - they break up the log file line into chucks that are used for $1,$2,$3, ... $n. In this case, for example, $1 is ": Invalid User "; $2 is all the junk in the first (.*); $3 is " from "; and $4 is all the junk in the second (.*) -- which happens to be the IP address you want. Note: the $ at the end signifies end of line. Also, note that the $4 works in both the first and third watchfor code block -- this is pure coincidence and you may need to change the $4 to a different paren set if you are working with your own custom watchfor block.
throttle threshold=3,delay=0:1:0,key=$4The "key" tell swatch how to identify the log line. We can't use the whole string here, because the same attacker (ie the same IP) will probably try multiple user names. The key can refer to one of the parens set in the search string (here, $4 = the IP).
The "threshold" is the number of times swatch need to see the "key" to execute the actions below.
The "delay" is the validity of each "key". When older that 'delay', the "key" is discarded. Use a syntax like HH:MM:SS.
Note: If this does work in 3.1.1, it may not work with loggers such as metalog, because it says instead "Last output repeated N times" So naturally swatch won't find multiple occurances often in those cases.
mail addresses=admin\@domain.com,subject="SSH:\ Invalid\ User\ Access-IPTables\ Rule\ Added"Mail a user stating that a new rule has been added to iptables.
exec "/sbin/iptables -A swatch_rejects -s $4 -j DROP"Add the offending ip to "swatch_rejects" and drop all future incoming packets from that address. If you are using shorewall, you can define in this way:
exec "/sbin/shorewall drop $4"3.7
Another way to control swatch to a greater degree 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, below I've got a .swatchrc entry for a medium-importance event I want to know about via console messages during weekdays, but I'll need e-mail messages to know about it during the weekend. To do this I set the when option:
/file system full/ echo=red mail addresses=mick\@visi.com, subject=Volume_Full,when=7-1:1-24The syntax of the when= option is when=range_of_days:range_of_hours. Thus, 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 e-mail, but only if it's Saturday (“7”) or Sunday (“1”).