SQL注入之MySQL注入

前言

MYSQL注入中首先要明确当前注入点权限,高权限注入时有更多的攻击手法,有的能直接进行getshell操作。其中也会遇到很多阻碍,相关防御方案也要明确,所谓知己知彼,百战不殆。不论作为攻击还是防御都需要了解其中的手法和原理,这样才是一个合格的安全工作者。

img

img

MySQL 5.x数据结构

1.在MySQL 5.0以上版本中,为了方便管理,默认定义了information_schema数据库,用来存储数据库元信息,其中具有表schemata(数据库名),tables(表名),columns(列名或字段名)
2.在schemata表中,schema_name字段用来存储数据库名
3.在tables表中,table_schema和table_name分别用来存储数据库名和表名
4.在columns表中,table_schema(数据库名),table_name(表名),column_name(字段名)

5.关于直接对数据库的增删改查

  • 增 create database 数据库名
  • 删 drop database 数据库名
  • 改 rename database 旧名 to 新名
  • 查 show databases

image.png

  • 创建表 create table table_name( id1 int,id2 varcher)

image.png

  • 重新给表命名 alter table table_name_old rename as table_name_new

6.关于对数据表的增删改查

  • 增 insert into table_name(列1,列2 ... ) values(值1,值2 ... )
  • 删 delete from 表名 where 列名 = 值
  • 改 update 表名 set 列名=新值 where 列名=某值

image.png

image.png

  • 查 select * from 表名 where 字段1=‘条件一’and 字段2 =‘条件二’
  • 扩展 limit
    1. limit m,n 从m行开始,到m+n行结束
    2. select * from admin limit 2,1 指的是 从第二行开始,到第三行结束

高权限注入及低权限注入

1.跨库查询及应用思路

information_schema 表特性,记录库名,表名,列名对应表
通过select * from schemata; 进行数据库名查询,再去选择进行查询获取指明数据库里的数据

这里我们是在sqli的数据库下面查找pikachu数据库的内容,所有在查询users表的时候要知名数据库是pikachu数据库,如下图红框中的内容

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

image-20211204180556224

获取所有数据库名:

?id=-1'  union select 1,2,group_concat(schema_name) from information_schema.schemata --+

获取指定pikachu数据库下的表名信息:

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

获取指定pikachu数据库下users表的列名信息:

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

获取指定pikachu数据库users表下的username,password信息:

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

2.文件读写操作

会用到MySQL数据库里两个内置函数,这两个函数是MySQL数据库特有的,在其他数据库是没有的或者在其他数据库中写法不同,所以这是为什么说注入点分数据库的原因,因为每个数据库内置的安全机制和它的功能不同,这才导致在注入的时候针对不用的数据库采取的攻击思路也不同。MySQL有内置读取的操作函数,我们可以调用这个函数作为注入的攻击。

load_file():读取函数

image.png

image-20211204200058438

into outfile 或 into dumpfile	写入函数

(将asd123写入test文件中)

image-20211204195200796

image-20211204195520794

可能会遇到的报错

The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

翻译:MySQL服务器使用——secure-file-priv选项运行,因此不能执行此语句

原因:MYSQL新特性secure_file_priv对读写文件的影响;此开关默认为NULL,即不允许导入导出。

解决方案:

  • windows下:修改my.ini。在[mysqld]内加入secure_file_priv =
  • linux下:修改my.cnf。在[mysqld]内加入secure_file_priv =
  • 然后重启mysql,再查询secure_file_priv

参考连接:https://www.cnblogs.com/zenghongfei/p/12837107.html

3.路径获取常见方法:

1.报错显示:一般网站出现错误的时候它会泄露出路径

搜索方式:

image-20211204192500043

image.png

2.遗留文件:站长为了调试信息的时候遗留的文件而泄露的 路径。用扫描工具可以扫出

image.png

image.png

3.漏洞暴露:知道对方是用什么程序搭建再去网上去搜索漏洞信息:phpcms 爆路径

image.png

image.png

4.平台配置文件:通过读取文件来读取搭建网站平台的配置文件。缺点:路径不是默认的,一旦更改很难找到路径

image.png

爆破:
windows:
d:/wwwroot/xiaodi8/
linux:
/var/www/xiaodi8
扩展连接:
https://blog.csdn.net/weixin_30292843/article/details/99381669

常见读取文件列表:

常见写入文件问题:魔术引号开关
magic_quotes_gpc

魔术引号及常见防护

a.编码或宽字节绕过:

比如在sqlmap中添加--temper脚本参数转码
或者使用转换工具

image.png

b. 防护:

1.)自带防御:魔术引号
2.)内置函数:int等

img

3.)自定义关键字:select等

image.png

4.)WAF防护软件:安全狗、宝塔等

image.png

5.)低版本注入配合读取或暴力
字典或读取
涉及资源:
https://blog.csdn.net/weixin_30292843/article/details/99381669

sqli-labs测试

1.根据注入位置数据类型将sql注入分类

image.png

可以从SQL语法错误得出我们需要的东西

image.png

'1'' LIMIT 0,1 这个引号是我们自己加的 可以知道这个就是‘1',也就是说,这是个字符串类型的sql注入

2.利用order by 来判断字段数

image.png

在经过一番测试之后可以发现有三个字段

3.利用 union select 联合查询,将id值设置成不成立,即可探测到可利用的字段数

image.png

利用函数database(),user(),version()可以得到所探测数据库的数据库名、用户名和版本号

image.png

4.利用 union select 联合查询,获取表名

image.png

5.利用 union select 联合查询,获取字段名

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

image-20220102213949814

6.利用 union select 联合查询,获取字段值

image.png

即可成功得到我们想要的用户名和密码

posted @ 2021-11-04 20:34  流失流逝  阅读(436)  评论(0)    收藏  举报