简体   繁体   English

python 脚本上的 Setuid 位:Linux 与 Solaris

[英]Setuid bit on python script : Linux vs Solaris

I am running this small python script on both linux and Solaris as a not privileged user :我在 linux 和 Solaris 上都以非特权用户身份运行这个小的 python 脚本:

#!/usr/bin/python
import os
print 'uid,euid =',os.getuid(),os.geteuid()

Before running, the setuid bit is set on the script (not on python interpreter) :在运行之前,setuid 位在脚本上设置(不是在 python 解释器上):

chown root:myusergrp getuid.py
chmod 4750 getuid.py

On Solaris, the effective uid is set because of the setuid bit :在 Solaris 上,有效 uid 由于 setuid 位而设置:

uid,euid = 10002 0

But not on Linux :但不是在 Linux 上:

uid,euid = 10002 10002

Note the python version is 2.6 for both Solaris and Linux注意 Solaris 和 Linux 的 python 版本都是 2.6

Is it possibe to have Python Linux working as Python Solaris ?是否可以让 Python Linux 作为 Python Solaris 工作?

Most Unix distributions normally don't allow you to use setuid on a file that uses a #!大多数 Unix 发行版通常不允许您在使用 #! interpreter.口译员。 Solaris happens to be one that allows it due to its use of a more secure implementation than most other distributions. Solaris 恰好是允许它的,因为它使用了比大多数其他发行版更安全的实现。

See this FAQ entry for more background about why the mechanism is so dangerous: How can I get setuid shell scripts to work?有关该机制为何如此危险的更多背景信息,请参阅此 FAQ 条目:如何让 setuid shell 脚本工作?

See this link for more discussion and how to compile a setuid executable that will run your script: setuid on shell scripts有关更多讨论以及如何编译将运行您的脚本的 setuid 可执行文件,请参阅此链接: shell 脚本上的 setuid

The pertinent part:相关部分:

int main()
{
   setuid( 0 );
   system( "/path/to/script.sh" );

   return 0;
}

I just put two and two together today and came up with an alternative solution: cython --embed .我今天只是把两个和两个放在一起,想出了一个替代解决方案: cython --embed

Follow the examples at the link above and you'll get binary executables from your Python that you'll be able to chown and chmod u+s , completing the circle without a wrapper program.按照上面链接中的示例,您将从 Python 中获得二进制可执行文件,您将能够chownchmod u+s ,无需包装程序即可完成循环。

Of course, beware the risks (of this or any other setuid use)—bugs in your script can result in elevated privileges on the system.当然,请注意风险(使用此或任何其他setuid的风险)— 脚本中的错误可能会导致系统权限提升。

Based on David K. Hess answer, but with arguments:基于David K. Hess 的回答,但有论据:

#include <unistd.h>

int main(int argc, char **argv)
{
    setuid(0);
    execv("/path/to/script.sh", argv);

    return 0;
}

You could potentially use sudo to achieve what you want.您可以潜在地使用 sudo 来实现您想要的。 It runs stuff as different users:它以不同的用户身份运行:

 sudo -u otheruser command

Permissions are set by root using visudo.权限由 root 使用 visudo 设置。 The setuid/setguid stuff doesn't appear to apply to scripts or the shell in linux, only compiled code. setuid/setguid 的东西似乎不适用于 linux 中的脚本或 shell,仅适用于编译后的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM