一個度過菜鳥期的后臺程序員都會小心的避開SQL注入常見低級錯誤。
除了白盒和代碼評審定硝,作為第三方的滲透測試晨雳,使用sqlmap這樣的常用工具進行SQL注入掃描可以降低上述問題發(fā)生概率。
工具環(huán)境準(zhǔn)備
0.先檢查一下是否安裝過sqlmap工具紧卒,顯示下版本
sqlmap --version
如果運行成功說明已經(jīng)安裝此工具侥衬,比如返回
1.1.6#pip
如果沒有安裝過sqlmap,請參考后面兩個步驟
1.安裝pip工具
多數(shù)新的發(fā)行版linux都帶有python和pip跑芳,檢查pip版本
[jim@host]$ pip --version
pip 9.0.1 from /usr/lib/python2.6/site-packages (python 2.6)
對于centos 6.5 或者以下轴总,沒有安裝pip的話,請用root用戶安裝
[root@host]# wget https://bootstrap.pypa.io/get-pip.py
[root@host]# python get-pip.py
注:如果需要升級到 python2.7博个,可以參考 centos_6.5安裝python2.7
2. 安裝sqlmap工具怀樟,建議使用國內(nèi)的源,比如阿里云盆佣,速度快很多
[root@host]# pip install sqlmap -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
...
Successfully built sqlmap
Installing collected packages: sqlmap
Successfully installed sqlmap-1.1.6
很多步驟和依賴包安裝完畢后可回到 步驟0 檢查工具安裝結(jié)果
常規(guī)注入掃描
最簡單的注入原理就是在請求參數(shù)中增加“;sql statment”, 如果響應(yīng)告訴掃描工具請求成功往堡,那么這個請求就是有嚴(yán)重注入攻擊漏洞的。
0. 了解常用的參數(shù)
sqlmap -h
常用到的必選項
-u URL, --url=URL Target URL (e.g. "http://www.site.com/vuln.php?id=1")
--data=DATA Data string to be sent through POST
--cookie=COOKIE HTTP Cookie header value
-H HEADER, --hea.. Extra header (e.g. "X-Forwarded-For: 127.0.0.1")
暴力的掃描選項
-a, --all Retrieve everything
如果已經(jīng)知道目標(biāo)數(shù)據(jù)庫共耍,比如mysql虑灰,請指定數(shù)據(jù)庫參數(shù),否則sqlmap會遍歷嘗試各種數(shù)據(jù)庫注入
--dbms mysql
- 匿名訪問和使用cookie來授權(quán)用戶 如果要檢測的應(yīng)用屬于匿名用戶公開訪問痹兜,不用考慮cookie
如果要檢測的應(yīng)用屬于用戶登錄授權(quán)的穆咐,則需要使用cookie來保持授權(quán)用戶會話,參考如何模擬http_https請求頭和cookie
- 準(zhǔn)備被掃描對象 找到api的列表,作為被掃描對象对湃,但是構(gòu)造參數(shù)可能是瑣碎的事情崖叫,此外還要注意幾點:
盡量從本機 127.0.0.1 來訪問目標(biāo)請求api
目的是降低網(wǎng)絡(luò)要求和防火墻等安全產(chǎn)品的誤報
不要在生產(chǎn)環(huán)境運行,可能是有破壞性的
獲得訪問請求api和參數(shù)拍柒,對于上線的系統(tǒng)归露,可從生產(chǎn)日志里面找到掃描請求
- 運行和查看掃描結(jié)果 我們看一個簡單的掃描例子,只有一個參數(shù)的GET方法
sqlmap -u'http://127.0.0.1:8118/app/dealreport/query/user-info?user_name=%E4%B8%81%E4%BF%8A&_=1498102883406' -a --dbms mysql
___
__H__
___ ___[)]_____ ___ ___ {1.1.6#pip}
|_ -| . [.] | .'| . |
|___|_ ["]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illeg\
al. It is the end user's responsibility to obey all applicable local, state and federal laws. Dev\
elopers assume no liability and are not responsible for any misuse or damage caused by this progr\
am
[*] starting at 14:49:49
[14:49:49] [INFO] testing connection to the target URL
[14:49:49] [INFO] testing if the target URL is stable
[14:49:50] [INFO] target URL is stable
[14:49:50] [INFO] testing if GET parameter 'user_id_assistant' is dynamic
[14:49:50] [WARNING] GET parameter 'user_id_assistant' does not appear to be dynamic
[14:49:50] [WARNING] heuristic (basic) test shows that GET parameter 'user_id_assistant' might no\
t be injectable
[14:49:50] [INFO] testing for SQL injection on GET parameter 'user_id_assistant'
[14:49:50] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[14:49:51] [INFO] testing 'MySQL >= 5.0 boolean-based blind - Parameter replace'
[14:49:52] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (FLOOR)'
...
...
[14:50:01] [INFO] testing 'MySQL >= 5.0 error-based - Parameter replace (FLOOR)'
[14:50:01] [INFO] testing 'MySQL inline queries'
[14:50:01] [INFO] testing 'MySQL >= 5.0.12 AND time-based blind'
[14:50:02] [INFO] testing 'Generic UNION query (NULL) - 1 to 10 columns'
[14:50:08] [WARNING] GET parameter '_' does not seem to be injectable
[14:50:08] [CRITICAL] all tested parameters appear to be not injectable. Try to increase '--level'/'--risk' values to perform more tests. Also, you can try to rerun by providing either a valid value for option '--string' (or '--regexp'). If you suspect that there is some kind of protection mechanism involved (e.g. WAF) maybe you could retry with an option '--tamper' (e.g. '--tamper=space2comment')
[*] shutting down at 14:50:08
注意到 [14:50:08] [CRITICAL] all tested parameters appear to be not injectable. 斤儿, 那么這個api通過了常規(guī)注入套路的檢測剧包,未見可疑。
進階注入掃描
- 我們需要用到更詳細(xì)和高級的sqlmap運行選項
sqlmap -hh
- 更高效的api參數(shù)組合
使用代理來記錄遍歷所有的有效請求
- 快速評估
當(dāng)不能對所有的api進行掃描往果,需要抽樣評估疆液,這是一個抽樣數(shù)量計算的工具https://www.surveysystem.com/sscalc.htm,根據(jù)可接受誤差和置信度來決定抽樣數(shù)量