Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute a command as root

Tags:

c

linux

root

I develop a C code on Linux (Debian). Time to time, I need to execute some commands through system()

I wonder if it is possible to execute a command via system()as root. If it is not the case, is there any function to execute a command (or run a binary) as root that I can use on the C code?

like image 210
Angs Avatar asked Jul 23 '13 23:07

Angs


People also ask

How do I run a command as root in terminal?

Simply type sudo before the rest of your command. The command “sudo” stands for “substitute user do.” When a user executes sudo to the beginning of a command, the command runs as root. Note that you will be prompted to enter your password before sudo runs the command.

Can be used to execute a command as the root user?

The su command exists on most unix-like systems. It lets you run a command as another user, provided you know that user's password. When run with no user specified, su will default to the root account. The command to run must be passed using the -c option.

How do I run a command as root in Windows?

If you're used to using the “Run” box to open apps, you can use that to launch Command Prompt with admin privileges. Press Windows+R to open the “Run” box. Type “cmd” into the box and then press Ctrl+Shift+Enter to run the command as an administrator.

What does it mean to execute as root?

root is the superuser account on the system — it (basically) has all privileges. Many systems are configured so that you can use the sudo command in front of another command to run that command "as root" — that is, as if you are the root user, with the same privileges.


3 Answers

We met the situation before that we want to execute a root command by a normal user, here is our solution (using setuid/SUID):

assume that:

  • username: Tom
  • group: gTom
  • C program file: my_pro.c

Step 1: Write a C code tool: my_sudo.c

...
int main(int args, char *argv[]) {
    if (args < 2) 
        printf("Usage: my_sudo [cmd] [arg1 arg2 ...]");

    // cmd here is the shell cmd that you want execute in "my_pro"
    // you can check the shell cmd privilege here
    // example:  if (argv[1] != "yum") return; we just allow yum execute here

    char cmd[MAX_CMD];
    int i;
    for ( i = 2; i < args; i ++) {
    // concatenate the cmd, example: "yum install xxxxx"
        strcat(cmd, " ");
        strcat(cmd, argv[i]);
    }

    system(cmd);
} 

Step 2: Compile my_sudo.c to get a my_sudo executable file

   sudo chown root:gTom my_sudo   // user root && gTom group
   sudo chmod 4550 my_sudo        // use SUID to get root privilege

   #you will see my_sudo like this(ls -l)
   #-r-sr-x--- 1 root my_sudo 9028 Jul 19 10:09 my_sudo*

   #assume we put my_sudo to /usr/sbin/my_sudo

Step 3: In your C code

...
int main() {
    ...
    system("/usr/bin/mysudo yum install xxxxx");
    ...
}

#gcc && ls -l
#-rwxr--r--  1 Tom gTom 1895797 Jul 23 13:55 my_pro

Step 4: Execute./my_pro

You can execute the yum install without sudo.

like image 56
Yifan Wang Avatar answered Oct 22 '22 13:10

Yifan Wang


If you are a user on your system that has sudo privileges to run commands as root, just pre-pend sudo to the command.

system("sudo yum install some-package");

If you want anybody to be able to do it, then you have to be administrator on your system, change the owner of the file to be root, and modify the permissions of your executable to run as root. By doing so, you do not need to modify your system() command string with sudo.

chmod +s my_program
chown root my_program

Realize that doing this may open you up to security problems, unless you have proven that your program has no security issues.

The file-system may be such to disallow you from setting the setuid bit on your program. If you need more information along these lines, you should consult SuperUser.

like image 43
jxh Avatar answered Oct 22 '22 14:10

jxh


This is one of those bag-o-tricks things to keep in mind. There are security risks, so just be aware of who will use it. In the "system" command you can even execute external scripts...although that opens major security risks because while this binary has to have the permissions re-set every time it's compiled, a script can be changed endlessly and this binary will keep calling it.

#include <stdio.h>
#include <stdlib.h>

//Create as root
//gcc fixmusic.c -o fixmusic 
//chmod u+s fixmusic
//now run as non-root user and it should work despite limitations of user


int main(int argc, char *argv[] )
{

    setuid(0);

    char command[100];
    sprintf(command,"/usr/bin/chmod -R a+w /mnt/Local/Music");
    system(command);
    //This is just optional info if someone cat's the binary
    volatile const char comment [] = "INFO: Fixes music permissions";
    return 0;
}
like image 29
Westrock Avatar answered Oct 22 '22 13:10

Westrock