{"id":462,"date":"2013-09-20T01:45:23","date_gmt":"2013-09-20T01:45:23","guid":{"rendered":"https:\/\/shan.info\/new\/log-iptables-messages-to-a-separate-file-with-rsyslog\/"},"modified":"2013-09-20T01:45:23","modified_gmt":"2013-09-20T01:45:23","slug":"log-iptables-messages-to-a-separate-file-with-rsyslog","status":"publish","type":"post","link":"https:\/\/shan.info\/new\/log-iptables-messages-to-a-separate-file-with-rsyslog\/","title":{"rendered":"Log iptables Messages to a Separate File with rsyslog"},"content":{"rendered":"<div class=\"postinfo\" style=\"margin: 0px; padding: 0px; border-bottom-width: 0px; color: #333333; font-size: 13px; line-height: 19px;\">\n<h1 style=\"margin-bottom: 1em; font-size: 18px;\">Log iptables Messages to a Separate File with rsyslog<\/h1>\n<\/div>\n<div class=\"entry\" style=\"margin: 0px; padding: 0px; color: #333333; font-size: 13px; line-height: 19px;\">\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">Firewall logging is very important, both to detect break-in attempts and to ensure that firewall rules are working properly. Unfortunately, it\u2019s often difficult to predict in advance which rules and what information should be logged. Consequently, it\u2019s common practice to err on the side of verbosity. Given the amount of traffic that any machine connected to the Internet is exposed to, it\u2019s critical that firewall logs be separated from normal logs in order to ease monitoring. What follows are two methods to accomplish this using iptables on Linux. The first method uses traditional syslog facility\/priority filtering. The second, more robust method filters based on message content with rsyslog.<\/p>\n<h2 style=\"margin-top: 1em; margin-bottom: 1em; font-size: 16px;\">The Old Way: Use a Fixed Priority for iptables<\/h2>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">The traditional UNIX syslog service only has two ways to categorize, and consequently route, messages: facility and priority. Facilities include kernel, mail, daemon, etc. Priorities include emergency, alert, warning, debug, etc. The Linux&nbsp;<a style=\"color: #5d81ae;\" href=\"http:\/\/www.netfilter.org\/\">iptables<\/a>&nbsp;firewall runs in the kernel and therefore always has the facility set to kern. Using traditional syslog software, the only way you can separate iptables messages from other kernel messages is to set the priority on all iptables messages to something specific that hopefully isn\u2019t used for other kernel logging.<\/p>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">For example, you could add something like the following to&nbsp;<code class=\"path\" style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666;\">\/etc\/syslog.conf<\/code>:<\/p>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">kern.=debug -\/var\/log\/iptables.log<\/pre>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">and specifically remove the kernel debugging messages from all other logs like so:<\/p>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">kern.*;kern.!=debug -\/var\/log\/kern.log<\/pre>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">and in each iptables logging rule use the command line option&nbsp;<code style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666; background-color: #f9f4bf;\">--log-level debug<\/code>.<\/p>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">There are two distinct disadvantages to this approach. First, there\u2019s no guarantee that other kernel components won\u2019t use the priority you\u2019ve set iptables to log at. There\u2019s a real possibility that useful messages will be lost in the deluge of firewall logging. Second, this approach prevents you from actually setting meaningful priorities in your firewall logs. You might not care about random machines hammering Windows networking ports, but you definitely want to know about malformed packets reaching your server.<\/p>\n<h2 style=\"margin-top: 1em; margin-bottom: 1em; font-size: 16px;\">The New Way: Filter Based on Message Content with rsyslog<\/h2>\n<ol style=\"list-style-type: lower-alpha;\">\n<li><a style=\"color: #5d81ae;\" href=\"http:\/\/www.rsyslog.com\/\">rsyslog<\/a>&nbsp;is mostly a drop-in replacement for a tradtional syslog daemon\u2013on Linux, klogd and sysklogd. In fact, on Debian and Ubuntu, you can simply:<\/li>\n<\/ol>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\"><code style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666; background-color: #f9f4bf;\">$ sudo apt-get install rsyslog<\/code><\/p>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">and if you haven\u2019t customized&nbsp;<code class=\"path\" style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666;\">\/etc\/syslog.conf<\/code>, logging should continue to work in precisely the same way. rsyslog has been the default syslog on Red Hat\/Fedora based systems for a number of versions now, but if it\u2019s not installed:<\/p>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\"><code style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666; background-color: #f9f4bf;\">$ sudo yum install rsyslog<\/code><\/p>\n<h3 style=\"margin-top: 1em; margin-bottom: 1em; font-size: 14px;\">Configure iptables to Use a Unique Prefix<\/h3>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">We\u2019ll setup rsyslog to filter based on the beginning of a message from iptables. So, for each logging rule in your firewall script, add&nbsp;<code style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666; background-color: #f9f4bf;\">--log-prefix \"iptables: \"<\/code>. Most firewall builder applications can be easily configured to add a prefix to every logging rule. For example, if you\u2019re using&nbsp;<a style=\"color: #5d81ae;\" href=\"http:\/\/firehol.sourceforge.net\/\">firehol<\/a>&nbsp;as I am, you could add:<\/p>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">FIREHOL_LOG_PREFIX=\"firehol: \"<\/pre>\n<ol style=\"list-style-type: upper-alpha;\">\n<li><code class=\"path\" style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666;\">\/etc\/firehol\/firehol.conf<\/code>.<\/li>\n<li><code class=\"path\" style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666;\">\/etc\/rsyslog.d\/iptables.conf<\/code>&nbsp;with the following contents:<\/li>\n<\/ol>\n<h3 style=\"margin-top: 1em; margin-bottom: 1em; font-size: 14px;\">Configure rsyslog to Filter Based on Prefix<\/h3>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">:msg, startswith, \"iptables: \" -\/var\/log\/iptables.log\r\n&amp; ~<\/pre>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">The first line means send all messages that start with \u201ciptables: \u201d to&nbsp;<code class=\"path\" style=\"margin: 0px; padding: 0px; font-style: inherit; line-height: 13px; color: #666666;\">\/var\/log\/iptables.log<\/code>. The second line means discard the messages that were matched in the previous line. The second line is of course optional, but it saves the trouble of explicitly filtering out firewall logs from subsequent syslog rules.<\/p>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">When I configured this on my own machines, I did notice one issue that may be a peculiarity of firehol, but it\u2019s probably worth mentioning anyway. It seems that firehol adds an extra single quote at the beginning of log messages that needs to be matched in the rsyslog rule. For example, here\u2019s a log message from firehol:<\/p>\n<pre class=\"wrap\" style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto; white-space: pre-wrap;\">Apr 17 12:41:07 tick kernel: 'firehol: 'IN-internet':'IN=eth0 OUT= MAC=fe:fd:cf:c0:47:b5:00:0e:39:6f:48:00:08:00 SRC=189.137.225.191 DST=207.192.75.74 LEN=64 TOS=0x00 PREC=0x00 TTL=32 ID=5671 DF PROTO=TCP SPT=3549 DPT=5555 WINDOW=65535 RES=0x00 SYN URGP=0<\/pre>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">Notice the extra quote after \u201ckernel: \u201d and before \u201cfirehol: \u201c. So, on my machine I configured the rsyslog filter like so:<\/p>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">:msg, startswith, \"'firehol: \" -\/var\/log\/iptables.log\r\n&amp; ~<\/pre>\n<h3 style=\"margin-top: 1em; margin-bottom: 1em; font-size: 14px;\">Configure iptables Log Rotation<\/h3>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">Finally, since we\u2019re logging to a new file, it\u2019s useful to create a log rotation rule. Create a file \/etc\/logrotate.d\/iptables with the following contents:<\/p>\n<pre style=\"margin: 0px 0px 1em; padding: 5px; font-family: monospace; line-height: 13px; background-color: #eeeeee; border: 1px solid #999999; overflow: auto;\">\/var\/log\/iptables.log\r\n{\r\n\trotate 7\r\n\tdaily\r\n\tmissingok\r\n\tnotifempty\r\n\tdelaycompress\r\n\tcompress\r\n\tpostrotate\r\n\t\tinvoke-rc.d rsyslog reload &gt; \/dev\/null\r\n\tendscript\r\n}<\/pre>\n<p style=\"margin-top: 0px; margin-bottom: 1em; padding: 0px;\">The preceding script tells logrotate to rotate the firewall log daily and keep logs from the past seven days.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Log iptables Messages to a Separate File with rsyslog Firewall logging is very important, both to detect break-in attempts and to ensure that firewall rules are working properly. Unfortunately, it\u2019s often difficult to predict in advance which rules and what information should be logged. Consequently, it\u2019s common practice to err on the side of verbosity. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-centos-6"],"_links":{"self":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/comments?post=462"}],"version-history":[{"count":0,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"wp:attachment":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}