暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用离线IP库分析 Windows 防火墙日志

生有可恋 2022-12-03
1562

Windows防火墙日志会记录所有流入和流出的数据包的IP信息,我们通过ip2region 提供的离线地址库对 IP 位置信息进行统计。

主要统计日志中目标 IP 的数据包数量和地址位置信息,从而分析主机访问外网的情况。其中 ip2region 项目地址为:

  • https://github.com/lionsoul2014/ip2region


从 ip2region 中可以获取 ip2region.xdb 数据文件和 python 库文件 xdbSearcher.py,将其与测试代码放在一起即可运行。在正式解析防火墙日志前,我们需要将防火墙日志中的过滤范围调整一下,如下图所示,我们只记录放行的包:

运行测试代码:

    $ $ cat d/pfirewall.log | python getIPRegin.py

    输出内容如下图所示:

    最后一列为日志中同一IP数据包的数量,初步过滤后可以对国内外地址进行分类。如果服务器只供国内用户使用,可以在网络防火墙中将境外地址屏蔽掉。测试代码如下:

      #!python3


      import sys
      import re


      from xdbSearcher import XdbSearcher


      dbPath = 'ip2region.xdb'
      buff = XdbSearcher.loadContentFromFile(dbfile=dbPath)
      searcher = XdbSearcher(contentBuff=buff)
      ip_search = searcher.search


      class IP:
      n = 0
      ip = ''
      notes = ''


      def __init__(self, ip, notes):
      self.n = 1
      self.ip = ip
      self.notes = notes


      def add(self):
      self.n = self.n+1




      def regin(ip):
      s = ip.split('|')
      r = []
      for i in s:
      if u'\u4e00' <= i <= u'\u9fa5':
      r.append(i)
      #if i != '0':
      # r.append(i)
      ret = chr(12288).join(r)
      return ret




      f = sys.stdin
      d = {}


      pattern = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
      for line in f:
      r = line.split()
      if len(r) != 17:
      continue
      if r[3] == 'UDP':
      continue
      if r[5] == '127.0.0.1':
      continue
      if r[7] == '53':
      continue
      if not re.match(pattern, r[5]):
      continue
      dst_ip = r[5]
      location = regin(ip_search(dst_ip))
      if location.split()[0] == '内网IP':
      continue
      if dst_ip in d:
      d[dst_ip].add()
      else:
      dst = IP(dst_ip, location)
      d[dst_ip] = dst


      for x in d:
      print("{0:<16} {1:{3}<18} {2:>6}".format(x, d[x].notes, d[x].n, chr(12288)))





      全文完。


      如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。


      文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论