Solved

PHP shell_exec    executing a mysql query

Posted on 2014-01-18
16
1,396 Views
Last Modified: 2014-01-19
i have a PHP script where the following shell_exec seems to return null
echo "SIMPLE MYSQL:".       shell_exec('mysql cphulkd -e "SELECT 1+1 "')  ;

i've tried various ways to find out what it's returning but it seems to be blank/null.

the plain mysql shell command
mysql cphulkd -e "SELECT 1+1 "

works fine when executed directly from linux shell.

any thoughts on debugging PHP shell_exec ?

thanks
0
Comment
Question by:willsherwood
  • 9
  • 6
16 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791232
It's not a matter of debugging, it's that the web server user doesn't have permissions to do that.  Your web server would have to be running 'suexec' to run that under your own user permissions.  Which would have to be in your own directory.
0
 

Author Comment

by:willsherwood
ID: 39791243
i'm running from root, with the files owned by and in grp of 'nobody'
permissions:  -rw-r--r--.

i've sandwiched the  shell_exec(mysql)  with
echo shell_exec(uptime);

and the uptime prints properly twice -- just nothing for the mysql command in between
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791252
If you are running PHP thru a web server, you are Not running as 'root', you are probably running as 'www' which has very limited permissions on purpose.  In theory, you should be able to run that PHP script on the command line instead of thru the web server and have it work.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791259
This page http://us1.php.net/manual/en/function.shell-exec.php says that you can use 'exec' instead to get the program error code.  Note the notes about running under Apache and seeing a blank line for a multi-line response.

Of course, the more interesting question is why aren't you accessing MySQL thru the PHP drivers?
0
 

Author Comment

by:willsherwood
ID: 39791324
the tables are internal WHM database tables with accessibility restrictions.
I'm trying to make a sort of limited API that i can call externally without security risks.
i cannot run mysql directly from PHP because i don't know (nor want to know) the authentication for the connection.

perhaps it's just not permitted to call anything related to mysql from shell_exec ?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791376
It's probably not allowed to run anything at all thru 'shell_exec' because it doesn't have permission.  Anything run thru 'shell_exec' is run as the user that calls it and thru the web server that would be 'www', not 'root'.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39791378
Is shell_exec() enabled in php.ini and not disabled?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791415
No.  It's all about user permissions.  You may run as 'root' at the command line but you will never run as 'root' thru the web server.  shell_exec() probably only works completely right if you also have 'suexec' running on the server.  'suexec' allows Apache to give you user access to the command line.  However... that can still be limited by the owner / user of the directory and files.

Your example "mysql cphulkd -e "SELECT 1+1 "" must be doing some kind of auto-login because I can't do that on my system.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:willsherwood
ID: 39791458
sorry if my previous reply didn't get thru or wasn't clear... from within this PHP context:
    echo shell_exec(uptime);
works fine.
fwiw, also:
    echo shell_exec(dir);
    echo shell_exec("echo '1'");
work as expected.

cphulk is a cpanel system login monitor "firewall" for login protection, and its blocks are kept in the mysql table  cphulkd   which is accessible via the shell mysql command previously shown;   it's this mysql cmd line i cannot get to execute from within the  shell_exec.
Are you saying that some shell commands have differing permissions from other commands
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39791600
It's possible.  Note that all your working examples don't change anything, they just report pretty generic information, and any user can run those commands.  The 'mysql' command line does require a login on every system that I know of.  What are you getting for a response from "mysql cphulkd -e "SELECT 1+1 ""?
0
 

Author Comment

by:willsherwood
ID: 39791628
when i execute it directly in the shell, it gives the  +----+    tabled  format
 1+1

   2

when called from php   shell_exec,    nothing.  (null, or maybe a logged/hidden error)
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39791689
I tried this on my Ubuntu 10.04 system and got basically the same results as you have.  'uptime' and 'dir' work.  But on the command line, my 'mysql' will not accept the password from the command line, only 'interactively'.  And it definitely won't accept a login with no user or password.
0
 

Author Closing Comment

by:willsherwood
ID: 39792043
thanks for your help.
upon further inspection, indeed it was a mysql user/pw access problem
appreciate your patience and peristence
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39792459
You're welcome, glad to help.
0
 

Author Comment

by:willsherwood
ID: 39792786
it was just so curious that mysql seemed to be the only command that didn't work (that i tried).
but now we have an easy API for determining if someone is blocked via cpHulk login denial.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39792804
Yes, that did seem odd.  Even odder on my system where it wouldn't allow the password on the command line.  I know that my older MySQL on my Windows box doesn't act that way.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now