Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to allow user to inject and run php code

I've been thinking for a while about the idea of allowing user to inject code on website and run it on a web server. It's not a new idea - many websites allow users to "test" their code online - such as http://ideone.com/.

For example: Let's say that we have a form containing <textarea> element in which that user enters his piece of code and then submits it. Server reads POST data, saves as PHP file and require()s it while being surrounded by ob_*() output buffering handlers. Captured output is presented to end user.

My question is: how to do it properly? Things that we should take into account [and possible solutions]:

  • security, user is not allowed to do anything evil,
    • php.ini's disable_functions
  • stability, user is not allowed to kill webserver submitting while(true){},
    • set_time_limit()
  • performance, server returns answer in an acceptable time,
  • control, user can do anything that matches previous points.

I would prefer PHP-oriented answers, but general approach is also welcome. Thank you in advance.

like image 987
Tomasz Kowalczyk Avatar asked May 15 '11 03:05

Tomasz Kowalczyk


People also ask

Can you inject PHP code?

Description: PHP code injectionIf the user data is not strictly validated, an attacker can use crafted input to modify the code to be executed, and inject arbitrary code that will be executed by the server.

Where should PHP codes be executed?

PHP code is executed on the server.

What is PHP Command injection?

What Is Command Injection? A command injection attack is based on the execution of arbitrary (and most likely malicious) code on the target system. In other words, it's a way to use an application designed to do one thing for a completely different purpose. Let's take the example of a simple contact form.

Can we run PHP from HTML file if yes how?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.


1 Answers

I would think about this problem one level higher, above and outside of the web server. Have a very unprivileged, jailed, chroot'ed standalone process for running these uploaded PHP scripts, then it doesn't matter what PHP functions are enabled or not, they will fail based on permissions and lack of access.

Have a parent process that monitors how long the above mentioned "worker" process has been running, if its been too long, kill it, and report back a timeout error to the end user.

Obviously there are many implementation details to work out as to how to run this system asynchronously outside of the browser request, but I think it would provide a pretty secure way to run your untrusted PHP scripts.

like image 51
ctcherry Avatar answered Oct 30 '22 00:10

ctcherry