user3416803 user3416803 - 7 months ago 31
Python Question

Python itertools.combinations continue from certain value?

I used itertools to generate all combinations or printable ascii chars:

for combo in product('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c', repeat=10):


However, script was interrupted, but i got last sequence string. Is there a way to continue generation values using this string as starting sequence? Thank you.

UPD: I trying to solve some CTF task, by bruteforcing XOR cipher text. Xortool's output:

2: 11.2%
5: 15.6%
7: 11.2%
10: 18.4%
15: 9.6%
18: 6.6%
20: 12.1%
25: 5.8%
30: 5.5%
40: 4.0%


I can't see other solution now, at lest will try to bf 5-byte keys.

Answer

Suppose the last string that was processed started with the character '5'. Then you can ignore all strings that started with previous characters, and set up the iteration like so:

for start in ('567...'):
    for subcombo in product('01234567...', repeat=9):
        yield (start,) + subcombo

However you really can't get through this search space. It's easy to calculate the total number of combinations:

>>> len('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c') ** 10
73742412689492826049L

While even if you could process a billion combinations a second, you wouldn't get close in a year:

>>> 1000000000 * 60 * 60 * 24 * 365
31536000000000000
Comments