0

如何动态计算蛮力方法的大小?例如,如果将所有 IPv6 地址从 0:0:0:0:0:0:0:0 - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 打印到文件中,将需要多少次迭代和空间?棘手的部分是线条长度变化的部分。IP 地址只是示例。

想法是您给出给定部分的格式和最大长度。因此,如果变量类型为 '%c' (char),maxlen 为 26,则迭代次数为 26,文本文件中人类格式所需的空间为 26 + 26(一个字符作为分隔符)

def calculate(format, rules):

  end = format

  for i in rules:
    (vartype, maxlen) = rules[i]
    end = end.replace(i, vartype % maxlen)


  start = format

  for i in rules:
    (vartype, maxlen) = rules[i]
    minlen = 0
    start = start.replace(i, vartype % minlen)

  start_bytes = len(start)
  end_bytes = len(end)

  # how to add for example IPv4 calculations
  #         0.0.0.0 - 9.9.9.9
  #     10.10.10.10 - 99.99.99.99
  # 100.100.100.100 - 255.255.255.255

  iterations = 0

  for i in rules:
    if format.find(i) is not -1:
      (vartype, maxlen) = rules[i]
      if iterations == 0:
        iterations = int(maxlen) + 1
      else:
        iterations *= int(maxlen) + 1

  iterations -= 1

  needed_space = 0

  if start_bytes == end_bytes:
    # +1 for separator (space / new line)
    needed_space = (1 + start_bytes) * iterations
  else:
    needed_space = "How to calculate?"

  return [iterations, needed_space, start, end, start_bytes, end_bytes]


if __name__ == '__main__':

  # IPv4
  print calculate(
    "%a.%b.%c.%d",
    {
      '%a': ['%d', 255],
      '%b': ['%d', 255],
      '%c': ['%d', 255],
      '%d': ['%d', 255]
    },
  )

  # IPv4 zero filled version
  print calculate(
    "%a.%b.%c.%d",
    {
      '%a': ['%03d', 255],
      '%b': ['%03d', 255],
      '%c': ['%03d', 255],
      '%d': ['%03d', 255]
    },
  )


  # IPv6
  print calculate(
    "%a:%b:%c:%d:%e:%f:%g:%h",
    {
      '%a': ['%x', 65535],
      '%b': ['%x', 65535],
      '%c': ['%x', 65535],
      '%d': ['%x', 65535],
      '%e': ['%x', 65535],
      '%f': ['%x', 65535],
      '%g': ['%x', 65535],
      '%h': ['%x', 65535]
    },
  )

  # days in year, simulate with day numbers
  print calculate(
    "ddm%a", #ddmmyy
    {
      '%a': ['%03d', 365],
    },
  )

例如:

  • 1.2.3.4 占用 7 个字节
  • 9.9.9.10 占用 8 个字节
  • 1.1.1.100 占用 9 个字节
  • 5.7.10.100 占用 10 个字节
  • 128.1.1.1 占用 9 个字节
  • 等等

示例 0.0.0.0 - 10.10.10.10:

  iterations = 0
  needed_space = 0

  for a in range(0, 11):
    for b in range(0, 11):
      for c in range(0, 11):
        for d in range(0, 11):
          line = "%d.%d.%d.%d\n" % (a, b, c, d)
          needed_space += len(line)
          iterations += 1

  print "iterations: %d needed_space: %d bytes" % (iterations, needed_space)

迭代次数:14641 所需空间:122452 字节

进入

  print calculate(
    "%a.%b.%c.%d",
    {
      '%a': ['%d', 10],
      '%b': ['%d', 10],
      '%c': ['%d', 10],
      '%d': ['%d', 10]
    },
  )

结果:[14641, 122452]

4

3 回答 3

4

使用组合数学和离散数学:

IPv4 地址空间为 256 *256 *256 *256 = 2^32 = 4,294,967,296 个地址。

IPv6 有 2^128 个地址(8 组 16 *16 *16 *16)。

IPv4 地址使用 32 位,因此 32 位 * 4,294,967,296 个地址 = 16 GB(如果存储在磁盘上)。

IPv6 地址使用 128 位,因此 128 位 * 2^128 个地址 = 5.07 * 10^30 GB

于 2009-09-23T04:08:43.753 回答
0

将其分解为组件可能是要走的路。对于 IPv4,您有四个部分,由三个点分隔,每个部分的长度可以是 1、2 或 3 个字符(0-9、10-99、100-255)。所以你的组合是:

comp_length = {1: 10, 2: 90, 3: 156}

您可以通过遍历每个组合来计算总长度:

def ipv4_comp_length(n=4):
    if n == 1:
        return comp_length
    res = {}
    for rest, restcnt in ipv4_comp_length(n-1).iteritems():
        for first, firstcnt in comp_length.iteritems():
             l = first + 1 + rest # "10" + "." + "0.0.127"
             res[l] = res.get(l,0) + (firstcnt * restcnt)
    return res

print sum( l*c for l,c in ipv4_comp_length().iteritems() )

请注意,这没有考虑记录分隔符(“1.2.3.4 1.2.3.5”中的空格)。

扩展到 IPv6 应该很简单——除非您想处理缩写的 IPv6 地址,例如 0::0 == 0:0:0:0:0:0:0:0。

于 2009-09-23T04:27:32.797 回答
0

首先从计算您需要的行数开始。IPv6 地址为 128 位,因此您的输出文件将有 2 128行长。如果你所有的行都是的,那大约是 3.4 × 10 38字节。1 TB 只有大约 10 12字节,因此您需要超过 3 × 10 26 1 TB 的硬盘驱动器来存储行,然后再将任何数据放入其中。

每行存储 40 个字节将需要更多的存储空间。

于 2009-09-23T04:17:40.570 回答