Zaintaowanie logcheck

Z wykorzystaniem właśnego repozytorium pakietów należy zainstalować w systemie:

pkg install -y security/logcheck textproc/gnugrep

Konfiguracja logcheck

Uprawnienia do plików

Po zainstalowaniu logcheck (od wersji logcheck-1.2.69_1) trzeba nieco "poprawić" uprawnienia w systemie plików, aby logcheck działał poprawnie:

# dla partycji UFS
#
setfacl -m u:logcheck:rwx /var/log
setfacl -m g:logcheck:r /var/log/security /var/log/auth.log

# dla partycji ZFS
#
setfacl -m u:logcheck:rwx::allow /var/log
setfacl -m g:logcheck:r::allow /var/log/security /var/log/auth.log

chgrp -R logcheck /var/log/sudo/
chown -R logcheck /usr/local/share/logcheck/detectrotate/

Patchowanie wybranych plików

Wszystkie poniższe poprawki są umieszczone w repozytorium konfiguracji.

violations.d/kernel

--- /usr/local/etc/logcheck/violations.d/kernel 2015-06-30 01:57:35.000000000 +0200
+++ /usr/local/etc/logcheck/violations.d/kernel.new     2015-07-07 05:23:31.130598196 +0200
@@ -1,2 +1,2 @@
-^\w{3} [ :0-9]{11} [._[:alnum:]-]+ kernel:( \[ *[[:digit:]]+\.[[:digit:]]+\])? [[:alnum:]]+: media error \(bad sector\): status=0x[[:xdigit:]]+ { DriveReady SeekComplete Error }$
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ kernel:( \[ *[[:digit:]]+\.[[:digit:]]+\])? [[:alnum:]]+: media error \(bad sector\): status=0x[[:xdigit:]]+ \{ DriveReady SeekComplete Error \}$
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ kernel:( \[ *[[:digit:]]+\.[[:digit:]]+\])? end_request: I/O error, dev [[:alnum:]]+, sector [[:digit:]]+

ignore.d.server/cyrus

--- cyrus.orig  2014-03-18 00:18:27.000000000 +0100
+++ cyrus       2014-07-30 00:47:50.000000000 +0200
@@ -4,3 +4,12 @@
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ cyrus/lmtpd\[[0-9]+\]: DBERROR db3: [12] lockers$
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ cyrus/notifyd\[[0-9]+\]: MAIL, , [^[:space:]]+, [^[:space:]]+,  \"[ [:alnum:][:punct:]]+\"$
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ cyrus/squatter\[[0-9]+\]: (skipping|indexing) mailbox [[:alpha:]^\.]+\.\.\.$
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ ctl_cyrusdb\[[0-9]+\]: (done )?checkpointing cyrus databases
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ pop3s?\[[0-9]+\]: counts: retr
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ pop3s?\[[0-9]+\]: starttls: TLSv1 with cipher
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ pop3s?\[[0-9]+\]: imapd:Loading hard-coded DH parameters
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (pop3s?|lmtpunix|imap)\[[0-9]+\]: USAGE [^[:space:]]+ user:
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ pop3s?\[[0-9]+\]: Expunged [0-9]+ messages from user
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (pop3s?|imap)\[[0-9]+\]: .* User logged in SESSIONID=
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ sieve\[[0-9]+\]: .* User logged in
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (pop3s?|imap)\[[0-9]+\]: .* starttls:
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (lmtpunix|pop3|imap)s?\[[0-9]+\]: (Expunged|USAGE)
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (lmtpunix|pop3|imap)s?\[[0-9]+\]: imapd:Loading hard-coded DH parameters
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ ctl_cyrusdb\[[0-9]+\]: checkpointing cyrus databases
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ ctl_cyrusdb\[[0-9]+\]: done checkpointing cyrus databases
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (pop3|imap)lo0s?\[[0-9]+\]: (login|counts|starttls):
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ (lmtpunix|pop3lo0|imaplo0)s?\[[0-9]+\]: (Expunged|USAGE)

ignore.d.server/kernel

--- /usr/local/etc/logcheck/ignore.d.server/kernel      2015-07-10 15:04:00.081964266 +0200
+++ /usr/local/etc/logcheck/ignore.d.server/kernel.new  2015-07-10 15:03:53.648964998 +0200
@@ -79,3 +79,4 @@
 ^\w{3} [ :[:digit:]]{11} [._[:alnum:]-]+ kernel: imklog [.[:digit:]]+, log source = /proc/kmsg started\.$
 ^\w{3} [ :[:digit:]]{11} [._[:alnum:]-]+ kernel: Kernel logging \(proc\) stopped\.$
 ^\w{3} [ :[:digit:]]{11} [._[:alnum:]-]+ kernel:( \[ *[[:digit:]]+\.[[:digit:]]+\])? kvm: emulating exchange as write$
+^\w{3} [ :[:digit:]]{11} [._[:alnum:]-]+ kernel:( \[ *[[:digit:]]+\.[[:digit:]]+\])? .*NetScreen device_id=

ignore.d.server/stunnel

--- /usr/local/etc/logcheck/ignore.d.server/stunnel.orig        2015-06-30 01:57:35.000000000 +0200
+++ /usr/local/etc/logcheck/ignore.d.server/stunnel     2015-07-07 02:17:53.273364642 +0200
@@ -5,3 +5,4 @@
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ stunnel(\[[0-9]+\])?: stunnel [0-9.]+ on i386-pc-linux-gnu PTHREAD\+POLL\+IPv6\+LIBWRAP with OpenSSL [0-9a-z.]+ [0-9]{2} \w{3} [0-9]{4}$
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ stunnel(\[[0-9]+\])?: [0-9]+ clients allowed$
 ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ stunnel(\[[0-9]+\])?: SSL_accept: Peer suddenly disconnected$
+^\w{3} [ :0-9]{11} [._[:alnum:]-]+ stunnel(\[[0-9]+\])?: LOG[35]

Poprawki dla logcheck z wykorzystaniem gnugrep

System FreeBSD posiada własną wersję grep (tzw. bsdgrep) niekopatybilne z gnugrep. Dlatego też należy poprawić skrypt /usr/local/sbin/logcheck:

--- /usr/local/sbin/logcheck.orig       2017-07-30 22:28:16.000000000 +0200
+++ /usr/local/sbin/logcheck    2017-08-07 22:58:38.212723000 +0200
@@ -211,7 +211,7 @@ cleanrules() {
            if [ -f "${dir}/${rulefile}" ]; then
                debug "cleanrules: ${dir}/${rulefile}"
                # pipe to cat on greps to get usable exit status
-               egrep --text -v '^[[:space:]]*$|^#' "$dir/$rulefile" | cat \
+               /usr/local/bin/egrep --text -v '^[[:space:]]*$|^#' "$dir/$rulefile" | cat \
                        >> "$cleaned/$rulefile" \
                || error "Couldn't append to $cleaned/$rulefile."
            fi
@@ -231,7 +231,7 @@ report() {
        if [ "$SYSLOGSUMMARY" -eq 1 ] && [ -x "$SYSLOG_SUMMARY" ]; then
            debug "report: running syslog-summary - $*"
            $SYSLOG_SUMMARY "$TMPDIR/checked" | \
-               egrep -v "^Summarizing " | cat >> "$TMPDIR/report" \
+               /usr/local/bin/egrep -v "^Summarizing " | cat >> "$TMPDIR/report" \
                    || error "Could not append to report."
        else
            if [ "$SYSLOGSUMMARY" -eq 1 ] && [ ! -x "$SYSLOG_SUMMARY" ]; then
@@ -313,7 +313,7 @@ greplogoutput() {
        debug "greplogoutput: $grepfile"

        # Raise entries that match
-       egrep --text -f "$raise/$grepfile" "$TMPDIR/logoutput-sorted" | cat \
+       /usr/local/bin/egrep --text -f "$raise/$grepfile" "$TMPDIR/logoutput-sorted" | cat \
            > "$TMPDIR/checked" \
            || error "Could not output to $TMPDIR/checked."

@@ -384,13 +384,13 @@ greplogoutput() {
     return $RETURN
 }

-# Process a logfile snippet with egrep.
+# Process a logfile snippet with /usr/local/bin/egrep.
 cleanchecked() {
     clean="$1"

     if [ -f "$clean" ]; then
        debug "cleanchecked - file: $clean"
-        egrep --text -v -f "$clean" "$TMPDIR/checked" | cat >> "$TMPDIR/checked.1"  \
+        /usr/local/bin/egrep --text -v -f "$clean" "$TMPDIR/checked" | cat >> "$TMPDIR/checked.1"  \
            || error "Could not output to $TMPDIR/checked.1."
        mv "$TMPDIR/checked.1" "$TMPDIR/checked" \
            || error "Could not move $TMPDIR/checked.1 to $TMPDIR/checked"
@@ -398,7 +398,7 @@ cleanchecked() {
        debug "cleanchecked - dir - $clean"
        for file in $(ls -1 "$clean/"); do
        debug "cleanchecked - dir - $clean/$file"
-           egrep --text -v -f "$clean/$file" "$TMPDIR/checked" | cat \
+           /usr/local/bin/egrep --text -v -f "$clean/$file" "$TMPDIR/checked" | cat \
                >> "$TMPDIR/checked.1" \
                    || error "Could not output to TMPDIR/checked.1."
            mv "$TMPDIR/checked.1" "$TMPDIR/checked" \
@@ -653,7 +653,7 @@ if [ ! "$LOGFILE" ] && [ -n "$LOGFILES"
     SAVEIFS=$IFS; IFS=$(echo -en "\n\b");
     for file_list in ${LOGFILES} ; do
         if [ -f  "$file_list" ] ; then
-            for file in $(egrep --text -v -h "(^#|^[[:space:]]*$)" "$file_list"); do
+            for file in $(/usr/local/bin/egrep --text -v -h "(^#|^[[:space:]]*$)" "$file_list"); do
                 logoutput "$file"
             done
         else

Zbiorcze nakładanie poprawek - po (re)instalacji

W celu uproszczenia nakładania poprawek, można wykonać następujące polecenia:

# nałożenie poprawek
for p in `ls  /home/wtp/scripts/patches/*logcheck*`; do patch -p0 < $p; done

# znalezienie plików oryginalnych - nie są już potrzebne
find /usr/local/etc/logcheck/ -type f -name '*.orig' -delete

# jeżeli pliki mają niepoprawne upgrawnienia (grupę), to modyfikujemy
find /usr/local/etc/logcheck/ -type f -group wheel -exec chgrp logcheck {} \;

# usunięcie pliku sbin/logcheck.orig
rm /usr/local/sbin/logcheck.orig

Sprawdzenie poprawności działania

Po wykonaniu całości konfiguracji jest możliwość sprawdzenia poprawności konfiguracji:

# wersja głośna
su -l logcheck -c "logcheck -dot"

# wersja cicha
su -l logcheck -c "logcheck -ot"

Dodanie zadań w crontab

Z wykorzystaniem własnego repozytorium konfiguracji należy zainstalować zadań crond dla użytkownika logcheck:

# --- definicje zmiennych lokalnych ---------------------------------------
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
MAILTO=root

# --- uruchamianie zada/n -------------------------------------------------
#
@reboot    if [ -x /usr/local/sbin/logcheck ]; then nice -n10 /usr/local/sbin/logcheck -R; fi
2 07,14,20 * * *  if [ -x /usr/local/sbin/logcheck ]; then nice -n10 /usr/local/sbin/logcheck; fi

Aktualizacja sum kontrolnych dla pkg

Po nałożeniu poprawek warto wykonać ponowne sprawdzenie sum kontrolnych przez pkg - ominięcie ostrzeżeń w codziennej poczcie administracyjnej:

pkg check -r logcheck


{i}  (c) 2004 - 2026 BSD Serwis Krzysztof Stryjek. W przypadku błędów, poprawek proszę kontaktować się z administratorem wiki.