【问题标题】:Python - how to generate wordlist from given characters of specific lengthPython - 如何从特定长度的给定字符生成单词表
【发布时间】:2014-02-28 19:25:07
【问题描述】:

我想执行字典攻击,为此我需要单词列表。如何从特定长度的给定字符(或从最小长度到最大长度的单词长度)生成单词列表?我试过itertools.combinations_with_replacementsitertools.permutations,但没有帮助。他们没有应该返回的所有单词列表。任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 我认为您不了解字典攻击背后的概念。这个想法是不要生成所有可能的组合(有很多),而是使用包含实际字典单词(以及其他可能是密码的单词)的文件。

标签: python python-2.7 dictionary-attack


【解决方案1】:

使用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

从最小长度到最大长度获取单词:

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)

【讨论】:

    【解决方案2】:
    from itertools import product
    
    def allwords(chars, length):
        for letters in product(chars, repeat=length):
            yield ''.join(letters)
    
    def main():
        letters = "abc"
        for wordlen in range(3, 5):
            for word in allwords(letters, wordlen):
                print(word)
    
    if __name__=="__main__":
        main()
    

    返回

    aaa
    aab
    aac
    aba
    abb
    
    ...
    
    ccbc
    ccca
    cccb
    cccc    
    

    【讨论】:

      【解决方案3】:

      这是一个幼稚的实现:

      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
      

      我还没有足够的声誉来发表评论,所以,根据上面的 itertools 示例制作一个完整的列表:

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

      这样,您的列表中就有长度为 1 到 n 的所有单词。

      【讨论】:

        【解决方案4】:

        def word_gen(start= 3,end= 3, elements = 1): """ Hud Seidu Daannaa Wordlist gen MSC 信息安全,CEH"

        README
        #for start&end
        #e.g. start= 3,end= 3
        #means first words to last words should be 3 characters
        
        #for elements
        1 is asci
        2 is numbers
        3 is asci&numbers
        """
        import itertools
        #types of elements
        if elements ==1: elements= 'abcdefghijklmnopqrstuvwxyx'
        if elements ==2: elements= '0123456789'
        if elements== 3: elements= 'abcdefghijklmnopqrstuvwxyx0123456789'
        else: pass
        wl = []
        for i in range(start,end+1):
            for xs in itertools.product(elements, repeat=i):
                wl.append(''.join(xs))
        return wl
        

        【讨论】:

        • 欢迎来到堆栈溢出。你能解释一下你的答案吗?
        猜你喜欢
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        相关资源
        最近更新 更多