LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 05-06-2008, 11:22 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
Question on new php exploit


Question on new php exploit, it seems this is only and issue if you are using php in safe mode, and disabled php functions that are normally available.

So this would only effect you if you tried to harden php and this would just make it less hard. And this is not a root exploit.

Is that correct?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


SektionEins GmbH
www.sektioneins.de

-= Security Advisory =-


Advisory: PHP Multibyte Shell Command Escaping Bypass Vulnerability Release Date: 2008/05/06 Last Modified: 2008/05/06
Author: Stefan Esser [stefan.esser[at]sektioneins.de]

Application: PHP 5 <= 5.2.5
PHP 4 <= 4.4.8
Severity: Several shell locales with support for east asian
variable width encodings allow bypassing PHP's
shell command escaping functions, safe_mode and
disable_functions
Risk: Medium/High
Vendor Status: Vendor has released PHP 5.2.6 which uses locale
aware shell command/argument escaping
Reference: http://www.sektioneins.de/advisories/SE-2008-03.txt


Overview:

Quote from http://www.php.net
"PHP is a widely-used general-purpose scripting language that
is especially suited for Web development and can be embedded
into HTML."

In PHP there exist two functions to escape shell commands or
arguments to shell commands that are used in PHP applications
to protect against shell command injection vulnerabilities.

- escapeshellcmd()
- escapeshellarg()

Unfortunately it was discovered that both functions fail to
protect against shell command injection when the shell uses
a locale with a variable width character set like GBK,
EUC-KR, SJIS, ..

This can lead to arbitrary shell command injection vulnerabilities
in PHP applications believed to be safe. In addition to that
exploiting this problem in PHP functions that use this shell
escaping internally allows safe_mode and disable_functions
bypass.


Details:

[1] escapeshellcmd()

escapeshellcmd() will put a single backslash character in front
of every shell meta character like ; $ < > ... to escape it.
This function is normally used to ensure that only a single shell
command is executed and that it is not possible to append further
shell commands.

The problem is that the backslash character is a legal second
byte of several variable width encodings. Because of this a shell
that is for example configured to use a locale with the GBK
character set will consider the introduced backslash as part of
a multibyte character instead of an escaping of following meta
character.

Example:
escapeshellcmd("echo ".chr(0xc0).";id");

Executing the result of this will therefore result in echo and
id being executed.

[2] escapeshellarg()

escapeshellarg() does not use the backslash character to escape
shell meta characters. Instead it places the argument in single
quotes and only escapes single quotes in the qrgument with the
string '\'' . Because of this it is not possible to use the same
trick. However in case there are multiple inputs it is possible
to "eat" the terminating single quote which results in a shell
command injection through the second argument.

Example:
$arg1 = chr(0xc0);
$arg2 = "; id ; #";
$cmd = "echo ".escapeshellarg($arg1)." ".escapeshellarg($arg2);

In this example the 0xC0 character forms a multibyte character
with the terminating single quote. Therefore the starting single
quote of $arg2 will be used as terminating single quote and the
content of $arg2 can be used to inject everything.

NOTE: This attack works because even invalid second byte characters
are accepted on several platforms as valid.

[3] safe_mode_exec_dir bypass

Because of the vulnerability described in [1] it is possible to
bypass the safe_mode_exec_dir directive of PHP. This directive
is supposed to ensure that only shell commands within the allowed
directory can be executed.

This attack is however only feasible when the shell uses one of
the vulnerable locales, because during safe_mode it is not possible
to set the LANG environment variable that would influence the shell.

[4] mail() fifth parameter - disable_functions bypass

Because of the vulnerability described in [1] it is possible to
execute arbitrary shell commands on a system even when all shell
execution functions like shell_exec(), system(), ... are disabled
by the disable_functions directive, but mail() is still allowed.
This attack relies on the fact that the fifth mail() parameter is
used as argument to the sendmail binary and escaped with
escapeshellcmd() internally to ensure that no further shell commands
are appended.

Because PHP scripts can influence the locale of the shell (unless
running in safe_mode) this attack allows bypassing the setting of
disable_functions when a vulnerable locale is installed on the
system. In case the system's shell does not support one of the
vulnerable character sets the attack is not feasible.


Proof of Concept:

SektionEins GmbH is not going to release a proof of concept
exploit for this vulnerability.


Disclosure Timeline:

07. March 2008 - Notified security@php.net
01. May 2008 - PHP developers released PHP 5.2.6
06. May 2008 - Public Disclosure


Recommendation:

It is recommended to upgrade to the latest version of PHP
which also fixes additional vulnerabilities reported by
third parties.

Grab your copy at:

http://www.php.net/downloads.php


CVE Information:

The Common Vulnerabilities and Exposures project (cve.mitre.org) has
not assigned a name to this vulnerability yet.


GPG-Key:

pub 1024D/15ABDA78 2004-10-17 Stefan Esser <stefan.esser@sektioneins.de>
Key fingerprint = 7806 58C8 CFA8 CE4A 1C2C 57DD 4AE1 795E 15AB DA78


Copyright 2008 SektionEins GmbH. All rights reserved.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8

iEYEARECAAYFAkggLkEACgkQSuF5XhWr2ni3jwCeKVl6Vm/dJ78TbJxc8Pnkztbm
Pe8An2Zok9MCrK7SCHnFDLnPPb0rbu0A
=WPG6
-----END PGP SIGNATURE-----
 
Old 05-06-2008, 11:27 AM   #2
unixfool
Member
 
Registered: May 2005
Location: Northern VA
Distribution: Slackware, Ubuntu, FreeBSD, OpenBSD, OS X
Posts: 782
Blog Entries: 8

Rep: Reputation: 158Reputation: 158
isc.sans.org -- PHP 5.2.6 out w/ security updates
 
Old 05-06-2008, 12:17 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Thanks, that definately seems to be more than just being able to execute disabled functions.
 
Old 05-06-2008, 03:14 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
The two functions in the advisory are used to make sure that any system call you run from within your php code can ONLY do what you want it to do and nothing else. If you don't use these functions to clean up a system call, you are already vulnerable to whatever they are not blocking correctly. If you do use them, there is something wrong with them, and they don't block all potential injections under certain circumstances.

An injection is when a cracker attempts to run a command or statement by putting bad input into an input field. For example, suppose you wrote a php script that lists the contents of a file in a user's home directory. Now lets also pretend you are NOT escaping the input, and furthermore, are doing this with the "cat" command ("Why?" I ask myself and then realize, Oh yeah, I need an example of really bad programming). You ask for a user name and get the input:

johndoe;cat /etc/passwd

The web browser may have just given an error, but if your code was simply "cat /home/$USERNAME/filename" you just ran cat on the /home/johndoe directory and then listed the contents of /etc/passwd and tried to run filename in the shell's default path (why the error would occur). This example doesn't do much in modern systems that have shadow files, but you get the idea. (It may not even work at all. I'm trying to teach good programming, not how to crack). If you use the escape functions, $USERNAME would be truncated to only include everything before the ";" and you would only cat the desired file.

If you do not have the vulnerable locals installed, your escape functions will work properly. If you have them installed, you are vulnerable if you are not running in safe_mode. If you have them installed and you ARE running in safe_mode, you are vulnerable if your php shell is set to use the local.

HTH

Forrest

Last edited by forrestt; 05-06-2008 at 03:16 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
exploit question SEARCH /\x90\xc9\ mnauta Linux - Security 12 05-24-2006 07:44 AM
PHP / VideoLAN / Fedora Core Question - how can I get the PHP "exec" function to work gtrawoger Linux - Software 3 12-21-2005 06:51 AM
*BSD exploit??? mcleodnine *BSD 3 05-13-2005 12:35 PM
What exploit is this? Boss Hoss Linux - Security 6 06-11-2004 06:16 PM
|more exploit Benamoz Linux - General 3 09-03-2003 04:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration