在 python 脚本中更改为 sudo 用户
我有一个问题。我正在编写一个软件,需要它来执行需要用户处于 sudo 模式的操作。运行“sudo python filename.py”不是一个选项,这引出了我的问题。有没有一种方法可以通过 python 脚本更改为 sudo ,安全性不是问题,因为用户会知道程序应该按以下方式运行的 sudo 密码,以说明
- 以普通用户身份运行的问题程序
- .... ..执行操作
- 用户输入sudo密码
- 用户更改为sudo
- 需要sudo权限的子程序
- 在触发器上运行即使(子程序结束)用户再次成为普通用户
- ......执行操作
我的问题在于步骤3,任何指针或者你建议的框架会有很大帮助。
干杯
克里斯
I have a problem. I am writing a piece of software, which is required to perform an operation which requires the user to be in sudo mode. running 'sudo python filename.py' isn't an option, which leads me to my question. Is there a way of changing to sudo half way through a python script, security isn't an issue as the user will know the sudo password the program should run in the following way to illustrate the issue
- program running as normal user
- ...... performing operations
- user enters sudo password
- user changed to sudo
- sub program requiring sudo permission is run
- on trigger even (end of sub program) user becomes normal user again
- ...... performing operations
My problem lies in step 3, any pointers or frameworks you could suggest would be of great help.
Cheers
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
最好以提升的权限运行尽可能少的程序。您可以通过 subprocess.call() 函数运行需要更多权限的小部分,例如
It is better to run as little of the program as possible with elevated privileges. You can run the small part that needs more privilege via the
subprocess.call()
function, e.g.不要尝试让自己 sudo 只是检查你是否是,如果不是则错误
Don't try and make yourself sudo just check if you are and error if your not
我最近在制作系统安装脚本时处理了这个问题。要切换到超级用户权限,我将 subprocess.call() 与 'sudo' 一起使用:
请注意,您需要使用
shlex.split()
使您的命令可用于subprocess.call()
。如果您想使用命令的输出,可以使用subprocess.check_output()
。还有一个名为“sh”的软件包(http://amoffat.github.com/sh/ ),您可以将其用于此目的。I've recently dealt with this problem while making a system installation script. To switch to superuser permissions, I used subprocess.call() with 'sudo':
Note that you need to use
shlex.split()
to make your command usable forsubprocess.call()
. If you want to use the output from a command, you can usesubprocess.check_output()
. There is also a package called 'sh' (http://amoffat.github.com/sh/) that you can use for this purpose.使用Tcl和Expect,再加上subprocess来提升自己。所以基本上是这样的:
sudo.tcl
sudo.py
然后运行 sudo.py。
Use Tcl and Expect, plus subprocess to elevate yourself. So basically it's like this:
sudo.tcl
sudo.py
And then run sudo.py.
如果您能够将需要提升权限的必要功能封装在单独的可执行文件中,则可以在可执行程序上使用 setuid 位,并从用户级 python 脚本中调用它。
这样,只有 setuid-executable 中的活动以 root 身份运行,但是执行此操作不需要 sudo,即 root 权限。仅创建/修改 setuid 可执行文件需要 sudo。
有一些安全隐患,例如确保您的 setuid 可执行程序正确清理任何用户输入(例如参数),以便它不会被欺骗做一些不应该做的事情(混乱的代理问题)。
参考:
编辑
:仅setuid似乎适用于编译的可执行文件(二进制文件),而不是解释的脚本,因此您可能需要使用编译的 setuid 包装器。
If you are able to encapsulate just the necessary functionality requiring elevated privileges in a separate executable, you could use the setuid bit on the executable program, and call it from your user-level python script.
In this way, only the activity in the setuid-executable run as root, however executing this does NOT require sudo, i.e., root privileges. Only creating/modifying the setuid-executable requires sudo.
There are a few security implications, such as ensuring that your setuid executable program properly sanitizes any user input (e.g., parameters), so that it cannot be tricked into doing something it should not (confused deputy problem).
ref:
http://en.wikipedia.org/wiki/Setuid#setuid_on_executables
edit: setuid only seems to work for compiled executables (binaries), and not interpreted scripts, so you may need to use a compiled setuid wrapper.
您可以使用 setuid 来设置用户 uid。但出于明显的安全原因,您只能在 root 身份(或程序具有 suid root 权限)的情况下执行此操作。这两者可能都是一个坏主意。
在这种情况下,您需要 sudo 权限才能运行特定程序。在这种情况下,只需替换为“sudo theprogram”即可。
You can use setuid to set the users uid. But for obvious security reasons you can only do this if you are root (or the program has suid root rights). Both of these are probably a bad idea.
In this case you need to sudo rights to run a specific program. In that case just sub to "sudo theprogram" instead.
不确定这对您有何帮助,并且它没有回答问题,但是,当您遇到所需的“root”用户问题并且您需要成为“root”才能阅读时,这是一种解决方法/写入文件夹或文件。
然后您可以更改权限并随后将其切换回来。我将其放在 docker-compose 文件中,该文件启动了一个将应用程序部署到服务器的 Python 脚本。这个解决方法是我让它运行的唯一方法。我什至不需要更改容器 bash 的权限,而是由脚本执行此操作,并且只需要密码两次。
在此解决方法之前,我尝试更改为 root 用户,然后使用该 root 用户执行大块代码,但无济于事。
以及输出:
此后,服务器文件夹具有与之前相同的权限,并且代码不需要root用户来运行。
Not sure how this would help you, and it does not answer the question, yet, it is a workaround to think about when you run into a needed "root" user problem and you need to be "root" only to read / write in a folder or file.
You can then change the permissions and also switch them back afterwards. I had this in a docker-compose file that started a Python script that deployed an application to a server. This workaround was the only way how I got it to run. I do not even need to change the permissions from the container bash, instead, the script does that, and only the password is needed twice.
Before this workaround, I tried to change to the root user and then execute large blocks of code with that root user, to no avail.
And the output:
After this, the server folder had the same permissions as before, and the code did not need the root user to run through.
您是在谈论让用户在执行过程中输入密码吗? raw_input() 可以从控制台获取用户输入,但它不会屏蔽密码。
Are you talking about having the user input password half way through your execution? raw_input() can take a user input from console, but it will not mask the password.