ブルートフォースメソッドのサイズを動的に計算するにはどうすればよいですか? たとえば、0:0:0:0:0:0:0:0 から ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff までのすべての IPv6 アドレスをファイルに出力した場合、何回の反復とスペースが必要になるでしょうか? トリッキーな部分は、ラインの長さが変化する部分です。IPアドレスは一例です。
アイデアは、特定の部分の形式と最大長を指定することです。したがって、変数の型が '%c' (char) で maxlen が 26 の場合、反復回数は 26 であり、テキスト ファイルのヒューマン形式で必要なスペースは 26 + 26 (区切り文字として 1 文字) です。
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]