How you could calculate size of brute force method dynamically? For example how many iterations and space would take if you printed all IPv6 addresses from 0:0:0:0:0:0:0:0 - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff to file? The tricky parts are those when length of line varies. IP address is only example.
Idea is that you give the format and maximum lenghts of given parts. So if variable type is '%c' (char), and maxlen is 26 then iteration count is 26 and needed space in human format in text file is 26 + 26 (one char for separator)
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],
},
)
So for example:
- 1.2.3.4 takes 7 bytes
- 9.9.9.10 takes 8 bytes
- 1.1.1.100 takes 9 bytes
- 5.7.10.100 takes 10 bytes
- 128.1.1.1 takes 9 bytes
- and so on
Example 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)
iterations: 14641 needed_space: 122452 bytes
Into
print calculate(
"%a.%b.%c.%d",
{
'%a': ['%d', 10],
'%b': ['%d', 10],
'%c': ['%d', 10],
'%d': ['%d', 10]
},
)
Result: [14641, 122452]