1

I have some code which mainly revoles around the following couple of lines:

#! /usr/bin/env python

from crypt import crypt
import itertools
from string import ascii_letters, digits

def decrypt(all_hashes, salt, charset=ascii_letters + digits + '-' + '/'):
    products = (itertools.product(charset, repeat=r) for r in range(10))
    chain = itertools.chain.from_iterable(products)
    for i, candidate in enumerate(chain, 1):
        if i % 100 == 0:
            print ('%d th candidate: %s' % (i, candidate))
        hash = crypt(''.join(candidate), salt)
        if hash in all_hashes:

            yield candidate, hash
            all_hashes.remove(hash)

        if not all_hashes:
            return

all_hashes = ('aaRrt6qwqR7xk', 'aaacT.VSMxhms' , 'aaWIa93yJI9kU',
'aakf8kFpfzD5E', 'aaMOPiDnXYTPE', 'aaz71s8a0SSbU', 'aa6SXFxZJrI7E',
'aa9hi/efJu5P.', 'aaBWpr07X4LDE', 'aaqwyFUsGMNrQ', 'aa.lUgfbPGANY',
'aaHgyDUxJGPl6', 'aaTuBoxlxtjeg', 'aaluQSsvEIrDs', 'aajuaeRAx9C9g',
'aat0FraNnWA4g', 'aaya6nAGIGcYo', 'aaya6nAGIGcYo', 'aawmOHEectP/g',
'aazpGZ/jXGDhw', 'aadc1hd1Uxlz.', 'aabx55R4tiWwQ', 'aaOhLry1KgN3.',
'aaGO0MNkEn0JA', 'aaGxcBxfr5rgM', 'aa2voaxqfsKQA', 'aahdDVXRTugPc',
'aaaLf47tEydKM', 'aawZuilJMRO.w', 'aayxG5tSZJJHc', 'aaPXxZDcwBKgo',
'aaZroUk7y0Nao', 'aaZo046pM1vmY', 'aa5Be/kKhzh.o', 'aa0lJMaclo592',
'aaY5SpAiLEJj6', 'aa..CW12pQtCE', 'aamVYXdd9MlOI', 'aajCM.48K40M.',
'aa1iXl.B1Zjb2', 'aapG.//419wZU')

all_hashes = set(all_hashes)
salt = 'aa'
for candidate, hash in decrypt(all_hashes, salt):
    print 'Found', hash, '! The original string was', candidate

The program is basically a Brute Force attack and because the output can take a while I need to know if the program is still running or has crashed so I was thinking of having an output statement that says how many combinations of characters have been tried and maybe what the last tried string was but have no idea how to do it.

Thanks in advance for assistance

4

3 回答 3

9

用于enumerate计算候选人。

for i, candidate in enumerate(chain, 1):
    if i % 100 == 0:
        print("%d'th candidate: %s" % (i, candidate))
    # perform actual work
于 2012-11-27T13:49:56.390 回答
1
PRINT_COUNT = 10
chain = itertools.chain.from_iterable(products)
count = 0
for candidate in chain:
  hash = crypt(''.join(candidate), salt)
  count = count + 1
  if count % PRINT_COUNT == 0:
    print "count = " + str(count)
于 2012-11-27T13:49:22.783 回答
0

好的,我们先举另一个例子:

import time
import sys

for i in range(30):
    sys.stdout.write('\r%d'%i)
    sys.stdout.flush()
    time.sleep(0.1)

运行它,它将打印 i 的运行时值。

注意'\r'

因此,您可以在代码中执行以下操作:

chain = itertools.chain.from_iterable(products)
i = 0
for candidate in chain:
    sys.stdout.write('\r%d'%i)
    sys.stdout.flush()
    i += 1
    hash = crypt(''.join(candidate), salt)

如果输出值在增加,意味着你的程序正在运行。甚至您也可以添加您的所有者代码来确定剩余时间。

于 2012-11-27T14:01:13.933 回答