標籤:python
本篇將會涉及:
資源探測
資源探測在滲透測試中還是屬於資源的映射和資訊的收集階段。
主要有以下三個類型:
字典攻擊,在破解密碼或密鑰的時候,通過自訂的字典檔案,有針對性地嘗試字典檔案內所有的字典組合。
暴力破解,也叫做窮舉法,按照特定的組合,進行枚舉所有的組合。簡單來說就是將密碼進行逐個推算直到找出真正的密碼為止。
模糊測試,指通過向目標系統提供非預期性的輸入並監視其發生的異常結果來發現目標系統的漏洞。
資源探測的作用
通過資源探測,我們可以在目標系統中發現檔案、目錄、活動、服務還有相關的參數,為下一步的行動提供資訊參考。
一個開源的模糊測試資料庫
https://github.com/fuzzdb-project/fuzzdb是一個開源的漏洞注入和資摘要搜索的原語字典。其提供了攻擊、資摘要搜索和響應分析的資源。
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-324c0c2963df70ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
第一個暴力探測器
在之前的章節,我們瞭解了使用Python進行HTTP請求的方法,在本章,我們瞭解的資源探測的作用的用途。接下面我們就利用Python編寫一個資源探測器,用來對Web網站進行資源探測。
我們將上面介紹的開源模糊測試資料庫FUZZDB從github上複製或下載下來:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-c784b7f5e34afa1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
這個資料庫會作為我們的資源探測器的字典,來對web網站進行針對性的探測。
建立一個Python檔案,開始我們的暴力探測器的編寫。
首先,引入相關的模組:
# coding:utf-8import requestsfrom threading import Threadimport sysimport getopt
requests用於請求目標網站;
threading用於啟用多線程;
sys用於解析命令列參數;
getopt用於處理命令列參數;
然後,定義一個程式的橫幅:
# 程式標識def banner(): print("\n********************") name = ‘‘‘ ______ _ _ |___ / (_) | | / / _ __ ___ _ ___| |_ ___ _ __ / / | ‘_ ` _ \| / __| __/ _ \ ‘__| / /__| | | | | | \__ \ || __/ | /_____|_| |_| |_|_|___/\__\___|_| ‘‘‘ print(name) print("州的先生-暴力發掘器 v0.1") print("***********************")
這個橫幅用於在程式啟動的時候顯示出來,除了讓程式個性一點之外,也沒啥用。
再定義一個函數,用來顯示程式的用法:
# 程式用法def usage(): print("用法:") print(" -w:網址 (http://wensite.com/FUZZ)") print(" -t:線程數") print(" -f:字典檔案") print("例子:bruteforcer.py -w http://zmister.com/FUZZ -t 5 -f commom.txt")
我們的程式因為是在命令列下啟動並執行,所以需要設定一些參數,在這裡,我們用:
-w來指定網址
-t 來指定線程數
-f來指定字典檔案
這三個參數缺一不可。
這兩個函數建立好後,運行程式便會出現如下介面:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-f2bbcc2d97ee06dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
看上去是不是有那麼點意思。
接著,我們建立一個繼承於Thread的類request_performer(),用於建立線程並向目標網站發起請求以及擷取響應:
class request_performer(Thread): def __init__(self,word,url): Thread.__init__(self) try: self.word = word.split("\n")[0] self.urly = url.replace(‘FUZZ‘,self.word) self.url = self.urly except Exception as e: print(e) def run(self): try: r = requests.get(self.url) print(self.url,"-",str(r.status_code)) i[0] = i[0] -1 except Exception as e: print(e)
在request_performer()類的run()方法裡面,我們利用requests對URL進行請求並將響應的狀態代碼列印出來。而這,就是我們這個探測器的最主要功能了。
再建立一個啟動request_performer()類的函數launcher_thread(),用於遍曆字典檔案中的關鍵字組合成URL並產生新的線程。
def launcher_thread(names,th,url): global i i = [] resultlist = [] i.append(0) while len(names): try: if i[0] < int(th): n = names.pop(0) i[0] = i[0]+1 thread = request_performer(n,url) thread.start() except KeyboardInterrupt: print("使用者停止了程式運行。完成探測") sys.exit() return True
繼續建立一個函數start(),用於接收命令列中的參數將其傳遞給launcher_thread()函數:
def start(argv): banner() if len(sys.argv) < 5: usage() sys.exit() try: opts,args = getopt.getopt(sys.argv[1:],"w:t:f:") except getopt.GetoptError: print("錯誤的參數") sys.exit() for opt,arg in opts: if opt == ‘-w‘: url = arg elif opt == ‘-f‘: dicts = arg elif opt == ‘-t‘: threads = int(arg) try: f = open(dicts,‘r‘) words = f.readlines() except Exception as e: print("開啟檔案錯誤:",dicts,"\n") print(e) sys.exit() launcher_thread(words,threads,url)
最後,當然是在主程式中運行了:
if __name__ == ‘__main__‘: try: start(sys.argv[1:]) except KeyboardInterrupt: print("使用者停止了程式運行。完成探測")
咱們這個程式到底有什麼用呢?
在這裡,我們不得不再提一下上面提及過的FUZZDB資料庫。fuzzdb是一個用於模糊測試的資料庫,類似於一個龐大的字典。而這些字典的內容呢,都是安全大神們維護的、在實踐中發現很有可能會是攻擊點的目錄或路徑。
我們可以開啟資料庫中的一個txt檔案看看:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-e7219155fdbed6e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
這是一個針對wordpress部落格系統外掛程式的一個字典,這裡面都是外掛程式的路徑和目錄。
測試暴力探測器
還記得在滲透測試環境搭建那篇文章介紹的虛擬機器環境嗎?
裡面有一個充滿漏洞的Web應用http://www.scruffybank.com/,我們可以使用我們剛剛編寫好的暴力探測器對這個網站進行一下探測。
字典檔案我們先採用一個簡單的字典:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-fd8697c6f14e00b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
我們在命令列運行命令:
python3 brutediscovery.py -w http://www.scruffybank.com/FUZZ -t 5 -f common.txt
得到結果:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-b38c11828ea5ae5e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
common.txt字典中有三個是成功的響應,我們開啟其中一個http://www.scruffybank.com/robots.txt看看:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-436cec91eeee7648.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
包含了三個禁止搜尋引擎爬取的連結,看字面意思,其中一個還是後台地址admin,但是在結果頁我們知道/admin是404錯誤,但是有一個/Admin,我們開啟看看:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-ea49e92c45e610f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
彈出了認證登入框,但是我們沒有使用者名稱和密碼,目前來說只能作罷。
我們再使用FUZZDB資料庫裡的字典測試一下。選擇fuzzdb-master/discovery/predictable-filepaths/php目錄下的PHP.fuzz.txt:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-d4a75ac3a61f05a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
同樣在終端命令列運行命令:
python3 brutediscovery.py -w http://www.scruffybank.com/FUZZ -t 5 -f PHP.fuzz.txt
得到結果:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-cfdef7c68141acaa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
雖然有很多404,但是我們還是發現了一些成功的響應:
比如info.php,開啟原來是PHP的info介面:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-276774889999ced8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
login.php為登入頁面:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-e8883433bfe71e5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
phpmyadmin是mysql資料庫的web管理入口:
650) this.width=650;" src="http://upload-images.jianshu.io/upload_images/38544-cfb9f99505d5b5fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1240" />
在資料探測收集階段,我們通過我們自己編寫的暴力探測器,獲得了這些頁面的資訊,對分析伺服器和web應用的漏洞並進行針對性的滲透有很大的協助。
在接下來的文章裡,我們將豐富和完善我們編寫的滲透測試工具的功能。
敬請期待!
文章首發:http://zmister.com/archives/180.html
Python爬蟲、資料分析、機器學習、滲透測試、Web應用、GUI開發,http://zmister.com/
本文出自 “州的先生” 部落格,轉載請與作者聯絡!
使用Python編寫一個滲透測試探測器