免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5266 | 回复: 1

[FreeBSD] 详解ipfw的log功能 [复制链接]

论坛徽章:
54
2017金鸡报晓
日期:2017-02-08 10:39:42操作系统版块每日发帖之星
日期:2016-03-08 06:20:00操作系统版块每日发帖之星
日期:2016-03-07 06:20:00操作系统版块每日发帖之星
日期:2016-02-22 06:20:00操作系统版块每日发帖之星
日期:2016-01-29 06:20:00操作系统版块每日发帖之星
日期:2016-01-27 06:20:00操作系统版块每日发帖之星
日期:2016-01-20 06:20:00操作系统版块每日发帖之星
日期:2016-01-06 06:20:0015-16赛季CBA联赛之江苏
日期:2015-12-21 20:00:24操作系统版块每日发帖之星
日期:2015-12-21 06:20:00IT运维版块每日发帖之星
日期:2015-11-17 06:20:002015亚冠之广州恒大
日期:2015-11-12 10:58:02
发表于 2015-06-20 17:30 |显示全部楼层

[原创]ipfw日志功能详解

在不少系统中,ifconfig的时候,会出来一个ipfw0界面,注意最后一个字符中“零”,有人会问有没有ipfw1(壹)?回答是:可以有。
这是为什么呢?

自从FreeBSD9.0以后,IPFW添加了一个虚拟的日志接口,看来读一下ipfw的man:
  1. ……
  2. log [logamount number]
  3. Packets matching a    rule with the log keyword will be made available for logging in two ways: if the sysctl variable net.inet.ip.fw.verbose is set to 0    (default), one can use bpf(4) attached to the ipfw0 pseudo interface.
复制代码
当数据包匹配了带有log关键字的规则时,有两种方式进行记录:如果内核变量net.inet.ip.fw.verbose为0(默认值为零,译注:可以在内核配置文件中修改),就可以使用bpf(4)来连接ipfw0这个虚拟网卡。
  1. This pseudo interface can be created after a boot manually by using the following command:
  2. # ifconfig ipfw0 create
  3. Or, automatically at boot time by adding the following line to the rc.conf(5) file:
  4. firewall_logif="YES"
复制代码
这个虚拟网卡可以使用ifconfig来创建:
  1. # ifconfig ipfw0 create
复制代码
或者可以在rc.conf中添加下面一行:
  1. firewall_logif="YES"
  2. There is no overhead if no    bpf(4) is attached to the pseudo interface.
复制代码
在没有bfp连接的时候,这个虚拟界面不会有任何开销。
  1. If    net.inet.ip.fw.verbose is set to 1, packets will be logged to syslogd(8)    with a LOG_SECURITY facility up    to a maximum of logamount packets.     If no logamount is specified, the limit is taken from    the sysctl variable net.inet.ip.fw.verbose_limit.  In both cases, a value of 0 means unlimited logging.
  2. Once the limit is reached,    logging    can be re-enabled by clearing the logging counter or the    packet counter for that    entry, see the resetlog command.
复制代码
如果内核变量net.inet.ip.fw.verbose为1,则使用syslogd(3)来使用LOG_SECURITY设备来记录,记录的数据包数最大值为指定的logamount,如果没有指定logamount,则使用内核变量net.inet.ip.fw.verbose_limit。如果logamount或net.inet.ip.fw.verbose_limit值为零,则表示没有任何限制,将一直记录。
一旦达到了记录的最大值,记录将会停止,可以通过重置log计数器或数据包计数器的方式来重新激活记录功能。
  1. Note: logging is done after all other packet matching conditions have been successfully verified, and before performing the    final action (accept, deny, etc.) on the    packet.
  2. ……
复制代码
日志记录的时间是:所有的条件完成匹配之后,进行最后的动作(accept,deny等)之前。

这段说的比较清楚,ipfw的日志可以使用log关键字,记录的方式根据net.inet.ip.fw.verbose值分为两种,一种是传统的log方式,另一种是虚拟网卡的方式。
这两种有什么区别呢?
传统的方式使用syslogd来记录,如果不加特殊配置的话,就记录在/var/log/security里面,日志像下面的样子:
  1. Jun  5 22:49:58 mm kernel: ipfw: 40 Reset TCP 192.168.82.253:21420 192.168.1.100:22 in via re0
  2. Jun  5 22:50:31 mm kernel: ipfw: 40 Reset TCP 192.168.146.253:38534 192.168.1.100:22 in via re0
  3. Jun  5 22:51:48 mm kernel: ipfw: 40 Reset TCP 192.168.35.253:18943 192.168.1.100:22 in via re0
  4. Jun  5 22:52:07 mm kernel: ipfw: 40 Reset TCP 192.168.226.253:26218 192.168.1.100:22 in via re0
  5. Jun  5 22:53:18 mm kernel: ipfw: 40 Reset TCP 192.168.145.1:61878 192.168.1.100:22 in via re0
  6. Jun  5 22:54:11 mm kernel: ipfw: 40 Reset TCP 192.168.98.253:42187 192.168.1.100:22 in via re0
  7. Jun  5 22:54:58 mm kernel: ipfw: 40 Reset TCP 192.168.82.253:25475 192.168.1.100:22 in via re0
  8. Jun  5 22:55:32 mm kernel: ipfw: 40 Reset TCP 192.168.146.253:37578 192.168.1.100:22 in via re0
复制代码
对应的40规则是阻止所有连接ssh的包:
  1. ipfw add 40 deny log logamount 80000 ip from any to me dst-port 22
复制代码
在这个日志中,可以看到(1)时间;(2)对应的ipfw规则号;(3)协议;(4)源地址和端口;(5)目的地址和端口。
虽然几个信息对大多数情况来说足够用,但是如果想深入分析的话,还是远远不够的,比如想看一下数据包拆分的情况,tos,数据包里面的内容,这种方式就没有用了。
ipfw提供了虚拟网卡的方式,可以让你看到任何的细节,因为从使用的角度来说,它就是一个网卡,只不过通过这个网卡的数据是从ipfw的log规则里面转过来的:
  1. root@test:/root # ifconfig ipfw0 create
  2. root@test:/root # ipfw add 80 allow log logamount 80000 ip from any to me dst-port 80
  3. root@test:/root # sysctl net.inet.ip.fw.verbose=0
  4. root@test:/root # tcpdump -D
  5. 1.em0
  6. 2.ng0
  7. 3.ipfw0
  8. 4.em1
  9. root@test:/root # tcpdump -ani ipfw0
  10. tcpdump: WARNING: ipfw0: That device doesn't support promiscuous mode
  11. (BIOCPROMISC: Invalid argument)
  12. tcpdump: WARNING: ipfw0: no IPv4 address assigned
  13. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  14. listening on ipfw0, link-type EN10MB (Ethernet), capture size 65535 bytes
  15. capability mode sandbox enabled
  16. 09:39:46.653210 53:53:53:53:53:53 > 44:44:44:44:44:44, ethertype IPv4 (0x0800), length 115: 101.28.208.118.80 > 192.168.1.100.80: UDP, length 73
  17. 0x0000:  4500 0065 6c92 0000 3911 9bcd 651c d076  E..el...9...e..v
  18. 0x0010:  dd02 6693 0050 0050 0051 5c79 fe46 0000  ..f..P.P.Q\y.F..
  19. 0x0020:  4601 b002 0502 a040 d903 4800 008d 1881  F......@..H.....
  20. 0x0030:  09e7 aa1a 8cba b1ae deea dbeb 2a0a 6461  ............*.da
  21. 0x0040:  b09b 574d e6b6 9c3d 3add ea1e dff3 c55a  ..WM...=:......Z
  22. 0x0050:  032a d015 8e2c 107c 7884 ea38 7419 69f0  .*...,.|x..8t.i.
  23. 0x0060:  0147 149d fc                             .G...
  24. 09:39:56.718059 53:53:53:53:53:53 > 44:44:44:44:44:44, ethertype IPv4 (0x0800), length 475: 125.39.202.107.8000 > 221.2.102.147.80: UDP, length 433
  25. ……

复制代码
当然你也可以使用-w参数,把tcpdump的内容记录到文件中去。

本文开始的问题:只能有ipfw0吗?答案是可以是任何数字,但是只能有一个:
  1. root@test:/root # ifconfig ipfw1 create
  2. ifconfig: SIOCIFCREATE2: File exists
  3. root@test:/root # ifconfig ipfw0 destroy
  4. root@test:/root # ifconfig ipfw1 create
  5. root@test:/root # ifconfig ipfw1 destroy
  6. root@test:/root # ifconfig ipfw2 destroy
  7. ifconfig: interface ipfw2 does not exist
  8. root@test:/root # ifconfig ipfw2 create
  9. root@test:/root # ifconfig ipfw2 destroy
  10. root@test:/root # ifconfig ipfw20 create
  11. root@test:/root # ifconfig ipfw20
  12. ipfw20: flags=8801<UP,SIMPLEX,MULTICAST> metric 0 mtu 65536
复制代码
但是最大值能到327xx(难道FreeBSD只能支持到32768个网卡?):
  1. root@test:/root # ifconfig ipfw65530 create
  2. ifconfig: SIOCIFCREATE2: No space left on device
  3. root@test:/root # ifconfig ipfw32768 create
  4. ifconfig: SIOCIFCREATE2: No space left on device
  5. root@test:/root # ifconfig ipfw16384 create
  6. root@test:/root # ifconfig ipfw16384 destroy
  7. root@test:/root # ifconfig ipfw32000 create
  8. root@test:/root # ifconfig ipfw32000 destroy
  9. root@test:/root # ifconfig ipfw32700 create
  10. root@test:/root # ifconfig ipfw32700
  11. ipfw32700: flags=8801<UP,SIMPLEX,MULTICAST> metric 0 mtu 65536
  12. root@test:/root # ifconfig ipfw4096 create
  13. root@test:/root # ifconfig ipfw4096 destroy
复制代码
另发于:landingbj

评分

参与人数 2可用积分 +10 信誉积分 +12 收起 理由
ulovko + 10 + 2 ^_^
蛮多肉 + 10 很给力!

查看全部评分

论坛徽章:
9
2015年亚洲杯之卡塔尔
日期:2015-05-07 07:05:542015亚冠之鹿岛鹿角
日期:2015-05-29 14:55:522015亚冠之鹿岛鹿角
日期:2015-06-11 09:55:192015亚冠之山东鲁能
日期:2015-06-19 23:53:042015亚冠之大阪钢巴
日期:2015-06-23 21:03:17操作系统版块每日发帖之星
日期:2015-06-23 22:20:00操作系统版块每日发帖之星
日期:2015-06-27 22:20:002015亚冠之布里斯班狮吼
日期:2015-07-04 03:40:012015亚冠之平阳省
日期:2015-07-12 09:32:55
发表于 2015-06-20 22:04 |显示全部楼层
逛逛BSD板块

看看楼主的贴

真是享受
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP