ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

以root身份执行命令而不使用root密码或sudo

2019-05-19 08:49:04  阅读:337  来源: 互联网

标签:c-3 linux root setuid


我理解以root身份运行脚本的含义,尤其是Web应用程序.但是作为我的Web应用程序的一部分,我需要使用cur的tor,这需要偶尔重置tor ip.通过service tor restart重新启动服务时,tor可以获得新的ip.由于只有root可以做到这一点,我编写了一个C包装器脚本来完成我需要的工作,并编译它并在其上设置setuid root,并更改为root用户所有权.但是,当它作为非特权用户运行时,它仍然会询问root密码.作为root用户,服务重启不应该询问密码.

我的剧本:

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

void ExecAsRoot (char* str);
int main ()
{
  setuid (0);
  setvbuf(stdout, NULL, _IONBF, 0);
  printf ("Host real ip is: ");
  ExecAsRoot("ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'");
  ExecAsRoot("/usr/sbin/service tor restart");
  // sleep(2);
  printf ("Tor should have switched to a new ip by now.\nNew ip is: ");
  ExecAsRoot("torify curl ifconfig.co 2>/dev/null");
  return 0;
 }

void ExecAsRoot (char* str) {
  system (str);
}

我做了以下事情:

chown root restartor
chmod u=rwx,go=xr restartor 

输出:

Host real ip is: 7.17.11.23
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'tor.service'.
Authenticating as: root
Password:

如何在不提供root密码的情况下将其作为Web用户运行?

解决方法:

您没有在文件权限中设置setuid位:

#-------v
chmod u=srwx,go=xr restartor

标签:c-3,linux,root,setuid
来源: https://codeday.me/bug/20190519/1133092.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有