Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how to generate wordlist from given characters of specific length

I want to perform a dictionary attack and for that I need word lists. How to generate word list from given characters of specific length ( or word length from min length to max length )? I have tried itertools.combinations_with_replacements and itertools.permutations, but it does not help. They does not have all the word lists that it should return. Any help will be greatly appreciated. Thank you.

like image 446
Aamu Avatar asked Dec 26 '22 14:12

Aamu


2 Answers

Use itertools.product:

>>> import itertools
>>>
>>> chrs = 'abc'
>>> n = 2
>>>
>>> for xs in itertools.product(chrs, repeat=n):
...     print ''.join(xs)
...
aa
ab
ac
ba
bb
bc
ca
cb
cc

To get word from min length to max length:

chrs = 'abc'
min_length, max_length = 2, 5    
for n in range(min_length, max_length+1):
    for xs in itertools.product(chrs, repeat=n):
        print ''.join(xs)
like image 91
falsetru Avatar answered May 10 '23 04:05

falsetru


This is a naïve implementation:

list='abcdefg'
depth=8

def generate(l,d):
  if d<1:
    return
  for c in l:
    if d==1:
      yield c
    else:
      for k in generate(l,d-1):
        yield c+k

for d in range(1,depth):
  for c in generate(list,d):
    print c

I don't have enough reputation to comment yet, so, to make a full list based on the itertools sample above:

import itertools
chrs='abc'
n=6
for i in range(1,n):
  for xs in itertools.product(chrs, repeat=i):
    print ''.join(xs)

This way, you have all words from length 1 up to n in your list.

like image 21
Marco Costa Avatar answered May 10 '23 02:05

Marco Costa