如何配置 syslog.conf 檔案以將 iptables 訊息記錄在單獨的檔案中?

如何配置 syslog.conf 檔案以將 iptables 訊息記錄在單獨的檔案中?

如何設定/etc/syslog.conf檔以便將日誌資訊保存iptables在特定文件中。

我想單獨保存這些信息,這樣我就可以輕鬆快速地提取我想要的內容。

答案1

系統日誌

查看 的手冊頁iptables。它顯示了一個名為的目標,LOG它可以做你想做的事情。

例子

  1. 將日誌記錄等級設定為LOG4。

    # DROP everything and Log it
    iptables -A INPUT -j LOG --log-level 4
    iptables -A INPUT -j DROP
    
  2. 配置syslog.conf將這些訊息寫入單獨的檔案。

    # /etc/syslog.conf
    kern.warning     /var/log/iptables.log
    
  3. 重新啟動 syslogd。

    Debian/Ubuntu

    $ sudo /etc/init.d/sysklogd restart
    

    Fedora/CentOS/RHEL

    $ sudo /etc/init.d/syslog restart
    

筆記:這種記錄方法稱為固定優先權。它們可以是數字或名稱 (1,2,3,4,..) 或 (DEBUG, WARN, INFO, 等等)。

系統日誌

如果您偶然使用rsyslog,您可以建立一個基於屬性的篩選器,如下所示:

# /etc/rsyslog.conf
:msg, contains, "NETFILTER"       /var/log/iptables.log
:msg, contains, "NETFILTER"     ~

然後將這個開關加入您要錄製的 iptables 規則中:

–log-prefix NETFILTER

作為替代方案,您也可以使用此類屬性篩選器記錄訊息:

:msg, startswith, "iptables: " -/var/log/iptables.log
& ~
:msg, regex, "^\[ *[0-9]*\.[0-9]*\] iptables: " -/var/log/iptables.log
& ~

筆記:第二種方法不需要對iptables.

參考

答案2

這假設您的防火牆已經產生日誌,就像任何正常的防火牆一樣。對於某些範例,它需要可識別的訊息,例如 slm 範例中的「NETFILTER」。

在 rsyslog.d 中建立一個文件

vim /etc/rsyslog.d/10-firewall.conf

這在 CentOS 7 中有效。除非下一個版本不起作用,否則不要使用它。

# into separate file and stop their further processing
if  ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    & ~
}

這適用於 CentOS 7 並檢查訊息內容(將「Shorewall」替換為 -j LOG 規則訊息中的任何內容):

# into separate file and stop their further processing
if  ($msg contains 'Shorewall') and \
    ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    & ~
}

這適用於其他系統(Ubuntu、Debian、openSUSE)。這是最好的方法。不搜尋訊息中的字串:

# into separate file and stop their further processing
if  ($syslogfacility-text == 'kern') and \\
($msg contains 'IN=' and $msg contains 'OUT=') \\
then    -/var/log/firewall
    &   ~

這是預設的 openSUSE 機器所具有的(我認為每個發行版都應該具有但缺少)(差異似乎只是「stop」而不是「& ~」;並非所有系統都支援這兩種語法):

if  ($syslogfacility-text == 'kern') and \
    ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    stop
}

對於上述所有內容,也不要忘記 logrotate.d 檔案:

vim /etc/logrotate.d/firewall

包含:

/var/log/firewall {
    rotate 7
    size 500k
    postrotate
        # before using this, run the command yourself to make sure 
        # it is right... the daemon name may vary
        /usr/bin/killall -HUP rsyslogd
    endscript
}

相關內容