1

I'm trying to run Python file with root access with php index

in php there is :

passthru('python /home/register/register.py '. $_POST['username'] . ' example.com ' . $_POST['password'] . ' ' . $_POST['email'] . ' ' . $ip . ' 1 2>&1');

and in Python file there is:

os.popen("sudo -u root -p password /sbin/ejabberdctl register %s %s %s" % (user,domain,password)).read()

is there any command with Python to login with root user then do command like : ls or mkdir

thnx.

5
  • Yes, using subprocess Commented Feb 28, 2015 at 20:20
  • @PadraicCunningham How can I do it ? Commented Feb 28, 2015 at 20:24
  • you should look into how to use a sudoers policy to allow the user to execute commands as root. Also, the way you process the arguments is very dangerous and allows for shell injection (for both the call in PHP and python)! Commented Feb 28, 2015 at 20:28
  • @mata are there any command with python to login with root, and use password to login Commented Feb 28, 2015 at 20:30
  • Passing in $_POST variables without any sanitation to Python (and then to sudo! Again without sanitation) is a huge security hole. Commented Feb 28, 2015 at 20:38

2 Answers 2

1
from subprocess import PIPE,Popen

p = Popen(["sudo", "-s", "-S"], stdin=PIPE, stdout=PIPE, universal_newlines=True)

p.stdin.write("password\n")
p.stdin.write("mkdir foo\n")
p.stdin.write("id -u")

To see output use communicate:

from subprocess import PIPE,Popen

p = Popen(["sudo", "-s", "-S"], stdin=PIPE, stdout=PIPE, universal_newlines=True)

p.stdin.write("password\n")
p.stdin.write("ls -la\n")
p.stdin.write("/usr/bin/pip list\n")
p.stdin.write("id -u")
print(p.communicate()[0])

But be very sure you know what commands you are running.

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

5 Comments

Traceback (most recent call last): File "test.py", line 6, in <module> p = Popen(["sudo","-s","-S"],stdin=PIPE, stdout=PIPE,cuniversal_newlines=True) TypeError: __init__() got an unexpected keyword argument 'cuniversal_newlines'
I had a typo, it has since been fixed, should not have c before universal
ok in p.stdin.write("password\n") am should to write my root password Right ??
it's return to me with this error : None None [sudo] password for apache
p.stdin.write("/sbin/ejabberdctl register user example.com 123456\n")
0

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);

$return1  = $shell->exeCmd("mkdir -p /some/path");
$return2  = $shell->exeCmd("ls --color=none /some/path");

notice the ls command has a switch called --color=none, that is needed because the bash shell will return color information as odd chars, the switch prevents it.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.