How to run bash with root rights in C program?

后端 未结 1 805
别跟我提以往
别跟我提以往 2021-01-27 15:14

I have to write program in C that run bash with root rights. I try to do this with exec but i dont how to login. Is this a good idea?

int main() {
    char *name         


        
相关标签:
1条回答
  • 2021-01-27 15:51

    Your executable needs to be setuid-root for this to work.

    sudo chown root:root myprog 
    sudo chmod 4755 myprog
    

    Even if you do this, the shell might not give you root privileges if only the effective user ID is root. You'll need to set the real user ID as well:

    int main() {
        char *name[2];
        name[0] = "bash";
        name[1] = NULL;
        setuid(0);      // sets the real user ID to 0 i.e. root
        execvp("/bin/bash", name);
    }
    
    0 讨论(0)
提交回复
热议问题