如何在C程序中以root权限运行bash? [英] How to run bash with root rights in C program?

查看:295
本文介绍了如何在C程序中以root权限运行bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用C编写具有root权限运行bash的程序. 我尝试用exec执行此操作,但我不怎么登录.这是个好主意吗?

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[2];
    name[0] = "bash";
    name[1] = NULL;
    execvp("/bin/bash", name);
}

推荐答案

您的可执行文件必须是setuid-root才能起作用.

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

sudo chown root:root myprog 
sudo chmod 4755 myprog

即使您执行此操作,如果仅有效用户ID是root,shell也可能不会为您提供root特权.您还需要设置真实用户ID:

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);
}

这篇关于如何在C程序中以root权限运行bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆