使用 rsyslog 单独保存 iptables log 日志实践

2019-05-14 16:53:00 +08:00
 wsgzao

前言

iptables 作为经典的软件防火墙大家已经很熟悉了,不过各位应该比较少会使用到 log 日志记录保存的功能。这次因为 Ngnix stream 模块的编译和获取 realip(ngx_http_realip_module / ngx_stream_realip_module)的方案改动成本过高,退而求其次的方式是通过 iptables 做转发,需要解决的问题就是如何保存日志和按时间 rotate。原本计划使用 Filebeat 直接接入 EFK 但因为某些原因暂时搁浅了,最后选择比较简单的 rsyslog 在本地服务器上做处理。

使用 rsyslog 单独保存 iptables log 日志实践

更新历史

2019 年 05 月 09 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/iptables-log/

扩展阅读

rsyslog - https://www.rsyslog.com/guides/

How to Enable Logging in Iptables on Linux - https://tecadmin.net/enable-logging-in-iptables-on-linux/


RedHat 官方教程

How to configure syslog to log the iptables messages to a different log file in Red Hat Enterprise Linux 5/6/7

Environment

Red Hat Enterprise Linux 5

Red Hat Enterprise Linux 6

Red Hat Enterprise Linux 7

syslog

Issue

Resolution

# Make a backup of /etc/syslog.conf before making any changes to it.
cp /etc/syslog.conf /etc/syslog.conf.bak

# Edit /etc/syslog.conf with an editor such as vi and add lines:
# comment iptables log
kern.warning                    /var/log/iptables

# Make sure the iptables rule is logging at the appropriate level. This can be done by using the log-level switch. Default log-level is warning.
# Below example will log ssh attempts:
iptables -I INPUT -p tcp --dport 22 -j LOG --log-level 4

# Note: Log Levels can be found using command:
man syslog

# Note: Consider adding a prefix to your iptables rule. This makes it easier to separate the firewall message from the few random messages that the kernel puts out. 
# Below example use to log ping and add the prefix "#### Firewall ####".
iptables -I INPUT -p icmp --icmp-type ping -j LOG --log-prefix "#### Firewall ####"

# Note:- Follow below steps if iptables print all the logs on the console:-
# Step1:- Add below entry in /etc/sysctl.conf
kernel.printk = 4 1 1 7
# Step2:- Run below command to make changes effectively at runtime.
/sbin/sysctl -p /etc/sysctl.conf
# Step3:- Check the changes at below file.
cat /proc/sys/kernel/printk

个人实践过程

iptables 防火墙日志

# 修改防火墙 NAT 表中的 PREROUTING 和 POSTROUTING 链,添加自定义 log-prefix
vim /etc/sysconfig/iptables

*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -p tcp -d <IP> --dport 443 -j LOG --log-prefix seatalk:
-A PREROUTING -p tcp -d <IP> --dport 443 -j DNAT --to-destination 10.71.19.142:443
-A POSTROUTING -j MASQUERADE
COMMIT

# 重启 iptables
service iptables reload

配置 rsyslog 读取和保存 iptables 日志

rsyslog 是一个 syslogd 的多线程增强版。现在 Fedora / RHEL / CentOS / Ubuntu 默认的日志系统都是 rsyslog 了。

rsyslog 负责写入日志,logrotate 负责备份和删除旧日志,以及更新日志文件

# 创建 iptables 日志目录
mkdir -p /var/log/iptables/

# 编辑 rsyslog.conf
vim /etc/rsyslog.conf
# Save iptables log
kern.warning /var/log/iptables/iptables.log

# 重启 rsyslog
service rsyslog restart

配置 log rotate

rotate 轮换,日志切换

logrotate 是一个日志管理程序,用来把旧的日志文件删除(备份),并创建新的日志文件,这个过程称为 "转储"。我们可以根据日志的大小,或者根据其使用的天数来转储。

# 添加 iptables log rotate 策略
vim /etc/logrotate.d/iptables

/var/log/iptables/iptables.log {
        daily
        rotate 7
        compress
        delaycompress
        missingok
        notifempty
        create 0664 root root
}

# 重启 rsyslog
service rsyslog restart

# 这篇文章有更多实例
rsyslog 和 logrotate 服务 - http://xstarcd.github.io/wiki/Linux/rsyslog_logrotate.html

检查日志输出

如果条件允许建议直接采用 EFK 一步到位

cd /var/log/iptables
iptables.log
iptables.log-20190512.gz
iptables.log-20190513

cat iptables.log

May 14 15:08:35 <localhost> kernel: IN=em1 OUT= MAC=14:18:77:28:56:59:a0:f8:49:5f:b2:c3:08:00 SRC=<IP> DST=<IP> LEN=60 TOS=0x00 PREC=0x00 TTL=57 ID=43701 DF PROTO=TCP SPT=4150 DPT=443 WINDOW=65535 RES=0x00 SYN URGP=0
May 14 15:09:00 <localhost> kernel: IN=em1 OUT= MAC=14:18:77:28:56:59:00:f8:2c:91:79:43:08:00 SRC=<IP> DST=<IP> LEN=60 TOS=0x00 PREC=0x00 TTL=54 ID=31497 DF PROTO=TCP SPT=43586 DPT=443 WINDOW=65535 RES=0x00 SYN URGP=0

1164 次点击
所在节点    程序员
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/564072

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX