0

I could not find the reason why my request fail for the following
My php code is:

if (isset($_COOKIE["user"])) {
   echo '<p><h3><strong>Welcome '.$_COOKIE["param"].'</strong></h3></p>'; .....

When i request exec('ls -al') as param , the php code did not run the command.

On the response of the request it was like parameterized:

Welcome exec('ls -al')

What may the reason that failed this execution?

3
  • 3
    I'm not exactly sure what you are trying to, but putting php code to be evaulated in a cookie sounds like a very bad idea. Commented Jan 22, 2013 at 22:25
  • I know its a bad idea but wonder why it fails.. Commented Jan 22, 2013 at 22:31
  • @user2001965 Can you please accept the answer that you think is the most relevant? See this meta.stackexchange.com/a/5235/191928 if you are not sure how to accept an answer. Commented Jan 23, 2013 at 14:29

2 Answers 2

2

$_COOKIE["param"] is a string. You are echoing it. It is not supposed to run anything.

If you wanted to run a command in your PHP, you would have to use eval(). But as for running a command from a cookie value:

DON'T DO IT!

Sign up to request clarification or add additional context in comments.

3 Comments

I do not agree with you , can you explain why the following works ? when it is tried manually ?? echo '<p><h3><strong>Welcome '.exec('ls -al).'</strong></h3></p>';
@user2001965 The difference in your comment is that you're directly calling the shell_exec function, whereas the value coming from the cookie is a string, not a function call. You can't run strings directly.
@user2001965 What you are actually doing is echo '<p><h3><strong>Welcome '."exec('ls -al)".'</strong></h3></p>';, notice the ". This WILL echo exec('ls -al)
0

So you're saying that the value of $_COOKIE['param'] is exec('ls -al'), and you're expecting that to run when you echo it out?

That's not how it works. The value of that cookie will be the string value "exec('ls -al')", not the result of the executed code. If you think about it for a second, you'll understand why it would be a bad idea for a cookie to be able to auto-execute code.

It's not really a great idea to be running random commands through exec() anyway, especially if that input came from a user (which cookies do - the user can and will change them to try to attack you).

Instead, you should be using other input that your code can interpret as a signal to run certain code. For example, you could have the param value hold the string list files, and your code would see that value and run exec('ls -al') for you.

You still shouldn't be execing code to do this though, since it's very easy to accidentally run dangerous commands that way. Instead, you should use PHP's built-in functions as much as possible, and only after sanitizing your inputs and only running known values.

For your case, PHP has a bunch of functions that let you interact with the filesystem of your server. Use those to get a list of files on the system instead.

2 Comments

I do not agree with you , can you explain why the following works ? when it is tried manually ?? echo '<p><h3><strong>Welcome '.exec('ls -al).'</strong></h3></p>';
@user2001965 I explained it in response to your same comment on Mathieu Imbert's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.