LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2006, 09:44 PM   #1
tritong
Member
 
Registered: Nov 2005
Posts: 40

Rep: Reputation: 15
write a PHP code to run some command as ROOT ???


I'm trying to write PHP code to run shel command, but there're some command I used must be run AS ROOT.
For example:

Code:
shell_exec("ifconfig eth0 down");
I know the problem is the user apache hasn't root privileges to run it.
Is there any solution ??
Help me.
 
Old 02-16-2006, 10:06 PM   #2
arsham
Member
 
Registered: Mar 2004
Location: London
Distribution: Arch Linux, Debian
Posts: 184

Rep: Reputation: 30
You can make a daemon that runs under root previlege and waits for a connection from localhost , then run the raw command
this by itself is a security hole
 
Old 02-17-2006, 12:53 AM   #3
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks, but is there another way that's more secure than make a daemon?
 
Old 02-17-2006, 01:06 AM   #4
arsham
Member
 
Registered: Mar 2004
Location: London
Distribution: Arch Linux, Debian
Posts: 184

Rep: Reputation: 30
you can sit behind xinetd , most of daemons do like this
see man pages for inetd and xinetd
but still I'm not sure about security
I will search for this and will let you know if I find any solution , but keep searching

Regards
Arsham
 
Old 02-17-2006, 04:30 AM   #5
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
Thank you Arsham.
I think I can make it a daemon and wait for connection to run sepecific commands but password is required. And the web UI must also need username and password to access it. I may try to secure them by SSL.
But I feel this solution is unsafe. So if there is better solution, please tell me soon.
 
Old 02-17-2006, 10:07 AM   #6
arsham
Member
 
Registered: Mar 2004
Location: London
Distribution: Arch Linux, Debian
Posts: 184

Rep: Reputation: 30
Kernel doesn't let you to post the root password , I mean you have to sit in front of the computer or ssh/telnet it and open a shell to login as root
you must limit the access of the daemon to localhost , for reson of security

Regards
 
Old 02-17-2006, 10:18 AM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Perhaps this would work... Write a script/program to run the command for you, set the owner to root and as root run chmod 4755 on it. This makes it run as the user who created it, i.e. root.
 
Old 02-17-2006, 10:35 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you know what the commands are you could set them up in a sudoers file, and then get php to call the command via sudo.
 
Old 02-22-2006, 01:41 AM   #9
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
I wrote a C program like this (for test only)
Code:
/* test.c */
#include <sys/types.h>

#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h> 

static char* usage="Usage: %s <command> [option] \n";

int err(char *, int);

int main(int argc, char* argv[])
{	
	int max=0,
	    check=0;

	if(argc<2)
	{
		printf(usage, argv[0]);
		return 1;
	}
	
	check = seteuid(0);
	if(check)
		return err("Set UID", 1);
	check = setegid(0);
	if(check)
		return err("Set GID", 1);

	if(argc>2)
	{
		max = argc;
		char *argl[max-1];
		
		int i, j=0;
		for(i=1; i<max;i++)
		{
			argl[j] = malloc(strlen(argv[i]));
			argl[j] = argv[i];
			j++;
		}
		argl[j] = NULL;

		check = execvp(argv[1], argl);
		if(check)
			return err("Run command", 1);
		
		return 0;
	}
	/*else: agrc=2*/
	char *argl[1];
	argl[0] = argv[1];
	argl[1] = NULL;
	check = execvp(argv[1], argl);
	if(check)
		return err("Run command", 1);
return 0;
}

int err(char* mess, int exitCode)
{
	perror(mess);
	return exitCode;
}
Compile it:
Code:
$gcc -o test test.c
Then, I change permission for it (as root):
Code:
$chmod +s test
Then I change to another user right and type
Code:
$test ifconfig eth0 down
It work OK.
But when I run it by PHP
Code:
<?php
shell_exec("test ifconfig eth0 up");
?>
I does nothing ???
Why???
 
Old 02-22-2006, 02:14 AM   #10
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
I added apache user to the sudoers too but it's no use!!!
Code:
%apache         ALL=(ALL)       NOPASSWD: ALL
 
Old 02-22-2006, 03:15 AM   #11
arsham
Member
 
Registered: Mar 2004
Location: London
Distribution: Arch Linux, Debian
Posts: 184

Rep: Reputation: 30
buddy
in your program , listen to a port , then after recieve the proper command , run the command via your daemon ( whick has root access )

then via PHP open a socket and send your command to the program which is listening

regards
 
Old 02-22-2006, 08:46 PM   #12
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
I'm doing what you said.
Thanks bro
 
Old 02-22-2006, 10:04 PM   #13
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
it's a good thing nobody in this forum knows how to find tritong's webapp on the internet... it would be pwned in about 3 seconds
 
Old 02-22-2006, 10:48 PM   #14
tritong
Member
 
Registered: Nov 2005
Posts: 40

Original Poster
Rep: Reputation: 15
This web application is for testing purpose only. So, I'm not afraid of hacking.
 
  


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
Run PHP shell_exec as root maneeshsethi Linux - General 5 12-30-2008 07:25 AM
run shell command inside of c code? khucinx Programming 2 05-17-2004 10:04 AM
help getting pptp-php-gtk.php to run as root mrtwice Linux - Software 0 11-21-2003 12:49 PM
C program code to run a Linux command line Linh Programming 10 06-11-2003 01:59 PM
Is their a way to run a command as root..or another user.. in php BaerRS Programming 2 04-25-2002 02:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

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