iptables之LOG目标

iptables 防火墙与日志系统配合使用的一个例子

1)、# iptables –A INPUT –p tcp --dport 22 –j LOG --log-level 5 --log-prefix ”IPTABLES:”

--log-level :日志级别为5, --log-prefix的前缀信息为“IPTABLES:” 

对于为什么日志级别为5,可能参看#man syslog的8种级别,这里面的日志级别要与iptables的--log-level 的日志级别要对应.

2)、在这里还要编辑/etc/syslog.conf配置文件

增加:kern.=notice                       /var/log/firewall.log  选项(kern表示内核信息; .=表示匹配; notice为消息 )

[root@localhost root]# iptables -A INPUT -p tcp -d 192.168.0.253 --dport 22 -j LOG --log-level 5 --log-prefix "IPTABLES:"
[root@localhost root]# vi /etc/syslog.conf  
[root@localhost root]# cat /etc/syslog.conf |grep notice
kern.=notice                                            /var/log/firewall.log  

[root@localhost root]# man syslog |grep '<*>'   
       #include 
       #include 
       #include 
       DEFAULT_MESSAGE_LOGLEVEL  - 1 (6) unless the line starts with where
       ventional  meaning  of  the  loglevel is defined in as
       #define KERN_EMERG    "<0>"  /* system is unusable               */
       #define KERN_ALERT    "<1>"  /* action must be taken immediately */
       #define KERN_CRIT     "<2>"  /* critical conditions              */
       #define KERN_ERR      "<3>"  /* error conditions                 */
       #define KERN_WARNING  "<4>"  /* warning conditions               */
       #define KERN_NOTICE   "<5>"  /* normal but significant condition */
       #define KERN_INFO     "<6>"  /* informational                    */
       #define KERN_DEBUG    "<7>"  /* debug-level messages             */
[root@localhost root]# service syslog restart   
Shutting down kernel logger:                               [  OK  ]
Shutting down system logger:                             [  OK  ]
Starting system logger:                                    [  OK  ]
Starting kernel logger:                                     [  OK  ]
[root@localhost root]# iptables -A INPUT -p tcp -d 192.168.0.253 --dport 22 -j ACCEPT  
[root@localhost root]# iptables -L -n --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  0.0.0.0/0            192.168.0.253      tcp dpt:22
2    ACCEPT     all  --  127.0.0.1            127.0.0.1
3    ACCEPT     udp  --  0.0.0.0/0            192.168.0.253      udp spt:53 state ESTABLISHED
4    LOG        tcp  --  0.0.0.0/0            192.168.0.253      tcp dpt:22 LOG flags 0 level 5 prefix `IPTABLES:'
5    ACCEPT     tcp  --  0.0.0.0/0            192.168.0.253      tcp dpt:22
  

Chain FORWARD (policy DROP)
num  target     prot opt source               destination

Chain OUTPUT (policy DROP)
num  target      prot  opt   source                destination
1    ACCEPT     tcp   --  192.168.0.253        0.0.0.0/0          tcp spt:22 state ESTABLISHED
2    ACCEPT      all   --  127.0.0.1               127.0.0.1
3    ACCEPT     udp  --  192.168.0.253        0.0.0.0/0          udp dpt:53
[root@localhost root]# iptables -D INPUT 1   <22 INPUTstrong>
[root@localhost root]# iptables -L -n --line-numbers   重新查看iptables记录及各条记录的顺序关系~!
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  127.0.0.1            127.0.0.1
2    ACCEPT     udp  --  0.0.0.0/0            192.168.0.253      udp spt:53 state ESTABLISHED
3    LOG        tcp  --  0.0.0.0/0            192.168.0.253      tcp dpt:22 LOG flags 0 level 5 prefix `IPTABLES:'
4    ACCEPT     tcp  --  0.0.0.0/0            192.168.0.253      tcp dpt:22

Chain FORWARD (policy DROP)
num  target     prot opt source               destination

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  192.168.0.253        0.0.0.0/0          tcp spt:22 state ESTABLISHED
2    ACCEPT     all  --  127.0.0.1            127.0.0.1
3    ACCEPT     udp  --  192.168.0.253        0.0.0.0/0          udp dpt:53
[root@localhost root]# tail  /var/log/firewall.log   
Aug  6 02:44:14 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=128 TOS=0x00 PREC=0x00 TTL=128 ID=18358 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=65483 RES=0x00 ACK PSH URGP=0
Aug  6 02:44:14 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18363 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=65399 RES=0x00 ACK URGP=0
Aug  6 02:44:15 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=128 TOS=0x00 PREC=0x00 TTL=128 ID=18368 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=65399 RES=0x00 ACK PSH URGP=0
Aug  6 02:44:15 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18369 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=65535 RES=0x00 ACK URGP=0
Aug  6 02:44:15 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18370 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64395 RES=0x00 ACK URGP=0
Aug  6 02:44:15 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18374 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64327 RES=0x00 ACK URGP=0
Aug  6 02:44:33 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=144 TOS=0x00 PREC=0x00 TTL=128 ID=18883 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64327 RES=0x00 ACK PSH URGP=0
Aug  6 02:44:34 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18889 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64259 RES=0x00 ACK URGP=0
Aug  6 02:44:35 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=128 TOS=0x00 PREC=0x00 TTL=128 ID=18917 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64259 RES=0x00 ACK PSH URGP=0
Aug  6 02:44:35 localhost kernel: IPTABLES:IN=eth0 OUT= MAC=00:0c:29:8f:07:60:00:e0:4d:b3:e3:88:08:00 SRC=192.168.0.153 DST=192.168.0.253 LEN=40 TOS=0x00 PREC=0x00 TTL=128 ID=18924 DF PROTO=TCP SPT=1812 DPT=22 WINDOW=64191 RES=0x00 ACK URGP=0
注:

1)、首先确认你所要增加到LOG里面的数据包在之前没有丢弃过;

2)、然后确认在LOG前面没有ACCEPT的规则,有的话,把ACCEPT先删掉,在加入到ACCEPT到LOG的后面(也就是先经过LOG,再经过ACCEPT,不然系统日志无法记录日志信息)。


转载地址:http://blog.163.com/leekwen@126/blog/static/33166229200973105543171/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值